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: abstract class Mage_Sales_Model_Resource_Order_Abstract extends Mage_Sales_Model_Resource_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_grid = false;
43:
44: 45: 46: 47: 48:
49: protected $_useIsObjectNew = true;
50:
51: 52: 53: 54: 55:
56: protected $_useIncrementId = false;
57:
58: 59: 60: 61: 62:
63: protected $_entityTypeForIncrementId = '';
64:
65: 66: 67: 68: 69:
70: protected $_virtualGridColumns = null;
71:
72: 73: 74: 75: 76:
77: protected $_gridColumns = null;
78:
79: 80: 81: 82: 83:
84: protected $_eventPrefix = 'sales_resource';
85:
86: 87: 88: 89: 90:
91: protected $_eventObject = 'resource';
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: public function addVirtualGridColumn($alias, $table, $joinCondition, $column)
103: {
104: $table = $this->getTable($table);
105:
106: if (!in_array($alias, $this->getGridColumns())) {
107: Mage::throwException(
108: Mage::helper('sales')->__('Please specify a valid grid column alias name that exists in grid table.')
109: );
110: }
111:
112: $this->_virtualGridColumns[$alias] = array(
113: $table, $joinCondition, $column
114: );
115:
116: return $this;
117: }
118:
119: 120: 121: 122: 123:
124: public function getVirtualGridColumns()
125: {
126: if ($this->_virtualGridColumns === null) {
127: $this->_initVirtualGridColumns();
128: }
129:
130: return $this->_virtualGridColumns;
131: }
132:
133: 134: 135: 136: 137:
138: protected function _initVirtualGridColumns()
139: {
140: $this->_virtualGridColumns = array();
141: if ($this->_eventPrefix && $this->_eventObject) {
142: Mage::dispatchEvent($this->_eventPrefix . '_init_virtual_grid_columns', array(
143: $this->_eventObject => $this
144: ));
145: }
146: return $this;
147: }
148:
149: 150: 151: 152: 153: 154:
155: public function updateGridRecords($ids)
156: {
157: if ($this->_grid) {
158: if (!is_array($ids)) {
159: $ids = array($ids);
160: }
161:
162: if ($this->_eventPrefix && $this->_eventObject) {
163: $proxy = new Varien_Object();
164: $proxy->setIds($ids)
165: ->setData($this->_eventObject, $this);
166:
167: Mage::dispatchEvent($this->_eventPrefix . '_update_grid_records', array('proxy' => $proxy));
168: $ids = $proxy->getIds();
169: }
170:
171: if (empty($ids)) {
172: return $this;
173: }
174: $columnsToSelect = array();
175: $table = $this->getGridTable();
176: $select = $this->getUpdateGridRecordsSelect($ids, $columnsToSelect);
177: $this->_getWriteAdapter()->query($select->insertFromSelect($table, $columnsToSelect, true));
178: }
179:
180: return $this;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190:
191: public function getUpdateGridRecordsSelect($ids, &$flatColumnsToSelect, $gridColumns = null)
192: {
193: $flatColumns = array_keys($this->_getReadAdapter()
194: ->describeTable(
195: $this->getMainTable()
196: )
197: );
198:
199: if ($gridColumns === null) {
200: $gridColumns = $this->getGridColumns();
201: }
202:
203: $flatColumnsToSelect = array_intersect($flatColumns, $gridColumns);
204:
205: $select = $this->_getWriteAdapter()->select()
206: ->from(array('main_table' => $this->getMainTable()), $flatColumnsToSelect)
207: ->where('main_table.' . $this->getIdFieldName() . ' IN(?)', $ids);
208:
209: $this->joinVirtualGridColumnsToSelect('main_table', $select, $flatColumnsToSelect);
210:
211: return $select;
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221:
222: public function joinVirtualGridColumnsToSelect($mainTableAlias, Zend_Db_Select $select, &$columnsToSelect)
223: {
224: $adapter = $this->_getWriteAdapter();
225: foreach ($this->getVirtualGridColumns() as $alias => $expression) {
226: list($table, $joinCondition, $column) = $expression;
227: $tableAlias = 'table_' . $alias;
228:
229: $joinConditionExpr = array();
230: foreach ($joinCondition as $fkField=>$pkField) {
231: $pkField = $adapter->quoteIdentifier(
232: $tableAlias . '.' . $pkField
233: );
234: $fkField = $adapter->quoteIdentifier(
235: $mainTableAlias . '.' . $fkField
236: );
237: $joinConditionExpr[] = $fkField . '=' . $pkField;
238: }
239:
240: $select->joinLeft(
241: array($tableAlias=> $table),
242: implode(' AND ', $joinConditionExpr),
243: array($alias => str_replace('{{table}}', $tableAlias, $column))
244: );
245:
246: $columnsToSelect[] = $alias;
247: }
248:
249: return $this;
250: }
251:
252: 253: 254: 255: 256:
257: public function getGridColumns()
258: {
259: if ($this->_gridColumns === null) {
260: if ($this->_grid) {
261: $this->_gridColumns = array_keys(
262: $this->_getReadAdapter()->describeTable($this->getGridTable())
263: );
264: } else {
265: $this->_gridColumns = array();
266: }
267: }
268:
269: return $this->_gridColumns;
270: }
271:
272: 273: 274: 275: 276:
277: public function getGridTable()
278: {
279: if ($this->_grid) {
280: return $this->getTable($this->_mainTable . '_grid');
281: }
282: return false;
283: }
284:
285: 286: 287: 288: 289: 290: 291:
292: protected function _beforeSaveAttribute(Mage_Core_Model_Abstract $object, $attribute)
293: {
294: if ($this->_eventObject && $this->_eventPrefix) {
295: Mage::dispatchEvent($this->_eventPrefix . '_save_attribute_before', array(
296: $this->_eventObject => $this,
297: 'object' => $object,
298: 'attribute' => $attribute
299: ));
300: }
301: return $this;
302: }
303:
304: 305: 306: 307: 308: 309: 310:
311: protected function _afterSaveAttribute(Mage_Core_Model_Abstract $object, $attribute)
312: {
313: if ($this->_eventObject && $this->_eventPrefix) {
314: Mage::dispatchEvent($this->_eventPrefix . '_save_attribute_after', array(
315: $this->_eventObject => $this,
316: 'object' => $object,
317: 'attribute' => $attribute
318: ));
319: }
320: return $this;
321: }
322:
323: 324: 325: 326: 327: 328: 329:
330: public function saveAttribute(Mage_Core_Model_Abstract $object, $attribute)
331: {
332: if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
333: $attribute = $attribute->getAttributeCode();
334: }
335:
336: if (is_string($attribute)) {
337: $attribute = array($attribute);
338: }
339:
340: if (is_array($attribute) && !empty($attribute)) {
341: $this->beginTransaction();
342: try {
343: $this->_beforeSaveAttribute($object, $attribute);
344: $data = new Varien_Object();
345: foreach ($attribute as $code) {
346: $data->setData($code, $object->getData($code));
347: }
348:
349: $updateArray = $this->_prepareDataForTable($data, $this->getMainTable());
350: $this->_postSaveFieldsUpdate($object, $updateArray);
351: if (!$object->getForceUpdateGridRecords() &&
352: count(array_intersect($this->getGridColumns(), $attribute)) > 0
353: ) {
354: $this->updateGridRecords($object->getId());
355: }
356: $this->_afterSaveAttribute($object, $attribute);
357: $this->commit();
358: } catch (Exception $e) {
359: $this->rollBack();
360: throw $e;
361: }
362: }
363:
364: return $this;
365: }
366:
367: 368: 369: 370: 371: 372:
373: protected function _beforeSave(Mage_Core_Model_Abstract $object)
374: {
375: if ($this->_useIncrementId && !$object->getIncrementId()) {
376:
377: $entityType = Mage::getModel('eav/entity_type')->loadByCode($this->_entityTypeForIncrementId);
378: $object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId()));
379: }
380: parent::_beforeSave($object);
381: return $this;
382: }
383:
384: 385: 386: 387: 388: 389: 390:
391: protected function _postSaveFieldsUpdate($object, $data)
392: {
393: if ($object->getId() && !empty($data)) {
394: $table = $this->getMainTable();
395: $this->_getWriteAdapter()->update($table, $data,
396: array($this->getIdFieldName() . '=?' => (int) $object->getId())
397: );
398: $object->addData($data);
399: }
400:
401: return $this;
402: }
403:
404: 405: 406: 407: 408: 409:
410: public function setMainTable($table)
411: {
412: $this->_mainTable = $table;
413: return $this;
414: }
415:
416: 417: 418: 419: 420: 421:
422: public function save(Mage_Core_Model_Abstract $object)
423: {
424: if (!$object->getForceObjectSave()) {
425: parent::save($object);
426: }
427:
428: return $this;
429: }
430:
431: 432: 433: 434: 435: 436: 437:
438: public function updateOnRelatedRecordChanged($field, $entityId)
439: {
440: $adapter = $this->_getWriteAdapter();
441: $column = array();
442: $select = $adapter->select()
443: ->from(array('main_table' => $this->getMainTable()), $column)
444: ->where('main_table.' . $field .' = ?', $entityId);
445: $this->joinVirtualGridColumnsToSelect('main_table', $select, $column);
446: $fieldsToUpdate = $adapter->fetchRow($select);
447: if ($fieldsToUpdate) {
448: return $adapter->update(
449: $this->getGridTable(),
450: $fieldsToUpdate,
451: $adapter->quoteInto($this->getGridTable() . '.' . $field . ' = ?', $entityId)
452: );
453: }
454: return 0;
455: }
456: }
457:
458: