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: * Websites collection
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_Resource_Website_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * @deprecated since 1.5.0.0
39: */
40: protected $_loadDefault = false;
41:
42: /**
43: * Map field to alias
44: *
45: * @var array
46: */
47: protected $_map = array('fields' => array('website_id' => 'main_table.website_id'));
48:
49: /**
50: * Define resource model
51: *
52: */
53: protected function _construct()
54: {
55: $this->setFlag('load_default_website', false);
56: $this->_init('core/website');
57: }
58:
59: /**
60: * Set flag for load default (admin) website
61: *
62: * @param boolean $loadDefault
63: * @return Mage_Core_Model_Resource_Website_Collection
64: */
65: public function setLoadDefault($loadDefault)
66: {
67: $this->setFlag('load_default_website', (bool)$loadDefault);
68: return $this;
69: }
70:
71: /**
72: * Is load default (admin) website
73: *
74: * @return boolean
75: */
76: public function getLoadDefault()
77: {
78: return $this->getFlag('load_default_website');
79: }
80:
81: /**
82: * Convert items array to array for select options
83: *
84: * @return Array
85: */
86: public function toOptionArray()
87: {
88: return $this->_toOptionArray('website_id', 'name');
89: }
90:
91: /**
92: * Convert items array to hash for select options
93: *
94: * @return Array
95: */
96: public function toOptionHash()
97: {
98: return $this->_toOptionHash('website_id', 'name');
99: }
100:
101:
102: /**
103: * Add website filter to collection
104: *
105: * @param int $ids|array
106: * @return Mage_Core_Model_Resource_Website_Collection
107: */
108: public function addIdFilter($ids)
109: {
110: if (is_array($ids)) {
111: if (empty($ids)) {
112: $this->addFieldToFilter('website_id', null);
113: } else {
114: $this->addFieldToFilter('website_id', array('in' => $ids));
115: }
116: } else {
117: $this->addFieldToFilter('website_id', $ids);
118: }
119: return $this;
120: }
121:
122: /**
123: * Load collection data
124: *
125: * @param boolean $printQuery
126: * @param boolean $logQuery
127: * @return Mage_Core_Model_Resource_Website_Collection
128: */
129: public function load($printQuery = false, $logQuery = false)
130: {
131: if (!$this->getLoadDefault()) {
132: $this->getSelect()->where('main_table.website_id > ?', 0);
133: }
134: $this->unshiftOrder('main_table.name', Varien_Db_Select::SQL_ASC) // website name SECOND
135: ->unshiftOrder('main_table.sort_order', Varien_Db_Select::SQL_ASC); // website sort order FIRST
136:
137: return parent::load($printQuery, $logQuery);
138:
139: }
140:
141: /**
142: * Join group and store info from appropriate tables.
143: * Defines new _idFiledName as 'website_group_store' bc for
144: * one website can be more then one row in collection.
145: * Sets extra combined ordering by group's name, defined
146: * sort ordering and store's name.
147: *
148: * @return Mage_Core_Model_Resource_Website_Collection
149: */
150: public function joinGroupAndStore()
151: {
152: if (!$this->getFlag('groups_and_stores_joined')) {
153: $this->_idFieldName = 'website_group_store';
154: $this->getSelect()->joinLeft(
155: array('group_table' => $this->getTable('core/store_group')),
156: 'main_table.website_id = group_table.website_id',
157: array('group_id' => 'group_id', 'group_title' => 'name')
158: )->joinLeft(
159: array('store_table' => $this->getTable('core/store')),
160: 'group_table.group_id = store_table.group_id',
161: array('store_id' => 'store_id', 'store_title' => 'name')
162: );
163: $this->addOrder('group_table.name', Varien_Db_Select::SQL_ASC) // store name
164: ->addOrder('CASE WHEN store_table.store_id = 0 THEN 0 ELSE 1 END', Varien_Db_Select::SQL_ASC) // view is admin
165: ->addOrder('store_table.sort_order', Varien_Db_Select::SQL_ASC) // view sort order
166: ->addOrder('store_table.name', Varien_Db_Select::SQL_ASC) // view name
167: ;
168: $this->setFlag('groups_and_stores_joined', true);
169: }
170: return $this;
171: }
172:
173: /**
174: * Adding filter by group id or array of ids but only if
175: * tables with appropriate information were joined before.
176: *
177: * @param int|array $groupIds
178: * @return Mage_Core_Model_Resource_Website_Collection
179: */
180: public function addFilterByGroupIds($groupIds)
181: {
182: if ($this->getFlag('groups_and_stores_joined')) {
183: $this->addFieldToFilter('group_table.group_id', $groupIds);
184: }
185: return $this;
186: }
187: }
188: