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: * Service model for managing statuses information. Statuses are just records with code, message and any
29: * additional data. The model helps to keep track and manipulate statuses, that different modules want to set
30: * to owner object of this model.
31: *
32: * @category Mage
33: * @package Mage_Sales
34: * @author Magento Core Team <core@magentocommerce.com>
35: */
36: class Mage_Sales_Model_Status_List
37: {
38: /**
39: * Status information entities
40: *
41: * @var array
42: */
43: protected $_items = array();
44:
45: /**
46: * Adds status information to the list of items.
47: *
48: * @param string|null $origin Usually a name of module, that adds this status
49: * @param int|null $code Code of status, unique for origin, that sets it
50: * @param string|null $message Status message
51: * @param Varien_Object|null $additionalData Any additional data, that caller would like to store
52: * @return Mage_Sales_Model_Status_List
53: */
54: public function addItem($origin = null, $code = null, $message = null, $additionalData = null)
55: {
56: $this->_items[] = array(
57: 'origin' => $origin,
58: 'code' => $code,
59: 'message' => $message,
60: 'additionalData' => $additionalData
61: );
62: return $this;
63: }
64:
65: /**
66: * Retrieves all items
67: *
68: * @return array
69: */
70: public function getItems()
71: {
72: return $this->_items;
73: }
74:
75: /**
76: * Removes items, that have parameters equal to passed in $params.
77: * Returns items removed.
78: * $params can have following keys (if not set - then any item is good for this key):
79: * 'origin', 'code', 'message'
80: *
81: * @param array $params
82: * @return array
83: */
84: public function removeItemsByParams($params)
85: {
86: $items = $this->getItems();
87: if (!$items) {
88: return array();
89: }
90:
91: $indexes = array();
92: $paramKeys = array('origin', 'code', 'message');
93: foreach ($items as $index => $item) {
94: $remove = true;
95: foreach ($paramKeys as $key) {
96: if (!isset($params[$key])) {
97: continue;
98: }
99: if ($params[$key] != $item[$key]) {
100: $remove = false;
101: break;
102: }
103: }
104: if ($remove) {
105: $indexes[] = $index;
106: }
107: }
108:
109: return $this->removeItems($indexes);
110: }
111:
112: /**
113: * Removes items at mentioned index/indexes.
114: * Returns items removed.
115: *
116: * @param int|array $indexes
117: * @return array
118: */
119: public function removeItems($indexes)
120: {
121: if (!array($indexes)) {
122: $indexes = array($indexes);
123: }
124: if (!$indexes) {
125: return array();
126: }
127:
128: $items = $this->getItems();
129: if (!$items) {
130: return array();
131: }
132:
133: $newItems = array();
134: $removedItems = array();
135: foreach ($items as $indexNow => $item) {
136: if (in_array($indexNow, $indexes)) {
137: $removedItems[] = $item;
138: } else {
139: $newItems[] = $item;
140: }
141: }
142:
143: $this->_items = $newItems;
144: return $removedItems;
145: }
146:
147: /**
148: * Clears list from all items
149: *
150: * @return Mage_Sales_Model_Status_List
151: */
152: public function clear()
153: {
154: $this->_items = array();
155: return $this;
156: }
157: }
158: