Deps needed to be stripped before exploding
[AOOS.git] / modules / Installer / Installer.php
blobb99ed88af8f9482dbc9585811388ece8e0d39e2f
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["module"])) {
16 if ($this->installModule($_POST["module"])) {
17 $str .= '<p style="color: green;">'.$_POST["module"].' installed successfully!</p>';
21 $str .= "<h1>Installed modules:</h1>";
22 foreach ($this->getInstalledModules() as $module) {
23 $str .= "<p>".$module."</p>";
25 $str .= "<h1>Uninstalled modules:</h1>";
26 foreach ($this->getUninstalledModules() as $module) {
27 $str .= '<form method="post" action="'.$url.'">
28 <p>'.$module.'
29 <input name="module" type="hidden" value="'.$module.'" />
30 <input type="submit" value="Install!" />
31 </p>
32 </form>';
34 return $str;
37 public function dataModelDefinition() {
38 $m = $this->core()->modules();
39 /* $m = new AOOSModel($this->core());
40 $m->setColumnIndex(array("NAME", "DEPS"));
42 $m->setProperty("NAME", AOOSMODEL_TYPE_STRING,
43 AOOSMODEL_PROP_FROM_DATABASE);*/
45 return $m;
48 public function postInitialization() {
49 $m = $this->dataModel();
50 $m->setTable($this->core()->getSetting("module_table"));
51 $m->populate();
54 /**
55 * Returns an array with names of all uninstalled modules
56 * @return array
58 public function getUninstalledModules() {
59 $all = $this->getAllModules();
60 $installed = $this->getInstalledModules();
61 $uninstalled = array();
63 foreach ($all as $module) {
64 if (!in_array($module, $installed)) {
65 $uninstalled[] = $module;
68 return $uninstalled;
71 /**
72 * Returns an array with names of all installed modules.
73 * @return array
75 public function getInstalledModules() {
76 $modules = $this->getAllModules();
77 $installed = array();
78 foreach ($modules as $module) {
79 if ($this->dataModel()->find(array("NAME" => $module)) !== false) {
80 $installed[] = $module;
83 return $installed;
86 /**
87 * Returns an array containing names of both installed and uninstalled modules
88 * @return array
90 public function getAllModules() {
91 $this->dataModel()->populate();
92 $d = dir($this->core()->getSetting("module_dir"));
93 $modules = array();
94 while (false !== ($entry = $d->read())) {
95 $path = $this->core()->getSetting("module_dir").$entry;
96 if (is_dir($path) && $entry != "." && $entry != "..") {
97 $modules[] = $entry;
100 return $modules;
104 * Tries to install $module. If install is successfull true is returned, otherwise an AOOSException is thrown and
105 * false is returned.
106 * @param $module A string representing the module name
107 * @return bool
109 public function installModule($module) {
110 $path = $this->core()->getSetting("module_dir").$module."/".$module.$this->core()->getSetting("extension");
111 require $path;
113 $deps = call_user_func(array($module,"dependencies"));
114 $this->dataModel()->insert(array("NAME" => $module,
115 "DEPS" => implode(",",$deps)));
116 $this->dataModel()->save();
117 print_r($this->core()->modules()->getColumn("NAME"));
119 $m = $this->core()->loadModule($module);
120 $m->dataModel()->create();
122 return true;
126 * Removes a module from the database. If the module is successfully removed true is returned. Otherwise false is
127 * returned. If $module isn't a module in the database an AOOSException is thrown.
128 * @param $module A string representing the module name
129 * @return bool
131 public function uninstallModule($module) {
132 return true;