PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Sales/Model/Resource/Order/Abstract.php

https://github.com/rgranadino/magento-mirror
PHP | 430 lines | 213 code | 48 blank | 169 comment | 28 complexity | d8348df65a5cf5e8abec5a827e85416c MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Flat sales resource abstract
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Sales_Model_Resource_Order_Abstract extends Mage_Sales_Model_Resource_Abstract
  34. {
  35. /**
  36. * Is grid available
  37. *
  38. * @var boolean
  39. */
  40. protected $_grid = false;
  41. /**
  42. * Use additional is object new check for this resource
  43. *
  44. * @var boolean
  45. */
  46. protected $_useIsObjectNew = true;
  47. /**
  48. * Flag for using of increment id
  49. *
  50. * @var boolean
  51. */
  52. protected $_useIncrementId = false;
  53. /**
  54. * Entity code for increment id (Eav entity code)
  55. *
  56. * @var string
  57. */
  58. protected $_entityTypeForIncrementId = '';
  59. /**
  60. * Grid virtual columns
  61. *
  62. * @var array|null
  63. */
  64. protected $_virtualGridColumns = null;
  65. /**
  66. * Grid columns
  67. *
  68. * @var array|null
  69. */
  70. protected $_gridColumns = null;
  71. /**
  72. * Event prefix
  73. *
  74. * @var string
  75. */
  76. protected $_eventPrefix = 'sales_resource';
  77. /**
  78. * Event object
  79. *
  80. * @var string
  81. */
  82. protected $_eventObject = 'resource';
  83. /**
  84. * Add new virtual grid column
  85. *
  86. * @param string $alias
  87. * @param string $table
  88. * @param array $joinCondition
  89. * @param string $column
  90. * @return Mage_Sales_Model_Resource_Order_Abstract
  91. */
  92. public function addVirtualGridColumn($alias, $table, $joinCondition, $column)
  93. {
  94. $table = $this->getTable($table);
  95. if (!in_array($alias, $this->getGridColumns())) {
  96. Mage::throwException(
  97. Mage::helper('sales')->__('Please specify a valid grid column alias name that exists in grid table.')
  98. );
  99. }
  100. $this->_virtualGridColumns[$alias] = array(
  101. $table, $joinCondition, $column
  102. );
  103. return $this;
  104. }
  105. /**
  106. * Retrieve virtual grid columns
  107. *
  108. * @return array
  109. */
  110. public function getVirtualGridColumns()
  111. {
  112. if ($this->_virtualGridColumns === null) {
  113. $this->_initVirtualGridColumns();
  114. }
  115. return $this->_virtualGridColumns;
  116. }
  117. /**
  118. * Init virtual grid records for entity
  119. *
  120. * @return Mage_Sales_Model_Resource_Order_Abstract
  121. */
  122. protected function _initVirtualGridColumns()
  123. {
  124. $this->_virtualGridColumns = array();
  125. if ($this->_eventPrefix && $this->_eventObject) {
  126. Mage::dispatchEvent($this->_eventPrefix . '_init_virtual_grid_columns', array(
  127. $this->_eventObject => $this
  128. ));
  129. }
  130. return $this;
  131. }
  132. /**
  133. * Update records in grid table
  134. *
  135. * @param array|int $ids
  136. * @return Mage_Sales_Model_Resource_Order_Abstract
  137. */
  138. public function updateGridRecords($ids)
  139. {
  140. if ($this->_grid) {
  141. if (!is_array($ids)) {
  142. $ids = array($ids);
  143. }
  144. if ($this->_eventPrefix && $this->_eventObject) {
  145. $proxy = new Varien_Object();
  146. $proxy->setIds($ids)
  147. ->setData($this->_eventObject, $this);
  148. Mage::dispatchEvent($this->_eventPrefix . '_update_grid_records', array('proxy' => $proxy));
  149. $ids = $proxy->getIds();
  150. }
  151. if (empty($ids)) { // If nothing to update
  152. return $this;
  153. }
  154. $columnsToSelect = array();
  155. $table = $this->getGridTable();
  156. $select = $this->getUpdateGridRecordsSelect($ids, $columnsToSelect);
  157. $this->_getWriteAdapter()->query($select->insertFromSelect($table, $columnsToSelect, true));
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Retrieve update grid records select
  163. *
  164. * @param array $ids
  165. * @param array $flatColumnsToSelect
  166. * @param array|null $gridColumns
  167. * @return Varien_Db_Select
  168. */
  169. public function getUpdateGridRecordsSelect($ids, &$flatColumnsToSelect, $gridColumns = null)
  170. {
  171. $flatColumns = array_keys($this->_getReadAdapter()
  172. ->describeTable(
  173. $this->getMainTable()
  174. )
  175. );
  176. if ($gridColumns === null) {
  177. $gridColumns = $this->getGridColumns();
  178. }
  179. $flatColumnsToSelect = array_intersect($flatColumns, $gridColumns);
  180. $select = $this->_getWriteAdapter()->select()
  181. ->from(array('main_table' => $this->getMainTable()), $flatColumnsToSelect)
  182. ->where('main_table.' . $this->getIdFieldName() . ' IN(?)', $ids);
  183. $this->joinVirtualGridColumnsToSelect('main_table', $select, $flatColumnsToSelect);
  184. return $select;
  185. }
  186. /**
  187. * Join virtual grid columns to select
  188. *
  189. * @param string $mainTableAlias
  190. * @param Zend_Db_Select $select
  191. * @param array $columnsToSelect
  192. * @return Mage_Sales_Model_Resource_Order_Abstract
  193. */
  194. public function joinVirtualGridColumnsToSelect($mainTableAlias, Zend_Db_Select $select, &$columnsToSelect)
  195. {
  196. $adapter = $this->_getWriteAdapter();
  197. foreach ($this->getVirtualGridColumns() as $alias => $expression) {
  198. list($table, $joinCondition, $column) = $expression;
  199. $tableAlias = 'table_' . $alias;
  200. $joinConditionExpr = array();
  201. foreach ($joinCondition as $fkField=>$pkField) {
  202. $pkField = $adapter->quoteIdentifier(
  203. $tableAlias . '.' . $pkField
  204. );
  205. $fkField = $adapter->quoteIdentifier(
  206. $mainTableAlias . '.' . $fkField
  207. );
  208. $joinConditionExpr[] = $fkField . '=' . $pkField;
  209. }
  210. $select->joinLeft(
  211. array($tableAlias=> $table),
  212. implode(' AND ', $joinConditionExpr),
  213. array($alias => str_replace('{{table}}', $tableAlias, $column))
  214. );
  215. $columnsToSelect[] = $alias;
  216. }
  217. return $this;
  218. }
  219. /**
  220. * Retrieve list of grid columns
  221. *
  222. * @return array
  223. */
  224. public function getGridColumns()
  225. {
  226. if ($this->_gridColumns === null) {
  227. if ($this->_grid) {
  228. $this->_gridColumns = array_keys(
  229. $this->_getReadAdapter()->describeTable($this->getGridTable())
  230. );
  231. } else {
  232. $this->_gridColumns = array();
  233. }
  234. }
  235. return $this->_gridColumns;
  236. }
  237. /**
  238. * Retrieve grid table
  239. *
  240. * @return string
  241. */
  242. public function getGridTable()
  243. {
  244. if ($this->_grid) {
  245. return $this->getTable($this->_mainTable . '_grid');
  246. }
  247. return false;
  248. }
  249. /**
  250. * Before save object attribute
  251. *
  252. * @param Mage_Core_Model_Abstract $object
  253. * @param string $attribute
  254. * @return Mage_Sales_Model_Resource_Order_Abstract
  255. */
  256. protected function _beforeSaveAttribute(Mage_Core_Model_Abstract $object, $attribute)
  257. {
  258. if ($this->_eventObject && $this->_eventPrefix) {
  259. Mage::dispatchEvent($this->_eventPrefix . '_save_attribute_before', array(
  260. $this->_eventObject => $this,
  261. 'object' => $object,
  262. 'attribute' => $attribute
  263. ));
  264. }
  265. return $this;
  266. }
  267. /**
  268. * After save object attribute
  269. *
  270. * @param Mage_Core_Model_Abstract $object
  271. * @param string $attribute
  272. * @return Mage_Sales_Model_Resource_Order_Abstract
  273. */
  274. protected function _afterSaveAttribute(Mage_Core_Model_Abstract $object, $attribute)
  275. {
  276. if ($this->_eventObject && $this->_eventPrefix) {
  277. Mage::dispatchEvent($this->_eventPrefix . '_save_attribute_after', array(
  278. $this->_eventObject => $this,
  279. 'object' => $object,
  280. 'attribute' => $attribute
  281. ));
  282. }
  283. return $this;
  284. }
  285. /**
  286. * Perform actions after object save
  287. *
  288. * @param Mage_Core_Model_Abstract $object
  289. * @param string $attribute
  290. * @return Mage_Sales_Model_Resource_Order_Abstract
  291. */
  292. public function saveAttribute(Mage_Core_Model_Abstract $object, $attribute)
  293. {
  294. if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
  295. $attribute = $attribute->getAttributeCode();
  296. }
  297. if (is_string($attribute)) {
  298. $attribute = array($attribute);
  299. }
  300. if (is_array($attribute) && !empty($attribute)) {
  301. $this->beginTransaction();
  302. try {
  303. $this->_beforeSaveAttribute($object, $attribute);
  304. $data = new Varien_Object();
  305. foreach ($attribute as $code) {
  306. $data->setData($code, $object->getData($code));
  307. }
  308. $updateArray = $this->_prepareDataForTable($data, $this->getMainTable());
  309. $this->_postSaveFieldsUpdate($object, $updateArray);
  310. if (!$object->getForceUpdateGridRecords() &&
  311. count(array_intersect($this->getGridColumns(), $attribute)) > 0
  312. ) {
  313. $this->updateGridRecords($object->getId());
  314. }
  315. $this->_afterSaveAttribute($object, $attribute);
  316. $this->commit();
  317. } catch (Exception $e) {
  318. $this->rollBack();
  319. throw $e;
  320. }
  321. }
  322. return $this;
  323. }
  324. /**
  325. * Perform actions before object save
  326. *
  327. * @param Varien_Object $object
  328. * @return Mage_Sales_Model_Resource_Order_Abstract
  329. */
  330. protected function _beforeSave(Mage_Core_Model_Abstract $object)
  331. {
  332. if ($this->_useIncrementId && !$object->getIncrementId()) {
  333. /* @var $entityType Mage_Eav_Model_Entity_Type */
  334. $entityType = Mage::getModel('eav/entity_type')->loadByCode($this->_entityTypeForIncrementId);
  335. $object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId()));
  336. }
  337. parent::_beforeSave($object);
  338. return $this;
  339. }
  340. /**
  341. * Update field in table if model have been already saved
  342. *
  343. * @param Mage_Core_Model_Abstract $object
  344. * @param array $data
  345. * @return Mage_Sales_Model_Resource_Order_Abstract
  346. */
  347. protected function _postSaveFieldsUpdate($object, $data)
  348. {
  349. if ($object->getId() && !empty($data)) {
  350. $table = $this->getMainTable();
  351. $this->_getWriteAdapter()->update($table, $data,
  352. array($this->getIdFieldName() . '=?' => (int) $object->getId())
  353. );
  354. $object->addData($data);
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Set main resource table
  360. *
  361. * @param string $table
  362. * @return Mage_Sales_Model_Resource_Order_Abstract
  363. */
  364. public function setMainTable($table)
  365. {
  366. $this->_mainTable = $table;
  367. return $this;
  368. }
  369. /**
  370. * Save object data
  371. *
  372. * @param Mage_Core_Model_Abstract $object
  373. * @return Mage_Sales_Model_Resource_Order_Abstract
  374. */
  375. public function save(Mage_Core_Model_Abstract $object)
  376. {
  377. if (!$object->getForceObjectSave()) {
  378. parent::save($object);
  379. }
  380. return $this;
  381. }
  382. }