Deps needed to be stripped before exploding
[AOOS.git] / AOOSModule.php
blob4304551ab0093a0c43b7574ba27862b6dfcb9630
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 static function dependencies() {
73 return 0;
76 /**
77 * Standard show-function.
78 * Returns the name of the module
80 public function show() {
81 return $this->name();
84 /**
85 * Translates a given string to language selected in /settings.php. Throws an AOOSLangException if the translated isn't
86 * found.
87 * @param $stringID The unique ID of the string to translate
88 * @return string
90 final public function tr($stringID) {
91 $string = $stringID;
92 try {
93 if ($this->name()) {
94 $string = $this->core()->getTranslatedString($stringID, $this->name());
96 else {
97 $string = $this->core()->getTranslatedString($stringID);
100 catch (AOOSLangException $e)
102 throw $e;
103 return false;
105 return $string;
109 * Returns the value of setting $setting. If the module is named, it will look in the module settings, otherwise it
110 * will look in the global settings. Throws an AOOSException if the setting isn't found.
111 * @param $setting Name of the setting
112 * @return string|false
114 final public function getSetting($setting) {
115 try {
116 if ($this->name()) {
117 $settingValue = $this->core()->getSetting($setting, $this->name());
119 else {
120 $settingValue = $this->core()->getSetting($setting);
123 catch (AOOSException $e) {
124 throw $e;
125 return false;
127 return $settingValue;
132 * Returns the current core
133 * @return AOOSCore
135 final public function core() {
136 return $this->_core;
140 * Returns the parent object or null if no parent object is set
141 * @return AOOSModule|null
143 final public function parent() {
144 return $this->_parent;
148 * Sets the name of the module. This is automatically called when a module is instantiated.
149 * @param $name Module name
150 * @sa name
151 * @return true
153 final public function setName($name) {
154 $this->_name = $name;
155 return true;
159 * Returns the name of the module
160 * @sa setName
161 * @return string
163 final public function name() {
164 if ($this->_name) {
165 return $this->_name;
167 return "Unnamed module";
171 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
172 * the data that the module should contain.
173 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
174 * done in AOOSModule::postInitialization().
175 * @sa setDataModel, dataModel
177 abstract public function dataModelDefinition();
180 * Sets the data model to be $model
181 * @param $model The model to use as data model. Optionally this can be an array containing models.
182 * @sa dataModelDefinition, dataModel
183 * @return true
185 final public function setDataModel($models) {
186 $this->_data = array(); // XXX Not sure about this
187 if (is_array($models)) {
188 foreach ($models as $name => $model) {
189 if ($model instanceof AOOSCore) {
190 $this->_data[$name] = $model;
194 elseif ($models instanceof AOOSModel || $models == null) {
195 $this->_data[] = $models;
200 * Returns the set data model
201 * @param $name An optional parameter to decide which data model to return if more than one data model is defined. If
202 * this parameter isn't set, the first data model set is returned. If $name doesn't match the name of any data model
203 * an AOOSException is thrown.
204 * @sa dataModelDefinition, setDataModel
205 * @return AOOSModel
207 final public function dataModel($name = null) {
208 if ($name == null) {
209 return $this->_data[0];
211 else {
212 if (in_array($name, array_keys($this->_data))) {
213 return $this->_data[$name];
215 else {
216 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
222 * Emits the signal $signal.
223 * This means that every slot connected to this function is called.
224 * @param $signal A string matching the signal name.
225 * @param $params An optional array of parameters to pass to the connected slot(s)
227 final public function emit($signal, $params = null) {
228 if (!in_array($signal, array_keys($this->_slots))) {
229 return false;
231 foreach ($this->_slots[$signal] as $slot) {
232 if (!is_array($params) || $slot[2] == 0) {
233 return call_user_func(array($slot[0], $slot[1]));
236 if (sizeof($params) > $slot[2]) {
237 $params = array_slice($params, 0, $slot[2]);
239 elseif (sizeof($params) < $slot[2]) {
240 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
241 return false;
243 return call_user_func_array(array($slot[0], $slot[1]), $params);
245 return true;
249 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
250 * @param $signal A string matching the signal name.
251 * @param $other The module in which the slot is.
252 * @param $slot A string matching the slot name.
253 * @param $numargs The number of arguments the $slot takes. Defaults to 0
254 * @return true
256 final public function connect($signal, $other, $slot, $numargs = 0) {
257 if (!in_array($signal, array_keys($this->_slots))) {
258 $this->_slots[$signal] = array();
260 $a = array($other, $slot, $numargs);
261 $this->_slots[$signal][] = $a;
262 return true;
265 // vim: number