00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_Poll_Model_Poll extends Mage_Core_Model_Abstract
00034 {
00035 const XML_PATH_POLL_CHECK_BY_IP = 'web/polls/poll_check_by_ip';
00036
00037 protected $_pollCookieDefaultName = 'poll';
00038 protected $_answersCollection = array();
00039 protected $_storeCollection = array();
00040
00041 protected function _construct()
00042 {
00043 $this->_init('poll/poll');
00044 }
00045
00046
00047
00048
00049
00050
00051 public function getCookie()
00052 {
00053 return Mage::app()->getCookie();
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 public function getCookieName($pollId = null)
00063 {
00064 return $this->_pollCookieDefaultName . $this->getPoolId($pollId);
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 public function getPoolId($pollId = null)
00074 {
00075 if (is_null($pollId)) {
00076 $pollId = $this->getId();
00077 }
00078 return $pollId;
00079 }
00080
00081
00082
00083
00084
00085
00086 public function isValidationByIp()
00087 {
00088 return (1 == Mage::getStoreConfig(self::XML_PATH_POLL_CHECK_BY_IP));
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 public function setVoted($pollId=null)
00098 {
00099 $this->getCookie()->set($this->getCookieName($pollId), $this->getPoolId($pollId));
00100
00101 return $this;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 public function isVoted($pollId = null)
00111 {
00112 $pollId = $this->getPoolId($pollId);
00113
00114
00115 $cookie = $this->getCookie()->get($this->getCookieName($pollId));
00116 if (false !== $cookie) {
00117 return true;
00118 }
00119
00120
00121 if (count($this->_getResource()->getVotedPollIdsByIp($_SERVER['REMOTE_ADDR'], $pollId))) {
00122 return true;
00123 }
00124
00125 return false;
00126 }
00127
00128
00129
00130
00131
00132
00133 public function getRandomId()
00134 {
00135 return $this->_getResource()->getRandomId($this);
00136 }
00137
00138
00139
00140
00141
00142
00143 public function addVote(Mage_Poll_Model_Poll_Vote $vote)
00144 {
00145 if ($this->hasAnswer($vote->getPollAnswerId())) {
00146 $vote->setPollId($this->getId())
00147 ->save();
00148 $this->setVoted();
00149 }
00150 return $this;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 public function hasAnswer($answer)
00160 {
00161 $answerId = false;
00162 if (is_numeric($answer)) {
00163 $answerId = $answer;
00164 }
00165 elseif ($answer instanceof Mage_Poll_Model_Poll_Answer) {
00166 $answerId = $answer->getId();
00167 }
00168
00169 if ($answerId) {
00170 return $this->_getResource()->checkAnswerId($this, $answerId);
00171 }
00172 return false;
00173 }
00174
00175 public function resetVotesCount()
00176 {
00177 $this->_getResource()->resetVotesCount($this);
00178 return $this;
00179 }
00180
00181
00182 public function getVotedPollsIds()
00183 {
00184 $idsArray = array();
00185
00186 foreach ($this->getCookie()->get() as $cookieName => $cookieValue) {
00187 $pattern = '#^' . preg_quote($this->_pollCookieDefaultName, '#') . '(\d+)$#';
00188 $match = array();
00189 if (preg_match($pattern, $cookieName, $match)) {
00190 if ($match[1] != Mage::getSingleton('core/session')->getJustVotedPoll()) {
00191 $idsArray[$match[1]] = $match[1];
00192 }
00193 }
00194 }
00195
00196
00197 foreach ($this->_getResource()->getVotedPollIdsByIp($_SERVER['REMOTE_ADDR']) as $pollId) {
00198 $idsArray[$pollId] = $pollId;
00199 }
00200
00201 return $idsArray;
00202 }
00203
00204 public function addAnswer($object)
00205 {
00206 $this->_answersCollection[] = $object;
00207 return $this;
00208 }
00209
00210 public function getAnswers()
00211 {
00212 return $this->_answersCollection;
00213 }
00214
00215 public function addStoreId($storeId)
00216 {
00217 $ids = $this->getStoreIds();
00218 if (!in_array($storeId, $ids)) {
00219 $ids[] = $storeId;
00220 }
00221 $this->setStoreIds($ids);
00222 return $this;
00223 }
00224
00225 public function getStoreIds()
00226 {
00227 $ids = $this->_getData('store_ids');
00228 if (is_null($ids)) {
00229 $this->loadStoreIds();
00230 $ids = $this->getData('store_ids');
00231 }
00232 return $ids;
00233 }
00234
00235 public function loadStoreIds()
00236 {
00237 $this->_getResource()->loadStoreIds($this);
00238 }
00239
00240 public function getVotesCount()
00241 {
00242 return $this->_getData('votes_count');
00243 }
00244
00245 }