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_Widget_Model_Resource_Widget_Instance extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('widget/widget_instance', 'instance_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _afterLoad(Mage_Core_Model_Abstract $object)
53: {
54: $adapter = $this->_getReadAdapter();
55: $select = $adapter->select()
56: ->from($this->getTable('widget/widget_instance_page'))
57: ->where('instance_id = ?', (int)$object->getId());
58: $result = $adapter->fetchAll($select);
59: $object->setData('page_groups', $result);
60: return parent::_afterLoad($object);
61: }
62:
63: 64: 65: 66: 67: 68:
69: protected function _afterSave(Mage_Core_Model_Abstract $object)
70: {
71: $pageTable = $this->getTable('widget/widget_instance_page');
72: $pageLayoutTable = $this->getTable('widget/widget_instance_page_layout');
73: $readAdapter = $this->_getReadAdapter();
74: $writeAdapter = $this->_getWriteAdapter();
75:
76: $select = $readAdapter->select()
77: ->from($pageTable, 'page_id')
78: ->where('instance_id = ?', (int)$object->getId());
79: $pageIds = $readAdapter->fetchCol($select);
80:
81: $removePageIds = array_diff($pageIds, $object->getData('page_group_ids'));
82:
83: if (is_array($pageIds) && count($pageIds) > 0) {
84: $inCond = $readAdapter->prepareSqlCondition('page_id', array('in' => $pageIds));
85:
86: $select = $readAdapter->select()
87: ->from($pageLayoutTable, 'layout_update_id')
88: ->where($inCond);
89: $removeLayoutUpdateIds = $readAdapter->fetchCol($select);
90:
91: $writeAdapter->delete($pageLayoutTable, $inCond);
92: $this->_deleteLayoutUpdates($removeLayoutUpdateIds);
93: }
94:
95: $this->_deleteWidgetInstancePages($removePageIds);
96:
97: foreach ($object->getData('page_groups') as $pageGroup) {
98: $pageLayoutUpdateIds = $this->_saveLayoutUpdates($object, $pageGroup);
99: $data = array(
100: 'page_group' => $pageGroup['group'],
101: 'layout_handle' => $pageGroup['layout_handle'],
102: 'block_reference' => $pageGroup['block_reference'],
103: 'page_for' => $pageGroup['for'],
104: 'entities' => $pageGroup['entities'],
105: 'page_template' => $pageGroup['template'],
106: );
107: $pageId = $pageGroup['page_id'];
108: if (in_array($pageGroup['page_id'], $pageIds)) {
109: $writeAdapter->update($pageTable, $data, array('page_id = ?' => (int)$pageId));
110: } else {
111: $writeAdapter->insert($pageTable,
112: array_merge(array('instance_id' => $object->getId()),
113: $data));
114: $pageId = $writeAdapter->lastInsertId($pageTable);
115: }
116: foreach ($pageLayoutUpdateIds as $layoutUpdateId) {
117: $writeAdapter->insert($pageLayoutTable, array(
118: 'page_id' => $pageId,
119: 'layout_update_id' => $layoutUpdateId
120: ));
121: }
122: }
123:
124: return parent::_afterSave($object);
125: }
126:
127: 128: 129: 130: 131: 132: 133:
134: protected function _saveLayoutUpdates($widgetInstance, $pageGroupData)
135: {
136: $writeAdapter = $this->_getWriteAdapter();
137: $pageLayoutUpdateIds = array();
138: $storeIds = $this->_prepareStoreIds($widgetInstance->getStoreIds());
139: $layoutUpdateTable = $this->getTable('core/layout_update');
140: $layoutUpdateLinkTable = $this->getTable('core/layout_link');
141:
142: foreach ($pageGroupData['layout_handle_updates'] as $handle) {
143: $xml = $widgetInstance->generateLayoutUpdateXml(
144: $pageGroupData['block_reference'],
145: $pageGroupData['template']
146: );
147: $insert = array(
148: 'handle' => $handle,
149: 'xml' => $xml
150: );
151: if (strlen($widgetInstance->getSortOrder())) {
152: $insert['sort_order'] = $widgetInstance->getSortOrder();
153: };
154:
155: $writeAdapter->insert($layoutUpdateTable, $insert);
156: $layoutUpdateId = $writeAdapter->lastInsertId($layoutUpdateTable);
157: $pageLayoutUpdateIds[] = $layoutUpdateId;
158:
159: $data = array();
160: foreach ($storeIds as $storeId) {
161: $data[] = array(
162: 'store_id' => $storeId,
163: 'area' => $widgetInstance->getArea(),
164: 'package' => $widgetInstance->getPackage(),
165: 'theme' => $widgetInstance->getTheme(),
166: 'layout_update_id' => $layoutUpdateId);
167: }
168: $writeAdapter->insertMultiple($layoutUpdateLinkTable, $data);
169: }
170: return $pageLayoutUpdateIds;
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: protected function _prepareStoreIds($storeIds)
181: {
182: if (in_array('0', $storeIds)) {
183: $storeIds = array(0);
184: }
185: return $storeIds;
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: protected function _beforeDelete(Mage_Core_Model_Abstract $object)
196: {
197: $writeAdapter = $this->_getWriteAdapter();
198: $select = $writeAdapter->select()
199: ->from(array('main_table' => $this->getTable('widget/widget_instance_page')), array())
200: ->joinInner(
201: array('layout_page_table' => $this->getTable('widget/widget_instance_page_layout')),
202: 'layout_page_table.page_id = main_table.page_id',
203: array('layout_update_id')
204: )
205: ->where('main_table.instance_id=?', $object->getId());
206: $result = $writeAdapter->fetchCol($select);
207: $object->setLayoutUpdateIdsToDelete($result);
208: return $this;
209: }
210:
211: 212: 213: 214: 215: 216: 217:
218: protected function _afterDelete(Mage_Core_Model_Abstract $object)
219: {
220: $this->_deleteLayoutUpdates($object->getLayoutUpdateIdsToDelete());
221: return parent::_afterDelete($object);
222: }
223:
224: 225: 226: 227: 228: 229:
230: protected function _deleteWidgetInstancePages($pageIds)
231: {
232: $writeAdapter = $this->_getWriteAdapter();
233: if ($pageIds) {
234: $inCond = $writeAdapter->prepareSqlCondition('page_id', array(
235: 'in' => $pageIds
236: ));
237: $writeAdapter->delete(
238: $this->getTable('widget/widget_instance_page'),
239: $inCond
240: );
241: }
242: return $this;
243: }
244:
245: 246: 247: 248: 249: 250:
251: protected function _deleteLayoutUpdates($layoutUpdateIds)
252: {
253: $writeAdapter = $this->_getWriteAdapter();
254: if ($layoutUpdateIds) {
255: $inCond = $writeAdapter->prepareSqlCondition('layout_update_id', array(
256: 'in' => $layoutUpdateIds
257: ));
258: $writeAdapter->delete(
259: $this->getTable('core/layout_update'),
260: $inCond
261: );
262: }
263: return $this;
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function lookupStoreIds($id)
273: {
274: $adapter = $this->_getReadAdapter();
275: $select = $adapter->select()
276: ->from($this->getMainTable(), 'store_ids')
277: ->where("{$this->getIdFieldName()} = ?", (int)$id);
278: $storeIds = $adapter->fetchOne($select);
279: return $storeIds ? explode(',', $storeIds) : array();
280: }
281:
282: }
283: