Horde_Notification_Handler::attach PHP Method

attach() public method

Registers a listener with the notification object and includes the necessary library file dynamically.
public attach ( string $listener, array $params = null, string $class = null ) : Horde_Notification_Listener
$listener string The name of the listener to attach. These names must be unique; further listeners with the same name will be ignored.
$params array A hash containing any additional configuration or connection parameters a listener driver might need.
$class string The class name from which the driver was instantiated if not the default one. If given you have to include the library file containing this class yourself. This is useful if you want the listener driver to be overriden by an application's implementation
return Horde_Notification_Listener The listener object.
    public function attach($listener, $params = null, $class = null)
    {
        if ($ob = $this->getListener($listener)) {
            return $ob;
        }
        if (is_null($class)) {
            $class = 'Horde_Notification_Listener_' . Horde_String::ucfirst(Horde_String::lower($listener));
        }
        if (class_exists($class)) {
            $this->_listeners[$listener] = new $class($params);
            if (!$this->_storage->exists($listener)) {
                $this->_storage->set($listener, array());
            }
            $this->_addTypes($listener);
            return $this->_listeners[$listener];
        }
        throw new Horde_Exception(sprintf('Notification listener %s not found.', $class));
    }

Usage Example

Example #1
0
 /**
  */
 protected function _notify(Horde_Notification_Handler $handler, Horde_Notification_Listener $listener)
 {
     global $injector, $prefs, $session;
     if (!$prefs->getValue('newmail_notify') || !$listener instanceof Horde_Notification_Listener_Status) {
         return;
     }
     /* Rate limit. If rate limit is not yet set, this is the initial
      * login so skip. */
     $curr = time();
     $ratelimit = $session->get('imp', self::SESS_RATELIMIT);
     if ($ratelimit && $ratelimit + self::RATELIMIT > $curr) {
         return;
     }
     $session->set('imp', self::SESS_RATELIMIT, $curr);
     if (!$ratelimit) {
         return;
     }
     $ajax_queue = $injector->getInstance('IMP_Ajax_Queue');
     $imp_imap = $injector->getInstance('IMP_Factory_Imap')->create();
     $recent = array();
     try {
         foreach ($imp_imap->status($injector->getInstance('IMP_Ftree')->poll->getPollList(), Horde_Imap_Client::STATUS_RECENT_TOTAL, array('sort' => true)) as $key => $val) {
             if (!empty($val['recent_total'])) {
                 /* Open the mailbox R/W so we ensure the 'recent' flag is
                  * cleared. */
                 $imp_imap->openMailbox($key, Horde_Imap_Client::OPEN_READWRITE);
                 $mbox = IMP_Mailbox::get($key);
                 $recent[$mbox->display] = $val['recent_total'];
                 $ajax_queue->poll($mbox);
             }
         }
     } catch (Exception $e) {
     }
     if (empty($recent)) {
         return;
     }
     $recent_sum = array_sum($recent);
     reset($recent);
     switch (count($recent)) {
         case 1:
             $mbox_list = key($recent);
             break;
         case 2:
             $mbox_list = implode(_(" and "), array_keys($recent));
             break;
         default:
             $akeys = array_keys($recent);
             $mbox_list = $akeys[0] . ', ' . $akeys[1] . ', ' . _("and") . ' ' . $akeys[2];
             if ($addl_mbox = count($recent) - 3) {
                 $mbox_list .= ' (' . sprintf(ngettext("and %d more mailbox", "and %d more mailboxes", $addl_mbox), $addl_mbox) . ')';
             }
             break;
     }
     $text = sprintf(ngettext("You have %d new mail message in %s.", "You have %d new mail messages in %s.", $recent_sum), $recent_sum, $mbox_list);
     /* Status notification. */
     $handler->push($text, 'horde.message');
     /* Web notifications. */
     $handler->attach('webnotification', null, 'Horde_Core_Notification_Listener_Webnotification');
     $handler->push(Horde_Core_Notification_Event_Webnotification::createEvent($text, array('icon' => strval(Horde_Themes::img('unseen.png')))), 'webnotification');
     if ($audio = $prefs->getValue('newmail_audio')) {
         $handler->attach('audio');
         $handler->push(Horde_Themes::sound($audio), 'audio');
     }
 }
All Usage Examples Of Horde_Notification_Handler::attach