modules/Installer/Installer.php:
[AOOS.git] / modules / Installer / Installer.php
blob8973c3e151d759d04a56d68d905d7da952ccf667
1 <?php
3 /**
4 * An installer-module
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ø
8 */
10 class Installer extends AOOSModule {
11 public function show() {
12 $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
13 $str = "";
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>';
24 else {
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.'">
32 <p>'.$module.'
33 <input name="uninstall" type="hidden" value="'.$module.'" />
34 <input type="submit" value="Uninstall" />
35 </p>
36 </form>';
38 $str .= "<h1>Uninstalled modules:</h1>";
39 foreach ($this->getUninstalledModules() as $module) {
40 $str .= '<form method="post" action="'.$url.'">
41 <p>'.$module.'
42 <input name="install" type="hidden" value="'.$module.'" />
43 <input type="submit" value="Install!" />
44 </p>
45 </form>';
47 return $str;
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);*/
58 return $m;
61 /* public function postInitialization() {
62 $m = $this->dataModel();
63 $m->setTable($this->core()->getSetting("module_table"));
64 $m->populate();
65 }*/
67 /**
68 * Returns an array with names of all uninstalled modules
69 * @return array
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;
81 return $uninstalled;
84 /**
85 * Returns an array with names of all installed modules.
86 * @return array
88 public function getInstalledModules() {
89 $modules = $this->getAllModules();
90 $installed = array();
91 foreach ($modules as $module) {
92 if ($this->dataModel()->find(array("NAME" => $module)) !== false) {
93 $installed[] = $module;
96 return $installed;
99 /**
100 * Returns an array containing names of both installed and uninstalled modules
101 * @return array
103 public function getAllModules() {
104 $d = dir($this->core()->getSetting("module_dir"));
105 $modules = array();
106 while (false !== ($entry = $d->read())) {
107 $path = $this->core()->getSetting("module_dir").$entry;
108 if (is_dir($path) && $entry != "." && $entry != "..") {
109 $modules[] = $entry;
112 return $modules;
116 * Tries to install $module. If install is successfull true is returned, otherwise an AOOSException is thrown and
117 * false is returned.
118 * @param $module A string representing the module name
119 * @return bool
121 public function installModule($module) {
122 $path = $this->core()->getSetting("module_dir").$module."/".$module.$this->core()->getSetting("extension");
123 require $path;
125 $deps = call_user_func(array($module,"dependencies"));
126 $deps = implode(",",$deps);
127 $this->dataModel()->insert(array("NAME" => $module,
128 "DEPS" => $deps));
129 $this->dataModel()->save();
131 $m = $this->core()->loadModule($module);
132 if ($dm = $m->dataModel()) {
133 $dm->create();
136 return true;
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
143 * @return bool
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();