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_Core
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: * Messages collection
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Model_Message_Collection
35: {
36: /**
37: * All messages by type array
38: *
39: * @var array
40: */
41: protected $_messages = array();
42: protected $_lastAddedMessage;
43:
44: /**
45: * Adding new message to collection
46: *
47: * @param Mage_Core_Model_Message_Abstract $message
48: * @return Mage_Core_Model_Message_Collection
49: */
50: public function add(Mage_Core_Model_Message_Abstract $message)
51: {
52: return $this->addMessage($message);
53: }
54:
55: /**
56: * Adding new message to collection
57: *
58: * @param Mage_Core_Model_Message_Abstract $message
59: * @return Mage_Core_Model_Message_Collection
60: */
61: public function addMessage(Mage_Core_Model_Message_Abstract $message)
62: {
63: if (!isset($this->_messages[$message->getType()])) {
64: $this->_messages[$message->getType()] = array();
65: }
66: $this->_messages[$message->getType()][] = $message;
67: $this->_lastAddedMessage = $message;
68: return $this;
69: }
70:
71: /**
72: * Clear all messages except sticky
73: *
74: * @return Mage_Core_Model_Message_Collection
75: */
76: public function clear()
77: {
78: foreach ($this->_messages as $type => $messages) {
79: foreach ($messages as $id => $message) {
80: if (!$message->getIsSticky()) {
81: unset($this->_messages[$type][$id]);
82: }
83: }
84: if (empty($this->_messages[$type])) {
85: unset($this->_messages[$type]);
86: }
87: }
88: return $this;
89: }
90:
91: /**
92: * Get last added message if any
93: *
94: * @return Mage_Core_Model_Message_Abstract|null
95: */
96: public function getLastAddedMessage()
97: {
98: return $this->_lastAddedMessage;
99: }
100:
101: /**
102: * Get first even message by identifier
103: *
104: * @param string $identifier
105: * @return Mage_Core_Model_Message_Abstract|null
106: */
107: public function getMessageByIdentifier($identifier)
108: {
109: foreach ($this->_messages as $type => $messages) {
110: foreach ($messages as $id => $message) {
111: if ($identifier === $message->getIdentifier()) {
112: return $message;
113: }
114: }
115: }
116: }
117:
118: public function deleteMessageByIdentifier($identifier)
119: {
120: foreach ($this->_messages as $type => $messages) {
121: foreach ($messages as $id => $message) {
122: if ($identifier === $message->getIdentifier()) {
123: unset($this->_messages[$type][$id]);
124: }
125: if (empty($this->_messages[$type])) {
126: unset($this->_messages[$type]);
127: }
128: }
129: }
130: }
131:
132: /**
133: * Retrieve messages collection items
134: *
135: * @param string $type
136: * @return array
137: */
138: public function getItems($type=null)
139: {
140: if ($type) {
141: return isset($this->_messages[$type]) ? $this->_messages[$type] : array();
142: }
143:
144: $arrRes = array();
145: foreach ($this->_messages as $messageType => $messages) {
146: $arrRes = array_merge($arrRes, $messages);
147: }
148:
149: return $arrRes;
150: }
151:
152: /**
153: * Retrieve all messages by type
154: *
155: * @param string $type
156: * @return array
157: */
158: public function getItemsByType($type)
159: {
160: return isset($this->_messages[$type]) ? $this->_messages[$type] : array();
161: }
162:
163: /**
164: * Retrieve all error messages
165: *
166: * @return array
167: */
168: public function getErrors()
169: {
170: return $this->getItemsByType(Mage_Core_Model_Message::ERROR);
171: }
172:
173: public function toString()
174: {
175: $out = '';
176: $arrItems = $this->getItems();
177: foreach ($arrItems as $item) {
178: $out.= $item->toString();
179: }
180:
181: return $out;
182: }
183:
184: /**
185: * Retrieve messages count
186: *
187: * @return int
188: */
189: public function count($type=null)
190: {
191: if ($type) {
192: if (isset($this->_messages[$type])) {
193: return count($this->_messages[$type]);
194: }
195: return 0;
196: }
197: return count($this->_messages);
198: }
199: }
200: