* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_UserMsgRcptSetter * * Add user to rcpt table. There are unique records for each user and message. * Hence one can read which user received a message and when it was marked confirmed. * * @version 1.1.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_UserMsgRcptSetter extends WBEvent_Handler { /** * table access * @var WBDatasource_Table */ static private $table; /** * text importer * @var patI18n_Importer */ static private $imp; /** * handler config * * - autostop: stop if no user was added * - field: enabled field * - hideuser: list of users to ignore * - clause: additional clause for querying users * * @var array */ protected $config = array( 'autostop' => '1', 'field' => 'enabled', 'hideuser' => array(), 'clause' => array() ); /** * Set users as recipients * * This event handler works with save event of usergroupmessage table. It requires saved * data and message id * * Check whether messages is marked enabled and therefore ready to be send to users. * If not, it will return (true or false regarding "autostop") * Check whether this usergroupmessage already has some recipients * If there are some recipients, return either false on autostop, true otherwise * Select members of user group * Add user ids as recipients in usergroupmessageuser * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { // wrong table if ('usergroupmessage' != $e->get('table')) { return false; } $id = $e->get('id'); $data = $e->get('save'); $switch = 0; if (isset($data[$this->config['field']])) { $switch = intval($data[$this->config['field']]); } if (1 > $switch) { if ($this->config['autostop']) { return false; } return true; } if (!self::$table) { self::$table = WBClass::create('WBDatasource_Table'); self::$table->switchTranslation(false); } $old = self::$table->count('usergroupmessageuser', null, $id); if (0 < $old) { if ($this->config['autostop']) { return false; } return true; } // get user ids for selected group $pUGMsg = self::$table->getIdentifier('usergroupmessage'); $pGroup = self::$table->getIdentifier('group'); $pUser = self::$table->getIdentifier('user'); $clause = $this->config['clause']; if (!is_array($clause)) { $clause = array(); } if (!empty($this->config['hideuser'])) { $clause[] = array( 'table' => 'user', 'field' => $pUser, 'value' => $this->config['hideuser'], 'relation' => 'not_in' ); } $options = array(); $options['limit'] = 0; // select rcpts by group or by uid list if (!isset($data['rcptselect']) || empty($data['rcptselect'])) { $clause[] = array( 'table' => 'usergroup', 'field' => $pGroup, 'value' => $data[$pGroup] ); $options['join'] = array(); $options['join'][] = array( 'table' => 'usergroup', 'using' => $pUser ); } else { $clause[] = array( 'table' => 'usergroupmessagercpt', 'field' => $pUGMsg, 'value' => $id ); $options['join'] = array(); $options['join'][] = array( 'table' => 'usergroupmessagercpt', 'using' => $pUser ); } $user = self::$table->getIds('user', null, $clause, $options); $e->set('rcpts', $user); $save = array(); foreach ($user as $u) { $save[] = array( $pUGMsg => $id, $pUser => $u ); } self::$table->save('usergroupmessageuser', '__new', $save); return true; } }