| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class ModuleModuleManager extends Module
|
|---|
| 4 | {
|
|---|
| 5 | public UpdateManager $UpdateManager;
|
|---|
| 6 |
|
|---|
| 7 | function __construct(System $System)
|
|---|
| 8 | {
|
|---|
| 9 | parent::__construct($System);
|
|---|
| 10 | $this->Name = 'ModuleManager';
|
|---|
| 11 | $this->Version = '1.0';
|
|---|
| 12 | $this->Creator = 'Chronos';
|
|---|
| 13 | $this->License = 'GNU/GPLv3';
|
|---|
| 14 | $this->Description = 'User interface for module management.';
|
|---|
| 15 | $this->Dependencies = array(ModuleSetup::GetName());
|
|---|
| 16 | $this->Revision = 1;
|
|---|
| 17 | $this->Type = ModuleType::System;
|
|---|
| 18 | $this->Models = array(ModelField::GetClassName(), ModuleDependency::GetClassName());
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function DoStart(): void
|
|---|
| 22 | {
|
|---|
| 23 | $this->Manager->OnInstallModel = array($this, 'InstallModel');
|
|---|
| 24 | $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
|
|---|
| 25 | $this->Manager->OnInstallModule = array($this, 'InstallModule');
|
|---|
| 26 | $this->Manager->OnUninstallModule = array($this, 'UninstallModule');
|
|---|
| 27 |
|
|---|
| 28 | Core::Cast($this->System)->RegisterPage(['modules'], 'PageModules');
|
|---|
| 29 | //$this->Manager->OnModuleChange = array($this, 'ModuleChange');
|
|---|
| 30 | //$this->LoadFromDatabase();
|
|---|
| 31 |
|
|---|
| 32 | $this->System->FormManager->RegisterClass('Module', array(
|
|---|
| 33 | 'Title' => 'Moduly',
|
|---|
| 34 | 'Table' => 'Module',
|
|---|
| 35 | 'Items' => array(
|
|---|
| 36 | 'Name' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
|
|---|
| 37 | 'Title' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
|
|---|
| 38 | 'Description' => array('Type' => 'Text', 'Caption' => 'Popis', 'Default' => ''),
|
|---|
| 39 | 'Version' => array('Type' => 'String', 'Caption' => 'Verze', 'Default' => ''),
|
|---|
| 40 | 'License' => array('Type' => 'String', 'Caption' => 'Licence', 'Default' => ''),
|
|---|
| 41 | 'Creator' => array('Type' => 'String', 'Caption' => 'Tvůrce', 'Default' => ''),
|
|---|
| 42 | 'HomePage' => array('Type' => 'Hyperlink', 'Caption' => 'Domovské stránky', 'Default' => ''),
|
|---|
| 43 | 'Installed' => array('Type' => 'Boolean', 'Caption' => 'Instalováno', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 44 | 'Models' => array('Type' => 'TModelListModule', 'Caption' => 'Modely', 'Default' => ''),
|
|---|
| 45 | 'Links' => array('Type' => 'TModuleLinkListModule', 'Caption' => 'Vazby', 'Default' => ''),
|
|---|
| 46 | ),
|
|---|
| 47 | 'Actions' => array(
|
|---|
| 48 | array('Caption' => 'Aktualizovat z disku', 'URL' => '/module/?A=SaveToDb'),
|
|---|
| 49 | ),
|
|---|
| 50 | ));
|
|---|
| 51 | $this->System->FormManager->RegisterFormType('TModule', array(
|
|---|
| 52 | 'Type' => 'Reference',
|
|---|
| 53 | 'Table' => 'Module',
|
|---|
| 54 | 'Id' => 'Id',
|
|---|
| 55 | 'Name' => 'Title',
|
|---|
| 56 | 'Filter' => '1',
|
|---|
| 57 | ));
|
|---|
| 58 | $this->System->FormManager->RegisterFormType('TModelListModule', array(
|
|---|
| 59 | 'Type' => 'ManyToOne',
|
|---|
| 60 | 'Table' => 'Model',
|
|---|
| 61 | 'Id' => 'Id',
|
|---|
| 62 | 'Ref' => 'Module',
|
|---|
| 63 | 'Filter' => '1',
|
|---|
| 64 | ));
|
|---|
| 65 | $this->System->FormManager->RegisterClass('Model', array(
|
|---|
| 66 | 'Title' => 'Modely',
|
|---|
| 67 | 'Table' => 'Model',
|
|---|
| 68 | 'Items' => array(
|
|---|
| 69 | 'Name' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
|
|---|
| 70 | 'Title' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
|
|---|
| 71 | 'Module' => array('Type' => 'TModule', 'Caption' => 'Module', 'Default' => ''),
|
|---|
| 72 | 'Query' => array('Type' => 'String', 'Caption' => 'SQL dotaz', 'Default' => ''),
|
|---|
| 73 | 'DefaultSortColumn' => array('Type' => 'String', 'Caption' => 'Výchozí sloupce řazení', 'Default' => ''),
|
|---|
| 74 | 'DefaultSortOrder' => array('Type' => 'Text', 'Caption' => 'Výchozí směr řazení', 'Default' => ''),
|
|---|
| 75 | 'Fields' => array('Type' => 'TModelFieldListModel', 'Caption' => 'Pole', 'Default' => ''),
|
|---|
| 76 | ),
|
|---|
| 77 | ));
|
|---|
| 78 | $this->System->FormManager->RegisterFormType('TModel', array(
|
|---|
| 79 | 'Type' => 'Reference',
|
|---|
| 80 | 'Table' => 'Model',
|
|---|
| 81 | 'Id' => 'Id',
|
|---|
| 82 | 'Name' => 'Title',
|
|---|
| 83 | 'Filter' => '1',
|
|---|
| 84 | ));
|
|---|
| 85 | $this->System->FormManager->RegisterFormType('TModelFieldListModel', array(
|
|---|
| 86 | 'Type' => 'ManyToOne',
|
|---|
| 87 | 'Table' => 'ModelField',
|
|---|
| 88 | 'Id' => 'Id',
|
|---|
| 89 | 'Ref' => 'Model',
|
|---|
| 90 | 'Filter' => '1',
|
|---|
| 91 | ));
|
|---|
| 92 | $this->System->FormManager->RegisterClass('ModelField', array(
|
|---|
| 93 | 'Title' => 'Pole modelu',
|
|---|
| 94 | 'Table' => 'ModelField',
|
|---|
| 95 | 'Items' => array(
|
|---|
| 96 | 'Name' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
|
|---|
| 97 | 'Title' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
|
|---|
| 98 | 'Model' => array('Type' => 'TModel', 'Caption' => 'Model', 'Default' => ''),
|
|---|
| 99 | 'Query' => array('Type' => 'String', 'Caption' => 'SQL dotaz', 'Default' => ''),
|
|---|
| 100 | 'Type' => array('Type' => 'String', 'Caption' => 'Typ', 'Default' => ''),
|
|---|
| 101 | 'DefaultValue' => array('Type' => 'String', 'Caption' => 'Výchozí hodnota', 'Default' => ''),
|
|---|
| 102 | 'IsNull' => array('Type' => 'Boolean', 'Caption' => 'Také nulová hodnota', 'Default' => ''),
|
|---|
| 103 | 'Suffix' => array('Type' => 'String', 'Caption' => 'Text za', 'Default' => ''),
|
|---|
| 104 | ),
|
|---|
| 105 | ));
|
|---|
| 106 | $this->System->FormManager->RegisterFormType('TModuleLink', array(
|
|---|
| 107 | 'Type' => 'Reference',
|
|---|
| 108 | 'Table' => 'ModuleLink',
|
|---|
| 109 | 'Id' => 'Id',
|
|---|
| 110 | 'Name' => 'Module',
|
|---|
| 111 | 'Filter' => '1',
|
|---|
| 112 | ));
|
|---|
| 113 | $this->System->FormManager->RegisterFormType('TModuleLinkListModule', array(
|
|---|
| 114 | 'Type' => 'ManyToOne',
|
|---|
| 115 | 'Table' => 'ModuleLink',
|
|---|
| 116 | 'Id' => 'Id',
|
|---|
| 117 | 'Ref' => 'Module',
|
|---|
| 118 | 'Filter' => '1',
|
|---|
| 119 | ));
|
|---|
| 120 | $this->System->FormManager->RegisterClass('ModuleLink', array(
|
|---|
| 121 | 'Title' => 'Vazby modulu',
|
|---|
| 122 | 'Table' => 'ModuleLink',
|
|---|
| 123 | 'Items' => array(
|
|---|
| 124 | 'Module' => array('Type' => 'TModule', 'Caption' => 'Modul', 'Default' => ''),
|
|---|
| 125 | 'LinkedModule' => array('Type' => 'TModule', 'Caption' => 'Vázaný modul', 'Default' => ''),
|
|---|
| 126 | 'Type' => array('Type' => 'String', 'Caption' => 'Typ vazby', 'Default' => ''),
|
|---|
| 127 | ),
|
|---|
| 128 | ));
|
|---|
| 129 | $this->System->FormManager->RegisterFormType('TModule', array(
|
|---|
| 130 | 'Type' => 'Reference',
|
|---|
| 131 | 'Table' => 'Module',
|
|---|
| 132 | 'Id' => 'Id',
|
|---|
| 133 | 'Name' => 'Name',
|
|---|
| 134 | 'Filter' => '1',
|
|---|
| 135 | ));
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | function DoStop(): void
|
|---|
| 139 | {
|
|---|
| 140 | Core::Cast($this->System)->UnregisterPage(['modules']);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | function DoBeforeInstall(): void
|
|---|
| 144 | {
|
|---|
| 145 | $this->AddModelDatabase(Module::GetModelDesc());
|
|---|
| 146 | $this->Manager->OnInstallModule = array($this, 'InstallModule');
|
|---|
| 147 | $this->Manager->OnUninstallModule = array($this, 'UninstallModule');
|
|---|
| 148 | $this->AddModelDatabase(Model::GetModelDesc());
|
|---|
| 149 | $this->Manager->OnInstallModel = array($this, 'InstallModel');
|
|---|
| 150 | $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | function DoAfterUninstall(): void
|
|---|
| 154 | {
|
|---|
| 155 | $this->Manager->OnInstallModel = null;
|
|---|
| 156 | $this->Manager->OnUninstallModel = null;
|
|---|
| 157 | $this->RemoveModelDatabase(Model::GetModelDesc());
|
|---|
| 158 | $this->Manager->OnInstallModule = null;
|
|---|
| 159 | $this->Manager->OnUninstallModule = null;
|
|---|
| 160 | $this->RemoveModelDatabase(Module::GetModelDesc());
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | function DoInstall(): void
|
|---|
| 164 | {
|
|---|
| 165 | $this->InstallModel(Module::GetModelDesc(), $this->System->GetModule($this::GetName()));
|
|---|
| 166 | $this->InstallModel(Model::GetModelDesc(), $this->System->GetModule($this::GetName()));
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | function AddModelDatabase(ModelDesc $ModelDesc)
|
|---|
| 170 | {
|
|---|
| 171 | $Query = "CREATE TABLE IF NOT EXISTS `".$ModelDesc->Name."` (\n";
|
|---|
| 172 | $Query .= ' `'.$ModelDesc->PrimaryKey.'` int(11) NOT NULL AUTO_INCREMENT,'."\n";
|
|---|
| 173 | foreach ($ModelDesc->Columns as $Column)
|
|---|
| 174 | {
|
|---|
| 175 | $Query .= " `".$Column->Name."` ";
|
|---|
| 176 | if ($Column->Type == ModelColumnType::Integer) $Query .= 'int(11)';
|
|---|
| 177 | else if ($Column->Type == ModelColumnType::String) $Query .= 'varchar(255)';
|
|---|
| 178 | else if ($Column->Type == ModelColumnType::Float) $Query .= 'varchar(255)';
|
|---|
| 179 | else if ($Column->Type == ModelColumnType::Text) $Query .= 'text';
|
|---|
| 180 | else if ($Column->Type == ModelColumnType::DateTime) $Query .= 'datetime';
|
|---|
| 181 | else if ($Column->Type == ModelColumnType::Reference) $Query .= 'int(11)';
|
|---|
| 182 | else if ($Column->Type == ModelColumnType::Boolean) $Query .= 'tinyint(1)';
|
|---|
| 183 | else if ($Column->Type == ModelColumnType::Date) $Query .= 'date';
|
|---|
| 184 | else if ($Column->Type == ModelColumnType::BigInt) $Query .= 'bigint(20)';
|
|---|
| 185 | else if ($Column->Type == ModelColumnType::Enum)
|
|---|
| 186 | {
|
|---|
| 187 | $Query .= 'enum("'.implode('", "', $Column->States).'")';
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if ($Column->Nullable) $Query .= '';
|
|---|
| 191 | else $Query .= ' NOT NULL';
|
|---|
| 192 |
|
|---|
| 193 | $Query .= ' COLLATE utf8_general_ci';
|
|---|
| 194 |
|
|---|
| 195 | if ($Column->HasDefault)
|
|---|
| 196 | {
|
|---|
| 197 | if ($Column->Default == null)
|
|---|
| 198 | $Query .= ' DEFAULT NULL';
|
|---|
| 199 | else $Query .= ' DEFAULT '.$Column->GetDefault();
|
|---|
| 200 | }
|
|---|
| 201 | $Query .= ",\n";
|
|---|
| 202 | }
|
|---|
| 203 | $Query .= ' PRIMARY KEY (`'.$ModelDesc->PrimaryKey.'`)';
|
|---|
| 204 | foreach ($ModelDesc->Columns as $Column)
|
|---|
| 205 | {
|
|---|
| 206 | if ($Column->Type == ModelColumnType::Reference)
|
|---|
| 207 | $Query .= ','."\n".' KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
|
|---|
| 208 | else if ($Column->Unique)
|
|---|
| 209 | $Query .= ','."\n".' UNIQUE KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
|
|---|
| 210 | }
|
|---|
| 211 | $Query .= "\n";
|
|---|
| 212 |
|
|---|
| 213 | if ($ModelDesc->Memory) $Engine = 'MEMORY';
|
|---|
| 214 | else $Engine = 'InnoDB';
|
|---|
| 215 | $Query .= ') ENGINE='.$Engine.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;';
|
|---|
| 216 | foreach ($ModelDesc->Columns as $Column)
|
|---|
| 217 | {
|
|---|
| 218 | if ($Column->Type == ModelColumnType::Reference)
|
|---|
| 219 | $Query .= "ALTER TABLE `".$ModelDesc->Name."` ".
|
|---|
| 220 | "ADD CONSTRAINT `".$ModelDesc->Name."_ibfk_".$Column->Name."` FOREIGN KEY (`".$Column->Name."`) REFERENCES `".$Column->RefTable."` (`Id`);";
|
|---|
| 221 | }
|
|---|
| 222 | $this->Database->query($Query);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | function RemoveModelDatabase(ModelDesc $ModelDesc)
|
|---|
| 226 | {
|
|---|
| 227 | /*$Query = '';
|
|---|
| 228 | foreach ($ModelDesc->Columns as $Column)
|
|---|
| 229 | {
|
|---|
| 230 | if ($Column->Type == ModelColumnType::Reference)
|
|---|
| 231 | $Query .= "ALTER TABLE `".$ModelDesc->Name."` ".
|
|---|
| 232 | "DROP FOREIGN KEY `".$ModelDesc->Name."_ibfk_".$Column->Name."`;";
|
|---|
| 233 | }
|
|---|
| 234 | if (($Query != '') and $this->Database->TableExists($ModelDesc->Name))
|
|---|
| 235 | {
|
|---|
| 236 | $this->Database->query($Query);
|
|---|
| 237 | }
|
|---|
| 238 | */
|
|---|
| 239 | $this->Database->query('SET foreign_key_checks = 0; DROP TABLE IF EXISTS `'.$ModelDesc->Name.'`');
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | function InstallModel(ModelDesc $ModelDesc, Module $Module)
|
|---|
| 243 | {
|
|---|
| 244 | $this->AddModelDatabase($ModelDesc);
|
|---|
| 245 | $this->Database->insert('Model', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id));
|
|---|
| 246 | $ModelId = $this->Database->insert_id;
|
|---|
| 247 | foreach ($ModelDesc->Columns as $Field)
|
|---|
| 248 | {
|
|---|
| 249 | $this->Database->insert('ModelField', array('Name' => $Field->Name, 'Model' => $ModelId,
|
|---|
| 250 | 'Type' => ModelColumnType::GetName($Field->Type), 'Nullable' => (int)$Field->Nullable));
|
|---|
| 251 | }
|
|---|
| 252 | if ($ModelDesc->DefaultValues != null)
|
|---|
| 253 | {
|
|---|
| 254 | $Values = call_user_func('self::'.$ModelDesc->DefaultValues);
|
|---|
| 255 | foreach ($Values as $Value)
|
|---|
| 256 | {
|
|---|
| 257 | $this->Database->insert($ModelDesc->Name, $Value);
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | function UninstallModel(ModelDesc $ModelDesc, Module $Module)
|
|---|
| 263 | {
|
|---|
| 264 | $DbResult = $this->Database->select('Model', 'Id', '(Name="'.$ModelDesc->Name.'") AND (Module='.$Module->Id.')');
|
|---|
| 265 | if ($DbResult->num_rows > 0)
|
|---|
| 266 | {
|
|---|
| 267 | $DbRow = $DbResult->fetch_assoc();
|
|---|
| 268 | $ModelId = $DbRow['Id'];
|
|---|
| 269 | $DbResult = $this->Database->delete('ModelField', '(Model='.$ModelId.')');
|
|---|
| 270 | $this->Database->delete('Model', '(Id='.$ModelId.')');
|
|---|
| 271 | } else echo('Can\'t remove model '.$ModelDesc->Name.'<br/>');
|
|---|
| 272 | $this->RemoveModelDatabase($ModelDesc);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | function InstallModule(Module $Module)
|
|---|
| 276 | {
|
|---|
| 277 | $this->Database->insert('Module', array(
|
|---|
| 278 | 'Id' => $Module->Id,
|
|---|
| 279 | 'Name' => $Module->Name,
|
|---|
| 280 | 'Title' => $Module->Title,
|
|---|
| 281 | 'Creator' => $Module->Creator,
|
|---|
| 282 | 'Version' => $Module->Version,
|
|---|
| 283 | 'License' => $Module->License,
|
|---|
| 284 | 'Installed' => (int)$Module->Installed,
|
|---|
| 285 | 'Description' => $Module->Description,
|
|---|
| 286 | 'InstalledVersion' => $Module->InstalledVersion
|
|---|
| 287 | ));
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | function UninstallModule(Module $Module)
|
|---|
| 291 | {
|
|---|
| 292 | $this->Database->delete('Module', 'Name="'.$Module->Name.'"');
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | function ModuleChange(Module $Module): void
|
|---|
| 296 | {
|
|---|
| 297 | if ($Module->Installed) $Installed = 1;
|
|---|
| 298 | else $Installed = 0;
|
|---|
| 299 | $this->Database->query('UPDATE `Module` SET `Installed`='.$Installed.' WHERE `Name`="'.$Module->Name.'"');
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | function LoadFromDatabase(): void
|
|---|
| 303 | {
|
|---|
| 304 | //DebugLog('Loading modules...');
|
|---|
| 305 | $this->Modules = array();
|
|---|
| 306 | $Query = 'SELECT `Id`, `Name`,`Installed` FROM `Module`';
|
|---|
| 307 | $DbResult = $this->Database->query($Query);
|
|---|
| 308 | while ($Module = $DbResult->fetch_array())
|
|---|
| 309 | {
|
|---|
| 310 | include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
|
|---|
| 311 | $ModuleClassName = 'Module'.$Module['Name'];
|
|---|
| 312 | $NewModule = new $ModuleClassName($this->Database, $this->Manager);
|
|---|
| 313 | $NewModule->Id = $Module['Id'];
|
|---|
| 314 | $NewModule->Installed = $Module['Installed'];
|
|---|
| 315 | $this->Manager->RegisterModule($NewModule);
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | function SaveToDatabase(): string
|
|---|
| 320 | {
|
|---|
| 321 | $Output = '';
|
|---|
| 322 | $Modules = array();
|
|---|
| 323 | $DbResult = $this->Database->query('SELECT * FROM `Module`');
|
|---|
| 324 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 325 | {
|
|---|
| 326 | $Modules[$DbRow['Name']] = $DbRow;
|
|---|
| 327 | if ($this->System->ModuleManager->ModulePresent($DbRow['Name']))
|
|---|
| 328 | $this->System->ModuleManager->GetModule($DbRow['Name'])->Id = $DbRow['Id'];
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | // Add missing
|
|---|
| 332 | foreach ($this->System->ModuleManager->Modules as $Module)
|
|---|
| 333 | {
|
|---|
| 334 | if (!array_key_exists($Module->Name, $Modules))
|
|---|
| 335 | {
|
|---|
| 336 | $this->Database->insert('Module', array('Name' => $Module->Name,
|
|---|
| 337 | 'Version' => $Module->Version, 'Creator' => $Module->Creator,
|
|---|
| 338 | 'HomePage' => $Module->HomePage, 'Title' => $Module->Title,
|
|---|
| 339 | 'Description' => $Module->Description, 'License' => $Module->License,
|
|---|
| 340 | 'Installed' => $Module->Installed));
|
|---|
| 341 | $this->System->ModuleManager->GetModule($Module->Name)->Id = $this->Database->insert_id;
|
|---|
| 342 | }
|
|---|
| 343 | else $this->Database->update('Module', 'Name = "'.$Module->Name.'"', array(
|
|---|
| 344 | 'Version' => $Module->Version, 'Creator' => $Module->Creator,
|
|---|
| 345 | 'HomePage' => $Module->HomePage, 'Title' => $Module->Title,
|
|---|
| 346 | 'Description' => $Module->Description, 'License' => $Module->License,
|
|---|
| 347 | 'Installed' => $Module->Installed));
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | // Remove exceeding
|
|---|
| 351 | foreach ($Modules as $Module)
|
|---|
| 352 | if (!$this->System->ModuleManager->ModulePresent($Module['Name']))
|
|---|
| 353 | {
|
|---|
| 354 | $Output .= 'Removing module '.$Module['Name'].' from list</br/>';
|
|---|
| 355 | $this->Database->query('DELETE FROM `ModuleLink` WHERE `Module` = '.$Module['Id']);
|
|---|
| 356 | $this->Database->query('DELETE FROM `ModuleLink` WHERE `LinkedModule` = '.$Module['Id']);
|
|---|
| 357 | $DbResult = $this->Database->query('SELECT Id FROM `PermissionOperation` WHERE `Module` = '.$Module['Id']);
|
|---|
| 358 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 359 | {
|
|---|
| 360 | $this->Database->query('DELETE FROM `PermissionGroupAssignment` WHERE `AssignedOperation` = '.$DbRow['Id']);
|
|---|
| 361 | $this->Database->query('DELETE FROM `PermissionUserAssignment` WHERE `AssignedOperation` = '.$DbRow['Id']);
|
|---|
| 362 | }
|
|---|
| 363 | $this->Database->query('DELETE FROM `PermissionOperation` WHERE `Module` = '.$Module['Id']);
|
|---|
| 364 | $this->Database->query('DELETE FROM `Model` WHERE `Module` = '.$Module['Id']);
|
|---|
| 365 | $DbResult = $this->Database->query('SELECT Id FROM `Model` WHERE `Module` = '.$Module['Id']);
|
|---|
| 366 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 367 | $this->Database->query('DELETE FROM `ModelField` WHERE `Model` = '.$DbRow['Id']);
|
|---|
| 368 | $this->Database->query('DELETE FROM `Module` WHERE `Id` = '.$Module['Id']);
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | // Reload dependencies
|
|---|
| 372 | $DbDependency = array();
|
|---|
| 373 | $DbResult = $this->Database->query('SELECT * FROM `ModuleLink`');
|
|---|
| 374 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 375 | $DbDependency[$DbRow['Module']][] = $DbRow['LinkedModule'];
|
|---|
| 376 |
|
|---|
| 377 | foreach ($this->System->ModuleManager->Modules as $Module)
|
|---|
| 378 | {
|
|---|
| 379 | // Add missing
|
|---|
| 380 | foreach ($Module->Dependencies as $Dependency)
|
|---|
| 381 | {
|
|---|
| 382 | if (!array_key_exists($Module->Id, $DbDependency) or
|
|---|
| 383 | !in_array($this->System->ModuleManager->GetModule($Dependency)->Id, $DbDependency[$Module->Id]))
|
|---|
| 384 | {
|
|---|
| 385 | if ($this->System->ModuleManager->ModulePresent($Dependency))
|
|---|
| 386 | $DependencyId = $this->System->ModuleManager->GetModule($Dependency)->Id;
|
|---|
| 387 | else throw new Exception('Dependent module '.$Dependency.' not found');
|
|---|
| 388 | $this->Database->insert('ModuleLink', array('Module' => $Module->Id,
|
|---|
| 389 | 'LinkedModule' => $DependencyId, 'Type' => 'DependOn'));
|
|---|
| 390 | }
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | // Remove exceeding
|
|---|
| 394 | if (array_key_exists($Module->Id, $DbDependency))
|
|---|
| 395 | foreach ($DbDependency[$Module->Id] as $Dep)
|
|---|
| 396 | {
|
|---|
| 397 | $DepModName = $this->System->ModuleManager->SearchModuleById($Dep);
|
|---|
| 398 | if (!in_array($DepModName, $Module->Dependencies))
|
|---|
| 399 | $this->Database->query('DELETE FROM `ModuleLink` WHERE `Module` = '.
|
|---|
| 400 | $Module->Id.' AND LinkedModule='.$Dep);
|
|---|
| 401 | }
|
|---|
| 402 | }
|
|---|
| 403 | return $Output;
|
|---|
| 404 | }
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | class PageModules extends Page
|
|---|
| 408 | {
|
|---|
| 409 | public array $YesNo;
|
|---|
| 410 |
|
|---|
| 411 | function __construct(System $System)
|
|---|
| 412 | {
|
|---|
| 413 | parent::__construct($System);
|
|---|
| 414 | $this->Title = T('Modules');
|
|---|
| 415 | $this->ParentClass = 'PageSetup';
|
|---|
| 416 | $this->YesNo = array(false => T('No'), true => T('Yes'));
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | function Show(): string
|
|---|
| 420 | {
|
|---|
| 421 | $Output = '';
|
|---|
| 422 | if (array_key_exists('op', $_GET)) $Operation = $_GET['op'];
|
|---|
| 423 | else $Operation = '';
|
|---|
| 424 | if ($Operation == 'install')
|
|---|
| 425 | {
|
|---|
| 426 | $this->System->ModuleManager->GetModule($_GET['name'])->Install();
|
|---|
| 427 | $this->System->ModuleManager->SaveState();
|
|---|
| 428 | $Output .= 'Modul '.$_GET['name'].' instalován.<br/>';
|
|---|
| 429 | } else
|
|---|
| 430 | if ($Operation == 'uninstall')
|
|---|
| 431 | {
|
|---|
| 432 | $this->System->ModuleManager->GetModule($_GET['name'])->Uninstall();
|
|---|
| 433 | $this->System->ModuleManager->SaveState();
|
|---|
| 434 | $Output .= 'Modul '.$_GET['name'].' odinstalován.<br/>';
|
|---|
| 435 | } else
|
|---|
| 436 | if ($Operation == 'enable')
|
|---|
| 437 | {
|
|---|
| 438 | $this->System->ModuleManager->GetModule($_GET['name'])->Enable();
|
|---|
| 439 | $this->System->ModuleManager->SaveState();
|
|---|
| 440 | $Output .= 'Modul '.$_GET['name'].' povolen.<br/>';
|
|---|
| 441 | } else
|
|---|
| 442 | if ($Operation == 'disable')
|
|---|
| 443 | {
|
|---|
| 444 | $this->System->ModuleManager->GetModule($_GET['name'])->Disable();
|
|---|
| 445 | $this->System->ModuleManager->SaveState();
|
|---|
| 446 | $Output .= 'Modul '.$_GET['name'].' zakázán.<br/>';
|
|---|
| 447 | } else
|
|---|
| 448 | if ($Operation == 'upgrade')
|
|---|
| 449 | {
|
|---|
| 450 | $this->System->ModuleManager->GetModule($_GET['name'])->Upgrade();
|
|---|
| 451 | $this->System->ModuleManager->SaveState();
|
|---|
| 452 | $Output .= 'Modul '.$_GET['name'].' povýšen.<br/>';
|
|---|
| 453 | } else
|
|---|
| 454 | if ($Operation == 'install-all')
|
|---|
| 455 | {
|
|---|
| 456 | $this->System->ModuleManager->InstallAll();
|
|---|
| 457 | $this->System->ModuleManager->SaveState();
|
|---|
| 458 | $Output .= 'Všechny moduly instalovány.<br/>';
|
|---|
| 459 | } else
|
|---|
| 460 | if ($Operation == 'uninstall-all')
|
|---|
| 461 | {
|
|---|
| 462 | $this->System->ModuleManager->UninstallAll(array(ModuleCondition::Application));
|
|---|
| 463 | $this->System->ModuleManager->UninstallAll(array(ModuleCondition::Library));
|
|---|
| 464 | $this->System->ModuleManager->SaveState();
|
|---|
| 465 | $Output .= 'Všechny moduly odinstalovány.<br/>';
|
|---|
| 466 | } else
|
|---|
| 467 | if ($Operation == 'enable-all')
|
|---|
| 468 | {
|
|---|
| 469 | $this->System->ModuleManager->EnableAll();
|
|---|
| 470 | $this->System->ModuleManager->SaveState();
|
|---|
| 471 | $Output .= 'Všechny moduly povoleny.<br/>';
|
|---|
| 472 | } else
|
|---|
| 473 | if ($Operation == 'disable-all')
|
|---|
| 474 | {
|
|---|
| 475 | $this->System->ModuleManager->DisableAll();
|
|---|
| 476 | $this->System->ModuleManager->SaveState();
|
|---|
| 477 | $Output .= 'Všechny moduly zakázány.<br/>';
|
|---|
| 478 | } else
|
|---|
| 479 | if ($Operation == 'upgrade-all')
|
|---|
| 480 | {
|
|---|
| 481 | $this->System->ModuleManager->UpgradeAll();
|
|---|
| 482 | $this->System->ModuleManager->SaveState();
|
|---|
| 483 | $Output .= 'Všechny moduly povýšeny.<br/>';
|
|---|
| 484 | }
|
|---|
| 485 | $Output .= '<h3>Správa modulů</h3>';
|
|---|
| 486 | $Output .= $this->ShowList();
|
|---|
| 487 | return $Output;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | function ShowList(): string
|
|---|
| 491 | {
|
|---|
| 492 | $Output = '';
|
|---|
| 493 |
|
|---|
| 494 | $Pageing = new Paging();
|
|---|
| 495 | $Pageing->TotalCount = count($this->System->ModuleManager->Modules);
|
|---|
| 496 | $Table = new VisualTable();
|
|---|
| 497 | $Table->SetColumns(array(
|
|---|
| 498 | array('Name' => 'Name', 'Title' => T('Name')),
|
|---|
| 499 | array('Name' => 'Creator', 'Title' => T('Creator')),
|
|---|
| 500 | array('Name' => 'Version', 'Title' => T('Version')),
|
|---|
| 501 | array('Name' => 'InstalledVersion', 'Title' => T('Installed version')),
|
|---|
| 502 | array('Name' => 'Type', 'Title' => T('Type')),
|
|---|
| 503 | array('Name' => 'License', 'Title' => T('License')),
|
|---|
| 504 | array('Name' => 'Installed', 'Title' => T('Installed')),
|
|---|
| 505 | array('Name' => 'Enabled', 'Title' => T('Enabled')),
|
|---|
| 506 | array('Name' => 'Description', 'Title' => T('Description')),
|
|---|
| 507 | array('Name' => 'Dependencies', 'Title' => T('Dependencies')),
|
|---|
| 508 | array('Name' => '', 'Title' => 'Akce'),
|
|---|
| 509 | ));
|
|---|
| 510 | $ModuleType = array(T('System'), T('Library'), T('Application'));
|
|---|
| 511 | foreach ($this->System->ModuleManager->Modules as $Module)
|
|---|
| 512 | {
|
|---|
| 513 | if (($Module->Dependencies) > 0) $Dependencies = implode(',', $Module->Dependencies);
|
|---|
| 514 | else $Dependencies = ' ';
|
|---|
| 515 | $Actions = '';
|
|---|
| 516 | if ($Module->Type != ModuleType::System)
|
|---|
| 517 | {
|
|---|
| 518 | if ($Module->Installed == true)
|
|---|
| 519 | {
|
|---|
| 520 | $Actions .= ' <a href="?op=uninstall&name='.$Module->Name.'">Odinstalovat</a>';
|
|---|
| 521 | if ($Module->Enabled == true) $Actions .= ' <a href="?op=disable&name='.$Module->Name.'">Zakázat</a>';
|
|---|
| 522 | else $Actions .= ' <a href="?op=enable&name='.$Module->Name.'">Povolit</a>';
|
|---|
| 523 | if ($Module->InstalledVersion != $Module->Version) $Actions .= ' <a href="?op=upgrade&name='.$Module->Name.'">Povýšit</a>';
|
|---|
| 524 | } else $Actions .= ' <a href="?op=install&name='.$Module->Name.'">Instalovat</a>';
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | $Table->Table->Cells[] = array($Module->Name,
|
|---|
| 528 | $Module->Creator, $Module->Version, $Module->InstalledVersion, $ModuleType[$Module->Type],
|
|---|
| 529 | $Module->License, $this->YesNo[$Module->Installed],
|
|---|
| 530 | $this->YesNo[$Module->Enabled], $Module->Description,
|
|---|
| 531 | $Dependencies, $Actions);
|
|---|
| 532 | }
|
|---|
| 533 | $Output .= $Pageing->Show();
|
|---|
| 534 | $Output .= $Table->Show();
|
|---|
| 535 | $Output .= $Pageing->Show();
|
|---|
| 536 |
|
|---|
| 537 | $Output .= '<div>'.
|
|---|
| 538 | '<a href="?op=install-all">Instalovat vše</a> '.
|
|---|
| 539 | '<a href="?op=uninstall-all">Odinstalovat vše</a> '.
|
|---|
| 540 | '<a href="?op=disable-all">Zakázat vše</a> '.
|
|---|
| 541 | '<a href="?op=enable">Povolit vše</a> '.
|
|---|
| 542 | '<a href="?op=upgrade">Povýšit vše</a> '.
|
|---|
| 543 | '</div>';
|
|---|
| 544 |
|
|---|
| 545 | return $Output;
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | class ModelField extends Model
|
|---|
| 550 | {
|
|---|
| 551 | static function GetModelDesc(): ModelDesc
|
|---|
| 552 | {
|
|---|
| 553 | $Desc = new ModelDesc(self::GetClassName());
|
|---|
| 554 | $Desc->AddString('Name');
|
|---|
| 555 | $Desc->AddReference('Model', Model::GetClassName());
|
|---|
| 556 | $Desc->AddString('Type');
|
|---|
| 557 | $Desc->AddBoolean('Nullable');
|
|---|
| 558 | return $Desc;
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | class ModuleDependency extends Model
|
|---|
| 563 | {
|
|---|
| 564 | static function GetModelDesc(): ModelDesc
|
|---|
| 565 | {
|
|---|
| 566 | $Desc = new ModelDesc(self::GetClassName());
|
|---|
| 567 | $Desc->AddReference('Module', Module::GetClassName());
|
|---|
| 568 | $Desc->AddReference('Dependency', Module::GetClassName());
|
|---|
| 569 | return $Desc;
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|