Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 250 lines (233 sloc) 7.842 kb
1 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Sales
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Configuration class for ordered items
*
* @category Mage
* @package Mage_Sales
* @author Magento Core Team <core@magentocommerce.com>
*/
abstract class Mage_Sales_Model_Config_Ordered extends Mage_Core_Model_Config_Base
{
    /**
* Cache key for collectors
*
* @var string|null
*/
    protected $_collectorsCacheKey = null;

    /**
* Configuration path where to collect registered totals
*
* @var string|null
*/
    protected $_totalsConfigNode = null;

    /**
* Prepared models
*
* @var array
*/
    protected $_models = array();

    /**
* Models configuration
*
* @var array
*/
    protected $_modelsConfig = array();

    /**
* Sorted models
*
* @var array
*/
    protected $_collectors = array();

    /**
* Initialize total models configuration and objects
*
* @return Mage_Sales_Model_Config_Ordered
*/
    protected function _initModels()
    {
        $totalsConfig = $this->getNode($this->_totalsConfigNode);

        foreach ($totalsConfig->children() as $totalCode => $totalConfig) {
            $class = $totalConfig->getClassName();
            if (!empty($class)) {
                $this->_models[$totalCode] = $this->_initModelInstance($class, $totalCode, $totalConfig);
            }
        }
        return $this;
    }

    /**
* Init model class by configuration
*
* @abstract
* @param string $class
* @param string $totalCode
* @param array $totalConfig
* @return mixed
*/
    abstract protected function _initModelInstance($class, $totalCode, $totalConfig);

    /**
* Prepare configuration array for total model
*
* @param string $code
* @param Mage_Core_Model_Config_Element $totalConfig
* @return array
*/
    protected function _prepareConfigArray($code, $totalConfig)
    {
        $totalConfig = (array)$totalConfig;
        if (isset($totalConfig['before'])) {
            $totalConfig['before'] = explode(',', $totalConfig['before']);
        } else {
            $totalConfig['before'] = array();
        }
        if (isset($totalConfig['after'])) {
            $totalConfig['after'] = explode(',', $totalConfig['after']);
        } else {
            $totalConfig['after'] = array();
        }
        $totalConfig['_code'] = $code;
        return $totalConfig;
    }


    /**
* Topological sort
*
* @param $nodeids Node Ids - example: array('subtotal','grand_total');
* @param $edges Array of Edges. Each edge is specified as an array with two elements: The source and destination node of the edge
* Example: array(array('subtotal','grand_total')); -> subtotal comes before grand_total
* @return array|null
*/
    public function _topologicalSort($nodeids, $edges) {
        $L = $S = $nodes = array();
        foreach($nodeids as $id) {
            $nodes[$id] = array('in'=>array(), 'out'=>array());
            foreach($edges as $e) {
                if ($id==$e[0]) { $nodes[$id]['out'][]=$e[1]; }
                if ($id==$e[1]) { $nodes[$id]['in'][]=$e[0]; }
            }
        }
        foreach ($nodes as $id=>$n) { if (empty($n['in'])) $S[]=$id; }
        while ($id = array_shift($S)) {
            if (!in_array($id, $L)) {
                $L[] = $id;
                foreach($nodes[$id]['out'] as $m) {
                    $nodes[$m]['in'] = array_diff($nodes[$m]['in'], array($id));
                    if (empty($nodes[$m]['in'])) { $S[] = $m; }
                }
                $nodes[$id]['out'] = array();
            }
        }
        foreach($nodes as $n) {
            if (!empty($n['in']) or !empty($n['out'])) {
                return null; // not sortable as graph is cyclic
            }
        }
        return $L;
    }

    /**
* Aggregate before/after information from all items and sort totals based on this data
*
* @return array
*/
    protected function _getSortedCollectorCodes()
    {
        if (Mage::app()->useCache('config')) {
            $cachedData = Mage::app()->loadCache($this->_collectorsCacheKey);
            if ($cachedData) {
                return unserialize($cachedData);
            }
        }
        $configArray = $this->_modelsConfig;
        // invoke simple sorting if the first element contains the "sort_order" key
        reset($configArray);
        $element = current($configArray);
        if (isset($element['sort_order'])) {
            uasort($configArray, array($this, '_compareSortOrder'));
            $sortedCollectors = array_keys($configArray);
        } else {
            // prepare data for topological sort
            $nodes = array_keys($configArray);
            $edges = array();

            foreach ($configArray as $data) {
                $_code = $data['_code'];
                if (!isset($configArray[$_code])) continue;
                foreach ($data['before'] as $beforeCode) {
                    if (!isset($configArray[$beforeCode])) continue;
                    $edges[] = array($_code, $beforeCode);
                }
                foreach ($data['after'] as $afterCode) {
                    if (!isset($configArray[$afterCode])) continue;
                    $edges[] = array($afterCode, $_code);
                }
            }
            $sortedCollectors = $this->_topologicalSort($nodes, $edges);

            if (is_null($sortedCollectors)) {
                throw new Mage_Sales_Exception('Total ordering before/after conditions can not be complied with');
            }
        }
        if (Mage::app()->useCache('config')) {
            Mage::app()->saveCache(serialize($sortedCollectors), $this->_collectorsCacheKey, array(
                    Mage_Core_Model_Config::CACHE_TAG
                )
            );
        }
        return $sortedCollectors;
    }

    /**
* Initialize collectors array.
* Collectors array is array of total models ordered based on configuration settings
*
* @return Mage_Sales_Model_Config_Ordered
*/
    protected function _initCollectors()
    {
        $sortedCodes = $this->_getSortedCollectorCodes();
        foreach ($sortedCodes as $code) {
            $this->_collectors[$code] = $this->_models[$code];
        }

        return $this;
    }

    /**
* Callback that uses sort_order for comparison
*
* @param array $a
* @param array $b
* @return int
*/
    protected function _compareSortOrder($a, $b)
    {
        if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
            return 0;
        }
        if ($a['sort_order'] > $b['sort_order']) {
            $res = 1;
        } elseif ($a['sort_order'] < $b['sort_order']) {
            $res = -1;
        } else {
            $res = 0;
        }
        return $res;
    }
}
Something went wrong with that request. Please try again.