| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage View | 
					
						
							|  |  |  |  * @since 2010-03-09 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  | class MsgViewHelper extends ViewHelper | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-11-21 15:43:20 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     const SUCCESS = 'success'; | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     const ERROR = 'error'; | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const INFO = 'info'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const WARNING = 'warning'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-10 11:32:32 +04:00
										 |  |  |     protected $use_as_html = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-23 13:41:04 +04:00
										 |  |  |     private static $type_to_class = array( | 
					
						
							|  |  |  |         self::SUCCESS => 'success', | 
					
						
							|  |  |  |         self::ERROR => 'error', | 
					
						
							|  |  |  |         self::INFO => 'info', | 
					
						
							|  |  |  |         self::WARNING => 'warning', | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |     protected $css_prefix = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     public function msg($msg = null, $type = null) | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |         if ($msg && $type) { | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |             if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) { | 
					
						
							| 
									
										
										
										
											2011-11-24 17:57:40 +04:00
										 |  |  |                 throw new GeneralException('Unknown message type: "' . $type . '"'); | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |             $this->set($msg, $type); | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-11-21 15:43:20 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     public function success($msg) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |         $this->set($msg, self::SUCCESS); | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-11-21 15:43:20 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     public function error($msg) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |         $this->set($msg, self::ERROR); | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function info($msg) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->set($msg, self::INFO); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function warning($msg) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->set($msg, self::WARNING); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function set($msg, $type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Session::set(__CLASS__, array('message' => $msg, 'type' => $type)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-10 11:32:32 +04:00
										 |  |  |     public static function getType() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $msg = Session::get(__CLASS__, false); | 
					
						
							|  |  |  |         return $msg ? $msg['type'] : ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-21 18:11:38 +04:00
										 |  |  |     public function withPrefix($css_prefix) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->css_prefix = $css_prefix; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     public function __toString() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $msg = Session::get(__CLASS__, false); | 
					
						
							|  |  |  |         if ($msg) { | 
					
						
							|  |  |  |             Session::del(__CLASS__); | 
					
						
							| 
									
										
										
										
											2013-09-23 13:41:04 +04:00
										 |  |  |             $type_to_class = static::getTypeToClass(); | 
					
						
							| 
									
										
										
										
											2013-10-10 11:32:32 +04:00
										 |  |  |             return '<div class="' . $this->css_prefix . $type_to_class[$msg['type']] . '">' . (($this->use_as_html)?$msg['message']:$this->view->escape($msg['message'])) . '</div>'; | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-09-06 09:15:54 +00:00
										 |  |  |         return ''; | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-09-23 13:41:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected static function getTypeToClass() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::$type_to_class; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-03-14 19:48:02 +00:00
										 |  |  | } |