5 * This module installs a chosen module using the data model definitions of the module. At the moment it is assumed that
6 * all dependencies is installed when trying to install a module.
7 * @author Sebastian Skejø
10 class Installer
extends AOOSModule
{
11 public function show() {
12 $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
15 if (isset($_POST["install"])) {
16 if ($this->installModule($_POST["install"])) {
17 $str .= '<p style="color: green;">'.$_POST["install"].' installed successfully!</p>';
20 elseif (isset($_POST["uninstall"])) {
21 if ($this->uninstallModule($_POST["uninstall"])) {
22 $str .= '<p style="color: green;">'.$_POST["uninstall"].' uninstalled successfully!</p>';
25 $str .= '<p style="color: red;">'.$_POST["uninstall"].' wasn\'t uninstalled!</p>';
29 $str .= "<h1>Installed modules:</h1>";
30 foreach ($this->getInstalledModules() as $module) {
31 $str .= '<form method="post" action="'.$url.'">
33 <input name="uninstall" type="hidden" value="'.$module.'" />
34 <input type="submit" value="Uninstall" />
38 $str .= "<h1>Uninstalled modules:</h1>";
39 foreach ($this->getUninstalledModules() as $module) {
40 $str .= '<form method="post" action="'.$url.'">
42 <input name="install" type="hidden" value="'.$module.'" />
43 <input type="submit" value="Install!" />
50 public function dataModelDefinition() {
51 $m = $this->core()->modules();
52 /* $m = new AOOSModel($this->core());
53 $m->setColumnIndex(array("NAME", "DEPS"));
55 $m->setProperty("NAME", AOOSMODEL_TYPE_STRING,
56 AOOSMODEL_PROP_FROM_DATABASE);*/
61 /* public function postInitialization() {
62 $m = $this->dataModel();
63 $m->setTable($this->core()->getSetting("module_table"));
68 * Returns an array with names of all uninstalled modules
71 public function getUninstalledModules() {
72 $all = $this->getAllModules();
73 $installed = $this->getInstalledModules();
74 $uninstalled = array();
76 foreach ($all as $module) {
77 if (!in_array($module, $installed)) {
78 $uninstalled[] = $module;
85 * Returns an array with names of all installed modules.
88 public function getInstalledModules() {
89 $modules = $this->getAllModules();
91 foreach ($modules as $module) {
92 if ($this->dataModel()->find(array("NAME" => $module)) !== false) {
93 $installed[] = $module;
100 * Returns an array containing names of both installed and uninstalled modules
103 public function getAllModules() {
104 $d = dir($this->core()->getSetting("module_dir"));
106 while (false !== ($entry = $d->read())) {
107 $path = $this->core()->getSetting("module_dir").$entry;
108 if (is_dir($path) && $entry != "." && $entry != "..") {
116 * Tries to install $module. If install is successfull true is returned, otherwise an AOOSException is thrown and
118 * @param $module A string representing the module name
121 public function installModule($module) {
122 $path = $this->core()->getSetting("module_dir").$module."/".$module.$this->core()->getSetting("extension");
125 $deps = call_user_func(array($module,"dependencies"));
126 $deps = implode(",",$deps);
127 $this->dataModel()->insert(array("NAME" => $module,
129 $this->dataModel()->save();
131 $m = $this->core()->loadModule($module);
132 if ($dm = $m->dataModel()) {
140 * Removes a module from the database. If the module is successfully removed true is returned. Otherwise false is
141 * returned. If $module isn't a module in the database an AOOSException is thrown.
142 * @param $module A string representing the module name
145 public function uninstallModule($module) {
146 $m = $this->core()->getModule($module);
147 $m->dataModel()->drop();
148 $this->dataModel()->remove(array("NAME" => $module));
149 return $this->dataModel()->save();