/app/code/core/Mage/Poll/Model/Poll.php

https://github.com/edbull/custard · PHP · 245 lines · 132 code · 31 blank · 82 comment · 12 complexity · 5baa6f440347802e7c981c2493830005 MD5 · raw file

  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) 2010 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. * Poll model
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Poll_Model_Poll extends Mage_Core_Model_Abstract
  32. {
  33. const XML_PATH_POLL_CHECK_BY_IP = 'web/polls/poll_check_by_ip';
  34. protected $_pollCookieDefaultName = 'poll';
  35. protected $_answersCollection = array();
  36. protected $_storeCollection = array();
  37. protected function _construct()
  38. {
  39. $this->_init('poll/poll');
  40. }
  41. /**
  42. * Retrieve Cookie Object
  43. *
  44. * @return Mage_Core_Model_Cookie
  45. */
  46. public function getCookie()
  47. {
  48. return Mage::app()->getCookie();
  49. }
  50. /**
  51. * Get Cookie Name
  52. *
  53. * @param int $pollId
  54. * @return string
  55. */
  56. public function getCookieName($pollId = null)
  57. {
  58. return $this->_pollCookieDefaultName . $this->getPoolId($pollId);
  59. }
  60. /**
  61. * Retrieve defined or current Id
  62. *
  63. * @param int $pollId
  64. * @return int
  65. */
  66. public function getPoolId($pollId = null)
  67. {
  68. if (is_null($pollId)) {
  69. $pollId = $this->getId();
  70. }
  71. return $pollId;
  72. }
  73. /**
  74. * Check if validation by IP option is enabled in config
  75. *
  76. * @return bool
  77. */
  78. public function isValidationByIp()
  79. {
  80. return (1 == Mage::getStoreConfig(self::XML_PATH_POLL_CHECK_BY_IP));
  81. }
  82. /**
  83. * Declare poll as voted
  84. *
  85. * @param int $pollId
  86. * @return Mage_Poll_Model_Poll
  87. */
  88. public function setVoted($pollId=null)
  89. {
  90. $this->getCookie()->set($this->getCookieName($pollId), $this->getPoolId($pollId));
  91. return $this;
  92. }
  93. /**
  94. * Check if poll is voted
  95. *
  96. * @param int $pollId
  97. * @return bool
  98. */
  99. public function isVoted($pollId = null)
  100. {
  101. $pollId = $this->getPoolId($pollId);
  102. // check if it is in cookie
  103. $cookie = $this->getCookie()->get($this->getCookieName($pollId));
  104. if (false !== $cookie) {
  105. return true;
  106. }
  107. // check by ip
  108. if (count($this->_getResource()->getVotedPollIdsByIp(Mage::helper('core/http')->getRemoteAddr(), $pollId))) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Get random active pool identifier
  115. *
  116. * @return int
  117. */
  118. public function getRandomId()
  119. {
  120. return $this->_getResource()->getRandomId($this);
  121. }
  122. /**
  123. * Add vote to poll
  124. *
  125. * @return unknown
  126. */
  127. public function addVote(Mage_Poll_Model_Poll_Vote $vote)
  128. {
  129. if ($this->hasAnswer($vote->getPollAnswerId())) {
  130. $vote->setPollId($this->getId())
  131. ->save();
  132. $this->setVoted();
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Check answer existing for poll
  138. *
  139. * @param mixed $answer
  140. * @return boll
  141. */
  142. public function hasAnswer($answer)
  143. {
  144. $answerId = false;
  145. if (is_numeric($answer)) {
  146. $answerId = $answer;
  147. }
  148. elseif ($answer instanceof Mage_Poll_Model_Poll_Answer) {
  149. $answerId = $answer->getId();
  150. }
  151. if ($answerId) {
  152. return $this->_getResource()->checkAnswerId($this, $answerId);
  153. }
  154. return false;
  155. }
  156. public function resetVotesCount()
  157. {
  158. $this->_getResource()->resetVotesCount($this);
  159. return $this;
  160. }
  161. public function getVotedPollsIds()
  162. {
  163. $idsArray = array();
  164. foreach ($this->getCookie()->get() as $cookieName => $cookieValue) {
  165. $pattern = '#^' . preg_quote($this->_pollCookieDefaultName, '#') . '(\d+)$#';
  166. $match = array();
  167. if (preg_match($pattern, $cookieName, $match)) {
  168. if ($match[1] != Mage::getSingleton('core/session')->getJustVotedPoll()) {
  169. $idsArray[$match[1]] = $match[1];
  170. }
  171. }
  172. }
  173. // load from db for this ip
  174. foreach ($this->_getResource()->getVotedPollIdsByIp(Mage::helper('core/http')->getRemoteAddr()) as $pollId) {
  175. $idsArray[$pollId] = $pollId;
  176. }
  177. return $idsArray;
  178. }
  179. public function addAnswer($object)
  180. {
  181. $this->_answersCollection[] = $object;
  182. return $this;
  183. }
  184. public function getAnswers()
  185. {
  186. return $this->_answersCollection;
  187. }
  188. public function addStoreId($storeId)
  189. {
  190. $ids = $this->getStoreIds();
  191. if (!in_array($storeId, $ids)) {
  192. $ids[] = $storeId;
  193. }
  194. $this->setStoreIds($ids);
  195. return $this;
  196. }
  197. public function getStoreIds()
  198. {
  199. $ids = $this->_getData('store_ids');
  200. if (is_null($ids)) {
  201. $this->loadStoreIds();
  202. $ids = $this->getData('store_ids');
  203. }
  204. return $ids;
  205. }
  206. public function loadStoreIds()
  207. {
  208. $this->_getResource()->loadStoreIds($this);
  209. }
  210. public function getVotesCount()
  211. {
  212. return $this->_getData('votes_count');
  213. }
  214. }