PageRenderTime 2ms CodeModel.GetById 55ms app.highlight 10ms RepoModel.GetById 99ms app.codeStats 0ms

/components/com_fabrik/models/pluginmanager.php

https://github.com/danimara/fabrik
PHP | 438 lines | 297 code | 51 blank | 90 comment | 48 complexity | 42e34a3a6065c1900b8104d992eaf84e MD5 | raw file
  1<?php
  2/**
  3 * @package Joomla
  4 * @subpackage Fabrik
  5 * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
  6 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7 */
  8
  9// Check to ensure this file is included in Joomla!
 10defined('_JEXEC') or die();
 11
 12jimport('joomla.application.component.model');
 13jimport('joomla.filesystem.file');
 14
 15class FabrikFEModelPluginmanager extends JModel{
 16
 17	/** @var array plugins */
 18	var $_plugIns = array();
 19	var $_loading = null;
 20	var $_group = null;
 21	var $_runPlugins = 0;
 22
 23	var $_paths = array();
 24
 25	/** @var array element lists */
 26	var $_elementLists = array();
 27
 28	/** @var array containing out put from run plugins */
 29	var $_data = array();
 30
 31	/**
 32	 * constructor
 33	 */
 34
 35	function __construct()
 36	{
 37		parent::__construct();
 38	}
 39
 40	/**
 41	 * get a html drop down list of the elment types with this objs element type selected as default
 42	 * @param string default selected option
 43	 * @param string html name for drop down
 44	 * @param string extra info for drop down
 45	 * @return string html element type list
 46	 */
 47
 48	function getElementTypeDd($default, $name='plugin', $extra='class="inputbox elementtype"  size="1"', $defaultlabel='')
 49	{
 50		$hash = $default.$name.$extra.$defaultlabel;
 51		if (!array_key_exists($hash, $this->_elementLists)) {
 52			if ($defaultlabel == '') {
 53				$defaultlabel = JText::_('COM_FABRIK_PLEASE_SELECT');
 54			}
 55			$a = array(JHTML::_('select.option', '', $defaultlabel));
 56			$elementstypes = $this->_getList();
 57			$elementstypes = array_merge($a, $elementstypes);
 58			$this->_elementLists[$hash] = JHTML::_('select.genericlist',  $elementstypes, $name, $extra , 'value', 'text', $default);
 59		}
 60		return $this->_elementLists[$hash];
 61	}
 62
 63	function canUse()
 64	{
 65		return true;
 66	}
 67
 68	/**
 69	 * get an unordered list of plugins
 70	 * @param string plugin group
 71	 * @param string ul id
 72	 */
 73
 74	function getList($group, $id)
 75	{
 76		$str = "<ul id='$id'>";
 77		$elementstypes = $this->_getList();
 78		foreach ($elementstypes as $plugin) {
 79			$str .= "<li>" . $plugin->text . "</li>";
 80		}
 81		$str .= "</ul>";
 82		return $str;
 83	}
 84
 85	/**
 86	 * get a list of plugin ids/names for usin in a drop down list
 87	 * if no group set defaults to element list
 88	 * @return array plugin list
 89	 */
 90
 91	function _getList()
 92	{
 93		$db = FabrikWorker::getDbo();
 94		if (is_null($this->_group)) {
 95			$this->_group = 'element';
 96		}
 97		$query = $db->getQuery(true);
 98		$folder = $db->Quote('fabrik_'.$this->_group);
 99		//$query->select('name AS value, element AS text')->from('#__extensions')->where('folder ='.$folder);
100		$query->select('element AS value, name AS text')->from('#__extensions')->where('folder ='.$folder);
101		$db->setQuery($query);
102		$elementstypes = $db->loadObjectList();
103		return $elementstypes;
104	}
105
106	/**
107	 * get a certain group of plugins
108	 * @param string plugin group to load
109	 * @return array plugins
110	 */
111
112	function &getPlugInGroup($group)
113	{
114		if (array_key_exists($group, $this->_plugIns))
115		{
116			return $this->_plugIns[$group];
117		} else {
118			return $this->loadPlugInGroup($group);
119		}
120	}
121
122	/**
123	 * add to the document head all element js files
124	 * used in calendar to ensure all element js files are loaded from unserialized form
125	 */
126
127	function loadJS()
128	{
129		//JHtml::_('script', 'https://raw.github.com/headjs/headjs/v0.96/dist/head.load.min.js');
130		JHtml::_('script', 'media/com_fabrik/js/head/head.min.js');
131		$plugins = JFolder::folders(JPATH_SITE . '/plugins/fabrik_element', '.', false, false);
132		$files = array();
133		foreach ($plugins as $plugin) {
134			$files[] =  JPATH_SITE . '/plugins/fabrik_element/'.$plugin.'/'.$plugin.'.js';
135		}
136		foreach ($files as $f) {
137			$f =  str_replace("\\", "/", str_replace(JPATH_SITE, '', $f));
138			$file = basename($f);
139			$folder = dirname($f);
140			$folder = FabrikString::ltrimword($folder, '/') .'/';
141			FabrikHelperHTML::script($folder.$file, true);
142		}
143	}
144
145	/**
146	 *@param string plugin type - element/form/table/validationrule supported
147	 *loads ABSTRACT version of a plugin group
148	 */
149
150	function &loadPlugInGroup($group)
151	{
152		$folder = 'fabrik_'.$group;
153		$this->_plugIns[$group] = array();
154		$plugins = JPluginHelper::getPlugin($folder);
155		foreach($plugins as $plugin) {
156			$this->_plugIns[$group][$plugin->name] = $plugin;
157		}
158		return $this->_plugIns[$group];
159	}
160
161	/**
162	 * @param string plugin name e.g. fabrikfield
163	 * @param string plugin type element/ form or table
164	 */
165
166	function getPlugIn($className = '', $group)
167	{
168		if ($className != '' && (array_key_exists($group, $this->_plugIns) && array_key_exists($className, $this->_plugIns[$group]))) {
169			return $this->_plugIns[$group][$className];
170		} else {
171			// $$$ rob 04/06/2011 hmm this was never caching the plugin so we were always loading it
172			//return $this->loadPlugIn($className, $group);
173			$this->_plugIns[$group][$className] = $this->loadPlugIn($className, $group);
174			return $this->_plugIns[$group][$className];
175		}
176	}
177
178	/**
179	 * @param string plugin name e.g. fabrikfield
180	 * @param string plugin type element/ form or table
181	 * @return mixed false if not loaded - otherwise plugin object
182	 */
183
184	protected function loadPlugIn($className = '', $group)
185	{
186		if ($group == 'table'){
187			$group = 'list';
188		}
189		$ok = JPluginHelper::importPlugin('fabrik_'.strtolower($group));
190
191		$dispatcher = JDispatcher::getInstance();
192		if ($className != '') {
193			if (JFile::exists(JPATH_PLUGINS.DS.'fabrik_'.strtolower($group).DS.$className.DS.$className.'.php')) {
194				require_once(JPATH_PLUGINS.DS.'fabrik_'.strtolower($group).DS.$className.DS.$className.'.php');
195			} else {
196				if (JFile::exists((JPATH_PLUGINS.DS.'fabrik_'.strtolower($group).DS.$className.DS.'models'.DS.$className.'.php'))) {
197					require_once(JPATH_PLUGINS.DS.'fabrik_'.strtolower($group).DS.$className.DS.'models'.DS.$className.'.php');
198				} else {
199					return false;
200				}
201			}
202		}
203		$class = 'plgFabrik_'.JString::ucfirst($group).JString::ucfirst($className);
204		$conf = array();
205		$conf['name'] = strtolower($className);
206		$conf['type'] = strtolower('fabrik_'.$group);
207		$plugIn = new $class($dispatcher, $conf);
208
209		//needed for viz
210		$client	= JApplicationHelper::getClientInfo(0);
211		$lang = JFactory::getLanguage();
212		$folder = 'fabrik_'.strtolower($group);
213		$lang->load('plg_'.$folder.'_'.$className, $client->path.'/plugins/'.$folder.'/'.$className, null, false, false)
214				 ||	$lang->load('plg_'.$folder.'_'.$className, $client->path.'/plugins/'.$folder.'/'.$className, $lang->getDefault(), false, false);
215		return $plugIn;
216	}
217
218	/**
219	 * load all the forms element plugins
220	 *
221	 * @param object form model
222	 * @return array of group objects with plugin objects loaded in group->elements
223	 */
224
225	function getFormPlugins(&$form)
226	{
227		global $_PROFILER;
228		if (!isset($this->formplugins)) {
229			$this->formplugins = array();
230		}
231		$sig = $form->get('id');
232		JDEBUG ? $_PROFILER->mark('pluginmanager:getFormPlugins:start - '.$sig) : null;
233		if (!array_key_exists($sig, $this->formplugins)) {
234			$this->formplugins[$sig] = array();
235			$lang = JFactory::getLanguage();
236			$folder = 'fabrik_element';
237			$client	= JApplicationHelper::getClientInfo(0);
238			$groupIds = $form->getGroupIds();
239			if (empty($groupIds)) { //new form
240				return array();
241			}
242			$db = FabrikWorker::getDbo(true);
243			$query = $db->getQuery(true);
244			$query->select('*, e.name AS name, e.id AS id, e.published AS published, e.label AS label, e.plugin, e.params AS params, e.access AS access');
245			$query->from('#__{package}_elements AS e');
246			$query->join('INNER', '#__extensions AS p ON p.element = e.plugin');
247			$query->where('group_id IN (' . implode(',', $groupIds) . ')');
248			$query->where('p.folder = "fabrik_element"');
249			$query->where('e.published != -2'); // ignore trashed elements
250			$query->order("group_id, e.ordering");
251			$db->setQuery($query);
252			$elements = (array)$db->loadObjectList();
253
254			if ($db->getErrorNum()) {
255				JError::raiseError(500, $db->getErrorMsg());
256			}
257
258			//dont assign the elements into Joomla's main dispatcher as this causes out of memory errors in J1.6rc1
259			//$dispatcher =& JDispatcher::getInstance();
260			$dispatcher = new JDispatcher();
261			$groupModels = $form->getGroups();
262			$group = 'element';
263			foreach ($elements as $element) {
264				JDEBUG ? $_PROFILER->mark('pluginmanager:getFormPlugins:'.$element->id . ''.$element->plugin) : null;
265					require_once(JPATH_PLUGINS.DS.'fabrik_element'.DS.$element->plugin.DS.$element->plugin.'.php');
266					$class = 'plgFabrik_Element'.$element->plugin;
267					$pluginModel = new $class($dispatcher, array());
268					if (!is_object($pluginModel)) {
269						continue;
270					}
271
272				 $pluginModel->_xmlPath = COM_FABRIK_FRONTEND.DS.'plugins'.DS.$group.DS.$element->plugin.DS.$element->plugin.'.xml';
273
274				 $pluginModel->setId($element->id);
275				 $groupModel =& $groupModels[$element->group_id];
276
277				 $lang->load('plg_'.$folder.'_'.$element->plugin, $client->path.'/plugins/'.$folder.'/'.$element->plugin, null, false, false)
278				 ||	$lang->load('plg_'.$folder.'_'.$element->plugin, $client->path.'/plugins/'.$folder.'/'.$element->plugin, $lang->getDefault(), false, false);
279
280				 $pluginModel->setContext($groupModel, $form, $form->_table);
281				 $pluginModel->bindToElement($element);
282				 $groupModel->elements[$pluginModel->_id] = $pluginModel;
283
284			}
285
286			foreach ($groupModels as $groupid => $g) {
287				$this->formplugins[$sig][$groupid] = $g;
288			}
289		}
290		return $this->formplugins[$sig];
291	}
292
293	function getElementPlugin($id)
294	{
295		$el = FabTable::getInstance('Element', 'FabrikTable');
296		$el->load($id);
297		$o = $this->loadPlugIn($el->plugin, 'Element');
298		$o->setId($id);
299		$o->getElement();
300		return $o;
301	}
302
303	/**
304	 * @param string name of plugin group to load
305	 * @param array list of default element lists
306	 * @param array list of default and plugin element lists
307	 */
308
309	function loadLists($group, $lists, &$elementModel)
310	{
311		if (empty($this->_plugIns)) {
312			$this->loadPlugInGroup($group);
313		}
314		foreach ($this->_plugIns[$group] as $plugIn) {
315			if (method_exists($plugIn->object, 'getAdminLists')) {
316				$lists = $plugIn->object->getAdminLists($lists, $elementModel, $plugIn->params);
317			}
318		}
319		return $lists;
320	}
321
322	/**
323	 * run form & element plugins - yeah!
324	 * @param string method to check and call - corresponds to stage of form processing
325	 * @param object model calling the plugin form/table
326	 * @param string plugin type to call form/table
327	 * @return array of bools: false if error found and processed, otherwise true
328	 */
329
330	function runPlugins($method, &$oRequest, $type = 'form')
331	{
332		if ($type == 'form') {
333			// $$$ rob allow for table plugins to hook into form plugin calls - methods are mapped as:
334			//form method = 'onLoad' => table method => 'onFormLoad'
335			$tmethod = 'onForm'.FabrikString::ltrimword($method, 'on');
336			$this->runPlugins($tmethod, $oRequest->getListModel(), 'list');
337		}
338		$params =& $oRequest->getParams();
339		//$this->getPlugInGroup($type);
340		$return = array();
341		$usedPlugins = (array)$params->get('plugins');
342		$usedLocations = (array)$params->get('plugin_locations');
343		$usedEvents = (array)$params->get('plugin_events');
344		$this->_data = array();
345
346		if ($type != 'list') {
347
348			if (method_exists($oRequest, 'getGroupsHiarachy')) {
349				$groups =& $oRequest->getGroupsHiarachy();
350				foreach ($groups as $groupModel) {
351					$elementModels =& $groupModel->getPublishedElements();
352					foreach ($elementModels as $elementModel) {
353						if (method_exists($elementModel, $method)) {
354							$elementModel->$method($oRequest);
355						}
356					}
357				}
358			}
359		}
360		$c = 0;
361		$runPlugins = 0;
362		// if true then a plugin has returned true from runAway() which means that any other plugin in the same group
363		// should not be run.
364		$runningAway = false;
365
366		foreach ($usedPlugins as $usedPlugin) {
367			if ($runningAway) {
368				// "I soiled my armour I was so scared!"
369				break;
370			}
371			if ($usedPlugin != '') {
372				$plugin = $this->getPlugIn($usedPlugin, $type);
373				//testing this if statement as onLoad was being called on form email plugin when no method availbale
374				$plugin->renderOrder = $c;
375				if (method_exists($plugin, $method)) {
376					$modelTable = $oRequest->getTable();
377					$pluginParams =& $plugin->setParams($params, $c);
378
379					$location = JArrayHelper::getValue($usedLocations, $c);
380					$event = JArrayHelper::getValue($usedEvents, $c);
381					if ($plugin->canUse($oRequest, $location, $event) && method_exists($plugin, $method)) {
382						$pluginArgs = array();
383						if (func_num_args() > 3) {
384							$t =& func_get_args();
385							$pluginArgs =& array_splice($t, 3);
386						}
387						$preflightMethod = $method."_preflightCheck";
388						$preflightCheck = method_exists($plugin, $preflightMethod) ? $plugin->$preflightMethod($pluginParams, $oRequest, $pluginArgs) : true;
389						if ($preflightCheck) {
390							$ok = $plugin->$method($pluginParams, $oRequest, $pluginArgs);
391							if ($ok === false) {
392								$return[] = false;
393							} else {
394								$thisreturn = $plugin->customProcessResult($method, $oRequest);
395								$return[] = $thisreturn;
396								$m = $method.'_result';
397								if (method_exists($plugin, $m)) {
398									$this->_data[] = $plugin->$m($c);
399								}
400							}
401							$runPlugins ++;
402
403							if ($plugin->runAway($method)) {
404								$runningAway = true;
405							}
406							$mainData = $this->_data;
407							if ($type == 'list' && $method !== 'observe') {
408								$this->runPlugins('observe', $oRequest, 'list', $plugin, $method);
409							}
410							$this->_data = $mainData;
411						}
412					}
413				}
414				$c ++;
415			}
416		}
417		$this->_runPlugins = $runPlugins;
418		return array_unique($return);
419	}
420
421	/**
422	 * test if a plugin is installed
423	 * @param $group
424	 * @param $plugin
425	 * @return bol
426	 */
427
428	function pluginExists($group, $plugin)
429	{
430		$plugins =& $this->loadPlugInGroup($group);
431		if (in_array($plugin, array_keys($plugins))) {
432			return true;
433		}
434		return false;
435	}
436
437}
438?>