Init($prefix, $special);
}
$this->Name = $regs[3];
$this->_specificParams = $specific_params;
}
else {
$error_msg = 'Invalid event string: "' . $event_string . '". ';
$error_msg .= 'Should be in "prefix[.special]:OnEvent" format';
throw new InvalidArgumentException($error_msg);
}
}
/**
* Returns joined prefix and special if any.
*
* @param boolean $from_submit If true, then joins prefix & special by "_", uses "." otherwise.
*
* @return string
*/
public function getPrefixSpecial($from_submit = false)
{
if ( !$from_submit ) {
return parent::getPrefixSpecial();
}
return rtrim($this->Prefix . '_' . $this->Special, '_');
}
/**
* Sets event parameter.
*
* @param string $name Name.
* @param mixed $value Value.
*
* @return void
*/
public function setEventParam($name, $value)
{
$this->_specificParams[$name] = $value;
}
/**
* Returns event parameter by name (supports digging).
*
* @param string $name Name.
*
* @return mixed
*/
public function getEventParam($name)
{
$args = func_get_args();
if ( count($args) > 1 ) {
kUtil::array_unshift_ref($args, $this->_specificParams);
return call_user_func_array('getArrayValue', $args);
}
return array_key_exists($name, $this->_specificParams) ? $this->_specificParams[$name] : false;
}
/**
* Returns all event parameters.
*
* @return array
*/
public function getEventParams()
{
return $this->_specificParams;
}
/**
* Set's pseudo class that differs from the one specified in $Prefix.
*
* @param string $appendix Appendix.
*
* @return void
*/
public function setPseudoClass($appendix)
{
$this->_pseudoClass = $this->Prefix . $appendix;
}
/**
* Performs event initialization.
*
* Also sets pseudo class same $prefix.
*
* @param string $prefix Prefix.
* @param string $special Special.
*
* @return void
*/
public function Init($prefix, $special)
{
$this->_pseudoClass = $prefix;
parent::Init($prefix, $special);
}
/**
* Returns object used in event.
*
* @param array $params Params.
*
* @return kDBBase
*/
public function getObject(array $params = array())
{
if ( !$this->Application->hasObject($this->prefixSpecial) ) {
$top_event = $this;
// When OnSave calls OnPreSave in first line, then this would make sure OnSave is used.
while ( is_object($top_event->MasterEvent) ) {
$top_event = $top_event->MasterEvent;
}
$params['parent_event'] = $top_event;
}
return $this->Application->recallObject($this->prefixSpecial, $this->_pseudoClass, $params);
}
/**
* 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 Name of callable event (optionally could contain prefix_special as well).
*
* @return void
* @see kEvent::MasterEvent
* @todo Overwrites master event data with called event data, which makes 'parent_event' useless in most cases.
*/
public function CallSubEvent($name)
{
if ( strpos($name, ':') === false ) {
// PrefixSpecial not specified -> use from current event.
$name = $this->getPrefixSpecial() . ':' . $name;
}
$child_event = new kEvent($name);
$child_event->copyFrom($this, true);
$this->Application->HandleEvent($child_event);
$this->copyFrom($child_event);
$this->_specificParams = $child_event->_specificParams;
}
/**
* Allows to copy data between events.
*
* @param kEvent $source_event Source event.
* @param boolean $inherit Keep reference to source event in copied event.
*
* @return void
*/
public function copyFrom(kEvent $source_event, $inherit = false)
{
if ( $inherit ) {
$this->MasterEvent = $source_event;
}
else {
$this->status = $source_event->status;
}
$this->redirect = $source_event->redirect;
$this->_redirectParams = $source_event->_redirectParams;
$this->redirectScript = $source_event->redirectScript;
$this->_specificParams = $source_event->_specificParams;
}
/**
* Returns all redirect parameters.
*
* @return array
*/
public function getRedirectParams()
{
return $this->_redirectParams;
}
/**
* Returns redirect parameter.
*
* @param string $name Name.
*
* @return mixed
*/
public function getRedirectParam($name)
{
return array_key_exists($name, $this->_redirectParams) ? $this->_redirectParams[$name] : false;
}
/**
* Set's redirect param for event.
*
* @param string $name Name.
* @param string $value Value.
*
* @return void
*/
public function SetRedirectParam($name, $value)
{
$this->_redirectParams[$name] = $value;
}
/**
* Allows to merge passed redirect params hash with existing ones.
*
* @param array $params Params.
* @param boolean $append Append to existing parameters.
*
* @return void
*/
public function setRedirectParams(array $params, $append = true)
{
if ( $append ) {
$params = kUtil::array_merge_recursive($this->_redirectParams, $params);
}
$this->_redirectParams = $params;
}
/**
* Allows to tell if this event was called some how (e.g. sub-event, hook) from event requested.
*
* @param string $event_key Event key in format: "[prefix[.special]:]event_name".
*
* @return boolean
*/
public function hasAncestor($event_key)
{
if ( strpos($event_key, ':') === false ) {
$event_key = $this->getPrefixSpecial() . ':' . $event_key;
}
return $this->Application->EventManager->eventRunning($event_key);
}
/**
* Returns permission section associated with event.
*
* @return string
* @throws Exception When permission section not specified for event's unit.
*/
public function getSection()
{
$perm_section = $this->getEventParam('PermSection');
if ( $perm_section ) {
return $perm_section;
}
// 1. get section by current top_prefix.
$top_prefix = $this->getEventParam('top_prefix');
if ( $top_prefix == false ) {
$top_prefix = $this->Application->GetTopmostPrefix($this->Prefix, true);
$this->setEventParam('top_prefix', $top_prefix);
}
$section = $this->Application->getUnitConfig($top_prefix)->getPermSectionByName('main');
// 2. check if this section has perm_prefix mapping to other prefix.
/** @var kSectionsHelper $sections_helper */
$sections_helper = $this->Application->recallObject('SectionsHelper');
$section_data =& $sections_helper->getSectionData($section);
if ( $section_data && isset($section_data['perm_prefix']) && $section_data['perm_prefix'] != $top_prefix ) {
$this->setEventParam('top_prefix', $section_data['perm_prefix']);
$section = $this->Application->getUnitConfig($section_data['perm_prefix'])->getPermSectionByName('main');
}
if ( !$section ) {
$error_msg = 'Permission section not specified for';
$error_msg .= ' prefix ' . $top_prefix . '';
throw new Exception($error_msg);
}
return $section;
}
/**
* Casts object to string.
*
* @return string
*/
public function __toString()
{
return $this->getPrefixSpecial() . ':' . $this->Name;
}
}