AOOSCore: Modified to fit refactored AOOSModel.
[AOOS.git] / AOOSCore.php
bloba472ed823ebac20cc4e328bd1383ce827509efe2
1 <?php
3 /**
4 * AOOSCore
6 * The core class of AOOS
8 * @author Sebastian Skejø
9 */
11 class AOOSCore
13 private $_exceptions = array();
14 private $_modules = null;
15 private $_settings = array();
16 private $_transStrings = array();
17 private $_log = array();
19 /**
20 * Default constructor. An AOOSException is thrown if the language defined in $settings["lang"] isn't found the the
21 * array keys of the translated strings defined in lang.php.
23 public function __construct()
25 // Settings
26 require("settings.php");
27 $this->_settings = $settings; /* Set in settings.php */
28 $l = $this->getSetting("lang");
30 // Translated strings
31 require("lang.php");
32 if (!in_array($l, array_keys($lang))) /* $lang set in lang.php */
34 throw new AOOSException($this, "Language not found in lang.php. You will not be able to translate strings");
36 else
38 $this->_transStrings = $lang[$l];
41 $m = new AOOSModel($this);
42 $m->setColumnIndex(array(
43 // "ID",
44 "NAME",
45 "DEPS",
46 "INSTANCE"
47 ));
48 /* $m->setProperty("ID",
49 AOOSMODEL_TYPE_INTEGER,
50 AOOSMODEL_PROP_FROM_DATABASE|
51 AOOSMODEL_PROP_UNIQUE|
52 AOOSMODEL_PROP_DATA_INCREASING);*/
53 $m->setProperties("NAME",
54 AOOSMODEL_TYPE_STRING, 0,
55 AOOSMODEL_FLAG_FROM_DATABASE);
56 $m->setProperties("DEPS",
57 AOOSMODEL_TYPE_STRING, 0,
58 AOOSMODEL_FLAG_FROM_DATABASE);
59 $m->setProperties("INSTANCE",
60 AOOSMODEL_TYPE_UNKNOWN);
61 $m->setSource($this->getSetting("default_storage_type"));
62 $m->setTable($this->getSetting("module_table"));
63 try {
64 $m->populate();
65 } catch (AOOSException $e) {
66 throw $e;
68 $this->_modules = $m;
70 $this->log("Created AOOSCore");
73 /**
74 * Logs the string with a timestamp
75 * @param $str The string to log
77 public function log($str)
79 $this->_log[] = array(
80 "STRING" => $str,
81 "TIMESTAMP" => microtime()
85 /**
86 * Returns a string containing the log
88 public function log2str() {
89 $str = "";
90 foreach ($this->_log as $num => $entry) {
91 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
93 return $str;
96 /**
97 * Sets an exception
98 * @param $e The exception
100 public function setException($e)
102 $this->_exceptions[$e->getName()] = $e;
103 if ($e->getEcode() == 0)
105 exit($e);
108 return true;
112 * Prints all exceptions
114 public function printExceptions()
116 $str = "<div class=\"Exceptions\">";
117 foreach ($this->_exceptions as $e)
119 $str .= $e;
121 $str .= "</div>";
122 print $str;
126 * Returns the value of the the given setting. Throws an AOOSException if the setting key isn't found.
127 * @param $name Name of the setting
128 * @param $module This is used to fetch per-module settings. If null is given a global setting is fetched.
129 * @return string
131 public function getSetting($name, $module = null)
133 $a = $this->_settings;
134 if ($module != null && $this->getModule($module))
136 $a = $a[$module];
138 if (!in_array($name, array_keys($a)))
140 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
141 return false;
143 return $a[$name];
147 * Used by AOOSModule to get translated string. SHOULD NOT BE ACCESSED MANUALLY! In order to translate a string use
148 * the AOOSModule::tr() function. Throws an AOOSException if the string isn't found.
149 * @param $string The string to translate
150 * @param $module This is used to fetch translated strings per-module. If a module name is given the translated
151 * string will be fetched from this module. If null is given, the string will be fetched from /lang.php
152 * @return string
154 public function getTranslatedString($string, $module = null)
156 $a = $this->_transStrings;
157 if ($module !== null && $this->getModule($module)) {
158 $a = $a[$module];
160 if (!in_array($string, array_keys($a))) {
161 return $string;
163 return $a[$string];
166 public function tr($string) {
167 return $this->getTranslatedString($string);
171 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
172 * reloaded. If the module is successfully loaded the newly created instance is returned.
173 * @param $module Name of the module
174 * @return AOOSModule
175 * @sa getModule
177 public function loadModule($module) {
178 // $this->_modules->populate();
179 $m = $this->modules();
180 if (false === ($row = $m->find(array("NAME" => $module)))) {
181 throw new AOOSException($this, $this->getTranslatedString("unknown_module"), $module);
182 return false;
184 $deps = $row->DEPS;
185 if ($deps != "") {
186 foreach (explode(",",$deps) as $dep) {
187 if (!$this->getModule($dep)) {
188 try {
189 $this->loadModule($dep);
191 catch (AOOSException $e) {
192 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
193 return false;
199 $dir = $this->getSetting("module_dir").$module."/";
200 $path = $dir.$module.$this->getSetting("extension");
201 require_once($path);
202 $i = new $module($this);
203 if (($dm = $i->dataModelDefinition()) !== 0) {
204 $i->setDataModel($dm);
205 $i->dataModel()->setSource($this->getSetting("default_storage_type"));
206 $i->dataModel()->setTable($module);
208 $row->INSTANCE = $i;
209 $i->setName($module);
211 $sf = $dir."settings".$this->getSetting("extension");
212 if (file_exists($sf)) {
213 require($sf);
214 $this->_settings[$module] = $settings;
217 $lf = $dir."lang".$this->getSetting("extension");
218 if (file_exists($lf)) {
219 require($lf);
220 $this->_transStrings[$module] = $lang;
223 $i->postInitialization();
224 return $i;
228 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
229 * an AOOSException if loadModule fails.
230 * @param $module Name of the module
231 * @return AOOSModule|false
232 * @sa loadModule
234 public function getModule($module) {
235 $row = $this->modules()->find(array("NAME" => $module));
236 if ($row === false) {
237 return false;
239 return $row->INSTANCE ? $row->INSTANCE : $this->loadModule($module);
243 * Returns the AOOSModel containing all module names, dependencies and instances.
244 * @return AOOSModel
246 public function modules()
248 return $this->_modules;
252 * Simply returs a string containing "AOOSCore".
253 * \internal This could be better
255 public function __toString()
257 return "AOOSCore";
261 * Connects the signal $signal of $sender to the slot $slot of $reciever
262 * @param $sender The sender
263 * @param $signal The signal name
264 * @param $reciever The reciever
265 * @param $slot The slot
269 // vim: number