* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableIndexer * * concatenate list of columns into index table * * @version 0.2.1 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableRelationTrigger extends WBEvent_Handler { /** * table access * @var WBDatasource_Table */ private $table; /** * handler config * * - table name of table to update * - event event to trigger * - relationtables list of relation tables to trigger event for * * @var array */ protected $config = array( 'table' => '', 'event' => '', 'eventmsg' => 'trigger relation event', 'relationtables' => array() ); /** * Trigger event for all table records that are related to the current one * * * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $table = $this->config['table']; $id = null; if (empty($this->config['table'])) { $table = $e->get('table'); } $id = $e->get('id'); if (empty($id) || $id == '__new') { WBClass::load('WBException_Config'); throw new WBException_Config('Key may not be empty or "__new"', 1, __CLASS__); } if (empty($this->config['event'])) { WBClass::load('WBException_Config'); throw new WBException_Config('Target event must not be empty', 2, __CLASS__); } if ('__all' == $this->config['relationtables']) { $this->config['relationtables'] = $this->table->getRelationTables($table); } if (empty($this->config['relationtables']) || !is_array($this->config['relationtables'])) { WBClass::load('WBException_Config'); throw new WBException_Config('List of relation tables must not be empty', 3, __CLASS__); } $this->table = WBClass::create('WBDatasource_Table'); $this->table->switchTranslation(false); $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier($table), 'value' => $id ); $options = array( 'limit' => 0 ); foreach ($this->config['relationtables'] as $r) { $this->triggerRelation($r, $clause, $options); } return true; } /** * Trigger event for related tables * * Fetch rows of related table and trigger event for each row * * @param string $relation related table * @param array $clause to fetch related records * @param array $options fetch options */ private function triggerRelation($relation, $clause, $options) { $event = str_replace('{RELATIONTABLE}', $relation, $this->config['event']); $msg = $this->config['eventmsg']; $ids = $this->table->getIds($relation, null, $clause, $options); $eData = array( 'id' => 'id', 'table' => $relation ); foreach ($ids as $id) { $eData['id'] = $id; WBEvent::trigger($event, $msg, $eData); } } } ?>