source: trunk/Modules/System/System.php@ 523

Last change on this file since 523 was 523, checked in by chronos, 12 years ago
  • Upraveno: Záznam akcí Log předělán na aplikační modul.
  • Přidáno: Modul System a zobrazení seznamu dostupných modulů.
File size: 11.8 KB
Line 
1<?php
2
3class PageModules extends Page
4{
5 function __construct()
6 {
7 parent::__construct();
8 $this->FullTitle = 'Správa modulů';
9 $this->ShortTitle = 'Moduly';
10 $this->ParentClass = 'PagePortal';
11 }
12
13 function Show()
14 {
15 $Output = '';
16 if(array_key_exists('A', $_GET))
17 {
18 if($_GET['A'] == 'SaveToDb')
19 {
20 $this->System->ModuleManager->Modules['System']->SaveToDatabase();
21 } else
22 if($_GET['A'] == 'Install')
23 {
24 $this->System->ModuleManager->LoadModules(false);
25 $ModuleName = $this->System->ModuleManager->SearchModuleById($_GET['Id']);
26 if($ModuleName != '')
27 {
28 $this->System->Modules[$ModuleName]->Install();
29 $this->System->ModuleManager->Init();
30 } else $Output .= 'Modul id '.$_GET['Id'].' nenalezen';
31
32 }
33 if($_GET['A'] == 'Uninstall')
34 {
35 $ModuleName = $this->System->ModuleManager->SearchModuleById($_GET['Id']);
36 if($ModuleName != '')
37 {
38 $this->System->ModuleManager->Modules[$ModuleName]->UnInstall();
39 $this->System->ModuleManager->Init();
40 } else $Output .= 'Modul id '.$_GET['Id'].' nenalezen';
41 }
42 }
43 $DbResult = $this->Database->query('SELECT COUNT(*) FROM `SystemModule`');
44 $DbRow = $DbResult->fetch_row();
45 $PageList = GetPageList($DbRow[0]);
46
47 $Output = $PageList['Output'];
48 $Output .= '<table class="WideTable" style="font-size: small;">';
49
50 $TableColumns = array(
51 array('Name' => 'Name', 'Title' => 'Jméno'),
52 array('Name' => 'Creator', 'Title' => 'Tvůrce'),
53 array('Name' => 'Version', 'Title' => 'Verze'),
54 array('Name' => 'License', 'Title' => 'Licence'),
55 array('Name' => 'Installed', 'Title' => 'Instalováno'),
56 array('Name' => 'Description', 'Title' => 'Popis'),
57 array('Name' => 'Dependencies', 'Title' => 'Závislosti'),
58 array('Name' => '', 'Title' => 'Akce'),
59 );
60 $Order = GetOrderTableHeader($TableColumns, 'Name', 0);
61 $Output .= $Order['Output'];
62 $Query = 'SELECT *, (SELECT GROUP_CONCAT(`T1`.`Name` SEPARATOR ", ") FROM `SystemModuleDependency` '.
63 'LEFT JOIN `SystemModule` AS `T1` ON `T1`.`Id` = `SystemModuleDependency`.`DependencyModule` '.
64 'WHERE `SystemModuleDependency`.`Module` = `SystemModule`.`Id`) AS `Dependencies` '.
65 'FROM `SystemModule` '.$Order['SQL'].$PageList['SQLLimit'];
66
67 $DbResult = $this->Database->query($Query);
68 while($Module = $DbResult->fetch_assoc())
69 {
70 if($Module['Dependencies'] != '') $Dependencies = $Module['Dependencies'];
71 else $Module['Dependencies'] = '&nbsp;';
72 if($Module['Installed'] == 1) $Installed = 'Ano';
73 else $Installed = 'Ne';
74 if($Module['Installed'] == 1) $Actions = '<a href="?A=Uninstall&nbsp;Id='.$Module['Id'].'">Odinstalovat</a>';
75 else $Actions = '<a href="?A=Install&nbsp;Id='.$Module['Id'].'">Instalovat</a>';
76 $Output .= '<tr><td>'.$Module['Name'].'</td>'.
77 '<td>'.$Module['Creator'].'</td>'.
78 '<td>'.$Module['Version'].'</td>'.
79 '<td>'.$Module['License'].'</td>'.
80 '<td>'.$Installed.'</td>'.
81 '<td>'.$Module['Description'].'</td>'.
82 '<td>'.$Dependencies.'</td>'.
83 '<td>'.$Actions.'</td></tr>';
84 }
85 $Output .= '</table>';
86 $Output .= $PageList['Output'];
87 $Output .= '<p><a href="?A=SaveToDb">Uložit do databáze</a></p>';
88 return($Output);
89 }
90}
91
92
93class ModuleSystem extends AppModule
94{
95 var $InstalledChecked;
96
97 function __construct($System)
98 {
99 parent::__construct($System);
100 $this->Name = 'System';
101 $this->Version = '1.0';
102 $this->Creator = 'Chronos';
103 $this->License = 'GNU/GPL';
104 $this->Description = 'Base system module';
105 $this->Dependencies = array();
106 }
107
108 function Install()
109 {
110 if($this->IsInstalled()) return;
111 parent::Install();
112 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
113 `Id` int(11) NOT NULL AUTO_INCREMENT,
114 `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
115 `Description` datetime NOT NULL,
116 PRIMARY KEY (`Id`)
117) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
118 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModule` (
119 `Id` int(11) NOT NULL AUTO_INCREMENT,
120 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
121 `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,
122 `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
123 `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,
124 `Installed` int(11) NOT NULL,
125 `Description` text COLLATE utf8_czech_ci NOT NULL,
126 PRIMARY KEY (`Id`)
127) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
128
129 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModuleDependency` (
130 `Id` int(11) NOT NULL AUTO_INCREMENT,
131 `Module` int(11) NOT NULL,
132 `DependencyModule` int(11) NOT NULL,
133 PRIMARY KEY (`Id`),
134 KEY (`Module`),
135 KEY (`DependencyModule`),
136 UNIQUE (`Module` , `DependencyModule`)
137) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
138 $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
139 $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_2` FOREIGN KEY ( `DependencyModule` ) REFERENCES `SystemModule` (`Id`)');
140
141 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModel` (
142 `Id` int(11) NOT NULL AUTO_INCREMENT,
143 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
144 `Module` int(11) NOT NULL,
145 KEY (`Module`),
146 PRIMARY KEY (`Id`)
147) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
148 $this->Database->query('ALTER TABLE `SystemModel` ADD CONSTRAINT `SystemModel_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
149
150 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModelProperty` (
151 `Id` int(11) NOT NULL AUTO_INCREMENT,
152 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
153 `Type` varchar(255) COLLATE utf8_czech_ci NOT NULL,
154 `Model` int(11) NOT NULL,
155 KEY (`Model`),
156 PRIMARY KEY (`Id`)
157) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
158 $this->Database->query('ALTER TABLE `SystemModelProperty` ADD CONSTRAINT `SystemModelProperty_ibfk_1` FOREIGN KEY ( `Model` ) REFERENCES `SystemModel` (`Id`)');
159 }
160
161 function UnInstall()
162 {
163 parent::UnInstall();
164 if(!$this->IsInstalled()) return;
165
166 // Delete tables with reverse order
167 $this->Database->query('ALTER TABLE `SystemModelProperty` DROP FOREIGN KEY `SystemModelProperty_ibfk_1`');
168 $this->Database->query('DROP TABLE IF EXISTS `SystemModelProperty`');
169 $this->Database->query('ALTER TABLE `SystemModel` DROP FOREIGN KEY `SystemModel_ibfk_1`');
170 $this->Database->query('DROP TABLE IF EXISTS `SystemModel`');
171 $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_1`');
172 $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_2`');
173 $this->Database->query('DROP TABLE IF EXISTS `SystemModuleDependency`');
174 $this->Database->query('DROP TABLE IF EXISTS `SystemModule`');
175 $this->Database->query('DROP TABLE IF EXISTS `SystemVersion`');
176 }
177
178 function Start()
179 {
180 parent::Start();
181 $this->System->RegisterPage('module', 'PageModules');
182 //$this->Manager->OnModuleChange = array($this, 'ModuleChange');
183 //$this->LoadFromDatabase();
184 }
185
186 function Stop()
187 {
188 parent::Stop();
189 }
190
191 function IsInstalled()
192 {
193 if($this->InstalledChecked == false)
194 {
195 $DbResult = $this->Database->query('SELECT table_name FROM information_schema.tables
196WHERE table_schema = "'.$this->Database->Database.'" AND table_name = "SystemVersion";');
197 if($DbResult->num_rows > 0) $this->Installed = true;
198 else $this->Installed = false;
199 $this->InstalledChecked = true;
200 }
201 return($this->Installed);
202 }
203
204 function ModuleChange($Module)
205 {
206 //if($this->IsInstalled())
207 {
208
209 if($Module->IsInstalled()) $Installed = 1;
210 else $Installed = 0;
211 $this->Database->query('UPDATE `SystemModule` SET `Installed`=1 WHERE `Name`="'.$Module->Name.'"');
212 }
213 }
214
215 function LoadFromDatabase()
216 {
217 //DebugLog('Loading modules...');
218 $this->Modules = array();
219 $Query = 'SELECT `Id`, `Name`,`Installed` FROM `SystemModule`';
220 $DbResult = $this->Database->query($Query);
221 while($Module = $DbResult->fetch_array())
222 {
223 //echo($Module['Name'].',');
224 include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
225 $ModuleClassName = 'Module'.$Module['Name'];
226 $NewModule = new $ModuleClassName($this->Database, $this->Manager);
227 $NewModule->Id = $Module['Id'];
228 $NewModule->Installed = $Module['Installed'];
229 $this->Manager->RegisterModule($NewModule);
230 }
231 }
232
233 function SaveToDatabase()
234 {
235 $Modules = array();
236 $DbResult = $this->Database->query('SELECT * FROM `SystemModule`');
237 while($DbRow = $DbResult->fetch_assoc())
238 {
239 $Modules[$DbRow['Name']] = $DbRow;
240 if($this->System->ModuleManager->ModulePresent($DbRow['Name']))
241 $this->System->ModuleManager->Modules[$DbRow['Name']]->Id = $DbRow['Id'];
242 }
243
244 // Add missing
245 foreach($this->System->ModuleManager->Modules as $Module)
246 {
247 if(!array_key_exists($Module->Name, $Modules))
248 {
249 $this->Database->insert('SystemModule', array('Name' => $Module->Name,
250 'Version' => $Module->Version, 'Creator' => $Module->Creator,
251 'Description' => $Module->Description, 'License' => $Module->License,
252 'Installed' => $Module->Installed));
253 $this->System->ModuleManager->Modules[$Module->Name]->Id = $this->Database->insert_id;
254 }
255 else $this->Database->update('SystemModule', 'Name = "'.$Module->Name.'"', array(
256 'Version' => $Module->Version, 'Creator' => $Module->Creator,
257 'Description' => $Module->Description, 'License' => $Module->License,
258 'Installed' => $Module->Installed));
259 echo($this->Database->LastQuery."\n");
260 }
261
262 // Remove exceeding
263 foreach($Modules as $Module)
264 if(!$this->System->ModuleManager->ModulePresent($Module['Name']))
265 {
266 DebugLog('Removing module '.$Module['Name'].' from list');
267 $this->Database->query('DELETE FROM `SystemModule` WHERE `Id` = '.$Module['Id']);
268 }
269
270 // Reload dependencies
271 $DbDependency = array();
272 $DbResult = $this->Database->query('SELECT * FROM `SystemModuleDependency`');
273 while($DbRow = $DbResult->fetch_assoc())
274 $DbDependency[$DbRow['Module']][] = $DbRow['DependencyModule'];
275
276 foreach($this->System->ModuleManager->Modules as $Module)
277 {
278 // Add missing
279 foreach($Module->Dependencies as $Dependency)
280 {
281 if(!array_key_exists($Module->Id, $DbDependency) or
282 !in_array($this->System->ModuleManager->Modules[$Dependency]->Id, $DbDependency[$Module->Id]))
283 {
284 if(array_key_exists($Dependency, $this->System->ModuleManager->Modules))
285 $DependencyId = $this->System->ModuleManager->Modules[$Dependency]->Id;
286 else throw new Exception('Dependent module '.$Dependency.' not found');
287 $this->Database->insert('SystemModuleDependency', array('Module' => $Module->Id,
288 'DependencyModule' => $DependencyId));
289 }
290 }
291
292 // Remove exceeding
293 if(array_key_exists($Module->Id, $DbDependency))
294 foreach($DbDependency[$Module->Id] as $Dep)
295 {
296 $DepModName = $this->System->ModuleManager->SearchModuleById($Dep);
297 if(!in_array($DepModName, $Module->Dependencies))
298 $this->Database->query('DELETE FROM `SystemModuleDependency` WHERE `Module` = '.
299 $Module->Id.' AND DependencyModule='.$Dep);
300 }
301 }
302 }
303}
304
305?>
Note: See TracBrowser for help on using the repository browser.