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

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