Typo
[AOOS.git] / AOOSModule.php
blob26d24e50951c84a57cf5f735a12d0565edc6afef
1 <?php
3 /**
4 * AOOSModule
6 * Base module for all modules
8 * @author Sebastian Skejø
9 */
10 class AOOSModule
12 private $_core = null;
13 private $_parent = null;
14 private $_name = null;
15 private $_data = array();
16 private $_slots = array();
18 /**
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);
27 /**
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;
39 else {
40 return false;
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() {
55 return __class__;
58 /**
59 * This function is executed after object is constructed an data model is set
61 public function postInitialization() {
62 return true;
65 /**
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.
68 * @return array
70 public static function dependencies() {
71 return array();
74 /**
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.
77 * Example:
78 * Module Foo uses the class Bar. Bar is defined in modules/Foo/BarClass.php. Foo::files() should be defined as:
79 * \code
80 * class Foo {
81 * ...
82 * static public function files() {
83 * return array("Bar" => "BarClass");
84 * }
85 * ...
86 * }
87 * \code
88 * @return array
90 public static function files() {
91 return array();
94 /**
95 * Standard show-function.
96 * Returns the name of the module
98 public function show() {
99 return $this->name();
103 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
104 * found.
105 * @param $stringID The unique ID of the string to translate
106 * @return string
108 final public function tr($stringID) {
109 $string = $this->core()->getTranslatedString($stringID);
110 return $string;
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) {
120 try {
121 if ($this->name()) {
122 $settingValue = $this->core()->getSetting($setting, $this->name());
124 else {
125 $settingValue = $this->core()->getSetting($setting);
128 catch (AOOSException $e) {
129 throw $e;
130 return false;
132 return $settingValue;
137 * Returns the current core
138 * @return AOOSCore
140 final public function core() {
141 return $this->_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
155 * @sa name
156 * @return true
158 final public function setName($name) {
159 $this->_name = $name;
160 return true;
164 * Returns the name of the module
165 * @sa setName
166 * @return string
168 final public function name() {
169 if ($this->_name) {
170 return $this->_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() {
183 return 0;
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
190 * @return true
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
212 * @return AOOSModel
214 final public function dataModel($name = null) {
215 if (empty($this->_data)) {
216 return false;
218 if ($name == null) {
219 return $this->_data[0];
220 } else {
221 if (in_array($name, array_keys($this->_data))) {
222 return $this->_data[$name];
223 } else {
224 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
229 public function dataModels() {
230 return $this->_data;
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))) {
241 return false;
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"));
253 return false;
255 return call_user_func_array(array($slot[0], $slot[1]), $params);
257 return true;
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
266 * @return true
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;
274 return true;
277 // vim: number