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_Block extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('cms/block', 'block_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _beforeDelete(Mage_Core_Model_Abstract $object)
53: {
54: $condition = array(
55: 'block_id = ?' => (int) $object->getId(),
56: );
57:
58: $this->_getWriteAdapter()->delete($this->getTable('cms/block_store'), $condition);
59:
60: return parent::_beforeDelete($object);
61: }
62:
63: 64: 65: 66: 67: 68:
69: protected function _beforeSave(Mage_Core_Model_Abstract $object)
70: {
71: if (!$this->getIsUniqueBlockToStores($object)) {
72: Mage::throwException(Mage::helper('cms')->__('A block identifier with the same properties already exists in the selected store.'));
73: }
74:
75: if (! $object->getId()) {
76: $object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
77: }
78: $object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());
79: return $this;
80: }
81:
82: 83: 84: 85: 86: 87:
88: protected function _afterSave(Mage_Core_Model_Abstract $object)
89: {
90: $oldStores = $this->lookupStoreIds($object->getId());
91: $newStores = (array)$object->getStores();
92:
93: $table = $this->getTable('cms/block_store');
94: $insert = array_diff($newStores, $oldStores);
95: $delete = array_diff($oldStores, $newStores);
96:
97: if ($delete) {
98: $where = array(
99: 'block_id = ?' => (int) $object->getId(),
100: 'store_id IN (?)' => $delete
101: );
102:
103: $this->_getWriteAdapter()->delete($table, $where);
104: }
105:
106: if ($insert) {
107: $data = array();
108:
109: foreach ($insert as $storeId) {
110: $data[] = array(
111: 'block_id' => (int) $object->getId(),
112: 'store_id' => (int) $storeId
113: );
114: }
115:
116: $this->_getWriteAdapter()->insertMultiple($table, $data);
117: }
118:
119: return parent::_afterSave($object);
120:
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130:
131: public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
132: {
133: if (!is_numeric($value) && is_null($field)) {
134: $field = 'identifier';
135: }
136:
137: return parent::load($object, $value, $field);
138: }
139:
140: 141: 142: 143: 144: 145:
146: protected function _afterLoad(Mage_Core_Model_Abstract $object)
147: {
148: if ($object->getId()) {
149: $stores = $this->lookupStoreIds($object->getId());
150: $object->setData('store_id', $stores);
151: $object->setData('stores', $stores);
152: }
153:
154: return parent::_afterLoad($object);
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164:
165: protected function _getLoadSelect($field, $value, $object)
166: {
167: $select = parent::_getLoadSelect($field, $value, $object);
168:
169: if ($object->getStoreId()) {
170: $stores = array(
171: (int) $object->getStoreId(),
172: Mage_Core_Model_App::ADMIN_STORE_ID,
173: );
174:
175: $select->join(
176: array('cbs' => $this->getTable('cms/block_store')),
177: $this->getMainTable().'.block_id = cbs.block_id',
178: array('store_id')
179: )->where('is_active = ?', 1)
180: ->where('cbs.store_id in (?) ', $stores)
181: ->order('store_id DESC')
182: ->limit(1);
183: }
184:
185: return $select;
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function getIsUniqueBlockToStores(Mage_Core_Model_Abstract $object)
195: {
196: if (Mage::app()->isSingleStoreMode()) {
197: $stores = array(Mage_Core_Model_App::ADMIN_STORE_ID);
198: } else {
199: $stores = (array)$object->getData('stores');
200: }
201:
202: $select = $this->_getReadAdapter()->select()
203: ->from(array('cb' => $this->getMainTable()))
204: ->join(
205: array('cbs' => $this->getTable('cms/block_store')),
206: 'cb.block_id = cbs.block_id',
207: array()
208: )->where('cb.identifier = ?', $object->getData('identifier'))
209: ->where('cbs.store_id IN (?)', $stores);
210:
211: if ($object->getId()) {
212: $select->where('cb.block_id <> ?', $object->getId());
213: }
214:
215: if ($this->_getReadAdapter()->fetchRow($select)) {
216: return false;
217: }
218:
219: return true;
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function lookupStoreIds($id)
229: {
230: $adapter = $this->_getReadAdapter();
231:
232: $select = $adapter->select()
233: ->from($this->getTable('cms/block_store'), 'store_id')
234: ->where('block_id = :block_id');
235:
236: $binds = array(
237: ':block_id' => (int) $id
238: );
239:
240: return $adapter->fetchCol($select, $binds);
241: }
242: }
243: