6 * Base module for all modules
8 * @author Sebastian Skejø
14 private $_core = null;
15 private $_parent = null;
16 private $_name = null;
17 private $_data = array();
18 private $_slots = array();
21 * Subclasses which overrides a constructor *must* take an AOOSCore or an AOOSModule as first argument and call
22 * parent::__construct(core) in order to work properly
23 * @param $parent A reference to the parent object
25 public function __construct($parent) {
26 $this->_init($parent);
30 * Internal function used by constructor to set things up
31 * @param $core A reference to the core object
33 private function _init($parent) {
34 if ($parent instanceof AOOSModule
) {
35 $this->_core
= $parent->core();
36 $this->_parent
= $parent;
38 elseif ($parent instanceof AOOSCore
) {
39 $this->_core
= $parent;
45 $dbg = debug_backtrace();
46 $str = "AOOSModule instantiated in ";
47 $dbg = array_slice($dbg, 1,sizeof($dbg));
48 foreach ($dbg as $entry) {
49 $file = in_array("file", array_keys($entry)) ?
$entry["file"] : "unknown file";
50 $line = in_array("line", array_keys($entry)) ?
$entry["line"] : "unknown line";
51 $str .= $file.":".$line."<br />";
53 $this->core()->log($str);
56 public function __toString() {
61 * This function is executed after object is constructed an data model is set
63 public function postInitialization() {
68 * Returns an array of dependencies or 0. If your module depends on any other module you should override this
69 * function so it returns an array of the names of the module on which your module depends.
72 public static function dependencies() {
77 * Standard show-function.
78 * Returns the name of the module
80 public function show() {
85 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
87 * @param $stringID The unique ID of the string to translate
90 final public function tr($stringID) {
91 $string = $this->core()->getTranslatedString($stringID);
96 * Returns the value of setting $setting. If the module is named, it will look in the module settings, otherwise it
97 * will look in the global settings. Throws an AOOSException if the setting isn't found.
98 * @param $setting Name of the setting
99 * @return string|false
101 final public function getSetting($setting) {
104 $settingValue = $this->core()->getSetting($setting, $this->name());
107 $settingValue = $this->core()->getSetting($setting);
110 catch (AOOSException
$e) {
114 return $settingValue;
119 * Returns the current core
122 final public function core() {
127 * Returns the parent object or null if no parent object is set
128 * @return AOOSModule|null
130 final public function parent() {
131 return $this->_parent
;
135 * Sets the name of the module. This is automatically called when a module is instantiated.
136 * @param $name Module name
140 final public function setName($name) {
141 $this->_name
= $name;
146 * Returns the name of the module
150 final public function name() {
154 return "Unnamed module";
158 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
159 * the data that the module should contain.
160 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
161 * done in AOOSModule::postInitialization().
162 * @sa setDataModel, dataModel
164 public function dataModelDefinition() {
169 * Sets the data model to be $model
170 * @param $model The model to use as data model. Optionally this can be an array containing models.
171 * @sa dataModelDefinition, dataModel
174 final public function setDataModel($models) {
175 $this->_data
= array(); // XXX Not sure about this
176 if (is_array($models)) {
177 foreach ($models as $name => $model) {
178 if ($model instanceof AOOSCore
) {
179 $this->_data
[$name] = $model;
183 elseif ($models instanceof AOOSModel ||
$models == null) {
184 $this->_data
[] = $models;
189 * Returns the set data model
190 * @param $name An optional parameter to decide which data model to return if more than one data model is defined. If
191 * this parameter isn't set, the first data model set is returned. If $name doesn't match the name of any data model
192 * an AOOSException is thrown.
193 * @sa dataModelDefinition, setDataModel
196 final public function dataModel($name = null) {
197 if (empty($this->_data
)) {
201 return $this->_data
[0];
204 if (in_array($name, array_keys($this->_data
))) {
205 return $this->_data
[$name];
208 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
214 * Emits the signal $signal.
215 * This means that every slot connected to this function is called.
216 * @param $signal A string matching the signal name.
217 * @param $params An optional array of parameters to pass to the connected slot(s)
219 final public function emit($signal, $params = null) {
220 if (!in_array($signal, array_keys($this->_slots
))) {
223 foreach ($this->_slots
[$signal] as $slot) {
224 if (!is_array($params) ||
$slot[2] == 0) {
225 return call_user_func(array($slot[0], $slot[1]));
228 if (sizeof($params) > $slot[2]) {
229 $params = array_slice($params, 0, $slot[2]);
231 elseif (sizeof($params) < $slot[2]) {
232 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
235 return call_user_func_array(array($slot[0], $slot[1]), $params);
241 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
242 * @param $signal A string matching the signal name.
243 * @param $other The module in which the slot is.
244 * @param $slot A string matching the slot name.
245 * @param $numargs The number of arguments the $slot takes. Defaults to 0
248 final public function connect($signal, $other, $slot, $numargs = 0) {
249 if (!in_array($signal, array_keys($this->_slots
))) {
250 $this->_slots
[$signal] = array();
252 $a = array($other, $slot, $numargs);
253 $this->_slots
[$signal][] = $a;