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 extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_store = null;
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: $this->_init('cms/page', 'page_id');
51: }
52:
53: 54: 55: 56: 57: 58:
59: protected function _beforeDelete(Mage_Core_Model_Abstract $object)
60: {
61: $condition = array(
62: 'page_id = ?' => (int) $object->getId(),
63: );
64:
65: $this->_getWriteAdapter()->delete($this->getTable('cms/page_store'), $condition);
66:
67: return parent::_beforeDelete($object);
68: }
69:
70: 71: 72: 73: 74: 75:
76: protected function _beforeSave(Mage_Core_Model_Abstract $object)
77: {
78: 79: 80: 81: 82: 83:
84: foreach (array('custom_theme_from', 'custom_theme_to') as $field) {
85: $value = !$object->getData($field) ? null : $object->getData($field);
86: $object->setData($field, $this->formatDate($value));
87: }
88:
89: if (!$this->getIsUniquePageToStores($object)) {
90: Mage::throwException(Mage::helper('cms')->__('A page URL key for specified store already exists.'));
91: }
92:
93: if (!$this->isValidPageIdentifier($object)) {
94: Mage::throwException(Mage::helper('cms')->__('The page URL key contains capital letters or disallowed symbols.'));
95: }
96:
97: if ($this->isNumericPageIdentifier($object)) {
98: Mage::throwException(Mage::helper('cms')->__('The page URL key cannot consist only of numbers.'));
99: }
100:
101:
102: if ($object->isObjectNew() && !$object->hasCreationTime()) {
103: $object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
104: }
105:
106: $object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());
107:
108: return parent::_beforeSave($object);
109: }
110:
111: 112: 113: 114: 115: 116:
117: protected function _afterSave(Mage_Core_Model_Abstract $object)
118: {
119: $oldStores = $this->lookupStoreIds($object->getId());
120: $newStores = (array)$object->getStores();
121: if (empty($newStores)) {
122: $newStores = (array)$object->getStoreId();
123: }
124: $table = $this->getTable('cms/page_store');
125: $insert = array_diff($newStores, $oldStores);
126: $delete = array_diff($oldStores, $newStores);
127:
128: if ($delete) {
129: $where = array(
130: 'page_id = ?' => (int) $object->getId(),
131: 'store_id IN (?)' => $delete
132: );
133:
134: $this->_getWriteAdapter()->delete($table, $where);
135: }
136:
137: if ($insert) {
138: $data = array();
139:
140: foreach ($insert as $storeId) {
141: $data[] = array(
142: 'page_id' => (int) $object->getId(),
143: 'store_id' => (int) $storeId
144: );
145: }
146:
147: $this->_getWriteAdapter()->insertMultiple($table, $data);
148: }
149:
150: return parent::_afterSave($object);
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160:
161: public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
162: {
163: if (!is_numeric($value) && is_null($field)) {
164: $field = 'identifier';
165: }
166:
167: return parent::load($object, $value, $field);
168: }
169:
170: 171: 172: 173: 174: 175:
176: protected function _afterLoad(Mage_Core_Model_Abstract $object)
177: {
178: if ($object->getId()) {
179: $stores = $this->lookupStoreIds($object->getId());
180:
181: $object->setData('store_id', $stores);
182:
183: }
184:
185: return parent::_afterLoad($object);
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195:
196: protected function _getLoadSelect($field, $value, $object)
197: {
198: $select = parent::_getLoadSelect($field, $value, $object);
199:
200: if ($object->getStoreId()) {
201: $storeIds = array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId());
202: $select->join(
203: array('cms_page_store' => $this->getTable('cms/page_store')),
204: $this->getMainTable() . '.page_id = cms_page_store.page_id',
205: array())
206: ->where('is_active = ?', 1)
207: ->where('cms_page_store.store_id IN (?)', $storeIds)
208: ->order('cms_page_store.store_id DESC')
209: ->limit(1);
210: }
211:
212: return $select;
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222:
223: protected function _getLoadByIdentifierSelect($identifier, $store, $isActive = null)
224: {
225: $select = $this->_getReadAdapter()->select()
226: ->from(array('cp' => $this->getMainTable()))
227: ->join(
228: array('cps' => $this->getTable('cms/page_store')),
229: 'cp.page_id = cps.page_id',
230: array())
231: ->where('cp.identifier = ?', $identifier)
232: ->where('cps.store_id IN (?)', $store);
233:
234: if (!is_null($isActive)) {
235: $select->where('cp.is_active = ?', $isActive);
236: }
237:
238: return $select;
239: }
240:
241: 242: 243: 244: 245: 246:
247: public function getIsUniquePageToStores(Mage_Core_Model_Abstract $object)
248: {
249: if (Mage::app()->isSingleStoreMode() || !$object->hasStores()) {
250: $stores = array(Mage_Core_Model_App::ADMIN_STORE_ID);
251: } else {
252: $stores = (array)$object->getData('stores');
253: }
254:
255: $select = $this->_getLoadByIdentifierSelect($object->getData('identifier'), $stores);
256:
257: if ($object->getId()) {
258: $select->where('cps.page_id <> ?', $object->getId());
259: }
260:
261: if ($this->_getWriteAdapter()->fetchRow($select)) {
262: return false;
263: }
264:
265: return true;
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275:
276: protected function isNumericPageIdentifier(Mage_Core_Model_Abstract $object)
277: {
278: return preg_match('/^[0-9]+$/', $object->getData('identifier'));
279: }
280:
281: 282: 283: 284: 285: 286:
287: protected function isValidPageIdentifier(Mage_Core_Model_Abstract $object)
288: {
289: return preg_match('/^[a-z0-9][a-z0-9_\/-]+(\.[a-z0-9_-]+)?$/', $object->getData('identifier'));
290: }
291:
292:
293:
294: 295: 296: 297: 298: 299: 300: 301:
302: public function checkIdentifier($identifier, $storeId)
303: {
304: $stores = array(Mage_Core_Model_App::ADMIN_STORE_ID, $storeId);
305: $select = $this->_getLoadByIdentifierSelect($identifier, $stores, 1);
306: $select->reset(Zend_Db_Select::COLUMNS)
307: ->columns('cp.page_id')
308: ->order('cps.store_id DESC')
309: ->limit(1);
310:
311: return $this->_getReadAdapter()->fetchOne($select);
312: }
313:
314: 315: 316: 317: 318: 319:
320: public function getCmsPageTitleByIdentifier($identifier)
321: {
322: $stores = array(Mage_Core_Model_App::ADMIN_STORE_ID);
323: if ($this->_store) {
324: $stores[] = (int)$this->getStore()->getId();
325: }
326:
327: $select = $this->_getLoadByIdentifierSelect($identifier, $stores);
328: $select->reset(Zend_Db_Select::COLUMNS)
329: ->columns('cp.title')
330: ->order('cps.store_id DESC')
331: ->limit(1);
332:
333: return $this->_getReadAdapter()->fetchOne($select);
334: }
335:
336: 337: 338: 339: 340: 341:
342: public function getCmsPageTitleById($id)
343: {
344: $adapter = $this->_getReadAdapter();
345:
346: $select = $adapter->select()
347: ->from($this->getMainTable(), 'title')
348: ->where('page_id = :page_id');
349:
350: $binds = array(
351: 'page_id' => (int) $id
352: );
353:
354: return $adapter->fetchOne($select, $binds);
355: }
356:
357: 358: 359: 360: 361: 362:
363: public function getCmsPageIdentifierById($id)
364: {
365: $adapter = $this->_getReadAdapter();
366:
367: $select = $adapter->select()
368: ->from($this->getMainTable(), 'identifier')
369: ->where('page_id = :page_id');
370:
371: $binds = array(
372: 'page_id' => (int) $id
373: );
374:
375: return $adapter->fetchOne($select, $binds);
376: }
377:
378: 379: 380: 381: 382: 383:
384: public function lookupStoreIds($pageId)
385: {
386: $adapter = $this->_getReadAdapter();
387:
388: $select = $adapter->select()
389: ->from($this->getTable('cms/page_store'), 'store_id')
390: ->where('page_id = ?',(int)$pageId);
391:
392: return $adapter->fetchCol($select);
393: }
394:
395: 396: 397: 398: 399: 400:
401: public function setStore($store)
402: {
403: $this->_store = $store;
404: return $this;
405: }
406:
407: 408: 409: 410: 411:
412: public function getStore()
413: {
414: return Mage::app()->getStore($this->_store);
415: }
416: }
417: