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_Mysql4_Poll extends Mage_Core_Model_Mysql4_Abstract
00034 {
00035 protected function _construct()
00036 {
00037 $this->_init('poll/poll', 'poll_id');
00038 }
00039
00040
00041
00042
00043
00044
00045 protected function _initUniqueFields()
00046 {
00047 $this->_uniqueFields = array(array(
00048 'field' => 'poll_title',
00049 'title' => Mage::helper('poll')->__('Poll with the same question')
00050 ));
00051 return $this;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 public function getRandomId($object)
00061 {
00062 $read = $this->_getReadAdapter();
00063 $select = $read->select();
00064
00065 if ($object->getExcludeFilter()) {
00066 $select->where('main_table.poll_id NOT IN(?)', $object->getExcludeFilter());
00067 }
00068
00069 $select->from(array('main_table'=>$this->getMainTable()), $this->getIdFieldName())
00070 ->where('closed = ?', 0)
00071 ->order(new Zend_Db_Expr('RAND()'))
00072 ->limit(1);
00073
00074 if (($storeId = $object->getStoreFilter())) {
00075 $select->join(
00076 array('store' => $this->getTable('poll/poll_store')),
00077 $read->quoteInto('main_table.poll_id=store.poll_id AND store.store_id = ?', $storeId),
00078 array()
00079 );
00080 }
00081
00082 return $read->fetchOne($select);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 public function checkAnswerId($poll, $answerId)
00093 {
00094 $select = $this->_getReadAdapter()->select()
00095 ->from($this->getTable('poll_answer'), 'answer_id')
00096 ->where('poll_id=?', $poll->getId())
00097 ->where('answer_id=?', $answerId);
00098 return $this->_getReadAdapter()->fetchOne($select);
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 public function getVotedPollIdsByIp($ipAddress, $pollId = false)
00112 {
00113
00114 if (!Mage::getModel('poll/poll')->isValidationByIp()) {
00115 return array();
00116 }
00117
00118
00119 $select = $this->_getReadAdapter()->select()
00120 ->distinct()
00121 ->from($this->getTable('poll_vote'), 'poll_id')
00122 ->where('ip_address=?', ip2long($ipAddress));
00123 if (!empty($pollId)) {
00124 $select->where('poll_id=?', $pollId);
00125 }
00126 $result = $this->_getReadAdapter()->fetchCol($select);
00127 if (empty($result)) {
00128 $result = array();
00129 }
00130 return $result;
00131 }
00132
00133 public function resetVotesCount($object)
00134 {
00135 $read = $this->_getReadAdapter();
00136 $select = $read->select();
00137 $select->from($this->getTable('poll_answer'), new Zend_Db_Expr("SUM(votes_count)"))
00138 ->where("poll_id = ?", $object->getPollId());
00139
00140 $count = $read->fetchOne($select);
00141
00142 $write = $this->_getWriteAdapter();
00143 $condition = $write->quoteInto("{$this->getIdFieldName()} = ?", $object->getPollId());
00144 $write->update($this->getMainTable(), array('votes_count' => $count), $condition);
00145 return $object;
00146 }
00147
00148
00149 public function loadStoreIds(Mage_Poll_Model_Poll $object)
00150 {
00151 $pollId = $object->getId();
00152 $storeIds = array();
00153 if ($pollId) {
00154 $storeIds = $this->lookupStoreIds($pollId);
00155 }
00156 $object->setStoreIds($storeIds);
00157 }
00158
00159 public function _afterSave(Mage_Core_Model_Abstract $object)
00160 {
00161
00162 $deleteWhere = $this->_getWriteAdapter()->quoteInto('poll_id = ?', $object->getId());
00163 $this->_getWriteAdapter()->delete($this->getTable('poll/poll_store'), $deleteWhere);
00164
00165 foreach ($object->getStoreIds() as $storeId) {
00166 $pollStoreData = array(
00167 'poll_id' => $object->getId(),
00168 'store_id' => $storeId
00169 );
00170 $this->_getWriteAdapter()->insert($this->getTable('poll/poll_store'), $pollStoreData);
00171 }
00172
00173
00174 foreach ($object->getAnswers() as $answer) {
00175 $answer->setPollId($object->getId());
00176 $answer->save();
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 public function lookupStoreIds($id)
00187 {
00188 return $this->_getReadAdapter()->fetchCol($this->_getReadAdapter()->select()
00189 ->from($this->getTable('poll/poll_store'), 'store_id')
00190 ->where("{$this->getIdFieldName()} = ?", $id)
00191 );
00192 }
00193 }