1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Poll_Model_Resource_Poll extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('poll/poll', 'poll_id');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _initUniqueFields()
52: {
53: $this->_uniqueFields = array(array(
54: 'field' => 'poll_title',
55: 'title' => Mage::helper('poll')->__('Poll with the same question')
56: ));
57: return $this;
58: }
59:
60: 61: 62: 63: 64: 65:
66: protected function _getSelectIds($object)
67: {
68: $read = $this->_getReadAdapter();
69: $select = $read->select()
70: ->from(array('main_table'=>$this->getMainTable()), $this->getIdFieldName())
71: ->where('closed = ?', 0);
72:
73: $excludeIds = $object->getExcludeFilter();
74: if ($excludeIds) {
75: $select->where('main_table.poll_id NOT IN(?)', $excludeIds);
76: }
77:
78: $storeId = $object->getStoreFilter();
79: if ($storeId) {
80: $select->join(
81: array('store' => $this->getTable('poll/poll_store')),
82: 'main_table.poll_id=store.poll_id AND store.store_id = ' . $read->quote($storeId),
83: array()
84: );
85: }
86:
87: return $select;
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function getRandomId($object)
97: {
98: $select = $this->_getSelectIds($object)->orderRand()->limit(1);
99: return $this->_getReadAdapter()->fetchOne($select);
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function getAllIds($object)
109: {
110: $select = $this->_getSelectIds($object);
111: return $this->_getReadAdapter()->fetchCol($select);
112: }
113:
114: 115: 116: 117: 118: 119: 120:
121: public function checkAnswerId($poll, $answerId)
122: {
123: $select = $this->_getReadAdapter()->select()
124: ->from($this->getTable('poll_answer'), 'answer_id')
125: ->where('poll_id = :poll_id')
126: ->where('answer_id = :answer_id');
127: $bind = array(':poll_id' => $poll->getId(), ':answer_id' => $answerId);
128: return $this->_getReadAdapter()->fetchOne($select, $bind);
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138: 139:
140: public function getVotedPollIdsByIp($ipAddress, $pollId = false)
141: {
142:
143: if (!Mage::getModel('poll/poll')->isValidationByIp()) {
144: return array();
145: }
146:
147:
148: $select = $this->_getReadAdapter()->select()
149: ->distinct()
150: ->from($this->getTable('poll_vote'), 'poll_id')
151: ->where('ip_address = :ip_address');
152: $bind = array(':ip_address' => ip2long($ipAddress));
153: if (!empty($pollId)) {
154: $select->where('poll_id = :poll_id');
155: $bind[':poll_id'] = $pollId;
156: }
157: $result = $this->_getReadAdapter()->fetchCol($select, $bind);
158: if (empty($result)) {
159: $result = array();
160: }
161: return $result;
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function resetVotesCount($object)
171: {
172: $adapter = $this->_getWriteAdapter();
173: $select = $adapter->select()
174: ->from($this->getTable('poll_answer'), new Zend_Db_Expr("SUM(votes_count)"))
175: ->where('poll_id = ?', $object->getPollId());
176: $adapter->update(
177: $this->getMainTable(),
178: array('votes_count' => new Zend_Db_Expr("($select)")),
179: array('poll_id = ' . $adapter->quote($object->getPollId()))
180: );
181: return $object;
182: }
183:
184: 185: 186: 187: 188:
189: public function loadStoreIds(Mage_Poll_Model_Poll $object)
190: {
191: $pollId = $object->getId();
192: $storeIds = array();
193: if ($pollId) {
194: $storeIds = $this->lookupStoreIds($pollId);
195: }
196: $object->setStoreIds($storeIds);
197: }
198:
199: 200: 201: 202: 203: 204:
205: public function _afterSave(Mage_Core_Model_Abstract $object)
206: {
207:
208: $deleteWhere = $this->_getWriteAdapter()->quoteInto('poll_id = ?', $object->getId());
209: $this->_getWriteAdapter()->delete($this->getTable('poll/poll_store'), $deleteWhere);
210:
211: foreach ($object->getStoreIds() as $storeId) {
212: $pollStoreData = array(
213: 'poll_id' => $object->getId(),
214: 'store_id' => $storeId
215: );
216: $this->_getWriteAdapter()->insert($this->getTable('poll/poll_store'), $pollStoreData);
217: }
218:
219:
220: foreach ($object->getAnswers() as $answer) {
221: $answer->setPollId($object->getId());
222: $answer->save();
223: }
224: }
225:
226: 227: 228: 229: 230: 231:
232: public function lookupStoreIds($id)
233: {
234: return $this->_getReadAdapter()->fetchCol(
235: $this->_getReadAdapter()->select()
236: ->from($this->getTable('poll/poll_store'), 'store_id')
237: ->where("{$this->getIdFieldName()} = :id_field"),
238: array(':id_field' => $id)
239: );
240: }
241: }
242: