Paginator should now be working. Still needs to check whether a module can be shown...
[AOOS.git] / AOOSCore.php
blobd748cfe38ae7aeafb73119a41552ec7e6bb6149e
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();
23 /**
24 * During constructing a model is built with module names and properties
26 public function __construct()
28 require_once("config.php");
29 $this->_settings = $settings; /* Set in config.php */
30 $l = $this->getSetting("lang");
31 require_once("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","",true, 1);
36 else
38 $this->_transStrings = $lang[$l];
41 // Let's load modules
42 $s = new AOOSStorageDevice($this);
43 $s->setStorageType("mysql");
44 $s->setTable("modules");
45 $this->_modules = $s->selectModel("*");
46 $this->_modules->setEscape(false);
49 /**
50 * Sets an exception
51 * @param AOOSException $e The exception
53 public function setException($e)
55 $this->_exceptions[$e->getName()] = $e;
56 if ($e->getEcode() == 0)
58 exit($e);
61 return true;
64 public function printExceptions()
66 $str = "<div class=\"Exceptions\">";
67 foreach ($this->_exceptions as $e)
69 $str .= $e;
71 $str .= "</div>";
72 print $str;
75 public function getSetting($name, $module = "no")
77 $a = $this->_settings;
78 if ($module != "no")
80 $a = $a[$module];
82 if (!in_array($name, array_keys($a)))
84 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
85 return false;
87 return $this->_settings[$name];
90 public function getTranslatedString($string)
92 if (!in_array($string, array_keys($this->_transStrings)))
94 throw new AOOSException($this, "Translated string not found", "String name: ".$string, true, 2);
95 return $string;
97 return $this->_transStrings[$string];
100 public function modules()
102 return $this->_modules;
105 public function loadModule($name)
107 $m = $this->modules();
108 if (($r = $m->getRowFromValue("name", $name)) === false)
110 throw new AOOSException($this, $this->getTranslatedString("module_not_loadable"), "", true, 1);
111 return false;
114 $row = $m->getRow($r);
115 $dir = $this->getSetting("module_dir")."/".$row["name"]."/";
116 $filename = $row["name"].$this->getSetting("extension");
118 if ($row["instance"])
120 return $row["instance"];
122 else {
123 require_once($dir.$filename);
124 $i = new $row["name"]($this);
125 $m->setData($i, $r, "instance");
126 if (file_exists($dir."settings".$this->getSetting("extension")))
128 require_once($dir."settings".$this->getSetting("extenstion"));
129 $this->_settings[$row["name"]] = $settings;
131 return $i;
136 * Returns true if $module is loaded, otherwise return false.
137 * @param string $name Module name
138 * @return bool
140 public function isLoaded($name)
142 $m = $this->modules();
143 if (($row = $m->getRowFromValue("name", $name)) === false)
145 // TODO: throw exception
146 return false;
149 return true;
153 * Returns an instance to the given $module
154 * @param string $module The name of the module
155 * @return false|AOOSModule
157 public function getModule($module)
159 $m = $this->modules();
160 if (($row = $m->getRowFromValue("name", $module)) === false)
162 // TODO: throw
163 throw new AOOSException($this, $this->getTranslatedString("module_not_found"), "", true, 1);
164 return false;
166 return $this->loadModule($module);
169 public function __toString()
171 return "AOOSCore";