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_Cms_Model_Resource_Page_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_previewFlag;
43:
44:
45: 46: 47: 48:
49: protected function _construct()
50: {
51: $this->_init('cms/page');
52: $this->_map['fields']['page_id'] = 'main_table.page_id';
53: $this->_map['fields']['store'] = 'store_table.store_id';
54: }
55:
56: 57: 58: 59: 60:
61: public function toOptionArray()
62: {
63: return $this->_toOptionArray('identifier', 'title');
64: }
65:
66: 67: 68: 69: 70: 71:
72: public function toOptionIdArray()
73: {
74: $res = array();
75: $existingIdentifiers = array();
76: foreach ($this as $item) {
77: $identifier = $item->getData('identifier');
78:
79: $data['value'] = $identifier;
80: $data['label'] = $item->getData('title');
81:
82: if (in_array($identifier, $existingIdentifiers)) {
83: $data['value'] .= '|' . $item->getData('page_id');
84: } else {
85: $existingIdentifiers[] = $identifier;
86: }
87:
88: $res[] = $data;
89: }
90:
91: return $res;
92: }
93:
94: 95: 96: 97: 98: 99:
100: public function setFirstStoreFlag($flag = false)
101: {
102: $this->_previewFlag = $flag;
103: return $this;
104: }
105:
106: 107: 108: 109: 110:
111: protected function _afterLoad()
112: {
113: if ($this->_previewFlag) {
114: $items = $this->getColumnValues('page_id');
115: $connection = $this->getConnection();
116: if (count($items)) {
117: $select = $connection->select()
118: ->from(array('cps'=>$this->getTable('cms/page_store')))
119: ->where('cps.page_id IN (?)', $items);
120:
121: if ($result = $connection->fetchPairs($select)) {
122: foreach ($this as $item) {
123: if (!isset($result[$item->getData('page_id')])) {
124: continue;
125: }
126: if ($result[$item->getData('page_id')] == 0) {
127: $stores = Mage::app()->getStores(false, true);
128: $storeId = current($stores)->getId();
129: $storeCode = key($stores);
130: } else {
131: $storeId = $result[$item->getData('page_id')];
132: $storeCode = Mage::app()->getStore($storeId)->getCode();
133: }
134: $item->setData('_first_store_id', $storeId);
135: $item->setData('store_code', $storeCode);
136: }
137: }
138: }
139: }
140:
141: return parent::_afterLoad();
142: }
143:
144: 145: 146: 147: 148: 149: 150:
151: public function addStoreFilter($store, $withAdmin = true)
152: {
153: if (!$this->getFlag('store_filter_added')) {
154: if ($store instanceof Mage_Core_Model_Store) {
155: $store = array($store->getId());
156: }
157:
158: if (!is_array($store)) {
159: $store = array($store);
160: }
161:
162: if ($withAdmin) {
163: $store[] = Mage_Core_Model_App::ADMIN_STORE_ID;
164: }
165:
166: $this->addFilter('store', array('in' => $store), 'public');
167: }
168: return $this;
169: }
170:
171: 172: 173:
174: protected function _renderFiltersBefore()
175: {
176: if ($this->getFilter('store')) {
177: $this->getSelect()->join(
178: array('store_table' => $this->getTable('cms/page_store')),
179: 'main_table.page_id = store_table.page_id',
180: array()
181: )->group('main_table.page_id');
182:
183: 184: 185:
186: $this->_useAnalyticFunction = true;
187: }
188: return parent::_renderFiltersBefore();
189: }
190:
191:
192: 193: 194: 195: 196: 197:
198: public function getSelectCountSql()
199: {
200: $countSelect = parent::getSelectCountSql();
201:
202: $countSelect->reset(Zend_Db_Select::GROUP);
203:
204: return $countSelect;
205: }
206: }
207: