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: abstract class Mage_Sales_Model_Config_Ordered extends Mage_Core_Model_Config_Base
35: {
36: 37: 38: 39: 40:
41: protected $_collectorsCacheKey = null;
42:
43: 44: 45: 46: 47:
48: protected $_totalsConfigNode = null;
49:
50: 51: 52: 53: 54:
55: protected $_models = array();
56:
57: 58: 59: 60: 61:
62: protected $_modelsConfig = array();
63:
64: 65: 66: 67: 68:
69: protected $_collectors = array();
70:
71: 72: 73: 74: 75:
76: protected function _initModels()
77: {
78: $totalsConfig = $this->getNode($this->_totalsConfigNode);
79:
80: foreach ($totalsConfig->children() as $totalCode => $totalConfig) {
81: $class = $totalConfig->getClassName();
82: if (!empty($class)) {
83: $this->_models[$totalCode] = $this->_initModelInstance($class, $totalCode, $totalConfig);
84: }
85: }
86: return $this;
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97:
98: abstract protected function _initModelInstance($class, $totalCode, $totalConfig);
99:
100: 101: 102: 103: 104: 105: 106:
107: protected function _prepareConfigArray($code, $totalConfig)
108: {
109: $totalConfig = (array)$totalConfig;
110: if (isset($totalConfig['before'])) {
111: $totalConfig['before'] = explode(',', $totalConfig['before']);
112: } else {
113: $totalConfig['before'] = array();
114: }
115: if (isset($totalConfig['after'])) {
116: $totalConfig['after'] = explode(',', $totalConfig['after']);
117: } else {
118: $totalConfig['after'] = array();
119: }
120: $totalConfig['_code'] = $code;
121: return $totalConfig;
122: }
123:
124: 125: 126: 127: 128:
129: protected function _getSortedCollectorCodes()
130: {
131: if (Mage::app()->useCache('config')) {
132: $cachedData = Mage::app()->loadCache($this->_collectorsCacheKey);
133: if ($cachedData) {
134: return unserialize($cachedData);
135: }
136: }
137: $configArray = $this->_modelsConfig;
138:
139: reset($configArray);
140: $element = current($configArray);
141: if (isset($element['sort_order'])) {
142: uasort($configArray, array($this, '_compareSortOrder'));
143: } else {
144: foreach ($configArray as $code => $data) {
145: foreach ($data['before'] as $beforeCode) {
146: if (!isset($configArray[$beforeCode])) {
147: continue;
148: }
149: $configArray[$code]['before'] = array_unique(array_merge(
150: $configArray[$code]['before'], $configArray[$beforeCode]['before']
151: ));
152: $configArray[$beforeCode]['after'] = array_merge(
153: $configArray[$beforeCode]['after'], array($code), $data['after']
154: );
155: $configArray[$beforeCode]['after'] = array_unique($configArray[$beforeCode]['after']);
156: }
157: foreach ($data['after'] as $afterCode) {
158: if (!isset($configArray[$afterCode])) {
159: continue;
160: }
161: $configArray[$code]['after'] = array_unique(array_merge(
162: $configArray[$code]['after'], $configArray[$afterCode]['after']
163: ));
164: $configArray[$afterCode]['before'] = array_merge(
165: $configArray[$afterCode]['before'], array($code), $data['before']
166: );
167: $configArray[$afterCode]['before'] = array_unique($configArray[$afterCode]['before']);
168: }
169: }
170: uasort($configArray, array($this, '_compareTotals'));
171: }
172: $sortedCollectors = array_keys($configArray);
173: if (Mage::app()->useCache('config')) {
174: Mage::app()->saveCache(serialize($sortedCollectors), $this->_collectorsCacheKey, array(
175: Mage_Core_Model_Config::CACHE_TAG
176: )
177: );
178: }
179: return $sortedCollectors;
180: }
181:
182: 183: 184: 185: 186: 187:
188: protected function _initCollectors()
189: {
190: $sortedCodes = $this->_getSortedCollectorCodes();
191: foreach ($sortedCodes as $code) {
192: $this->_collectors[$code] = $this->_models[$code];
193: }
194:
195: return $this;
196: }
197:
198: 199: 200: 201: 202: 203: 204:
205: protected function _compareTotals($a, $b)
206: {
207: $aCode = $a['_code'];
208: $bCode = $b['_code'];
209: if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) {
210: $res = -1;
211: } elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) {
212: $res = 1;
213: } else {
214: $res = 0;
215: }
216: return $res;
217: }
218:
219: 220: 221: 222: 223: 224: 225:
226: protected function _compareSortOrder($a, $b)
227: {
228: if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
229: return 0;
230: }
231: if ($a['sort_order'] > $b['sort_order']) {
232: $res = 1;
233: } elseif ($a['sort_order'] < $b['sort_order']) {
234: $res = -1;
235: } else {
236: $res = 0;
237: }
238: return $res;
239: }
240: }
241: