1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-18 05:09:05 +01:00
php-e107/e107_handlers/event_class.php

209 lines
5.3 KiB
PHP
Raw Normal View History

2006-12-02 04:36:16 +00:00
<?php
/*
+ ----------------------------------------------------------------------------+
| e107 website system
|
2009-09-25 20:18:34 +00:00
| Steve Dunstan 2001-2002
2006-12-02 04:36:16 +00:00
| http://e107.org
| jalist@e107.org
|
| Released under the terms and conditions of the
| GNU General Public License (http://gnu.org).
|
| $Source: /cvs_backup/e107_0.8/e107_handlers/event_class.php,v $
2009-09-25 20:18:34 +00:00
| $Revision: 1.9 $
| $Date: 2009-09-25 20:18:34 $
| $Author: secretr $
2006-12-02 04:36:16 +00:00
+----------------------------------------------------------------------------+
*/
2006-12-02 04:36:16 +00:00
if (!defined('e107_INIT')) { exit; }
class e107_event
{
2006-12-02 04:36:16 +00:00
var $functions = array();
var $includes = array();
/**
* Register event
*
* @param string $eventname
* @param array|string $function [class_name, method_name] or function name
* @param string $include [optional] include path
* @return void
*/
function register($eventname, $function, $include='')
{
$this->includes[$eventname] = array();
if(!isset($this->functions[$eventname]) || !in_array($function, $this->functions[$eventname]))
{
if (!empty($include))
{
$this->includes[$eventname][] = $include;
}
$this->functions[$eventname][] = $function;
2006-12-02 04:36:16 +00:00
}
}
/**
* Trigger event
* TODO - admin log for failed callback attempts?
*
* @param string $eventname
* @param mixed $data
* @return mixed
*/
function trigger($eventname, &$data)
{
/*if (isset($this->includes[$eventname]))
{
foreach($this->includes[$eventname] as $evt_inc)
{
if (file_exists($evt_inc))
{
2006-12-02 04:36:16 +00:00
include_once($evt_inc);
}
}
}*/
if (isset($this->functions[$eventname]))
{
foreach($this->functions[$eventname] as $i => $evt_func)
{
$location = '';
if(isset($this->includes[$eventname][$i])) //no checks
{
$location = $this->includes[$eventname][$i];
e107_include_once($location);
unset($this->includes[$eventname][$i]);
}
if(is_array($evt_func)) //class, method
{
try
{
$class = $evt_func[0];
$method = $evt_func[1];
$tmp = new $class($eventname);
$ret = $tmp->{$method}($data, $eventname); //let callback know what event is calling it
unset($tmp);
if (!empty($ret))
{
break;
}
}
catch(Exception $e)
{
//TODO log errors $eventname, $location, $class, $method
}
}
if (function_exists($evt_func))
{
$ret = $evt_func($data, $eventname); //let callback know what event is calling it
if (!empty($ret))
{
2006-12-02 04:36:16 +00:00
break;
}
}
//TODO log errors $eventname, $location, $evt_func
2006-12-02 04:36:16 +00:00
}
}
return (isset($ret) ? $ret : false);
}
function triggerAdminEvent($type, $parms=array())
{
global $pref;
if(!is_array($parms))
{
$_tmp = parse_str($parms, $parms);
}
if(isset($pref['e_admin_events_list']) && is_array($pref['e_admin_events_list']))
{
2009-09-17 00:13:40 +00:00
// $called = getcachedvars('admin_events_called');
$called = e107::getRegistry('core/cachedvars/admin_events_called', false);
if(!is_array($called)) { $called = array(); }
foreach($pref['e_admin_events_list'] as $plugin)
{
2009-09-17 00:13:40 +00:00
if(e107::isInstalled($plugin))
{
2008-12-03 00:48:19 +00:00
$func = 'plugin_'.$plugin.'_admin_events';
if(!function_exists($func))
{
2008-12-03 00:48:19 +00:00
$fname = e_PLUGIN.$plugin.'/e_admin_events.php';
if(is_readable($fname)) { include_once($fname); }
2008-12-03 00:48:19 +00:00
}
if(function_exists($func))
{
$event_func = call_user_func($func, $type, $parms);
if ($event_func && function_exists($event_func) && !in_array($event_func, $called))
{
$called[] = $event_func;
2009-09-17 00:13:40 +00:00
// cachevars('admin_events_called', $called);
e107::setRegistry('core/cachedvars/admin_events_called', $called);
call_user_func($event_func);
}
}
}
}
}
}
/*
* triggerHook trigger a hooked in element
* four methods are allowed hooks: form, create, update, delete
* form : return array('caption'=>'', 'text'=>'');
* create, update, delete : return string message
* @param array $data array containing
* @param string $method form,insert,update,delete
* @param string $table the table name of the calling plugin
* @param int $id item id of the record
* @param string $plugin identifier for the calling plugin
* @param string $function identifier for the calling function
* @return string $text string of rendered html, or message from db handler
*/
function triggerHook($data='')
{
2009-09-25 20:18:34 +00:00
$text = '';
$e_event_list = e107::getPref('e_event_list');
if(is_array($e_event_list))
{
2009-09-25 20:18:34 +00:00
foreach($e_event_list as $hook)
{
2009-09-17 00:13:40 +00:00
if(e107::isInstalled($hook))
{
2009-01-22 23:14:48 +00:00
if(is_readable(e_PLUGIN.$hook."/e_event.php"))
{
2009-01-22 23:14:48 +00:00
require_once(e_PLUGIN.$hook."/e_event.php");
$name = "e_event_{$hook}";
if(class_exists($name))
{
2009-01-22 23:14:48 +00:00
$class = new $name();
switch($data['method'])
{
//returns array('caption'=>'', 'text'=>'');
case 'form':
if(method_exists($class, "event_{$data['method']}"))
{
$text[] = $class->event_form($data);
}
break;
//returns string message
case 'create':
case 'update':
case 'delete':
if(method_exists($class, "event_{$data['method']}"))
{
$text .= call_user_func(array($class, "event_{$data['method']}"), $data);
}
break;
}
}
}
}
}
}
return $text;
}
2006-12-02 04:36:16 +00:00
}
2006-12-02 04:36:16 +00:00
?>