Index: branches/5.2.x/core/kernel/utility/event.php =================================================================== diff -u -N -r14092 -r14095 --- branches/5.2.x/core/kernel/utility/event.php (.../event.php) (revision 14092) +++ branches/5.2.x/core/kernel/utility/event.php (.../event.php) (revision 14095) @@ -1,6 +1,6 @@ 's', // redirect to given template + 'pass' => 'all' // pass all discovered units to redirected page + ); /** - * php file to redirect to + * PHP file to redirect to. Defaults to "index.php" * * @var string * @access public */ - var $redirect_script = null; + public $redirectScript = null; /** - * Event processing result + * Event processing status * * @var int * @access public */ - var $status = erSUCCESS; + public $status = kEvent::erSUCCESS; /** - * Each event specific only params, - * that they use for communication + * Event parameters + * Usually indicate, how particular event should be processed. * * @var Array - * @access public + * @access private */ - var $specificParams = Array(); + private $specificParams = Array (); /** - * Pseudo class used to create object, - * in case if one is not already created + * Pseudo class used, to create object, based on event contents * * @var string - * @access public + * @access private */ - var $pseudoClass = ''; + private $pseudoClass = ''; /** - * Create event based on params passed + * Create event from given prefix, special, name and specific params. + * Parameter $params could be be an an array with following keys: "prefix", "special" (optional), "name". + * Parameter $params could be a string in format: "prefix:name" or "prefix.special:name". * - * @param Array $params + * @param mixed $params + * @param Array $specific_params event specific params (none by default) * @return kEvent * @access public */ - function kEvent($params=Array(), $specificParams=null) + public function __construct($params = Array(), $specific_params = null) { - parent::kBase(); - if($params && is_array($params)) - { - $prefix = isset($params['prefix']) ? $params['prefix'] : false; - $special = isset($params['special']) ? $params['special'] : false; - if($prefix) $this->Init($prefix,$special); - $this->Name = getArrayValue($params,'name'); - } - elseif ($params && is_string($params)) { - if (preg_match('/([^.:]*)[.]{0,1}([^:]*):(.*)/', $params, $regs)) { - $prefix = $regs[1]; - $special = $regs[2]; - if($prefix) $this->Init($prefix,$special); - $this->Name = $regs[3]; + parent::__construct(); + + if ($params) { + if ( is_array($params) ) { + $prefix = isset($params['prefix']) ? $params['prefix'] : false; + $special = isset($params['special']) ? $params['special'] : false; + + if ($prefix) { + $this->Init($prefix, $special); + } + + $this->Name = isset($params['name']) ? $params['name'] : ''; } - else { - trigger_error('Invalid event string '.$params.' should be prefix[.special]:OnEvent ', E_USER_ERROR); + elseif ( is_string($params) ) { + if (preg_match('/([^.:]*)[.]{0,1}([^:]*):(.*)/', $params, $regs)) { + $prefix = $regs[1]; + $special = $regs[2]; + + if ($prefix) { + $this->Init($prefix, $special); + } + + $this->Name = $regs[3]; + } + else { + throw new Exception('Invalid event string: ' . $params . '. $params should be "prefix[.special]:OnEvent" format'); + } } } - if (isset($specificParams)) $this->specificParams = $specificParams; + + if ( isset($specific_params) ) { + $this->specificParams = $specific_params; + } } - function setEventParam($name,$value) + /** + * Returns joined prefix and special if any + * + * @param bool $from_submit if true, then joins prefix & special by "_", uses "." otherwise + * @return string + * @access public + */ + public function getPrefixSpecial($from_submit = false) { - $this->specificParams[$name]=$value; + if (!$from_submit) { + return parent::getPrefixSpecial(); + } + + return rtrim($this->Prefix . '_' . $this->Special, '_'); } - function getEventParam($name) + /** + * Sets event parameter + * + * @param string $name + * @param mixed $value + * @access public + */ + public function setEventParam($name,$value) { + $this->specificParams[$name] = $value; + } + + /** + * Returns event parameter by name (supports digging) + * + * @param string $name + * @return mixed + * @access public + */ + public function getEventParam($name) + { $args = func_get_args(); + if (count($args) > 1) { - array_unshift_ref($args, $this->specificParams); + kUtil::array_unshift_ref($args, $this->specificParams); return call_user_func_array('getArrayValue', $args); // getArrayValue($this->specificParams, $name); } - return isset($this->specificParams[$name]) ? $this->specificParams[$name] : false; - } - function getPrefixSpecial($from_submit=false) - { - $separator=!$from_submit?'.':'_'; - $ret=$this->Prefix.$separator.$this->Special; - return rtrim($ret,$separator); + return array_key_exists($name, $this->specificParams) ? $this->specificParams[$name] : false; } /** @@ -191,119 +234,161 @@ * @param string $appendix * @access public */ - function setPseudoClass($appendix) + public function setPseudoClass($appendix) { - $this->pseudoClass = $this->Prefix.$appendix; + $this->pseudoClass = $this->Prefix . $appendix; } - function Init($prefix, $special = '') + /** + * Performs event initialization + * Also sets pseudo class same $prefix + * + * @param string $prefix + * @param string $special + * @access public + */ + public function Init($prefix, $special) { - $this->Prefix = $prefix; - $this->pseudoClass = $prefix; // default value - $this->Special = $special; - $this->Prefix_Special = rtrim($this->Prefix.'.'.$this->Special,'.'); + $this->pseudoClass = $prefix; + + parent::Init($prefix, $special); } /** * Returns object used in event * - * @access public * @return kDBBase + * @access public */ - function &getObject($params = Array()) + public function &getObject(array $params = Array()) { - $object =& $this->Application->recallObject($this->Prefix_Special, $this->pseudoClass, $params); + $object =& $this->Application->recallObject($this->prefixSpecial, $this->pseudoClass, $params); + return $object; } /** - * Calls passed event by name in current prefix/special environment - * Called event gets this event as MasterEvent, - * but its results (status and redirect* properties are copied back to current event) + * Executes given event in context of current event + * Sub-event gets this event in "kEvent::MasterEvent" attribute. + * Sub-event execution results (status and redirect* properties) are copied back to current event. * - * @param string $name EventName to call + * @param string $name name of callable event (optionally could contain prefix_special as well) + * @see kEvent::MasterEvent */ - function CallSubEvent($name) + public function CallSubEvent($name) { - if (strpos($name, ':') === false) { - // PrefixSpecial not specified -> use from current event - $name = $this->getPrefixSpecial() . ':' . $name; - } + if ( strpos($name, ':') === false ) { + // PrefixSpecial not specified -> use from current event + $name = $this->getPrefixSpecial() . ':' . $name; + } - $child_event = new kEvent($name); - $child_event->MasterEvent =& $this; + $child_event = new kEvent($name); + $child_event->copyFrom($this, true); - $child_event->redirect = $this->redirect; - $child_event->redirect_params = $this->redirect_params; - $child_event->redirect_script = $this->redirect_script; - $child_event->specificParams = $this->specificParams; + $this->Application->HandleEvent($child_event); + $this->copyFrom($child_event); + $this->specificParams = $child_event->specificParams; + } - $this->Application->HandleEvent($child_event); + /** + * Allows to copy data between events + * + * @param kEvent $source_event + * @param bool $inherit + * @access public + */ + public function copyFrom(&$source_event, $inherit = false) + { + if ($inherit) { + $this->MasterEvent =& $source_event; + } + else { + $this->status = $source_event->status; + } - $this->status = $child_event->status; + $this->redirect = $source_event->redirect; + $this->redirectParams = $source_event->redirectParams; + $this->redirectScript = $source_event->redirectScript; + $this->specificParams = $source_event->specificParams; + } - $this->redirect = $child_event->redirect; - $this->redirect_params = $child_event->redirect_params; - $this->redirect_script = $child_event->redirect_script; - $this->specificParams = $child_event->specificParams; + /** + * Returns all redirect parameters + * + * @return Array + * @access public + */ + public function getRedirectParams() + { + return $this->redirectParams; } /** - * Set's redirect param for event + * Returns redirect parameter * * @param string $name - * @param string $value + * @return mixed * @access public */ - function SetRedirectParam($name, $value) + public function getRedirectParam($name) { - $this->redirect_params[$name] = $value; + return array_key_exists($name, $this->redirectParams) ? $this->redirectParams[$name] : false; } /** - * Allows to merge passed redirect params hash with existing ones + * Set's redirect param for event * - * @param Array $params + * @param string $name + * @param string $value * @access public */ - function setRedirectParams($params) + public function SetRedirectParam($name, $value) { - $this->redirect_params = array_merge_recursive2($this->redirect_params, $params); + $this->redirectParams[$name] = $value; } /** - * Returns Master event name if any + * Allows to merge passed redirect params hash with existing ones * - * @return mixed + * @param Array $params + * @param bool $overwrite * @access public */ - function hasMasterEvent() + public function setRedirectParams($params, $overwrite = false) { - return is_object($this->MasterEvent) ? $this->MasterEvent->Name : false; + if ($overwrite) { + $this->redirectParams = $params; + + return ; + } + + // append new parameters to parameters set before + $this->redirectParams = kUtil::array_merge_recursive($this->redirectParams, $params); } /** * Allows to tell if this event was called some how (e.g. subevent, hook) from event requested * * @param string $event_key event key in format [prefix[.special]:]event_name - * @return unknown + * @return bool + * @access public */ - function hasAncestor($event_key) + public function hasAncestor($event_key) { - $event_manager =& $this->Application->recallObject('EventManager'); - if (strpos($event_key, ':') === false) { - $event_key = $this->getPrefixSpecial().':'.$event_key; + if ( strpos($event_key, ':') === false ) { + $event_key = $this->getPrefixSpecial() . ':' . $event_key; } - return $event_manager->eventRunning($event_key); + return $this->Application->EventManager->eventRunning($event_key); } /** - * Returns section for current event + * Returns permission section associated with event * * @return string + * @access public */ - function getSection() + public function getSection() { $perm_section = $this->getEventParam('PermSection'); if ($perm_section) { @@ -329,12 +414,14 @@ } if (!$section) { - if ($this->Application->isDebugMode()) { - $this->Application->Debugger->appendTrace(); - } - trigger_error('Permission section not specified for prefix '.$top_prefix.'', E_USER_ERROR); + throw new Exception('Permission section not specified for prefix ' . $top_prefix . ''); } + return $section; } + public function __toString() + { + return $this->getPrefixSpecial() . ':' . $this->Name; + } } \ No newline at end of file