* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_UserMsgMail * * Actually send user-group-message to all recipients via e-mail. * First selecte message data, then collect all recipients' user data * and send e-mail * * @version 1.1.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_UserMsgMail extends WBEvent_Handler { /** * handler config * * Parameter: * - "action": send or remind * - "remindagemin": min age of message to remind rcpt * - "remindagemax": max age of message to remind rcpt, if any * - "from": e-mail address in from header * - "clause": optional additional clause to select rpts from user table * * @var array */ protected $config = array( 'action' => 'send', 'remindagemin' => '-1 week', 'remindagemax' => '', 'from' => 'info@example.com', 'tmpl' => 'usermessage/email', 'clause' => array() ); /** * Message data * @var array */ private $msg = array(); /** * table access * @var WBDatasource_Table */ private $table; /** * E-Mail class * @var WBMailer */ private $mailer; /** * constructor * * Instanciate table and mailer */ public function __construct() { $this->table = WBClass::create('WBDatasource_Table'); $this->table->switchTranslation(false); $this->mailer = WBClass::create('WBMailer'); } /** * Send user group message to each recipient * * * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { if (!is_array($this->config['clause'])) { $this->config['clause'] = array(); } $ids = $this->getMsgIds($e); foreach ($ids as $id ) { $this->sendMsg($id); } return true; } /** * Get List of Msg Ids * * Collect msg-ids depending on config parameter "action": * - "send" select single id by event data "id" * - "remind" select message ids from database * * @param WBEvent $e * @return array */ private function getMsgIds(WBEvent $e) { $ids = array(); switch ($this->config['action']) { case 'remind': $primary = $this->table->getIdentifier('usergroupmessage'); $clause = array(); $clause[] = array( 'table' => 'usergroupmessageuser', 'field' => 'confirmed', 'value' => '1970-01-01 00:00:00' ); $clause[] = array( 'table' => 'usergroupmessageuser', 'field' => 'created', 'relation' => 'lt', 'value' => gmdate('Y-m-d H:i:s', strtotime($this->config['remindagemin'])) ); if (!empty($this->config['remindagemax'])) { $clause[] = array( 'table' => 'usergroupmessageuser', 'field' => 'created', 'relation' => 'gt', 'value' => gmdate('Y-m-d H:i:s', strtotime($this->config['remindagemax'])) ); } $options = array(); $options['column'] = array( $primary ); $options['groupby'] = array( 'field' => $primary ); $list = $this->table->get('usergroupmessageuser', null, null, $clause, $options); foreach ($list as $l) { $ids[] = $l[$primary]; } break; case 'send': default: $ids = array($e->get('id')); break; } return $ids; } /** * Send Message by Id * * Prepare message and send to all recipients that are marked as unconfirmed. * * @param string $id message id */ private function sendMsg($id) { $this->prepareMessage($id); // select recipients $clause = $this->config['clause']; $clause[] = array( 'table' => 'usergroupmessageuser', 'field' => $this->table->getIdentifier('usergroupmessage'), 'value' => $id ); $clause[] = array( 'table' => 'usergroupmessageuser', 'field' => 'confirmed', 'value' => '1970-01-01 00:00:00' ); $options = array( 'limit' => 100, 'join' => array( array( 'table' => 'usergroupmessageuser', 'using' => $this->table->getIdentifier('user') ) ) ); $pager = $this->table->getPager(__CLASS__ . ':' . $id, 'user', null, $clause, $options); $info = $pager->browse(); for ($i = 0; $i < $info['pages']; ++$i) { $rcpts = $pager->get(); foreach ($rcpts as $r) { $this->sendEMail($r); } $pager->browse('__next'); break; } } /** * Prepare user group message * * Load actual message from database and prepare mailer * * @param string $id usergroupmessage-id * @throws WBException_Datasource */ private function prepareMessage($id) { $msg = $this->table->get('usergroupmessage', $id, null, $this->config['clause']); if (1 != count($msg)) { WBClass::load('WBException_Datasource'); throw new WBException_Datasource('Failed to load user group message by id', 1, __CLASS__); } // prepare mailer $this->mailer->setTemplate($this->config['tmpl']); $this->mailer->setData($msg[0], 'msg_'); $this->mailer->setFrom($this->config['from']); } /** * Send e-mail to user * * Use WBMailer, add user data and send out mail. * * @param array $rcpt user data */ private function sendEMail($rcpt) { $email = $rcpt['emaillocal'] . '@' . $rcpt['emaildomain']; $this->mailer->addData($rcpt); $this->mailer->addRcpt($email); $this->mailer->send(); } }