* @package WB * @subpackage ITS */ WBClass::load('WBEvent_Handler' , 'WBEvent_Handler_Mail' , 'patI18n'); /** * WBEvent_Handler_ITS_MailWatcher * * Send e-mail to all ticket watchers * * @version 1.0.0 * @package Wombat * @subpackage ITS */ class WBEvent_Handler_ITS_MailWatcher extends WBEvent_Handler { /** * Handler config * * For parameter list see parent class, additional parameter: * - "skipsame" Don't send e-mail if editing user is the same as recipient (default: 1) * - "group" usergroup to select (default: "its-master") * - "enabled" only use enabled users * * @see WBEvent_Handler_Mail * @var array */ protected $config = array( 'from' => '', 'fromindex' => '', 'rcpt' => '', 'rcptindex' => '', 'addrcpt' => '', 'lang' => '', 'langindex' => '', 'tmpl' => 'email', 'skipsame' => '1', 'group' => '!its-master', 'enabled' => '1' ); /** * User object * * @var WBUser */ private $user; /** * Mail Handler * @var WBEvent_Handler_Mail */ private $mail; /** * Table access * * @var WBDatasource_Table */ private $table; /** * Process event * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $this->mail = WBClass::create('WBEvent_Handler_Mail'); if (empty($this->config['rcpt']) && empty($this->config['rcptindex']) ) { return $this->notifyTicketWatcherByTable($e); } return $this->notifyTicketWatcherByEmail($e); } /** * Send Notifction to List of E-Mail-Addresses * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function notifyTicketWatcherByEmail(WBEvent $e) { $rcpt = $this->config['rcpt']; if (!empty($this->config['rcptindex'])) { $rcpt = $e->get($this->config['rcptindex'], $rcpt); } $rcpt = trim($rcpt); $rcpt = str_replace(',', "\n", $rcpt); $rcpt = explode("\n", $rcpt); foreach ($rcpt as $r) { $r = trim($r); // simple address verification if (empty($r)) { continue; } if (!strstr($r, '@')) { continue; } $config = $this->config; $config['rcpt'] = $r; $this->mail->setConfig($config); $this->mail->process($e); } return true; } /** * Send Notifction to Ticket Watching Users * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function notifyTicketWatcherByTable(WBEvent $e) { $this->table = WBClass::create('WBDatasource_Table'); // select recipients $tPrimary = $this->table->getIdentifier('ticket'); $uPrimary = $this->table->getIdentifier('user'); $gPrimary = $this->table->getIdentifier('group'); $clause = array(); $clause[] = array( 'table' => 'ticketwatcher', 'field' => $tPrimary, 'value' => $e->get('id') ); $gnIn = array(); $gnOut = array(); if (!is_array($this->config['group'])) { $this->config['group'] = array($this->config['group']); } foreach ($this->config['group'] as $g) { if ('!' == $g[0]) { $gnOut = substr($g, 1); } else { $gnIn = $g; } } if (!empty($gnIn)) { $clause[] = array( 'table' => 'group', 'field' => 'groupname', 'relation' => 'in', 'value' => $gnIn ); } if (!empty($gnOut)) { $clause[] = array( 'table' => 'group', 'field' => 'groupname', 'relation' => 'not_in', 'value' => $gnOut ); } $options = array( 'limit' => 100, 'join' => array( array( 'table' => 'usergroup', 'using' => $uPrimary ), array( 'table' => 'group', 'using' => $gPrimary ) ), 'groupby' => array( array( 'field' => $uPrimary ) ) ); $pager = $this->table->getPager(__CLASS__ . ':ticketwatcher', 'ticketwatcher', null, $clause, $options); $info = $pager->browse(); // ticket editing user $uId = $e->get($uPrimary, 0); // recipients $this->user = WBClass::create('WBUser'); for ($i = 0; $i < $info['pages']; ++$i) { $rcpts = $pager->get(); foreach ($rcpts as $r) { // don't send an e-mail if ticket was saved by myself if (!empty($this->config['skipsame']) && 0 < intval($this->config['skipsame']) && $uId == $r[$uPrimary]) { continue; } if (!$this->user->load($r[$uPrimary])) { continue; } if (!empty($this->config['enabled']) && 0 < intval($this->config['enabled']) && !$this->user->isEnabled()) { continue; } $user = $this->user->getData(); $config = $this->config; $config['rcpt'] = $user['email']; $config['lang'] = $user['lang']; $this->mail->setConfig($config); $this->mail->process($e); } $pager->browse('__next'); break; } return true; } }