Changeset 888 for trunk/Packages/Common/Modules
- Timestamp:
- Dec 27, 2022, 7:50:23 PM (2 years ago)
- Location:
- trunk/Packages/Common/Modules
- Files:
-
- 2 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Modules/Setup.php
r887 r888 1 1 <?php 2 3 class ModuleSetup extends Module 4 { 5 public UpdateManager $UpdateManager; 6 7 function __construct(System $System) 8 { 9 global $DatabaseRevision; 10 11 parent::__construct($System); 12 $this->Name = 'Setup'; 13 $this->Version = '1.0'; 14 $this->Creator = 'Chronos'; 15 $this->License = 'GNU/GPLv3'; 16 $this->Description = 'Base setup module'; 17 $this->Revision = 1; 18 $this->Type = ModuleType::System; 19 20 // Check database persistence structure 21 $this->UpdateManager = new UpdateManager(); 22 $this->UpdateManager->Database = &$this->Database; 23 $this->UpdateManager->Revision = $DatabaseRevision; 24 $this->UpdateManager->InstallMethod = 'FullInstall'; 25 } 26 27 static function Cast(Module $Module): ModuleSetup 28 { 29 if ($Module instanceof ModuleSetup) 30 { 31 return $Module; 32 } 33 throw new Exception('Expected ModuleSetup type but '.gettype($Module)); 34 } 35 36 function DoStart(): void 37 { 38 Core::Cast($this->System)->RegisterPage([''], 'PageSetupRedirect'); 39 Core::Cast($this->System)->RegisterPage(['setup'], 'PageSetup'); 40 } 41 42 function DoStop(): void 43 { 44 unset($this->UpdateManager); 45 Core::Cast($this->System)->UnregisterPage(['']); 46 Core::Cast($this->System)->UnregisterPage(['setup']); 47 } 48 49 function CheckState(): bool 50 { 51 return $this->Database->Connected() and $this->UpdateManager->IsInstalled() and 52 $this->UpdateManager->IsUpToDate(); 53 } 54 55 function DoUpgrade(): string 56 { 57 $Updates = new Updates(); 58 $this->UpdateManager->Trace = $Updates->Get(); 59 $Output = $this->UpdateManager->Upgrade(); 60 return $Output; 61 } 62 } 2 63 3 64 class PageSetup extends Page 4 65 { 5 var $UpdateManager;6 var$ConfigDefinition;7 var$Config;8 var$DatabaseRevision;9 var$Revision;10 var $Updates;11 var $ConfigDir;12 13 function __construct( $System)66 public UpdateManager $UpdateManager; 67 public array $ConfigDefinition; 68 public array $Config; 69 public int $DatabaseRevision; 70 public int $Revision; 71 public string $ConfigDir; 72 public array $YesNo; 73 74 function __construct(System $System) 14 75 { 15 76 parent::__construct($System); 16 77 $this->Title = T('Application setup'); 17 //$this->ParentClass = 'Page Portal';78 //$this->ParentClass = 'PageSetupRedirect'; 18 79 $this->ConfigDir = dirname(dirname(dirname(__FILE__))).'/Config'; 19 80 $this->YesNo = array(false => T('No'), true => T('Yes')); 20 81 } 21 82 22 function LoginPanel() 83 function LoginPanel(): string 23 84 { 24 85 $Output = '<h3>Přihlášení k instalaci</h3>'. … … 32 93 } 33 94 34 function ControlPanel() 95 function ControlPanel(): string 35 96 { 36 97 $Output = '<h3>'.T('Instance management').'</h3>'; … … 49 110 $Output .= '<a href="?action=upgrade">'.T('Upgrade').'</a> '; 50 111 $Output .= '<a href="?action=insert_sample_data">Vložit vzorová data</a> '; 51 $Output .= '<a href="?action=reload _modules">Obnovit seznam modulů</a> ';112 $Output .= '<a href="?action=reload-modules">Obnovit seznam modulů</a> '; 52 113 $Output .= '<a href="?action=uninstall">Odinstalovat</a> '; 53 $Output .= '<a href=" ?action=modules">Správa modulů</a> ';114 $Output .= '<a href="'.$this->System->Link('/modules/').'">Správa modulů</a> '; 54 115 $Output .= '<a href="?action=models">Přegenerovat modely</a> '; 55 116 } else $Output .= '<a href="?action=install">Instalovat</a> '; … … 62 123 } 63 124 64 function Show() 125 function Show(): string 65 126 { 66 127 global $ConfigDefinition, $DatabaseRevision, $Config, $Updates; 67 128 68 $this->UpdateManager = $this->System->Setup->UpdateManager;129 $this->UpdateManager = ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager; 69 130 $DefaultConfig = new DefaultConfig(); 70 131 $this->ConfigDefinition = $DefaultConfig->Get(); … … 89 150 $Output .= 'Odhlášen'; 90 151 $Output .= $this->LoginPanel(); 91 } else92 if ($Action == 'models')152 } 153 else if ($Action == 'models') 93 154 { 94 155 $this->System->FormManager->UpdateSQLMeta(); 95 } else96 if ($Action == 'upgrade')156 } 157 else if ($Action == 'upgrade') 97 158 { 98 159 $Output .= '<h3>Povýšení</h3>'; 99 try { 100 $Output .= $this->System->Setup->Upgrade(); 101 } catch (Exception $E) { 160 try 161 { 162 $Output .= ModuleSetup::Cast($this->System->GetModule('Setup'))->DoUpgrade(); 163 $this->System->ModuleManager->UpgradeAll(array(ModuleCondition::System)); 164 } catch (Exception $E) 165 { 102 166 $Output .= $this->SystemMessage('Chyba aktualizace', 103 167 'Došlo k chybě v SQL dotazu při aktualizaci: <br/>'.$E->getMessage()); 104 168 } 105 169 $Output .= $this->ControlPanel(); 106 } else 107 if ($Action == 'install') 108 { 109 $Output .= '<h3>Instalace</h3>'; 110 $this->System->Setup->Install(); 170 } 171 else if ($Action == 'install') 172 { 173 $Output .= '<h3>Instalace systém</h3>'; 174 global $DatabaseRevision; 175 176 $this->Database->query("CREATE TABLE IF NOT EXISTS `".$this->UpdateManager->VersionTable."` (". 177 '`Id` int(11) NOT NULL AUTO_INCREMENT, '. 178 '`Revision` int(11) NOT NULL, '. 179 'PRIMARY KEY (`Id`) '. 180 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'); 181 $DbResult = $this->Database->select($this->UpdateManager->VersionTable, 'Id'); 182 if ($DbResult->num_rows == 0) 183 { 184 $this->Database->query("INSERT INTO `".$this->UpdateManager->VersionTable. 185 "` (`Id`, `Revision`) VALUES (1, ".$DatabaseRevision.");"); 186 } 187 $this->System->ModuleManager->InstallAll(array(ModuleCondition::System)); 111 188 $this->System->ModuleManager->LoadModules(); 112 189 $this->System->ModuleManager->SaveState(); 113 //$Output .= $this->System->Setup->Upgrade(); 114 $Output .= $this->ControlPanel(); 115 } else 116 if ($Action == 'uninstall') 117 { 118 $Output .= '<h3>Odinstalace</h3>'; 119 $this->System->Setup->Uninstall(); 120 $Output .= $this->ControlPanel(); 121 } else 122 if ($Action == 'reload_modules') 190 //$Output .= ModuleSetup::Cast($this->System->GetModule('Setup'))->Upgrade(); 191 $Output .= $this->ControlPanel(); 192 } 193 else if ($Action == 'uninstall') 194 { 195 $Output .= '<h3>Odinstalace vše</h3>'; 196 $this->System->ModuleManager->UninstallAll(array(ModuleCondition::All)); 197 $this->Database->query('DROP TABLE IF EXISTS `'.$this->UpdateManager->VersionTable.'`'); 198 $Output .= $this->ControlPanel(); 199 } 200 else if ($Action == 'reload-modules') 123 201 { 124 202 $Output .= '<h3>Znovunačtení seznamu modulů</h3>'; … … 126 204 $this->System->ModuleManager->SaveState(); 127 205 $Output .= $this->ControlPanel(); 128 } else129 if ($Action == 'insert_sample_data')206 } 207 else if ($Action == 'insert_sample_data') 130 208 { 131 209 $Output .= '<h3>Vložení vzorových dat</h3>'; 132 $this->System->Setup->InsertSampleData(); 133 $Output .= $this->ControlPanel(); 134 } else 135 if ($Action == 'modules') 136 { 137 $Output .= $this->ShowModules(); 138 } else 139 if ($Action == 'configure_save') 210 $this->System->ModuleManager->Perform(array(ModuleAction::InsertSampleData), array(ModuleCondition::Installed)); 211 $Output .= $this->ControlPanel(); 212 } 213 else if ($Action == 'configure_save') 140 214 { 141 215 $Output .= $this->ConfigSave($this->Config); 142 216 $Output .= $this->ControlPanel(); 143 } else144 if ($Action == 'configure')217 } 218 else if ($Action == 'configure') 145 219 { 146 220 $Output .= $this->PrepareConfig($this->Config); 147 } else 221 } 222 else 148 223 { 149 224 $Output .= $this->ControlPanel(); … … 163 238 } 164 239 165 function ShowModules() 166 { 167 $Output = ''; 168 if (array_key_exists('op', $_GET)) $Operation = $_GET['op']; 169 else $Operation = ''; 170 if ($Operation == 'install') 171 { 172 $this->System->ModuleManager->Modules[$_GET['name']]->Install(); 173 $this->System->ModuleManager->SaveState(); 174 $Output .= 'Modul '.$_GET['name'].' instalován<br/>'; 175 } else 176 if ($Operation == 'uninstall') 177 { 178 $this->System->ModuleManager->Modules[$_GET['name']]->Uninstall(); 179 $this->System->ModuleManager->SaveState(); 180 $Output .= 'Modul '.$_GET['name'].' odinstalován<br/>'; 181 } else 182 if ($Operation == 'enable') 183 { 184 $this->System->ModuleManager->Modules[$_GET['name']]->Enable(); 185 $this->System->ModuleManager->SaveState(); 186 $Output .= 'Modul '.$_GET['name'].' povolen<br/>'; 187 } else 188 if ($Operation == 'disable') 189 { 190 $this->System->ModuleManager->Modules[$_GET['name']]->Disable(); 191 $this->System->ModuleManager->SaveState(); 192 $Output .= 'Modul '.$_GET['name'].' zakázán<br/>'; 193 } else 194 if ($Operation == 'upgrade') 195 { 196 $this->System->ModuleManager->Modules[$_GET['name']]->Upgrade(); 197 $this->System->ModuleManager->SaveState(); 198 $Output .= 'Modul '.$_GET['name'].' povýšen<br/>'; 199 } 200 $Output .= '<h3>Správa modulů</h3>'; 201 $Output .= $this->ShowList(); 202 return $Output; 203 } 204 205 function ShowList() 206 { 207 $Output = ''; 208 209 $Pageing = new Paging(); 210 $Pageing->TotalCount = count($this->System->ModuleManager->Modules); 211 $Table = new VisualTable(); 212 $Table->SetColumns(array( 213 array('Name' => 'Name', 'Title' => 'Jméno'), 214 array('Name' => 'Creator', 'Title' => 'Tvůrce'), 215 array('Name' => 'Version', 'Title' => 'Verze'), 216 array('Name' => 'License', 'Title' => 'Licence'), 217 array('Name' => 'Installed', 'Title' => 'Instalováno'), 218 array('Name' => 'Enabled', 'Title' => 'Povoleno'), 219 array('Name' => 'Description', 'Title' => 'Popis'), 220 array('Name' => 'Dependencies', 'Title' => 'Závislosti'), 221 array('Name' => '', 'Title' => 'Akce'), 222 )); 223 foreach ($this->System->ModuleManager->Modules as $Module) 224 { 225 if (($Module->Dependencies) > 0) $Dependencies = implode(',', $Module->Dependencies); 226 else $Dependencies = ' '; 227 $Actions = ''; 228 if ($Module->Installed == true) 229 { 230 $Actions .= ' <a href="?action=modules&op=uninstall&name='.$Module->Name.'">Odinstalovat</a>'; 231 if ($Module->Enabled == true) $Actions .= ' <a href="?action=modules&op=disable&name='.$Module->Name.'">Zakázat</a>'; 232 else $Actions .= ' <a href="?action=modules&op=enable&name='.$Module->Name.'">Povolit</a>'; 233 if ($Module->InstalledVersion != $Module->Version) $Actions .= ' <a href="?action=modules&op=upgrade&name='.$Module->Name.'">Povýšit</a>'; 234 } else $Actions .= ' <a href="?action=modules&op=install&name='.$Module->Name.'">Instalovat</a>'; 235 236 $Table->Table->Cells[] = array($Module->Name, 237 $Module->Creator, $Module->Version, 238 $Module->License, $this->YesNo[$Module->Installed], 239 $this->YesNo[$Module->Enabled], $Module->Description, 240 $Dependencies, $Actions); 241 } 242 $Output .= $Pageing->Show(); 243 $Output .= $Table->Show(); 244 $Output .= $Pageing->Show(); 245 //$Output .= '<p><a href="?A=SaveToDb">Uložit do databáze</a></p>'; 246 return $Output; 247 } 248 249 function PrepareConfig($Config) 240 function PrepareConfig($Config): string 250 241 { 251 242 $Output = ''; … … 255 246 $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože soubor "'.$this->ConfigDir.'/Config.php" není povolen pro zápis!'; 256 247 $Output .= '<h3>Nastavení systému</h3>'. 257 258 248 '<form action="?action=configure_save" method="post">'. 249 '<table>'; 259 250 foreach ($this->ConfigDefinition as $Def) 260 251 { … … 278 269 } 279 270 $Output .= '</td></tr>'. 280 281 282 271 '<tr><td colspan="2"><input type="submit" name="configure_save" value="'.T('Save').'"/></td></tr>'. 272 '</table>'. 273 '</form>'; 283 274 return $Output; 284 275 } … … 322 313 } 323 314 324 function CreateConfig($Config) 315 function CreateConfig($Config): string 325 316 { 326 317 $Output = "<?php\n\n". … … 359 350 class PageSetupRedirect extends Page 360 351 { 361 function Show() 352 function Show(): string 362 353 { 363 354 $Output = ''; 364 if (!$this->Database->Connected()) $Output .= T('Can\'t connect to database').'<br>'; 365 else { 366 if (!$this->System->Setup->UpdateManager->IsInstalled()) 367 $Output .= T('System requires database initialization').'<br>'; 368 else 369 if (!$this->System->Setup->UpdateManager->IsUpToDate()) 370 $Output .= T('System requires database upgrade').'<br>'; 371 } 372 $Output .= sprintf(T('Front page was not configured. Continue to %s'), '<a href="'.$this->System->Link('/setup/').'">'.T('setup').'</a>'); 355 if (!$this->Database->Connected()) 356 { 357 $Output .= T('Can\'t connect to database.').'<br>'; 358 } else 359 { 360 if (!ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager->IsInstalled()) 361 { 362 $Output .= T('System requires database initialization.').'<br>'; 363 } else 364 if (!ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager->IsUpToDate()) 365 { 366 $Output .= T('System requires database upgrade.').'<br>'; 367 } 368 } 369 $Output .= sprintf(T('Front page was not configured. Continue to %s.'), '<a href="'.$this->System->Link('/setup/').'">'.T('setup').'</a>'); 373 370 return $Output; 374 371 } 375 372 } 376 377 class Setup extends Model378 {379 var $UpdateManager;380 381 function Start()382 {383 global $DatabaseRevision;384 385 $this->System->RegisterPage('', 'PageSetupRedirect');386 $this->System->RegisterPage('setup', 'PageSetup');387 388 // Check database persistence structure389 $this->UpdateManager = new UpdateManager();390 $this->UpdateManager->Database = &$this->Database;391 $this->UpdateManager->Revision = $DatabaseRevision;392 $this->UpdateManager->InstallMethod = 'FullInstall';393 }394 395 function Stop()396 {397 unset($this->UpdateManager);398 $this->System->UnregisterPage('');399 $this->System->UnregisterPage('setup');400 }401 402 function CheckState()403 {404 return $this->Database->Connected() and $this->UpdateManager->IsInstalled() and405 $this->UpdateManager->IsUpToDate();406 }407 408 function Install()409 {410 global $DatabaseRevision;411 412 $this->Database->query('CREATE TABLE IF NOT EXISTS `'.$this->UpdateManager->VersionTable.'` (413 `Id` int(11) NOT NULL AUTO_INCREMENT,414 `Revision` int(11) NOT NULL,415 PRIMARY KEY (`Id`)416 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');417 $this->Database->query('INSERT INTO `'.$this->UpdateManager->VersionTable.'` (`Id`, `Revision`) VALUES418 (1, '.$DatabaseRevision.');');419 $this->Database->query("CREATE TABLE IF NOT EXISTS `Module` (420 `Id` int(11) NOT NULL AUTO_INCREMENT,421 `Name` varchar(255) NOT NULL,422 `Title` varchar(255) NOT NULL,423 PRIMARY KEY (`Id`)424 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");425 }426 427 function Uninstall()428 {429 $this->System->ModuleManager->UninstallAll();430 $this->Database->query('DROP TABLE `Module`');431 $this->Database->query('DROP TABLE `'.$this->UpdateManager->VersionTable.'`');432 }433 434 function IsInstalled()435 {436 $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->UpdateManager->VersionTable.'"');437 return $DbResult->num_rows > 0;438 }439 440 function Upgrade()441 {442 $Updates = new Updates();443 $this->UpdateManager->Trace = $Updates->Get();444 $Output = $this->UpdateManager->Upgrade();445 return $Output;446 }447 }
Note:
See TracChangeset
for help on using the changeset viewer.