PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Sales/Model/Config/Ordered.php

https://github.com/OpenMage/magento-mirror
PHP | 240 lines | 123 code | 14 blank | 103 comment | 20 complexity | 23080de08539fa315cd8d11f2e2b9e8a 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Configuration class for ordered items
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Sales_Model_Config_Ordered extends Mage_Core_Model_Config_Base
  34. {
  35. /**
  36. * Cache key for collectors
  37. *
  38. * @var string|null
  39. */
  40. protected $_collectorsCacheKey = null;
  41. /**
  42. * Configuration path where to collect registered totals
  43. *
  44. * @var string|null
  45. */
  46. protected $_totalsConfigNode = null;
  47. /**
  48. * Prepared models
  49. *
  50. * @var array
  51. */
  52. protected $_models = array();
  53. /**
  54. * Models configuration
  55. *
  56. * @var array
  57. */
  58. protected $_modelsConfig = array();
  59. /**
  60. * Sorted models
  61. *
  62. * @var array
  63. */
  64. protected $_collectors = array();
  65. /**
  66. * Initialize total models configuration and objects
  67. *
  68. * @return Mage_Sales_Model_Config_Ordered
  69. */
  70. protected function _initModels()
  71. {
  72. $totalsConfig = $this->getNode($this->_totalsConfigNode);
  73. foreach ($totalsConfig->children() as $totalCode => $totalConfig) {
  74. $class = $totalConfig->getClassName();
  75. if (!empty($class)) {
  76. $this->_models[$totalCode] = $this->_initModelInstance($class, $totalCode, $totalConfig);
  77. }
  78. }
  79. return $this;
  80. }
  81. /**
  82. * Init model class by configuration
  83. *
  84. * @abstract
  85. * @param string $class
  86. * @param string $totalCode
  87. * @param array $totalConfig
  88. * @return mixed
  89. */
  90. abstract protected function _initModelInstance($class, $totalCode, $totalConfig);
  91. /**
  92. * Prepare configuration array for total model
  93. *
  94. * @param string $code
  95. * @param Mage_Core_Model_Config_Element $totalConfig
  96. * @return array
  97. */
  98. protected function _prepareConfigArray($code, $totalConfig)
  99. {
  100. $totalConfig = (array)$totalConfig;
  101. if (isset($totalConfig['before'])) {
  102. $totalConfig['before'] = explode(',', $totalConfig['before']);
  103. } else {
  104. $totalConfig['before'] = array();
  105. }
  106. if (isset($totalConfig['after'])) {
  107. $totalConfig['after'] = explode(',', $totalConfig['after']);
  108. } else {
  109. $totalConfig['after'] = array();
  110. }
  111. $totalConfig['_code'] = $code;
  112. return $totalConfig;
  113. }
  114. /**
  115. * Aggregate before/after information from all items and sort totals based on this data
  116. *
  117. * @return array
  118. */
  119. protected function _getSortedCollectorCodes()
  120. {
  121. if (Mage::app()->useCache('config')) {
  122. $cachedData = Mage::app()->loadCache($this->_collectorsCacheKey);
  123. if ($cachedData) {
  124. return unserialize($cachedData);
  125. }
  126. }
  127. $configArray = $this->_modelsConfig;
  128. // invoke simple sorting if the first element contains the "sort_order" key
  129. reset($configArray);
  130. $element = current($configArray);
  131. if (isset($element['sort_order'])) {
  132. uasort($configArray, array($this, '_compareSortOrder'));
  133. } else {
  134. foreach ($configArray as $code => $data) {
  135. foreach ($data['before'] as $beforeCode) {
  136. if (!isset($configArray[$beforeCode])) {
  137. continue;
  138. }
  139. $configArray[$code]['before'] = array_unique(array_merge(
  140. $configArray[$code]['before'], $configArray[$beforeCode]['before']
  141. ));
  142. $configArray[$beforeCode]['after'] = array_merge(
  143. $configArray[$beforeCode]['after'], array($code), $data['after']
  144. );
  145. $configArray[$beforeCode]['after'] = array_unique($configArray[$beforeCode]['after']);
  146. }
  147. foreach ($data['after'] as $afterCode) {
  148. if (!isset($configArray[$afterCode])) {
  149. continue;
  150. }
  151. $configArray[$code]['after'] = array_unique(array_merge(
  152. $configArray[$code]['after'], $configArray[$afterCode]['after']
  153. ));
  154. $configArray[$afterCode]['before'] = array_merge(
  155. $configArray[$afterCode]['before'], array($code), $data['before']
  156. );
  157. $configArray[$afterCode]['before'] = array_unique($configArray[$afterCode]['before']);
  158. }
  159. }
  160. uasort($configArray, array($this, '_compareTotals'));
  161. }
  162. $sortedCollectors = array_keys($configArray);
  163. if (Mage::app()->useCache('config')) {
  164. Mage::app()->saveCache(serialize($sortedCollectors), $this->_collectorsCacheKey, array(
  165. Mage_Core_Model_Config::CACHE_TAG
  166. )
  167. );
  168. }
  169. return $sortedCollectors;
  170. }
  171. /**
  172. * Initialize collectors array.
  173. * Collectors array is array of total models ordered based on configuration settings
  174. *
  175. * @return Mage_Sales_Model_Config_Ordered
  176. */
  177. protected function _initCollectors()
  178. {
  179. $sortedCodes = $this->_getSortedCollectorCodes();
  180. foreach ($sortedCodes as $code) {
  181. $this->_collectors[$code] = $this->_models[$code];
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Callback that uses after/before for comparison
  187. *
  188. * @param array $a
  189. * @param array $b
  190. * @return int
  191. */
  192. protected function _compareTotals($a, $b)
  193. {
  194. $aCode = $a['_code'];
  195. $bCode = $b['_code'];
  196. if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) {
  197. $res = -1;
  198. } elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) {
  199. $res = 1;
  200. } else {
  201. $res = 0;
  202. }
  203. return $res;
  204. }
  205. /**
  206. * Callback that uses sort_order for comparison
  207. *
  208. * @param array $a
  209. * @param array $b
  210. * @return int
  211. */
  212. protected function _compareSortOrder($a, $b)
  213. {
  214. if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
  215. return 0;
  216. }
  217. if ($a['sort_order'] > $b['sort_order']) {
  218. $res = 1;
  219. } elseif ($a['sort_order'] < $b['sort_order']) {
  220. $res = -1;
  221. } else {
  222. $res = 0;
  223. }
  224. return $res;
  225. }
  226. }