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: /**
29: * Pool Mysql4 collection model resource
30: *
31: * @category Mage
32: * @package Mage_Poll
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Poll_Model_Resource_Poll_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Initialize collection
39: *
40: */
41: public function _construct()
42: {
43: $this->_init('poll/poll');
44: }
45:
46: /**
47: * Redefine default filters
48: *
49: * @param string $field
50: * @param mixed $condition
51: * @return Varien_Data_Collection_Db
52: */
53: public function addFieldToFilter($field, $condition = null)
54: {
55: if ($field == 'stores') {
56: return $this->addStoresFilter($condition);
57: } else {
58: return parent::addFieldToFilter($field, $condition);
59: }
60: }
61:
62: /**
63: * Add store filter
64: *
65: * @deprecated
66: * @param mixed $store
67: * @return Mage_Poll_Model_Resource_Poll_Collection
68: */
69: public function addStoresFilter($store)
70: {
71: return $this->addStoreFilter($store);
72: }
73:
74: /**
75: * Add Stores Filter
76: *
77: * @param mixed $storeId
78: * @param bool $withAdmin
79: * @return Mage_Poll_Model_Resource_Poll_Collection
80: */
81: public function addStoreFilter($storeId, $withAdmin = true)
82: {
83: $this->getSelect()->join(
84: array('store_table' => $this->getTable('poll/poll_store')),
85: 'main_table.poll_id = store_table.poll_id',
86: array()
87: )
88: ->where('store_table.store_id in (?)', ($withAdmin ? array(0, $storeId) : $storeId))
89: ->group('main_table.poll_id');
90:
91: /*
92: * Allow analytic functions usage
93: */
94: $this->_useAnalyticFunction = true;
95:
96: return $this;
97: }
98:
99: /**
100: * Add stores data
101: *
102: * @return Mage_Poll_Model_Resource_Poll_Collection
103: */
104: public function addStoreData()
105: {
106: $pollIds = $this->getColumnValues('poll_id');
107: $storesToPoll = array();
108:
109: if (count($pollIds) > 0) {
110: $select = $this->getConnection()->select()
111: ->from($this->getTable('poll/poll_store'))
112: ->where('poll_id IN(?)', $pollIds);
113: $result = $this->getConnection()->fetchAll($select);
114:
115: foreach ($result as $row) {
116: if (!isset($storesToPoll[$row['poll_id']])) {
117: $storesToPoll[$row['poll_id']] = array();
118: }
119: $storesToPoll[$row['poll_id']][] = $row['store_id'];
120: }
121: }
122:
123: foreach ($this as $item) {
124: if (isset($storesToPoll[$item->getId()])) {
125: $item->setStores($storesToPoll[$item->getId()]);
126: } else {
127: $item->setStores(array());
128: }
129: }
130:
131: return $this;
132: }
133:
134: /**
135: * Set stores of the current poll
136: *
137: * @return Mage_Poll_Model_Resource_Poll_Collection
138: */
139: public function addSelectStores()
140: {
141: $pollId = $this->getId();
142: $select = $this->getConnection()->select()
143: ->from($this->getTable('poll/poll_store'), array('stor_id'))
144: ->where('poll_id = :poll_id');
145: $stores = $this->getConnection()->fetchCol($select, array(':poll_id' => $pollId));
146: $this->setSelectStores($stores);
147:
148: return $this;
149: }
150: }
151: