First implementation of AOOSModel->create(). Could be improved e.g. use user-defined...
[AOOS.git] / AOOSModule.php
blob19b5b7e12a5d76eb3ae99e8692f353750eb33cfb
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 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
69 * found.
70 * @param $stringID The unique ID of the string to translate
71 * @return string
73 final public function tr($stringID) {
74 $string = $stringID;
75 try {
76 if ($this->name()) {
77 $string = $this->core()->getTranslatedString($stringID, $this->name());
79 else {
80 $string = $this->core()->getTranslatedString($stringID);
83 catch (AOOSLangException $e)
85 throw $e;
86 return false;
88 return $string;
91 /**
92 * Returns the value of setting $setting. If the module is named, it will look in the module settings, otherwise it
93 * will look in the global settings. Throws an AOOSException if the setting isn't found.
94 * @param $setting Name of the setting
95 * @return string|false
97 final public function getSetting($setting) {
98 try {
99 if ($this->name()) {
100 $settingValue = $this->core()->getSetting($setting, $this->name());
102 else {
103 $settingValue = $this->core()->getSetting($setting);
106 catch (AOOSException $e) {
107 throw $e;
108 return false;
110 return $settingValue;
115 * Returns the current core
116 * @return AOOSCore
118 final public function core() {
119 return $this->_core;
123 * Returns the parent object or null if no parent object is set
124 * @return AOOSModule|null
126 final public function parent() {
127 return $this->_parent;
131 * Sets the name of the module. This is automatically called when a module is instantiated.
132 * @param $name Module name
133 * @sa name
134 * @return true
136 final public function setName($name) {
137 $this->_name = $name;
138 return true;
142 * Returns the name of the module
143 * @sa setName
144 * @return string
146 final public function name() {
147 return $this->_name;
151 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
152 * the data that the module should contain.
153 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
154 * done in AOOSModule::postInitialization().
155 * @sa setDataModel, dataModel
157 abstract public function dataModelDefinition();
160 * Sets the data model to be $model
161 * @param $model The model to use as data model. Optionally this can be an array containing models.
162 * @sa dataModelDefinition, dataModel
163 * @return true
165 final public function setDataModel($models) {
166 $this->_data = array(); // XXX Not sure about this
167 if (is_array($models)) {
168 foreach ($models as $name => $model) {
169 if ($model instanceof AOOSCore) {
170 $this->_data[$name] = $model;
174 elseif ($models instanceof AOOSModel || $models == null) {
175 $this->_data[] = $models;
180 * Returns the set data model
181 * @param $name An optional parameter to decide which data model to return if more than one data model is defined. If
182 * this parameter isn't set, the first data model set is returned. If $name doesn't match the name of any data model
183 * an AOOSException is thrown.
184 * @sa dataModelDefinition, setDataModel
185 * @return AOOSModel
187 final public function dataModel($name = null) {
188 if ($name == null) {
189 return $this->_data[0];
191 else {
192 if (in_array($name, array_keys($this->_data))) {
193 return $this->_data[$name];
195 else {
196 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
202 * Emits the signal $signal.
203 * This means that every slot connected to this function is called.
204 * @param $signal A string matching the signal name.
205 * @param $params An optional array of parameters to pass to the connected slot(s)
207 final public function emit($signal, $params = null) {
208 if (!in_array($signal, array_keys($this->_slots))) {
209 return false;
211 foreach ($this->_slots[$signal] as $slot) {
212 if (!is_array($params) || $slot[2] == 0) {
213 return call_user_func(array($slot[0], $slot[1]));
216 if (sizeof($params) > $slot[2]) {
217 $params = array_slice($params, 0, $slot[2]);
219 elseif (sizeof($params) < $slot[2]) {
220 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
221 return false;
223 return call_user_func_array(array($slot[0], $slot[1]), $params);
225 return true;
229 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
230 * @param $signal A string matching the signal name.
231 * @param $other The module in which the slot is.
232 * @param $slot A string matching the slot name.
233 * @param $numargs The number of arguments the $slot takes. Defaults to 0
234 * @return true
236 final public function connect($signal, $other, $slot, $numargs = 0) {
237 if (!in_array($signal, array_keys($this->_slots))) {
238 $this->_slots[$signal] = array();
240 $a = array($other, $slot, $numargs);
241 $this->_slots[$signal][] = $a;
242 return true;
245 // vim: number