e107 /e107_handlers/event_class.php

Language PHP Lines 237
MD5 Hash 860a6bb54bea02343fd821e25fa15074
Repository https://github.com/CasperGemini/e107.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
 * e107 website system
 *
 * Copyright (C) 2008-2012 e107 Inc (e107.org)
 * Released under the terms and conditions of the
 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
 *
 * $URL$
 * $Id$
 */


if (!defined('e107_INIT')) { exit; }

class e107_event
{
	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;
		}
	}



	function debug()
	{
		
		print_a($this->functions);
		print_a($this->includes);	
		
	}


	/**
	 * 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))
				{
					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
				{
					$class = $evt_func[0];
					$method = $evt_func[1];
						
					try
					{
					
						$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
					//	echo "event didn't work. Class=".$class." Method=".$method;
					//	echo "<br />".$e;
						//exit;
						continue;
					}
				}
				elseif (function_exists($evt_func))
				{
					$ret = $evt_func($data, $eventname); //let callback know what event is calling it
					if (!empty($ret))
					{
						break;
					}
				}
				//TODO log errors $eventname, $location, $evt_func
			}
		}
		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']))
		{
			// $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)
			{
				if(e107::isInstalled($plugin))
				{
					$func = 'plugin_'.$plugin.'_admin_events';
					if(!function_exists($func))
					{
						$fname = e_PLUGIN.$plugin.'/e_admin_events.php';
						if(is_readable($fname)) { include_once($fname); }
					}
					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;
							// 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='')
	{
		$text = ''; 
		$e_event_list = e107::getPref('e_event_list');
		
		if(is_array($e_event_list))
		{
			foreach($e_event_list as $hook)
			{
				if(e107::isInstalled($hook))
				{
					if(is_readable(e_PLUGIN.$hook."/e_event.php"))
					{
						require_once(e_PLUGIN.$hook."/e_event.php");
						$name = "e_event_{$hook}";
						if(class_exists($name))
						{
							$class = new $name();
							
							switch($data['method'])
							{
								//returns array('caption'=>'', 'text'=>'');
								case 'form':
									if(method_exists($class, "event_{$data['method']}"))
									{
										$ret = $class->event_form($data);
										
										if(!isset($ret[0]))
										{
											$text[$hook][0] = $ret;		
										}
										else 
										{
											$text[$hook] = $ret;
										}
										
										
									}
									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;
	}
}

?>
Back to Top