00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Cms_Model_Mysql4_Page extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040 protected function _construct()
00041 {
00042 $this->_init('cms/page', 'page_id');
00043 }
00044
00045
00046
00047
00048
00049
00050 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00051 {
00052 $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00053 foreach (array('custom_theme_from', 'custom_theme_to') as $dataKey) {
00054 if ($date = $object->getData($dataKey)) {
00055 $object->setData($dataKey, Mage::app()->getLocale()->date($date, $format, null, false)
00056 ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT)
00057 );
00058 }
00059 else {
00060 $object->setData($dataKey, new Zend_Db_Expr('NULL'));
00061 }
00062 }
00063
00064 if (!$this->getIsUniquePageToStores($object)) {
00065 Mage::throwException(Mage::helper('cms')->__('Page Identifier for specified store already exist.'));
00066 }
00067
00068 if ($this->isNumericPageIdentifier($object)) {
00069 Mage::throwException(Mage::helper('cms')->__('Page Identifier cannot consist only of numbers.'));
00070 }
00071
00072 if (! $object->getId()) {
00073 $object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
00074 }
00075
00076 $object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());
00077 return $this;
00078 }
00079
00080
00081
00082
00083
00084
00085 protected function _afterSave(Mage_Core_Model_Abstract $object)
00086 {
00087 $condition = $this->_getWriteAdapter()->quoteInto('page_id = ?', $object->getId());
00088 $this->_getWriteAdapter()->delete($this->getTable('cms/page_store'), $condition);
00089
00090 foreach ((array)$object->getData('stores') as $store) {
00091 $storeArray = array();
00092 $storeArray['page_id'] = $object->getId();
00093 $storeArray['store_id'] = $store;
00094 $this->_getWriteAdapter()->insert($this->getTable('cms/page_store'), $storeArray);
00095 }
00096
00097 return parent::_afterSave($object);
00098 }
00099
00100 public function load(Mage_Core_Model_Abstract $object, $value, $field=null)
00101 {
00102 if (strcmp($value, (int)$value) !== 0) {
00103 $field = 'identifier';
00104 }
00105 return parent::load($object, $value, $field);
00106 }
00107
00108
00109
00110
00111
00112 protected function _afterLoad(Mage_Core_Model_Abstract $object)
00113 {
00114 $select = $this->_getReadAdapter()->select()
00115 ->from($this->getTable('cms/page_store'))
00116 ->where('page_id = ?', $object->getId());
00117
00118 if ($data = $this->_getReadAdapter()->fetchAll($select)) {
00119 $storesArray = array();
00120 foreach ($data as $row) {
00121 $storesArray[] = $row['store_id'];
00122 }
00123 $object->setData('store_id', $storesArray);
00124 }
00125
00126 return parent::_afterLoad($object);
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 protected function _getLoadSelect($field, $value, $object)
00137 {
00138 $select = parent::_getLoadSelect($field, $value, $object);
00139
00140 if ($object->getStoreId()) {
00141 $select->join(
00142 array('cps' => $this->getTable('cms/page_store')),
00143 $this->getMainTable().'.page_id = `cps`.page_id'
00144 )
00145 ->where('is_active=1 AND `cps`.store_id in (0, ?) ', $object->getStoreId())
00146 ->order('store_id DESC')
00147 ->limit(1);
00148 }
00149 return $select;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 public function getIsUniquePageToStores(Mage_Core_Model_Abstract $object)
00159 {
00160 $select = $this->_getWriteAdapter()->select()
00161 ->from($this->getMainTable())
00162 ->join(array('cps' => $this->getTable('cms/page_store')), $this->getMainTable().'.page_id = `cps`.page_id')
00163 ->where($this->getMainTable().'.identifier = ?', $object->getData('identifier'));
00164 if ($object->getId()) {
00165 $select->where($this->getMainTable().'.page_id <> ?',$object->getId());
00166 }
00167 $select->where('`cps`.store_id IN (?)', (array)$object->getData('stores'));
00168
00169 if ($this->_getWriteAdapter()->fetchRow($select)) {
00170 return false;
00171 }
00172
00173 return true;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 protected function isNumericPageIdentifier (Mage_Core_Model_Abstract $object)
00184 {
00185 return preg_match('/^[0-9]+$/', $object->getData('identifier'));
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 public function checkIdentifier($identifier, $storeId)
00197 {
00198 $select = $this->_getReadAdapter()->select()->from(array('main_table'=>$this->getMainTable()), 'page_id')
00199 ->join(
00200 array('cps' => $this->getTable('cms/page_store')),
00201 'main_table.page_id = `cps`.page_id'
00202 )
00203 ->where('main_table.identifier=?', $identifier)
00204 ->where('main_table.is_active=1 AND `cps`.store_id in (0, ?) ', $storeId)
00205 ->order('store_id DESC');
00206
00207 return $this->_getReadAdapter()->fetchOne($select);
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 public function lookupStoreIds($id)
00217 {
00218 return $this->_getReadAdapter()->fetchCol($this->_getReadAdapter()->select()
00219 ->from($this->getTable('cms/page_store'), 'store_id')
00220 ->where("{$this->getIdFieldName()} = ?", $id)
00221 );
00222 }
00223 }