Added an Installer module
[AOOS.git] / AOOSModule.php
blobd31d9ea83984bd037e08288d3ff3d6f5527bde5a
1 <?php
3 /**
4 * AOOSModule
6 * Base module for all modules
8 * @author Sebastian Skejø
9 */
12 abstract class AOOSModule
14 private $_core = null;
15 private $_parent = null;
16 private $_name = null;
17 private $_data = array();
18 private $_slots = array();
20 /**
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);
29 /**
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;
41 else {
42 return false;
45 $dbg = debug_backtrace();
46 $dbg = array_slice($dbg, 1,sizeof($dbg));
47 $str = __class__." instantiated in ";
48 foreach ($dbg as $entry) {
49 $file = $entry["file"];
50 $line = $entry["line"];
51 $str .= $file.":".$line."<br />";
53 $this->core()->log($str);
56 public function __toString() {
57 return __class__;
60 /**
61 * This function is executed after object is constructed an data model is set
63 public function postInitialization() {
64 return true;
67 /**
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.
70 * @return array|0
72 public function dependencies() {
73 return 0;
76 /**
77 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
78 * found.
79 * @param $stringID The unique ID of the string to translate
80 * @return string
82 final public function tr($stringID) {
83 $string = $stringID;
84 try {
85 if ($this->name()) {
86 $string = $this->core()->getTranslatedString($stringID, $this->name());
88 else {
89 $string = $this->core()->getTranslatedString($stringID);
92 catch (AOOSLangException $e)
94 throw $e;
95 return false;
97 return $string;
101 * Returns the value of setting $setting. If the module is named, it will look in the module settings, otherwise it
102 * will look in the global settings. Throws an AOOSException if the setting isn't found.
103 * @param $setting Name of the setting
104 * @return string|false
106 final public function getSetting($setting) {
107 try {
108 if ($this->name()) {
109 $settingValue = $this->core()->getSetting($setting, $this->name());
111 else {
112 $settingValue = $this->core()->getSetting($setting);
115 catch (AOOSException $e) {
116 throw $e;
117 return false;
119 return $settingValue;
124 * Returns the current core
125 * @return AOOSCore
127 final public function core() {
128 return $this->_core;
132 * Returns the parent object or null if no parent object is set
133 * @return AOOSModule|null
135 final public function parent() {
136 return $this->_parent;
140 * Sets the name of the module. This is automatically called when a module is instantiated.
141 * @param $name Module name
142 * @sa name
143 * @return true
145 final public function setName($name) {
146 $this->_name = $name;
147 return true;
151 * Returns the name of the module
152 * @sa setName
153 * @return string
155 final public function name() {
156 return $this->_name;
160 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
161 * the data that the module should contain.
162 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
163 * done in AOOSModule::postInitialization().
164 * @sa setDataModel, dataModel
166 abstract 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
172 * @return true
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
194 * @return AOOSModel
196 final public function dataModel($name = null) {
197 if ($name == null) {
198 return $this->_data[0];
200 else {
201 if (in_array($name, array_keys($this->_data))) {
202 return $this->_data[$name];
204 else {
205 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
211 * Emits the signal $signal.
212 * This means that every slot connected to this function is called.
213 * @param $signal A string matching the signal name.
214 * @param $params An optional array of parameters to pass to the connected slot(s)
216 final public function emit($signal, $params = null) {
217 if (!in_array($signal, array_keys($this->_slots))) {
218 return false;
220 foreach ($this->_slots[$signal] as $slot) {
221 if (!is_array($params) || $slot[2] == 0) {
222 return call_user_func(array($slot[0], $slot[1]));
225 if (sizeof($params) > $slot[2]) {
226 $params = array_slice($params, 0, $slot[2]);
228 elseif (sizeof($params) < $slot[2]) {
229 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
230 return false;
232 return call_user_func_array(array($slot[0], $slot[1]), $params);
234 return true;
238 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
239 * @param $signal A string matching the signal name.
240 * @param $other The module in which the slot is.
241 * @param $slot A string matching the slot name.
242 * @param $numargs The number of arguments the $slot takes. Defaults to 0
243 * @return true
245 final public function connect($signal, $other, $slot, $numargs = 0) {
246 if (!in_array($signal, array_keys($this->_slots))) {
247 $this->_slots[$signal] = array();
249 $a = array($other, $slot, $numargs);
250 $this->_slots[$signal][] = $a;
251 return true;
254 // vim: number