6 * Base module for all modules
8 * @author Sebastian Skejø
12 private $_core = null;
13 private $_parent = null;
14 private $_name = null;
15 private $_data = array();
16 private $_slots = array();
19 * Subclasses which overrides a constructor *must* take an AOOSCore or an AOOSModule as first argument and call
20 * parent::__construct(core) in order to work properly
21 * @param $parent A reference to the parent object
23 public function __construct($parent) {
24 $this->_init($parent);
28 * Internal function used by constructor to set things up
29 * @param $core A reference to the core object
31 private function _init($parent) {
32 if ($parent instanceof AOOSModule
) {
33 $this->_core
= $parent->core();
34 $this->_parent
= $parent;
36 elseif ($parent instanceof AOOSCore
) {
37 $this->_core
= $parent;
43 $dbg = debug_backtrace();
44 $str = "AOOSModule instantiated in ";
45 $dbg = array_slice($dbg, 1,sizeof($dbg));
46 foreach ($dbg as $entry) {
47 $file = in_array("file", array_keys($entry)) ?
$entry["file"] : "unknown file";
48 $line = in_array("line", array_keys($entry)) ?
$entry["line"] : "unknown line";
49 $str .= $file.":".$line."<br />";
51 $this->core()->log($str);
54 public function __toString() {
59 * This function is executed after object is constructed an data model is set
61 public function postInitialization() {
66 * Returns an array of dependencies or 0. If your module depends on any other module you should override this
67 * function so it returns an array of the names of the module on which your module depends.
70 public static function dependencies() {
75 * Returns an associative array of classes used in the module as keys and the path to the file in which they are
76 * defined as values. Paths are relative to the module diretory.
78 * Module Foo uses the class Bar. Bar is defined in modules/Foo/BarClass.php. Foo::files() should be defined as:
82 * static public function files() {
83 * return array("Bar" => "BarClass");
90 public static function files() {
95 * Standard show-function.
96 * Returns the name of the module
98 public function show() {
103 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
105 * @param $stringID The unique ID of the string to translate
108 final public function tr($stringID) {
109 $string = $this->core()->getTranslatedString($stringID);
114 * Returns the value of setting $setting. If the module is named, it will look in the module settings, otherwise it
115 * will look in the global settings. Throws an AOOSException if the setting isn't found.
116 * @param $setting Name of the setting
117 * @return string|false
119 final public function getSetting($setting) {
122 $settingValue = $this->core()->getSetting($setting, $this->name());
125 $settingValue = $this->core()->getSetting($setting);
128 catch (AOOSException
$e) {
132 return $settingValue;
137 * Returns the current core
140 final public function core() {
145 * Returns the parent object or null if no parent object is set
146 * @return AOOSModule|null
148 final public function parent() {
149 return $this->_parent
;
153 * Sets the name of the module. This is automatically called when a module is instantiated.
154 * @param $name Module name
158 final public function setName($name) {
159 $this->_name
= $name;
164 * Returns the name of the module
168 final public function name() {
172 return "Unnamed module";
176 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
177 * the data that the module should contain.
178 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
179 * done in AOOSModule::postInitialization().
180 * @sa setDataModel, dataModel
182 public function dataModelDefinition() {
187 * Sets the data model to be $model
188 * @param $model The model to use as data model. Optionally this can be an array containing models.
189 * @sa dataModelDefinition, dataModel
192 final public function setDataModel($models) {
193 $this->_data
= array(); // XXX Not sure about this
194 if (is_array($models)) {
195 foreach ($models as $name => $model) {
196 if ($model instanceof AOOSModel
) {
197 $this->_data
[$name] = $model;
201 elseif ($models instanceof AOOSModel ||
$models == null) {
202 $this->_data
[] = $models;
207 * Returns the set data model
208 * @param $name An optional parameter to decide which data model to return if more than one data model is defined. If
209 * this parameter isn't set, the first data model set is returned. If $name doesn't match the name of any data model
210 * an AOOSException is thrown.
211 * @sa dataModelDefinition, setDataModel
214 final public function dataModel($name = null) {
215 if (empty($this->_data
)) {
219 return $this->_data
[0];
221 if (in_array($name, array_keys($this->_data
))) {
222 return $this->_data
[$name];
224 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
229 public function dataModels() {
234 * Emits the signal $signal.
235 * This means that every slot connected to this function is called.
236 * @param $signal A string matching the signal name.
237 * @param $params An optional array of parameters to pass to the connected slot(s)
239 final public function emit($signal, $params = null) {
240 if (!in_array($signal, array_keys($this->_slots
))) {
243 foreach ($this->_slots
[$signal] as $slot) {
244 if (!is_array($params) ||
$slot[2] == 0) {
245 return call_user_func(array($slot[0], $slot[1]));
248 if (sizeof($params) > $slot[2]) {
249 $params = array_slice($params, 0, $slot[2]);
251 elseif (sizeof($params) < $slot[2]) {
252 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
255 return call_user_func_array(array($slot[0], $slot[1]), $params);
261 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
262 * @param $signal A string matching the signal name.
263 * @param $other The module in which the slot is.
264 * @param $slot A string matching the slot name.
265 * @param $numargs The number of arguments the $slot takes. Defaults to 0
268 final public function connect($signal, $other, $slot, $numargs = 0) {
269 if (!in_array($signal, array_keys($this->_slots
))) {
270 $this->_slots
[$signal] = array();
272 $a = array($other, $slot, $numargs);
273 $this->_slots
[$signal][] = $a;