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 block
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Block_Messages extends Mage_Core_Block_Template
35: {
36: /**
37: * Messages collection
38: *
39: * @var Mage_Core_Model_Message_Collection
40: */
41: protected $_messages;
42:
43: /**
44: * Store first level html tag name for messages html output
45: *
46: * @var string
47: */
48: protected $_messagesFirstLevelTagName = 'ul';
49:
50: /**
51: * Store second level html tag name for messages html output
52: *
53: * @var string
54: */
55: protected $_messagesSecondLevelTagName = 'li';
56:
57: /**
58: * Store content wrapper html tag name for messages html output
59: *
60: * @var string
61: */
62: protected $_messagesContentWrapperTagName = 'span';
63:
64: /**
65: * Flag which require message text escape
66: *
67: * @var bool
68: */
69: protected $_escapeMessageFlag = false;
70:
71: /**
72: * Storage for used types of message storages
73: *
74: * @var array
75: */
76: protected $_usedStorageTypes = array('core/session');
77:
78: public function _prepareLayout()
79: {
80: $this->addMessages(Mage::getSingleton('core/session')->getMessages(true));
81: parent::_prepareLayout();
82: }
83:
84: /**
85: * Set message escape flag
86: * @param bool $flag
87: * @return Mage_Core_Block_Messages
88: */
89: public function setEscapeMessageFlag($flag)
90: {
91: $this->_escapeMessageFlag = $flag;
92: return $this;
93: }
94:
95: /**
96: * Set messages collection
97: *
98: * @param Mage_Core_Model_Message_Collection $messages
99: * @return Mage_Core_Block_Messages
100: */
101: public function setMessages(Mage_Core_Model_Message_Collection $messages)
102: {
103: $this->_messages = $messages;
104: return $this;
105: }
106:
107: /**
108: * Add messages to display
109: *
110: * @param Mage_Core_Model_Message_Collection $messages
111: * @return Mage_Core_Block_Messages
112: */
113: public function addMessages(Mage_Core_Model_Message_Collection $messages)
114: {
115: foreach ($messages->getItems() as $message) {
116: $this->getMessageCollection()->add($message);
117: }
118: return $this;
119: }
120:
121: /**
122: * Retrieve messages collection
123: *
124: * @return Mage_Core_Model_Message_Collection
125: */
126: public function getMessageCollection()
127: {
128: if (!($this->_messages instanceof Mage_Core_Model_Message_Collection)) {
129: $this->_messages = Mage::getModel('core/message_collection');
130: }
131: return $this->_messages;
132: }
133:
134: /**
135: * Adding new message to message collection
136: *
137: * @param Mage_Core_Model_Message_Abstract $message
138: * @return Mage_Core_Block_Messages
139: */
140: public function addMessage(Mage_Core_Model_Message_Abstract $message)
141: {
142: $this->getMessageCollection()->add($message);
143: return $this;
144: }
145:
146: /**
147: * Adding new error message
148: *
149: * @param string $message
150: * @return Mage_Core_Block_Messages
151: */
152: public function addError($message)
153: {
154: $this->addMessage(Mage::getSingleton('core/message')->error($message));
155: return $this;
156: }
157:
158: /**
159: * Adding new warning message
160: *
161: * @param string $message
162: * @return Mage_Core_Block_Messages
163: */
164: public function addWarning($message)
165: {
166: $this->addMessage(Mage::getSingleton('core/message')->warning($message));
167: return $this;
168: }
169:
170: /**
171: * Adding new nitice message
172: *
173: * @param string $message
174: * @return Mage_Core_Block_Messages
175: */
176: public function addNotice($message)
177: {
178: $this->addMessage(Mage::getSingleton('core/message')->notice($message));
179: return $this;
180: }
181:
182: /**
183: * Adding new success message
184: *
185: * @param string $message
186: * @return Mage_Core_Block_Messages
187: */
188: public function addSuccess($message)
189: {
190: $this->addMessage(Mage::getSingleton('core/message')->success($message));
191: return $this;
192: }
193:
194: /**
195: * Retrieve messages array by message type
196: *
197: * @param string $type
198: * @return array
199: */
200: public function getMessages($type=null)
201: {
202: return $this->getMessageCollection()->getItems($type);
203: }
204:
205: /**
206: * Retrieve messages in HTML format
207: *
208: * @param string $type
209: * @return string
210: */
211: public function getHtml($type=null)
212: {
213: $html = '<' . $this->_messagesFirstLevelTagName . ' id="admin_messages">';
214: foreach ($this->getMessages($type) as $message) {
215: $html.= '<' . $this->_messagesSecondLevelTagName . ' class="'.$message->getType().'-msg">'
216: . ($this->_escapeMessageFlag) ? $this->htmlEscape($message->getText()) : $message->getText()
217: . '</' . $this->_messagesSecondLevelTagName . '>';
218: }
219: $html .= '</' . $this->_messagesFirstLevelTagName . '>';
220: return $html;
221: }
222:
223: /**
224: * Retrieve messages in HTML format grouped by type
225: *
226: * @param string $type
227: * @return string
228: */
229: public function getGroupedHtml()
230: {
231: $types = array(
232: Mage_Core_Model_Message::ERROR,
233: Mage_Core_Model_Message::WARNING,
234: Mage_Core_Model_Message::NOTICE,
235: Mage_Core_Model_Message::SUCCESS
236: );
237: $html = '';
238: foreach ($types as $type) {
239: if ( $messages = $this->getMessages($type) ) {
240: if ( !$html ) {
241: $html .= '<' . $this->_messagesFirstLevelTagName . ' class="messages">';
242: }
243: $html .= '<' . $this->_messagesSecondLevelTagName . ' class="' . $type . '-msg">';
244: $html .= '<' . $this->_messagesFirstLevelTagName . '>';
245:
246: foreach ( $messages as $message ) {
247: $html.= '<' . $this->_messagesSecondLevelTagName . '>';
248: $html.= '<' . $this->_messagesContentWrapperTagName . '>';
249: $html.= ($this->_escapeMessageFlag) ? $this->htmlEscape($message->getText()) : $message->getText();
250: $html.= '</' . $this->_messagesContentWrapperTagName . '>';
251: $html.= '</' . $this->_messagesSecondLevelTagName . '>';
252: }
253: $html .= '</' . $this->_messagesFirstLevelTagName . '>';
254: $html .= '</' . $this->_messagesSecondLevelTagName . '>';
255: }
256: }
257: if ( $html) {
258: $html .= '</' . $this->_messagesFirstLevelTagName . '>';
259: }
260: return $html;
261: }
262:
263: protected function _toHtml()
264: {
265: return $this->getGroupedHtml();
266: }
267:
268: /**
269: * Set messages first level html tag name for output messages as html
270: *
271: * @param string $tagName
272: */
273: public function setMessagesFirstLevelTagName($tagName)
274: {
275: $this->_messagesFirstLevelTagName = $tagName;
276: }
277:
278: /**
279: * Set messages first level html tag name for output messages as html
280: *
281: * @param string $tagName
282: */
283: public function setMessagesSecondLevelTagName($tagName)
284: {
285: $this->_messagesSecondLevelTagName = $tagName;
286: }
287:
288: /**
289: * Get cache key informative items
290: *
291: * @return array
292: */
293: public function getCacheKeyInfo()
294: {
295: return array(
296: 'storage_types' => serialize($this->_usedStorageTypes)
297: );
298: }
299:
300: /**
301: * Add used storage type
302: *
303: * @param string $type
304: */
305: public function addStorageType($type)
306: {
307: $this->_usedStorageTypes[] = $type;
308: }
309: }
310: