[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Notification:: class provides a subject-observer pattern for 4 * raising and showing messages of different types and to different 5 * listeners. 6 * 7 * $Horde: framework/Notification/Notification.php,v 1.46.2.11 2006/01/18 15:19:01 jan Exp $ 8 * 9 * Copyright 2001-2006 Jan Schneider <jan@horde.org> 10 * 11 * See the enclosed file COPYING for license information (LGPL). If you 12 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 13 * 14 * @author Jan Schneider <jan@horde.org> 15 * @since Horde 2.1 16 * @package Horde_Notification 17 */ 18 class Notification { 19 20 /** 21 * Hash containing all attached listener objects. 22 * 23 * @var array 24 */ 25 var $_listeners = array(); 26 27 /** 28 * The name of the session variable where we store the messages. 29 * 30 * @var string 31 */ 32 var $_stack = 'hordeMessageStacks'; 33 34 /** 35 * Returns a reference to the global Notification object, only 36 * creating it if it doesn't already exist. 37 * 38 * This method must be invoked as: 39 * $notification = &Notification::singleton() 40 * 41 * @param string $stack The name of the message stack to use. 42 * 43 * @return Notification The Horde Notification instance. 44 */ 45 function &singleton($stack = 'hordeMessageStacks') 46 { 47 static $notification = array(); 48 49 if (!isset($notification[$stack])) { 50 $notification[$stack] = new Notification($stack); 51 } 52 53 return $notification[$stack]; 54 } 55 56 /** 57 * Initialize the notification system, set up any needed session 58 * variables, etc. Should never be called except by 59 * &Notification::singleton(); 60 * 61 * @param string $stack The name of the message stack to use. 62 */ 63 function Notification($stack = 'hordeMessageStacks') 64 { 65 $this->_stack = $stack; 66 67 /* Make sure the message stack is registered in the session, 68 * and obtain a global-scope reference to it. */ 69 if (!isset($_SESSION[$this->_stack])) { 70 $_SESSION[$this->_stack] = array(); 71 } 72 } 73 74 /** 75 * Registers a listener with the notification object and includes 76 * the necessary library file dynamically. 77 * 78 * @param string $driver The name of the listener to attach. These names 79 * must be unique; further listeners with the same 80 * name will be ignored. 81 * @param array $params A hash containing any additional configuration or 82 * connection parameters a listener driver might 83 * need. 84 * @param string $class The class name from which the driver was 85 * instantiated if not the default one. If given 86 * you have to include the library file containing 87 * this class yourself. This is useful if you want 88 * the listener driver to be overriden by an 89 * application's implementation. 90 */ 91 function &attach($listener, $params = array(), $class = null) 92 { 93 $listener = basename($listener); 94 if (!empty($this->_listeners[$listener])) { 95 return $this->_listeners[$listener]; 96 } 97 98 if (is_null($class)) { 99 @require_once dirname(__FILE__) . '/Notification/Listener/' . $listener . '.php'; 100 $class = 'Notification_Listener_' . $listener; 101 } 102 if (class_exists($class)) { 103 $this->_listeners[$listener] = &new $class($params); 104 if (!isset($_SESSION[$this->_stack][$listener])) { 105 $_SESSION[$this->_stack][$listener] = array(); 106 } 107 return $this->_listeners[$listener]; 108 } else { 109 return PEAR::raiseError(sprintf('Notification listener %s not found.', $listener)); 110 } 111 } 112 113 /** 114 * Remove a listener from the notification list. 115 * 116 * @param string $listner The name of the listener to detach. 117 */ 118 function detach($listener) 119 { 120 $listener = basename($listener); 121 if (!isset($this->_listeners[$listener])) { 122 return PEAR::raiseError(sprintf('Notification listener %s not found.', $listener)); 123 } 124 125 $list = $this->_listeners[$listener]; 126 unset($this->_listeners[$listener]); 127 unset($_SESSION[$this->_stack][$list->getName()]); 128 return true; 129 } 130 131 /** 132 * Add an event to the Horde message stack. 133 * 134 * The event type parameter should begin with 'horde.' unless the 135 * application defines its own Notification_Listener subclass that 136 * handles additional codes. 137 * 138 * @param mixed $event Notification_Event object or message string. 139 * @param integer $type The type of message: 'horde.error', 140 * 'horde.warning', 'horde.success', or 141 * 'horde.message'. 142 * @param array $flags Array of optional flags that will be passed to the 143 * registered listeners. 144 */ 145 function push($event, $type = null, $flags = array()) 146 { 147 if (!is_a($event, 'Notification_Event') && 148 !is_a($event, 'PEAR_Error')) { 149 /* Transparently create a Notification_Event object and 150 * set the message attribute. */ 151 require_once dirname(__FILE__) . '/Notification/Event.php'; 152 $event = &new Notification_Event($event); 153 } 154 if (is_a($event, 'PEAR_Error')) { 155 if (!isset($type)) { 156 $type = 'horde.error'; 157 } 158 Horde::logMessage($event, __FILE__, __LINE__, PEAR_LOG_DEBUG); 159 } 160 if (!isset($type)) { 161 $type = 'horde.message'; 162 } 163 164 foreach ($this->_listeners as $listener) { 165 if ($listener->handles($type)) { 166 $_SESSION[$this->_stack][$listener->getName()][] = 167 array('type' => $type, 168 'class' => get_class($event), 169 'event' => serialize($event), 170 'flags' => serialize($flags)); 171 } 172 } 173 } 174 175 /** 176 * Passes the message stack to all listeners and asks them to 177 * handle their messages. 178 * 179 * @param array $options An array containing display options for the 180 * listeners. 181 */ 182 function notify($options = array()) 183 { 184 if (!isset($options['listeners'])) { 185 $options['listeners'] = array_keys($this->_listeners); 186 } elseif (!is_array($options['listeners'])) { 187 $options['listeners'] = array($options['listeners']); 188 } 189 foreach ($options['listeners'] as $listener) { 190 $this->_listeners[$listener]->notify($_SESSION[$this->_stack][$this->_listeners[$listener]->getName()], $options); 191 } 192 } 193 194 /** 195 * Return the number of notification messages in the stack. 196 * 197 * @author David Ulevitch <davidu@everydns.net> 198 * 199 * @param string $my_listener The name of the listener. 200 * 201 * @return integer The number of messages in the stack. 202 */ 203 function count($my_listener = null) 204 { 205 if (is_null($my_listener)) { 206 $count = 0; 207 foreach ($this->_listeners as $listener) { 208 if (isset($_SESSION[$this->_stack][$listener->getName()])) { 209 $count += count($_SESSION[$this->_stack][$listener->getName()]); 210 } 211 } 212 return $count; 213 } else { 214 return @count($_SESSION[$this->_stack][$this->_listeners[$my_listener]->getName()]); 215 } 216 } 217 218 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |