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_Core
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: * Stores collection
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_Resource_Store_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Load default flag
39: *
40: * @deprecated since 1.5.0.0
41: * @var boolean
42: */
43: protected $_loadDefault = false;
44:
45: /**
46: * Define resource model
47: *
48: */
49: protected function _construct()
50: {
51: $this->setFlag('load_default_store', false);
52: $this->_init('core/store');
53: }
54:
55: /**
56: * Set flag for load default (admin) store
57: *
58: * @param boolean $loadDefault
59: * @return Mage_Core_Model_Resource_Store_Collection
60: */
61: public function setLoadDefault($loadDefault)
62: {
63: $this->setFlag('load_default_store', (bool)$loadDefault);
64: return $this;
65: }
66:
67: /**
68: * Is load default (admin) store
69: *
70: * @return boolean
71: */
72: public function getLoadDefault()
73: {
74: return $this->getFlag('load_default_store');
75: }
76:
77: /**
78: * Add disable default store filter to collection
79: *
80: * @return Mage_Core_Model_Resource_Store_Collection
81: */
82: public function setWithoutDefaultFilter()
83: {
84: $this->addFieldToFilter('main_table.store_id', array('gt' => 0));
85: return $this;
86: }
87:
88: /**
89: * Add filter by group id.
90: * Group id can be passed as one single value or array of values.
91: *
92: * @param int|array $groupId
93: * @return Mage_Core_Model_Resource_Store_Collection
94: */
95: public function addGroupFilter($groupId)
96: {
97: return $this->addFieldToFilter('main_table.group_id', array('in' => $groupId));
98: }
99:
100: /**
101: * Add store id(s) filter to collection
102: *
103: * @param int|array $store
104: * @return Mage_Core_Model_Resource_Store_Collection
105: */
106: public function addIdFilter($store)
107: {
108: return $this->addFieldToFilter('main_table.store_id', array('in' => $store));
109: }
110:
111: /**
112: * Add filter by website to collection
113: *
114: * @param int|array $website
115: * @return Mage_Core_Model_Resource_Store_Collection
116: */
117: public function addWebsiteFilter($website)
118: {
119: return $this->addFieldToFilter('main_table.website_id', array('in' => $website));
120: }
121:
122: /**
123: * Add root category id filter to store collection
124: *
125: * @param int|array $category
126: * @return Mage_Core_Model_Resource_Store_Collection
127: */
128: public function addCategoryFilter($category)
129: {
130: if (!is_array($category)) {
131: $category = array($category);
132: }
133: return $this->loadByCategoryIds($category);
134: }
135:
136: /**
137: * Convert items array to array for select options
138: *
139: * @return array
140: */
141: public function toOptionArray()
142: {
143: return $this->_toOptionArray('store_id', 'name');
144: }
145:
146: /**
147: * Convert items array to hash for select options
148: *
149: * @return array
150: */
151: public function toOptionHash()
152: {
153: return $this->_toOptionHash('store_id', 'name');
154: }
155:
156: /**
157: * Load collection data
158: *
159: * @param boolean $printQuery
160: * @param boolean $logQuery
161: * @return Mage_Core_Model_Resource_Store_Collection
162: */
163: public function load($printQuery = false, $logQuery = false)
164: {
165: if (!$this->getLoadDefault()) {
166: $this->setWithoutDefaultFilter();
167: }
168:
169: if (!$this->isLoaded()) {
170: $this->addOrder('CASE WHEN main_table.store_id = 0 THEN 0 ELSE 1 END', Varien_Db_Select::SQL_ASC)
171: ->addOrder('main_table.sort_order', Varien_Db_Select::SQL_ASC)
172: ->addOrder('main_table.name', Varien_Db_Select::SQL_ASC);
173: }
174: return parent::load($printQuery, $logQuery);
175: }
176:
177: /**
178: * Add root category id filter to store collection
179: *
180: * @param array $categories
181: * @return Mage_Core_Model_Resource_Store_Collection
182: */
183: public function loadByCategoryIds(array $categories)
184: {
185: $this->addRootCategoryIdAttribute();
186: $this->addFieldToFilter('group_table.root_category_id', array('in' => $categories));
187:
188: return $this;
189: }
190:
191: /**
192: * Add store root category data to collection
193: *
194: * @return Mage_Core_Model_Resource_Store_Collection
195: */
196: public function addRootCategoryIdAttribute()
197: {
198: if (!$this->getFlag('core_store_group_table_joined')) {
199: $this->getSelect()->join(
200: array('group_table' => $this->getTable('core/store_group')),
201: 'main_table.group_id = group_table.group_id',
202: array('root_category_id')
203: );
204: $this->setFlag('core_store_group_table_joined', true);
205: }
206:
207: return $this;
208: }
209: }
210: