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_Block extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038 protected function _construct()
00039 {
00040 $this->_init('cms/block', 'block_id');
00041 }
00042
00043
00044
00045
00046
00047
00048 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00049 {
00050 if (!$this->getIsUniqueBlockToStores($object)) {
00051 Mage::throwException(Mage::helper('cms')->__('Such a block identifier in selected store already exist.'));
00052 }
00053
00054 if (! $object->getId()) {
00055 $object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
00056 }
00057 $object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());
00058 return $this;
00059 }
00060
00061
00062
00063
00064
00065 protected function _afterSave(Mage_Core_Model_Abstract $object)
00066 {
00067 $condition = $this->_getWriteAdapter()->quoteInto('block_id = ?', $object->getId());
00068 $this->_getWriteAdapter()->delete($this->getTable('cms/block_store'), $condition);
00069
00070 foreach ((array)$object->getData('stores') as $store) {
00071 $storeArray = array();
00072 $storeArray['block_id'] = $object->getId();
00073 $storeArray['store_id'] = $store;
00074 $this->_getWriteAdapter()->insert($this->getTable('cms/block_store'), $storeArray);
00075 }
00076
00077 return parent::_afterSave($object);
00078 }
00079
00080 public function load(Mage_Core_Model_Abstract $object, $value, $field=null)
00081 {
00082
00083 if (!intval($value) && is_string($value)) {
00084 $field = 'identifier';
00085 }
00086 return parent::load($object, $value, $field);
00087 }
00088
00089
00090
00091
00092
00093 protected function _afterLoad(Mage_Core_Model_Abstract $object)
00094 {
00095 $select = $this->_getReadAdapter()->select()
00096 ->from($this->getTable('cms/block_store'))
00097 ->where('block_id = ?', $object->getId());
00098
00099 if ($data = $this->_getReadAdapter()->fetchAll($select)) {
00100 $storesArray = array();
00101 foreach ($data as $row) {
00102 $storesArray[] = $row['store_id'];
00103 }
00104 $object->setData('store_id', $storesArray);
00105 }
00106
00107 return parent::_afterLoad($object);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117 protected function _getLoadSelect($field, $value, $object)
00118 {
00119
00120 $select = parent::_getLoadSelect($field, $value, $object);
00121
00122 if ($object->getStoreId()) {
00123 $select->join(array('cbs' => $this->getTable('cms/block_store')), $this->getMainTable().'.block_id = cbs.block_id')
00124 ->where('is_active=1 AND cbs.store_id in (0, ?) ', $object->getStoreId())
00125 ->order('store_id DESC')
00126 ->limit(1);
00127 }
00128 return $select;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 public function getIsUniqueBlockToStores(Mage_Core_Model_Abstract $object)
00138 {
00139 $select = $this->_getWriteAdapter()->select()
00140 ->from($this->getMainTable())
00141 ->join(array('cbs' => $this->getTable('cms/block_store')), $this->getMainTable().'.block_id = `cbs`.block_id')
00142 ->where($this->getMainTable().'.identifier = ?', $object->getData('identifier'));
00143 if ($object->getId()) {
00144 $select->where($this->getMainTable().'.block_id <> ?',$object->getId());
00145 }
00146 $select->where('`cbs`.store_id IN (?)', (array)$object->getData('stores'));
00147
00148 if ($this->_getWriteAdapter()->fetchRow($select)) {
00149 return false;
00150 }
00151
00152 return true;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 public function lookupStoreIds($id)
00162 {
00163 return $this->_getReadAdapter()->fetchCol($this->_getReadAdapter()->select()
00164 ->from($this->getTable('cms/block_store'), 'store_id')
00165 ->where("{$this->getIdFieldName()} = ?", $id)
00166 );
00167 }
00168 }