6 * Base module for all modules
8 * @author Sebastian Skejø
12 abstract class AOOSModule
14 private $_core = null;
15 private $_data = array();
16 private $_slots = array();
19 * Subclasses which overrides a constructor *must* take an AOOSCore as first argument call
20 * parent::__construct(core) in order to work properly
21 * @param $core A reference to the core object
23 public function __construct($core) {
28 * Internal function used by constructor to set things up
29 * @param $core A reference to the core object
31 private function _init($core) {
32 if (!$core instanceof AOOSCore ||
!isset($core))
39 $dbg = debug_backtrace();
40 $dbg = array_slice($dbg, 1,sizeof($dbg));
41 $str = __class__
." instantiated in ";
42 foreach ($dbg as $entry) {
43 $file = $entry["file"];
44 $line = $entry["line"];
45 $str .= $file.":".$line."<br />";
47 $this->core()->log($str);
50 public function __toString() {
55 * This function is executed after object is constructed an data model is set
57 public function postInitialization() {
62 * Translates a given string to language selected in /settings.php. Throws an AOOSException if the settings isn't
64 * @param $stringID The unique ID of the string to translate
67 final public function tr($stringID) {
71 $string = $this->core()->getTranslatedString($stringID);
73 catch (AOOSException
$e)
80 * Returns the current core
83 final public function core() {
88 * This function must defined to either return a proper AOOSModel or 0. This model must be a representation of all
89 * the data that the module should contain.
90 * Please note that the model shouldn't be populated or have table or source set in this function. This should be
91 * done in AOOSModule::postInitialization().
93 abstract public function dataModelDefinition();
96 * Sets the data model to be $model
97 * @param $model The model to use as data model. Optionally this can be an array containing models.
100 final public function setDataModel($models) {
101 $this->_data
= array(); // XXX Not sure about this
102 if (is_array($models)) {
103 foreach ($models as $name => $model) {
104 if ($model instanceof AOOSCore
) {
105 $this->_data
[$name] = $model;
109 elseif ($models instanceof AOOSModel ||
$models == null) {
110 $this->_data
[] = $models;
115 * Returns the set data model
116 * @param $name An optional parameter to decide which data model to return if more than one data model is defined. If
117 * this parameter isn't set, the first data model set is returned. If $name doesn't match the name of any data model
118 * an AOOSException is thrown.
121 final public function dataModel($name = null) {
123 return $this->_data
[0];
126 if (in_array($name, array_keys($this->_data
))) {
127 return $this->_data
[$name];
130 throw new AOOSException($this->core(), $this->tr("data_model_not_found"));
136 * Emits the signal $signal.
137 * This means that every slot connected to this function is called.
138 * @param $signal A string matching the signal name.
139 * @param $params An optional array of parameters to pass to the connected slot(s)
141 final public function emit($signal, $params = null) {
142 if (!in_array($signal, array_keys($this->_slots
))) {
145 foreach ($this->_slots
[$signal] as $slot) {
146 if (!is_array($params) ||
$slot[2] == 0) {
147 return call_user_func(array($slot[0], $slot[1]));
150 if (sizeof($params) > $slot[2]) {
151 $params = array_slice($params, 0, $slot[2]);
153 elseif (sizeof($params) < $slot[2]) {
154 throw new AOOSException($this->core(), $this->tr("not_enough_params"));
157 return call_user_func_array(array($slot[0], $slot[1]), $params);
163 * Connects the signal $signal of $this module to the slot, $slot of the the $other module.
164 * @param $signal A string matching the signal name.
165 * @param $other The module in which the slot is.
166 * @param $slot A string matching the slot name.
167 * @param $numargs The number of arguments the $slot takes. Defaults to 0
170 final public function connect($signal, $other, $slot, $numargs = 0) {
171 if (!in_array($signal, array_keys($this->_slots
))) {
172 $this->_slots
[$signal] = array();
174 $a = array($other, $slot, $numargs);
175 $this->_slots
[$signal][] = $a;