* @package WB * @subpackage ITS */ WBClass::load('WBEvent_Handler' , 'WBEvent_Handler_Mail' , 'patI18n' , 'WBDatasource' , 'WBITS' ); /** * WBEvent_Handler_ITS_MailWatcher * * Send e-mail to all ticket watchers * * @version 1.1.2 * @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' => '', 'replyto' => '', 'replytoindex' => 'email', 'rcpt' => '', 'rcptindex' => '', 'addrcpt' => '', 'lang' => '', 'langindex' => '', 'tmpl' => 'email', 'skipsame' => '1', 'group' => '!its-master', 'enabled' => '1', 'ticketidindex' => 'id' ); /** * 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'); $this->mail->setLogger($this->log); 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); } $tId = $e->get($this->config['ticketidindex']); $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); $log = array( 'class' => __CLASS__, 'ticket' => $tId, 'action' => 'notifyByEMail', 'rcpt' => $r ); $this->log->debug($log); $this->debugPrint(sprintf('Ticket %s watcher %s, sent.', $tId, $r), 5); } 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) { if (empty($this->table)) { $this->table = WBClass::create('WBDatasource_Table'); } $tId = $e->get($this->config['ticketidindex']); // select recipients $tPrimary = $this->table->getIdentifier(WBITS::TABLE_TICKET, true); $uPrimary = $this->table->getIdentifier(WBDatasource::TABLE_USER, true); $gPrimary = $this->table->getIdentifier(WBDatasource::TABLE_GROUP, true); $clause = array(); $clause[] = array( 'table' => WBITS::TABLE_TICKETWATCHER, 'field' => $tPrimary, 'value' => $tId ); $gnIn = array(); $gnOut = array(); if (!is_array($this->config['group'])) { $this->config['group'] = trim($this->config['group']); if (empty($this->config['group'])) { $this->config['group'] = array(); } else { $this->config['group'] = array($this->config['group']); } } foreach ($this->config['group'] as $g) { if ('!' == $g[0]) { $gnOut[]= substr($g, 1); } else { $gnIn[] = $g; } } $options = array( 'limit' => 100 ); $pager = $this->table->getPager(__CLASS__ . ':ticketwatcher', 'ticketwatcher', null, $clause, $options); $info = $pager->browse(); $this->debugPrint(sprintf('Ticket %s, found %d watcher(s).', $tId, $info['total']), 5); // ticket editing user $uId = $e->get($uPrimary, 0); // prepare logging $log = array( 'class' => __CLASS__, 'ticket' => $tId, 'action' => 'notifyByTable', 'uid' => 0, 'msg' => 'OK' ); // recipients $this->user = WBClass::create('WBUser'); for ($i = 0; $i < $info['pages']; ++$i) { $rcpts = $pager->get(); foreach ($rcpts as $r) { $log['msg'] = 'OK'; $log['uid'] = $r[$uPrimary]; // 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]) { $this->debugPrint(sprintf('Ticket %s watcher %s, skiped because user is same.', $tId, $r[$uPrimary]), 5); $log['msg'] = 'user is same'; $this->log->debug($log); continue; } if (!$this->user->load($r[$uPrimary])) { $this->debugPrint(sprintf('Ticket %s watcher %s, could not load user by id.', $tId, $r[$uPrimary]), 5); $log['msg'] = 'invalid user id'; $this->log->debug($log); continue; } if (!empty($this->config['enabled']) && 0 < intval($this->config['enabled']) && !$this->user->isEnabled()) { $this->debugPrint(sprintf('Ticket %s watcher %s, user is not enabled.', $tId, $r[$uPrimary]), 5); $log['msg'] = 'user not enabled'; $this->log->debug($log); continue; } if (!empty($gnOut) && $this->user->isInGroup($gnOut)) { $this->debugPrint(sprintf('Ticket %s watcher %s, user is member of any ignore group (%s).', $tId, $r[$uPrimary], implode(', ', $gnOut)), 5); $log['msg'] = 'user in ignore group'; $this->log->debug($log); continue; } if (!empty($gnIn) && !$this->user->isInGroup($gnIn)) { $this->debugPrint(sprintf('Ticket %s watcher %s, user is not member of any required group (%s).', $tId, $r[$uPrimary], implode(', ', $gnIn)), 5); $log['msg'] = 'user not in required group'; $this->log->debug($log); continue; } $user = $this->user->getData(); $config = $this->config; $config['rcpt'] = $user['email']; $config['lang'] = $user['lang']; $this->log->debug($log); $this->mail->setConfig($config); $this->mail->process($e); $this->debugPrint(sprintf('Ticket %s watcher %s, sent.', $tId, $r[$uPrimary]), 5); } $pager->browse('__next'); break; } return true; } }