Added setSort() and order-param to selectModel()
[AOOS.git] / AOOSCore.php
blobfc72f241bf7c96d44b9cd86a4837db4d96f4c278
1 <?php
3 require_once("AOOSException.php");
4 require_once("AOOSModel.php");
5 require_once("AOOSStorageDevice.php");
8 /**
9 * AOOSCore
11 * The core class of AOOS
13 * @author Sebastian Skejø
16 class AOOSCore
18 private $_exceptions = array();
19 private $_modules = null;
20 private $_settings = array();
21 private $_transStrings = array();
22 private $_log = array();
24 /**
25 * During constructing a model is built with module names and properties
27 public function __construct()
29 require_once("config.php");
30 $this->_settings = $settings; /* Set in config.php */
31 $l = $this->getSetting("lang");
32 require_once("lang.php");
33 if (!in_array($l, array_keys($lang))) /* $lang set in lang.php */
35 throw new AOOSException($this, "Language not found in lang.php. You will not be able to translate strings","",true, 1);
37 else
39 $this->_transStrings = $lang[$l];
42 // Let's load modules
43 $s = new AOOSStorageDevice($this);
44 $s->setStorageType("mysql");
45 $s->setTable("modules");
46 $this->_modules = $s->selectModel("*");
47 $this->_modules->setEscape(false);
50 public function log($str)
52 $this->_log[] = $str;
55 /**
56 * Sets an exception
57 * @param AOOSException $e The exception
59 public function setException($e)
61 $this->_exceptions[$e->getName()] = $e;
62 if ($e->getEcode() == 0)
64 exit($e);
67 return true;
70 public function printExceptions()
72 $str = "<div class=\"Exceptions\">";
73 foreach ($this->_exceptions as $e)
75 $str .= $e;
77 $str .= "</div>";
78 print $str;
81 public function getSetting($name, $module = "no")
83 $a = $this->_settings;
84 if ($module != "no" && $this->isLoaded($module))
86 $a = $a[$module];
88 if (!in_array($name, array_keys($a)))
90 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
91 return false;
93 return $a[$name];
96 public function getTranslatedString($string, $module = "no")
98 $a = $this->_transStrings;
99 if ($module != "no" && $this->isLoaded($module))
101 $a = $a[$module];
103 if (!in_array($string, array_keys($a)))
105 throw new AOOSException($this, "Translated string not found", "String name: ".$string, true, 2);
106 return $string;
108 return $a[$string];
111 public function modules()
113 return $this->_modules;
116 public function loadModule($name)
118 $this->log("Loading ".$name);
119 $m = $this->modules();
120 if (($r = $m->getRowFromValue("name", $name)) === false)
122 $this->log("Failed!");
123 throw new AOOSException($this, $this->getTranslatedString("module_not_loadable"), "", true, 1);
124 return false;
127 $row = $m->getRow($r);
128 $dir = $this->getSetting("module_dir")."/".$row["name"]."/";
129 $filename = $row["name"].$this->getSetting("extension");
131 $deps = explode(",", $row["deps"]);
132 if (!empty($deps[0]))
134 foreach($deps as $dep)
136 if (!$this->isLoaded($dep))
138 try
140 $this->loadModule($dep);
142 catch(AOOSException $e)
144 throw new AOOSException($this, $this->getTranslatedString("deps_not_loadable"), $row["name"], true, 1);
145 return false;
151 if ($row["instance"])
153 $this->log("Done!");
154 return $row["instance"];
156 else {
157 require_once($dir.$filename);
158 if (file_exists($dir."settings".$this->getSetting("extension")))
160 require_once($dir."settings".$this->getSetting("extension"));
161 $this->_settings[$row["name"]] = $settings;
163 if (file_exists($dir."lang".$this->getSetting("extension")))
165 require_once($dir."lang".$this->getSetting("extension"));
166 $this->_transStrings[$row["name"]] = $lang;
168 $i = new $row["name"]($this);
169 $m->setData($i, $r, "instance");
170 $this->log("Done!");
171 return $i;
176 * Returns true if $module is loaded, otherwise return false.
177 * @param string $name Module name
178 * @return bool
180 public function isLoaded($name)
182 $m = $this->modules();
183 if (($row = $m->getRowFromValue("name", $name)) === false)
185 // TODO: throw exception
186 return false;
189 return true;
193 * Returns an instance to the given $module
194 * @param string $module The name of the module
195 * @return false|AOOSModule
197 public function getModule($module)
199 $m = $this->modules();
200 if (($row = $m->getRowFromValue("name", $module)) === false)
202 throw new AOOSException($this, $this->getTranslatedString("module_not_found"), "", true, 1);
203 return false;
205 return $this->loadModule($module);
208 public function __toString()
210 return "AOOSCore";