1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/Model.php');
|
---|
4 |
|
---|
5 | class Module
|
---|
6 | {
|
---|
7 | var $Id;
|
---|
8 | var $Name;
|
---|
9 | var $Version;
|
---|
10 | var $License;
|
---|
11 | var $Creator;
|
---|
12 | var $Description;
|
---|
13 | var $Dependencies;
|
---|
14 | var $Models = array();
|
---|
15 | var $SupportedModels = array();
|
---|
16 | var $Database;
|
---|
17 | var $Installed;
|
---|
18 | var $Initialized;
|
---|
19 | var $System;
|
---|
20 |
|
---|
21 | function __construct($Database, $System)
|
---|
22 | {
|
---|
23 | $this->Database = &$Database;
|
---|
24 | $this->System = &$System;
|
---|
25 | $this->Initialized = false;
|
---|
26 | $this->Installed = false;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function Install()
|
---|
30 | {
|
---|
31 | if($this->Installed) return;
|
---|
32 | DebugLog('Installing module '.$this->Name.'...');
|
---|
33 | $this->Installed = true;
|
---|
34 |
|
---|
35 | // Install dependencies first
|
---|
36 | foreach($this->Dependencies as $Dependency)
|
---|
37 | $this->System->Modules[$Dependency]->Install();
|
---|
38 |
|
---|
39 | $this->LoadModels();
|
---|
40 | foreach($this->Models as $Index => $Module)
|
---|
41 | {
|
---|
42 | $this->Models[$Index]->Install();
|
---|
43 | }
|
---|
44 | $this->Database->query('UPDATE SystemModule SET Installed=1 WHERE Name="'.$this->Name.'"');
|
---|
45 | }
|
---|
46 |
|
---|
47 | function UnInstall()
|
---|
48 | {
|
---|
49 | DebugLog('Uninstalling module '.$this->Name.'...');
|
---|
50 | $this->Installed = false;
|
---|
51 |
|
---|
52 | // Remove dependent modules first
|
---|
53 | foreach($this->System->Modules as $Module)
|
---|
54 | foreach($Module->Dependencies as $Dependency)
|
---|
55 | if(($Dependency == $this->Name) and ($Module->Installed)) $this->System->Modules[$Module->Name]->UnInstall();
|
---|
56 |
|
---|
57 | $this->LoadModels();
|
---|
58 | foreach(array_reverse($this->Models, true) as $Index => $Model)
|
---|
59 | {
|
---|
60 | $this->Models[$Index]->UnInstall();
|
---|
61 | }
|
---|
62 | $this->Database->query('UPDATE SystemModule SET Installed=0 WHERE Name="'.$this->Name.'"');
|
---|
63 | }
|
---|
64 |
|
---|
65 | function Init()
|
---|
66 | {
|
---|
67 | if($this->Initialized) return;
|
---|
68 | $this->Initialized = true;
|
---|
69 | foreach($this->Dependencies as $Dependency)
|
---|
70 | $this->System->Modules[$Dependency]->Init();
|
---|
71 | }
|
---|
72 |
|
---|
73 | function LoadModels()
|
---|
74 | {
|
---|
75 | $this->Models = array();
|
---|
76 | foreach($this->SupportedModels as $ModelName)
|
---|
77 | {
|
---|
78 | $NewModel = new $ModelName($this->Database, $this->System);
|
---|
79 | $NewModel->Module = &$this;
|
---|
80 | $this->Models[$ModelName] = $NewModel;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | class ModularSystem
|
---|
86 | {
|
---|
87 | var $Database;
|
---|
88 | var $Modules = array();
|
---|
89 | var $Models = array();
|
---|
90 | var $Menu = array();
|
---|
91 |
|
---|
92 | function __construct($Database)
|
---|
93 | {
|
---|
94 | $this->Database = &$Database;
|
---|
95 | }
|
---|
96 |
|
---|
97 | function ModulePresent($Name)
|
---|
98 | {
|
---|
99 | return(array_key_exists($Name, $this->Modules));
|
---|
100 | }
|
---|
101 |
|
---|
102 | function LoadModules($Installed = true)
|
---|
103 | {
|
---|
104 | //DebugLog('Loading modules...');
|
---|
105 | $this->Modules = array();
|
---|
106 | $Query = 'SELECT `Id`, `Name`,`Installed` FROM `SystemModule`';
|
---|
107 | if($Installed) $Query .= ' WHERE `Installed`=1';
|
---|
108 | else $Query .= ''; // WHERE `Installed`=0';
|
---|
109 | $DbResult = $this->Database->query($Query);
|
---|
110 | while($Module = $DbResult->fetch_array())
|
---|
111 | {
|
---|
112 | //echo($Module['Name'].',');
|
---|
113 | include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
|
---|
114 | $ModuleClassName = 'Module'.$Module['Name'];
|
---|
115 | $NewModule = new $ModuleClassName($this->Database, $this);
|
---|
116 | $NewModule->Id = $Module['Id'];
|
---|
117 | $NewModule->Installed = $Module['Installed'];
|
---|
118 | $this->Modules[$Module['Name']] = $NewModule;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | function Init()
|
---|
123 | {
|
---|
124 | $this->LoadModules();
|
---|
125 | foreach($this->Modules as $Index => $Module)
|
---|
126 | {
|
---|
127 | //DebugLog('Init module '.$Module->Name);
|
---|
128 | $this->Modules[$Index]->Init();
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | function Install()
|
---|
133 | {
|
---|
134 | //DebugLog('Installing modular system core...');
|
---|
135 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
|
---|
136 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
137 | `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
138 | `Description` datetime NOT NULL,
|
---|
139 | PRIMARY KEY (`Id`)
|
---|
140 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
|
---|
141 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModule` (
|
---|
142 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
143 | `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
144 | `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
145 | `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
146 | `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
147 | `Installed` int(11) NOT NULL,
|
---|
148 | `Description` text COLLATE utf8_czech_ci NOT NULL,
|
---|
149 | PRIMARY KEY (`Id`)
|
---|
150 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
|
---|
151 |
|
---|
152 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModuleDependency` (
|
---|
153 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
154 | `Module` int(11) NOT NULL,
|
---|
155 | `DependencyModule` int(11) NOT NULL,
|
---|
156 | PRIMARY KEY (`Id`),
|
---|
157 | KEY (`Module`),
|
---|
158 | KEY (`DependencyModule`),
|
---|
159 | UNIQUE (`Module` , `DependencyModule`)
|
---|
160 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
|
---|
161 | $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
|
---|
162 | $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_2` FOREIGN KEY ( `DependencyModule` ) REFERENCES `SystemModule` (`Id`)');
|
---|
163 |
|
---|
164 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModel` (
|
---|
165 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
166 | `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
167 | `Module` int(11) NOT NULL,
|
---|
168 | KEY (`Module`),
|
---|
169 | PRIMARY KEY (`Id`)
|
---|
170 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
|
---|
171 | $this->Database->query('ALTER TABLE `SystemModel` ADD CONSTRAINT `SystemModel_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
|
---|
172 |
|
---|
173 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModelProperty` (
|
---|
174 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
175 | `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
176 | `Type` varchar(255) COLLATE utf8_czech_ci NOT NULL,
|
---|
177 | `Model` int(11) NOT NULL,
|
---|
178 | KEY (`Model`),
|
---|
179 | PRIMARY KEY (`Id`)
|
---|
180 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
|
---|
181 | $this->Database->query('ALTER TABLE `SystemModelProperty` ADD CONSTRAINT `SystemModelProperty_ibfk_1` FOREIGN KEY ( `Model` ) REFERENCES `SystemModel` (`Id`)');
|
---|
182 |
|
---|
183 | $this->ReloadList();
|
---|
184 | $this->LoadModules(false);
|
---|
185 | $this->Modules['System']->Install();
|
---|
186 | /*foreach($this->Modules as $Index => $Module)
|
---|
187 | {
|
---|
188 | $this->Modules[$Index]->Install();
|
---|
189 | }*/
|
---|
190 | }
|
---|
191 |
|
---|
192 | function UnInstall()
|
---|
193 | {
|
---|
194 | DebugLog('Uninstalling modular system core...');
|
---|
195 | foreach(array_reverse($this->Modules, true) as $Index => $Module)
|
---|
196 | $this->Modules[$Index]->UnInstall();
|
---|
197 |
|
---|
198 | // Delete tables with reverse order
|
---|
199 | $this->Database->query('ALTER TABLE `SystemModelProperty` DROP FOREIGN KEY `SystemModelProperty_ibfk_1`');
|
---|
200 | $this->Database->query('DROP TABLE IF EXISTS `SystemModelProperty`');
|
---|
201 | $this->Database->query('ALTER TABLE `SystemModel` DROP FOREIGN KEY `SystemModel_ibfk_1`');
|
---|
202 | $this->Database->query('DROP TABLE IF EXISTS `SystemModel`');
|
---|
203 | $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_1`');
|
---|
204 | $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_2`');
|
---|
205 | $this->Database->query('DROP TABLE IF EXISTS `SystemModuleDependency`');
|
---|
206 | $this->Database->query('DROP TABLE IF EXISTS `SystemModule`');
|
---|
207 | $this->Database->query('DROP TABLE IF EXISTS `SystemVersion`');
|
---|
208 | }
|
---|
209 |
|
---|
210 | function IsInstalled()
|
---|
211 | {
|
---|
212 | $DbResult = $this->Database->query('SELECT table_name FROM information_schema.tables
|
---|
213 | WHERE table_schema = "'.$this->Database->Database.'" AND table_name = "SystemVersion";');
|
---|
214 | if($DbResult->num_rows > 0) return(true);
|
---|
215 | else return(false);
|
---|
216 | }
|
---|
217 |
|
---|
218 | function ReloadList()
|
---|
219 | {
|
---|
220 | // Load list of modules from database
|
---|
221 | $Modules = array();
|
---|
222 | $DbResult = $this->Database->query('SELECT * FROM `SystemModule`');
|
---|
223 | while($DbRow = $DbResult->fetch_assoc())
|
---|
224 | $Modules[$DbRow['Name']] = $DbRow;
|
---|
225 |
|
---|
226 | // Load list of modules on disk
|
---|
227 | $ModulesOnDisk = array();
|
---|
228 | $Files = scandir('Modules');
|
---|
229 | foreach($Files as $File)
|
---|
230 | if(is_dir('Modules/'.$File) and ($File != '.') and ($File != '..') and (substr($File, 0, 1) != '.'))
|
---|
231 | {
|
---|
232 | $ModulesOnDisk[] = $File;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Add new
|
---|
236 | foreach($ModulesOnDisk as $ModuleName)
|
---|
237 | if(!array_key_exists($ModuleName, $Modules))
|
---|
238 | {
|
---|
239 | DebugLog('Adding module '.$ModuleName.' to list');
|
---|
240 | include_once('Modules/'.$ModuleName.'/'.$ModuleName.'.php');
|
---|
241 | $ModuleClassName = 'Module'.$ModuleName;
|
---|
242 | if(class_exists($ModuleClassName))
|
---|
243 | {
|
---|
244 | $Module = new $ModuleClassName($this->Database, $this);
|
---|
245 | $this->Database->insert('SystemModule', array('Name' => $Module->Name,
|
---|
246 | 'Version' => $Module->Version, 'Creator' => $Module->Creator,
|
---|
247 | 'Description' => $Module->Description, 'License' => $Module->License,
|
---|
248 | 'Installed' => 0));
|
---|
249 | unset($Module);
|
---|
250 | } else throw new Exception('Missing class '.$ModuleClassName.' in module '.$ModuleName);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // Remove missing
|
---|
254 | foreach($Modules as $Module)
|
---|
255 | if(($Module['Installed'] == 0) and !in_array($Module['Name'], $ModulesOnDisk))
|
---|
256 | {
|
---|
257 | DebugLog('Removing module '.$Module['Name'].' from list');
|
---|
258 | $this->Database->query('DELETE FROM `SystemModule` WHERE `Id` = '.$Module['Id']);
|
---|
259 | }
|
---|
260 |
|
---|
261 | // Reload dependencies
|
---|
262 | $this->LoadModules(false);
|
---|
263 | foreach($this->Modules as $Module)
|
---|
264 | {
|
---|
265 | foreach($Module->Dependencies as $Dependency)
|
---|
266 | {
|
---|
267 | $this->Database->insert('SystemModuleDependency', array('Module' => $Module->Id,
|
---|
268 | 'DependencyModule' => $this->Modules[$Dependency]->Id));
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | function SearchModuleById($Id)
|
---|
274 | {
|
---|
275 | foreach($this->Modules as $Module)
|
---|
276 | {
|
---|
277 | DebugLog($Module->Name.' '.$Module->Id);
|
---|
278 | if($Module->Id == $Id) return($Module->Name);
|
---|
279 | }
|
---|
280 | return('');
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | ?>
|
---|