1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Poll
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Poll model
29: *
30: * @method Mage_Poll_Model_Resource_Poll _getResource()
31: * @method Mage_Poll_Model_Resource_Poll getResource()
32: * @method string getPollTitle()
33: * @method Mage_Poll_Model_Poll setPollTitle(string $value)
34: * @method Mage_Poll_Model_Poll setVotesCount(int $value)
35: * @method int getStoreId()
36: * @method Mage_Poll_Model_Poll setStoreId(int $value)
37: * @method string getDatePosted()
38: * @method Mage_Poll_Model_Poll setDatePosted(string $value)
39: * @method string getDateClosed()
40: * @method Mage_Poll_Model_Poll setDateClosed(string $value)
41: * @method int getActive()
42: * @method Mage_Poll_Model_Poll setActive(int $value)
43: * @method int getClosed()
44: * @method Mage_Poll_Model_Poll setClosed(int $value)
45: * @method int getAnswersDisplay()
46: * @method Mage_Poll_Model_Poll setAnswersDisplay(int $value)
47: *
48: * @category Mage
49: * @package Mage_Poll
50: * @author Magento Core Team <core@magentocommerce.com>
51: */
52:
53: class Mage_Poll_Model_Poll extends Mage_Core_Model_Abstract
54: {
55: const XML_PATH_POLL_CHECK_BY_IP = 'web/polls/poll_check_by_ip';
56:
57: protected $_pollCookieDefaultName = 'poll';
58: protected $_answersCollection = array();
59: protected $_storeCollection = array();
60:
61: protected function _construct()
62: {
63: $this->_init('poll/poll');
64: }
65:
66: /**
67: * Retrieve Cookie Object
68: *
69: * @return Mage_Core_Model_Cookie
70: */
71: public function getCookie()
72: {
73: return Mage::app()->getCookie();
74: }
75:
76: /**
77: * Get Cookie Name
78: *
79: * @param int $pollId
80: * @return string
81: */
82: public function getCookieName($pollId = null)
83: {
84: return $this->_pollCookieDefaultName . $this->getPollId($pollId);
85: }
86:
87: /**
88: * Retrieve defined or current Id
89: *
90: * @deprecated since 1.7.0.0
91: * @param int $pollId
92: * @return int
93: */
94: public function getPoolId($pollId = null)
95: {
96: return $this->getPollId($pollId);
97: }
98:
99: /**
100: * Retrieve defined or current Id
101: *
102: * @param int|null $pollId
103: * @return int
104: */
105: public function getPollId($pollId = null)
106: {
107: if (is_null($pollId)) {
108: $pollId = $this->getId();
109: }
110: return $pollId;
111: }
112:
113: /**
114: * Check if validation by IP option is enabled in config
115: *
116: * @return bool
117: */
118: public function isValidationByIp()
119: {
120: return (1 == Mage::getStoreConfig(self::XML_PATH_POLL_CHECK_BY_IP));
121: }
122:
123: /**
124: * Declare poll as voted
125: *
126: * @param int $pollId
127: * @return Mage_Poll_Model_Poll
128: */
129: public function setVoted($pollId=null)
130: {
131: $this->getCookie()->set($this->getCookieName($pollId), $this->getPollId($pollId));
132:
133: return $this;
134: }
135:
136: /**
137: * Check if poll is voted
138: *
139: * @param int $pollId
140: * @return bool
141: */
142: public function isVoted($pollId = null)
143: {
144: $pollId = $this->getPollId($pollId);
145:
146: // check if it is in cookie
147: $cookie = $this->getCookie()->get($this->getCookieName($pollId));
148: if (false !== $cookie) {
149: return true;
150: }
151:
152: // check by ip
153: if (count($this->_getResource()->getVotedPollIdsByIp(Mage::helper('core/http')->getRemoteAddr(), $pollId))) {
154: return true;
155: }
156:
157: return false;
158: }
159:
160: /**
161: * Get random active pool identifier
162: *
163: * @return int
164: */
165: public function getRandomId()
166: {
167: return $this->_getResource()->getRandomId($this);
168: }
169:
170: /**
171: * Get all ids for not closed polls
172: *
173: * @return array
174: */
175: public function getAllIds()
176: {
177: return $this->_getResource()->getAllIds($this);
178: }
179:
180: /**
181: * Add vote to poll
182: *
183: * @return unknown
184: */
185: public function addVote(Mage_Poll_Model_Poll_Vote $vote)
186: {
187: if ($this->hasAnswer($vote->getPollAnswerId())) {
188: $vote->setPollId($this->getId())
189: ->save();
190: $this->setVoted();
191: }
192: return $this;
193: }
194:
195: /**
196: * Check answer existing for poll
197: *
198: * @param mixed $answer
199: * @return boll
200: */
201: public function hasAnswer($answer)
202: {
203: $answerId = false;
204: if (is_numeric($answer)) {
205: $answerId = $answer;
206: }
207: elseif ($answer instanceof Mage_Poll_Model_Poll_Answer) {
208: $answerId = $answer->getId();
209: }
210:
211: if ($answerId) {
212: return $this->_getResource()->checkAnswerId($this, $answerId);
213: }
214: return false;
215: }
216:
217: public function resetVotesCount()
218: {
219: $this->_getResource()->resetVotesCount($this);
220: return $this;
221: }
222:
223:
224: public function getVotedPollsIds()
225: {
226: $idsArray = array();
227:
228: foreach ($this->getCookie()->get() as $cookieName => $cookieValue) {
229: $pattern = '#^' . preg_quote($this->_pollCookieDefaultName, '#') . '(\d+)$#';
230: $match = array();
231: if (preg_match($pattern, $cookieName, $match)) {
232: if ($match[1] != Mage::getSingleton('core/session')->getJustVotedPoll()) {
233: $idsArray[$match[1]] = $match[1];
234: }
235: }
236: }
237:
238: // load from db for this ip
239: foreach ($this->_getResource()->getVotedPollIdsByIp(Mage::helper('core/http')->getRemoteAddr()) as $pollId) {
240: $idsArray[$pollId] = $pollId;
241: }
242:
243: return $idsArray;
244: }
245:
246: public function addAnswer($object)
247: {
248: $this->_answersCollection[] = $object;
249: return $this;
250: }
251:
252: public function getAnswers()
253: {
254: return $this->_answersCollection;
255: }
256:
257: public function addStoreId($storeId)
258: {
259: $ids = $this->getStoreIds();
260: if (!in_array($storeId, $ids)) {
261: $ids[] = $storeId;
262: }
263: $this->setStoreIds($ids);
264: return $this;
265: }
266:
267: public function getStoreIds()
268: {
269: $ids = $this->_getData('store_ids');
270: if (is_null($ids)) {
271: $this->loadStoreIds();
272: $ids = $this->getData('store_ids');
273: }
274: return $ids;
275: }
276:
277: public function loadStoreIds()
278: {
279: $this->_getResource()->loadStoreIds($this);
280: }
281:
282: public function getVotesCount()
283: {
284: return $this->_getData('votes_count');
285: }
286:
287: }
288: