00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Core 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 /** 00028 * Messages block 00029 * 00030 * @category Mage 00031 * @package Mage_Core 00032 * @author Magento Core Team <core@magentocommerce.com> 00033 */ 00034 class Mage_Core_Block_Messages extends Mage_Core_Block_Template 00035 { 00036 /** 00037 * Messages collection 00038 * 00039 * @var Mage_Core_Model_Message_Collection 00040 */ 00041 protected $_messages; 00042 00043 public function _prepareLayout() 00044 { 00045 $this->addMessages(Mage::getSingleton('core/session')->getMessages(true)); 00046 00047 parent::_prepareLayout(); 00048 } 00049 00050 /** 00051 * Set messages collection 00052 * 00053 * @param Mage_Core_Model_Message_Collection $messages 00054 * @return Mage_Core_Block_Messages 00055 */ 00056 public function setMessages(Mage_Core_Model_Message_Collection $messages) 00057 { 00058 $this->_messages = $messages; 00059 return $this; 00060 } 00061 00062 public function addMessages(Mage_Core_Model_Message_Collection $messages) 00063 { 00064 foreach ($messages->getItems() as $message) { 00065 $this->getMessageCollection()->add($message); 00066 } 00067 return $this; 00068 } 00069 00070 /** 00071 * Retrieve messages collection 00072 * 00073 * @return Mage_Core_Model_Message_Collection 00074 */ 00075 public function getMessageCollection() 00076 { 00077 if (!($this->_messages instanceof Mage_Core_Model_Message_Collection)) { 00078 $this->_messages = Mage::getModel('core/message_collection'); 00079 } 00080 return $this->_messages; 00081 } 00082 00083 /** 00084 * Adding new message to message collection 00085 * 00086 * @param Mage_Core_Model_Message_Abstract $message 00087 * @return Mage_Core_Block_Messages 00088 */ 00089 public function addMessage(Mage_Core_Model_Message_Abstract $message) 00090 { 00091 $this->getMessageCollection()->add($message); 00092 return $this; 00093 } 00094 00095 /** 00096 * Adding new error message 00097 * 00098 * @param string $message 00099 * @return Mage_Core_Block_Messages 00100 */ 00101 public function addError($message) 00102 { 00103 $this->addMessage(Mage::getSingleton('core/message')->error($message)); 00104 return $this; 00105 } 00106 00107 /** 00108 * Adding new warning message 00109 * 00110 * @param string $message 00111 * @return Mage_Core_Block_Messages 00112 */ 00113 public function addWarning($message) 00114 { 00115 $this->addMessage(Mage::getSingleton('core/message')->warning($message)); 00116 return $this; 00117 } 00118 00119 /** 00120 * Adding new nitice message 00121 * 00122 * @param string $message 00123 * @return Mage_Core_Block_Messages 00124 */ 00125 public function addNotice($message) 00126 { 00127 $this->addMessage(Mage::getSingleton('core/message')->notice($message)); 00128 return $this; 00129 } 00130 00131 /** 00132 * Adding new success message 00133 * 00134 * @param string $message 00135 * @return Mage_Core_Block_Messages 00136 */ 00137 public function addSuccess($message) 00138 { 00139 $this->addMessage(Mage::getSingleton('core/message')->success($message)); 00140 return $this; 00141 } 00142 00143 /** 00144 * Retrieve messages array by message type 00145 * 00146 * @param string $type 00147 * @return array 00148 */ 00149 public function getMessages($type=null) 00150 { 00151 return $this->getMessageCollection()->getItems($type); 00152 } 00153 00154 /** 00155 * Retrieve messages in HTML format 00156 * 00157 * @param string $type 00158 * @return string 00159 */ 00160 public function getHtml($type=null) 00161 { 00162 $html = '<ul id="admin_messages">'; 00163 foreach ($this->getMessages($type) as $message) { 00164 $html.= '<li class="'.$message->getType().'-msg">'.$message->getText().'</li>'; 00165 } 00166 $html .= '</ul>'; 00167 return $html; 00168 } 00169 00170 /** 00171 * Retrieve messages in HTML format grouped by type 00172 * 00173 * @param string $type 00174 * @return string 00175 */ 00176 public function getGroupedHtml() 00177 { 00178 $types = array( 00179 Mage_Core_Model_Message::ERROR, 00180 Mage_Core_Model_Message::WARNING, 00181 Mage_Core_Model_Message::NOTICE, 00182 Mage_Core_Model_Message::SUCCESS 00183 ); 00184 $html = ''; 00185 foreach ($types as $type) { 00186 if ( $messages = $this->getMessages($type) ) { 00187 if ( !$html ) { 00188 $html .= '<ul class="messages">'; 00189 } 00190 $html .= '<li class="' . $type . '-msg">'; 00191 $html .= '<ul>'; 00192 00193 foreach ( $messages as $message ) { 00194 $html.= '<li>'; 00195 $html.= $message->getText(); 00196 $html.= '</li>'; 00197 } 00198 $html .= '</ul>'; 00199 $html .= '</li>'; 00200 } 00201 } 00202 if ( $html) { 00203 $html .= '</ul>'; 00204 } 00205 return $html; 00206 } 00207 00208 protected function _toHtml() 00209 { 00210 return $this->getGroupedHtml(); 00211 } 00212 }