aliroMambot.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroMambot is the class for mambot objects - the descriptors for Aliro plugins.
00038  *
00039  * aliroMambotHandler manages all the descriptors, operating as a cached singleton.
00040  * It provides some basic methods to support the installer, can return an array of
00041  * the mambots that will respond to a particular trigger (used, for example, to find
00042  * all the editors) and most significantly implements the trigger and call methods
00043  * that actually invoke mambots.  The method loadBotGroup is provided for compatibility
00044  * but does nothing since all mambots in Aliro are loaded using the smart class
00045  * loader when they are triggered - there is no need to call loadBotGroup.
00046  *
00047  */
00048 
00049 
00050 class aliroMambot extends aliroDatabaseRow {
00051     protected $DBclass = 'aliroCoreDatabase';
00052     protected $tableName = '#__mambots';
00053     protected $rowKey = 'id';
00054     protected $handler = 'aliroMambotHandler';
00055     protected $formalfield = 'element';
00056 
00057 }
00058 
00059 // Provided only for backwards compatibility
00060 class mosMambotHandler {
00061 
00062     public static function getInstance () {
00063         return aliroMambotHandler::getInstance();
00064     }
00065 }
00066 
00067 class aliroMambotHandler extends aliroCommonExtHandler  {
00068 
00069     protected static $instance = __CLASS__;
00070 
00071     private static $defaults = array (
00072     'onIniEditor' => 'bot_nulleditor',
00073     'onGetEditorContents' => 'bot_nulleditor',
00074     'onEditorArea' => 'bot_nulleditor'
00075     );
00076 
00077     private $_events=array();
00078     private $_bots=null;
00079     private $_bot_objects = array();
00080     private $_botsByName = array();
00081 
00082     protected $extensiondir = '/mambots/';
00083 
00084     protected function __construct() {
00085         $database = aliroCoreDatabase::getInstance();
00086         $this->_bots = $database->doSQLget( "SELECT element, class, triggers, published, params, 0 AS isdefault FROM #__mambots ORDER BY ordering");
00087         foreach ($this->_bots as $bot) $this->_botsByName[$bot->element] = true;
00088         foreach (self::$defaults as $trigger=>$default) {
00089             $defobj = new stdClass;
00090             $defobj->class = $default;
00091             $defobj->triggers = $trigger;
00092             $defobj->isdefault = 1;
00093             array_push($this->_bots, $defobj);
00094         }
00095         foreach ($this->_bots as $key=>$bot) {
00096             $triggers = explode (',', $bot->triggers);
00097             foreach ($triggers as $trigger) $this->_events[trim($trigger)][] = $key;
00098         }
00099     }
00100 
00101     public static function getInstance () {
00102         return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00103     }
00104     
00105     public function isPluginPresent ($formalname, $andPublished=false) {
00106         if (!isset($this->_botsByName[$formalname])) return false;
00107         if (!$andPublished) return true;
00108         $bot = $this->_botsByName[$formalname];
00109         return $bot->published ? true : false;
00110     }
00111 
00112     // Not currently used in Aliro or its common extensions - provided for compatibility?
00113     public function getMambotsForTrigger ($trigger) {
00114         if (isset($this->_events[$trigger])) foreach ($this->_events[$trigger] as $botkey) {
00115             $results[] = $this->_bots[$botkey];
00116         }
00117         else $results = array();
00118         return $results;
00119     }
00120 
00121     public function loadBotGroup( $group ) {
00122         // Only required for backward compatibility
00123         return true;
00124     }
00125 
00126     // The bulk of the work of running plugins is done here
00127     // The main method for invoking Aliro plugins
00128     public function trigger( $event, $args=null, $doUnpublished=false, $maxbot=0 ) {
00129         if ($args === null) $args = array();
00130         elseif (!is_array($args)) $args = array($args);
00131         $result = array();
00132         $botcount = 0;
00133         if (isset( $this->_events[$event] )) foreach ($this->_events[$event] as $botkey) {
00134             $bot = $this->_bots[$botkey];
00135             if ($bot->isdefault) {
00136                 if (!isset($defaultbotkey)) $defaultbotkey = $botkey;
00137             }
00138             else {
00139                 $botparams = new aliroParameters($bot->params);
00140                 if ($doUnpublished OR $bot->published) {
00141                     $result[] = $this->runOneBot($botkey, $args, $event, $botparams, $bot->published);
00142                     $botcount ++;
00143                     if ($maxbot AND $botcount >= $maxbot) break;
00144                 }
00145             }
00146         }
00147         if (0 == $botcount AND isset($defaultbotkey)) $result[] = $this->runOneBot($defaultbotkey, $args, $event, '', '1');
00148         return $result;
00149     }
00150 
00151     private function runOneBot ($botkey, $args, $event, $botparams, $published) {
00152         if (isset($this->_bot_objects[$botkey])) $botobject = $this->_bot_objects[$botkey];
00153         else $botobject = $this->_bot_objects[$botkey] = new $this->_bots[$botkey]->class;
00154         array_unshift($args, $event, $botparams, $published);
00155         return call_user_func_array(array($botobject, 'perform'), $args);
00156     }
00157 
00158     public function countBots ($event) {
00159         return isset($this->_events[$event]) ? count($this->_events[$event]) : 0;
00160     }
00161 
00162     // Trigger function for activating just one bot - provided for convenience in calling
00163     public function triggerOnce ($event, $args=null, $doUnpublished=false) {
00164         return $this->trigger ($event, $args, $doUnpublished, 1);
00165     }
00166 
00167     // Alternative way to invoke a plugin - doesn't appear to be used now
00168     public function call( $event ) {
00169         $args = func_get_args();
00170         array_shift($args);
00171         $result = $this->trigger($event, $args);
00172         if (isset($result[0])) return $result[0];
00173         return null;
00174     }
00175 }

Generated on Wed May 14 13:01:56 2008 for ALIRO by  doxygen 1.5.5