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) 2012 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: /**
28: * Order configuration model
29: *
30: * @category Mage
31: * @package Mage_Sales
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Sales_Model_Order_Config extends Mage_Core_Model_Config_Base
35: {
36: /**
37: * Statuses per state array
38: *
39: * @var array
40: */
41: protected $_stateStatuses;
42:
43: private $_states;
44:
45: public function __construct()
46: {
47: parent::__construct(Mage::getConfig()->getNode('global/sales/order'));
48: }
49:
50: protected function _getStatus($status)
51: {
52: return $this->getNode('statuses/'.$status);
53: }
54:
55: protected function _getState($state)
56: {
57: return $this->getNode('states/'.$state);
58: }
59:
60: /**
61: * Retrieve default status for state
62: *
63: * @param string $state
64: * @return string
65: */
66: public function getStateDefaultStatus($state)
67: {
68: $status = false;
69: if ($stateNode = $this->_getState($state)) {
70: $status = Mage::getModel('sales/order_status')
71: ->loadDefaultByState($state);
72: $status = $status->getStatus();
73: }
74: return $status;
75: }
76:
77: /**
78: * Retrieve status label
79: *
80: * @param string $code
81: * @return string
82: */
83: public function getStatusLabel($code)
84: {
85: $status = Mage::getModel('sales/order_status')
86: ->load($code);
87: return $status->getStoreLabel();
88: }
89:
90: /**
91: * State label getter
92: *
93: * @param string $state
94: * @return string
95: */
96: public function getStateLabel($state)
97: {
98: if ($stateNode = $this->_getState($state)) {
99: $state = (string) $stateNode->label;
100: return Mage::helper('sales')->__($state);
101: }
102: return $state;
103: }
104:
105:
106: /**
107: * Retrieve all statuses
108: *
109: * @return array
110: */
111: public function getStatuses()
112: {
113: $statuses = Mage::getResourceModel('sales/order_status_collection')
114: ->toOptionHash();
115: return $statuses;
116: }
117:
118: /**
119: * Order states getter
120: *
121: * @return array
122: */
123: public function getStates()
124: {
125: $states = array();
126: foreach ($this->getNode('states')->children() as $state) {
127: $label = (string) $state->label;
128: $states[$state->getName()] = Mage::helper('sales')->__($label);
129: }
130: return $states;
131: }
132:
133:
134: /**
135: * Retrieve statuses available for state
136: * Get all possible statuses, or for specified state, or specified states array
137: * Add labels by default. Return plain array of statuses, if no labels.
138: *
139: * @param mixed $state
140: * @param bool $addLabels
141: * @return array
142: */
143: public function getStateStatuses($state, $addLabels = true)
144: {
145: if (is_array($state)) {
146: $key = implode("|", $state) . $addLabels;
147: } else {
148: $key = $state . $addLabels;
149: }
150: if (isset($this->_stateStatuses[$key])) {
151: return $this->_stateStatuses[$key];
152: }
153: $statuses = array();
154: if (empty($state) || !is_array($state)) {
155: $state = array($state);
156: }
157: foreach ($state as $_state) {
158: if ($stateNode = $this->_getState($_state)) {
159: $collection = Mage::getResourceModel('sales/order_status_collection')
160: ->addStateFilter($_state)
161: ->orderByLabel();
162: foreach ($collection as $status) {
163: $code = $status->getStatus();
164: if ($addLabels) {
165: $statuses[$code] = $status->getStoreLabel();
166: } else {
167: $statuses[] = $code;
168: }
169: }
170: }
171: }
172: $this->_stateStatuses[$key] = $statuses;
173: return $statuses;
174: }
175:
176: /**
177: * Retrieve states which are visible on front end
178: *
179: * @return array
180: */
181: public function getVisibleOnFrontStates()
182: {
183: $this->_getStates();
184: return $this->_states['visible'];
185: }
186:
187: /**
188: * Get order states, visible on frontend
189: *
190: * @return array
191: */
192: public function getInvisibleOnFrontStates()
193: {
194: $this->_getStates();
195: return $this->_states['invisible'];
196: }
197:
198: private function _getStates()
199: {
200: if (null === $this->_states) {
201: $this->_states = array(
202: 'all' => array(),
203: 'visible' => array(),
204: 'invisible' => array(),
205: 'statuses' => array(),
206: );
207: foreach ($this->getNode('states')->children() as $state) {
208: $name = $state->getName();
209: $this->_states['all'][] = $name;
210: $isVisibleOnFront = (string)$state->visible_on_front;
211: if ((bool)$isVisibleOnFront || ($state->visible_on_front && $isVisibleOnFront == '')) {
212: $this->_states['visible'][] = $name;
213: }
214: else {
215: $this->_states['invisible'][] = $name;
216: }
217: foreach ($state->statuses->children() as $status) {
218: $this->_states['statuses'][$name][] = $status->getName();
219: }
220: }
221: }
222: }
223: }
224: