PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/e107_handlers/event_class.php

https://github.com/CasperGemini/e107
PHP | 237 lines | 153 code | 26 blank | 58 comment | 28 complexity | 860a6bb54bea02343fd821e25fa15074 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2012 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. * $URL$
  10. * $Id$
  11. */
  12. if (!defined('e107_INIT')) { exit; }
  13. class e107_event
  14. {
  15. var $functions = array();
  16. var $includes = array();
  17. /**
  18. * Register event
  19. *
  20. * @param string $eventname
  21. * @param array|string $function [class_name, method_name] or function name
  22. * @param string $include [optional] include path
  23. * @return void
  24. */
  25. function register($eventname, $function, $include='')
  26. {
  27. $this->includes[$eventname] = array();
  28. if(!isset($this->functions[$eventname]) || !in_array($function, $this->functions[$eventname]))
  29. {
  30. if (!empty($include))
  31. {
  32. $this->includes[$eventname][] = $include;
  33. }
  34. $this->functions[$eventname][] = $function;
  35. }
  36. }
  37. function debug()
  38. {
  39. print_a($this->functions);
  40. print_a($this->includes);
  41. }
  42. /**
  43. * Trigger event
  44. * TODO - admin log for failed callback attempts?
  45. *
  46. * @param string $eventname
  47. * @param mixed $data
  48. * @return mixed
  49. */
  50. function trigger($eventname, $data='')
  51. {
  52. /*if (isset($this->includes[$eventname]))
  53. {
  54. foreach($this->includes[$eventname] as $evt_inc)
  55. {
  56. if (file_exists($evt_inc))
  57. {
  58. include_once($evt_inc);
  59. }
  60. }
  61. }*/
  62. if (isset($this->functions[$eventname]))
  63. {
  64. foreach($this->functions[$eventname] as $i => $evt_func)
  65. {
  66. $location = '';
  67. if(isset($this->includes[$eventname][$i])) //no checks
  68. {
  69. $location = $this->includes[$eventname][$i];
  70. e107_include_once($location);
  71. unset($this->includes[$eventname][$i]);
  72. }
  73. if(is_array($evt_func)) //class, method
  74. {
  75. $class = $evt_func[0];
  76. $method = $evt_func[1];
  77. try
  78. {
  79. $tmp = new $class($eventname);
  80. $ret = $tmp->{$method}($data, $eventname); //let callback know what event is calling it
  81. unset($tmp);
  82. if (!empty($ret))
  83. {
  84. break;
  85. }
  86. }
  87. catch(Exception $e)
  88. {
  89. //TODO log errors $eventname, $location, $class, $method
  90. // echo "event didn't work. Class=".$class." Method=".$method;
  91. // echo "<br />".$e;
  92. //exit;
  93. continue;
  94. }
  95. }
  96. elseif (function_exists($evt_func))
  97. {
  98. $ret = $evt_func($data, $eventname); //let callback know what event is calling it
  99. if (!empty($ret))
  100. {
  101. break;
  102. }
  103. }
  104. //TODO log errors $eventname, $location, $evt_func
  105. }
  106. }
  107. return (isset($ret) ? $ret : false);
  108. }
  109. function triggerAdminEvent($type, $parms=array())
  110. {
  111. global $pref;
  112. if(!is_array($parms))
  113. {
  114. $_tmp = parse_str($parms, $parms);
  115. }
  116. if(isset($pref['e_admin_events_list']) && is_array($pref['e_admin_events_list']))
  117. {
  118. // $called = getcachedvars('admin_events_called');
  119. $called = e107::getRegistry('core/cachedvars/admin_events_called', false);
  120. if(!is_array($called)) { $called = array(); }
  121. foreach($pref['e_admin_events_list'] as $plugin)
  122. {
  123. if(e107::isInstalled($plugin))
  124. {
  125. $func = 'plugin_'.$plugin.'_admin_events';
  126. if(!function_exists($func))
  127. {
  128. $fname = e_PLUGIN.$plugin.'/e_admin_events.php';
  129. if(is_readable($fname)) { include_once($fname); }
  130. }
  131. if(function_exists($func))
  132. {
  133. $event_func = call_user_func($func, $type, $parms);
  134. if ($event_func && function_exists($event_func) && !in_array($event_func, $called))
  135. {
  136. $called[] = $event_func;
  137. // cachevars('admin_events_called', $called);
  138. e107::setRegistry('core/cachedvars/admin_events_called', $called);
  139. call_user_func($event_func);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /*
  147. * triggerHook trigger a hooked in element
  148. * four methods are allowed hooks: form, create, update, delete
  149. * form : return array('caption'=>'', 'text'=>'');
  150. * create, update, delete : return string message
  151. * @param array $data array containing
  152. * @param string $method form,insert,update,delete
  153. * @param string $table the table name of the calling plugin
  154. * @param int $id item id of the record
  155. * @param string $plugin identifier for the calling plugin
  156. * @param string $function identifier for the calling function
  157. * @return string $text string of rendered html, or message from db handler
  158. */
  159. function triggerHook($data='')
  160. {
  161. $text = '';
  162. $e_event_list = e107::getPref('e_event_list');
  163. if(is_array($e_event_list))
  164. {
  165. foreach($e_event_list as $hook)
  166. {
  167. if(e107::isInstalled($hook))
  168. {
  169. if(is_readable(e_PLUGIN.$hook."/e_event.php"))
  170. {
  171. require_once(e_PLUGIN.$hook."/e_event.php");
  172. $name = "e_event_{$hook}";
  173. if(class_exists($name))
  174. {
  175. $class = new $name();
  176. switch($data['method'])
  177. {
  178. //returns array('caption'=>'', 'text'=>'');
  179. case 'form':
  180. if(method_exists($class, "event_{$data['method']}"))
  181. {
  182. $ret = $class->event_form($data);
  183. if(!isset($ret[0]))
  184. {
  185. $text[$hook][0] = $ret;
  186. }
  187. else
  188. {
  189. $text[$hook] = $ret;
  190. }
  191. }
  192. break;
  193. //returns string message
  194. case 'create':
  195. case 'update':
  196. case 'delete':
  197. if(method_exists($class, "event_{$data['method']}"))
  198. {
  199. $text .= call_user_func(array($class, "event_{$data['method']}"), $data);
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. return $text;
  209. }
  210. }
  211. ?>