00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
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
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
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
00123 return true;
00124 }
00125
00126
00127
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
00163 public function triggerOnce ($event, $args=null, $doUnpublished=false) {
00164 return $this->trigger ($event, $args, $doUnpublished, 1);
00165 }
00166
00167
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 }