Ignore:
Timestamp:
Feb 17, 2021, 12:30:23 PM (4 years ago)
Author:
chronos
Message:
Location:
trunk/Packages/Common/Modules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Modules/ModuleManager.php

    r898 r899  
    11<?php
    22
    3 class ModuleModuleManager extends AppModule
     3class ModuleModuleManager extends Module
    44{
    55  public UpdateManager $UpdateManager;
     
    1313    $this->License = 'GNU/GPLv3';
    1414    $this->Description = 'User interface for module management.';
    15     $this->Dependencies = array('Setup');
     15    $this->Dependencies = array(ModuleSetup::GetName());
    1616    $this->Revision = 1;
    1717    $this->Type = ModuleType::System;
     18    $this->Models = array(ModelField::GetClassName(), ModuleDependency::GetClassName());
    1819  }
    1920
     
    142143  function DoBeforeInstall(): void
    143144  {
    144     $this->AddModelDatabase(Module::GetDesc());
     145    $this->AddModelDatabase(Module::GetModelDesc());
    145146    $this->Manager->OnInstallModule = array($this, 'InstallModule');
    146147    $this->Manager->OnUninstallModule = array($this, 'UninstallModule');
    147     $this->AddModelDatabase(ModuleModel::GetDesc());
     148    $this->AddModelDatabase(Model::GetModelDesc());
    148149    $this->Manager->OnInstallModel = array($this, 'InstallModel');
    149150    $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
     
    154155    $this->Manager->OnInstallModel = null;
    155156    $this->Manager->OnUninstallModel = null;
    156     $this->RemoveModelDatabase(ModuleModel::GetDesc());
     157    $this->RemoveModelDatabase(Model::GetModelDesc());
    157158    $this->Manager->OnInstallModule = null;
    158159    $this->Manager->OnUninstallModule = null;
    159     $this->RemoveModelDatabase(Module::GetDesc());
    160   }
    161 
    162   function GetModels(): array
    163   {
    164     return array(ModuleModelField::GetClassName(), ModuleDependency::GetClassName());
     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()));
    165167  }
    166168
     
    238240  }
    239241
    240   function InstallModel(ModelDesc $ModelDesc, AppModule $Module)
     242  function InstallModel(ModelDesc $ModelDesc, Module $Module)
    241243  {
    242244    $this->AddModelDatabase($ModelDesc);
    243     $this->Database->insert('ModuleModel', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id));
     245    $this->Database->insert('Model', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id));
    244246    $ModelId = $this->Database->insert_id;
    245247    foreach ($ModelDesc->Columns as $Field)
    246248    {
    247       $this->Database->insert('ModuleModelField', array('Name' => $Field->Name, 'Model' => $ModelId,
     249      $this->Database->insert('ModelField', array('Name' => $Field->Name, 'Model' => $ModelId,
    248250        'Type' => ModelColumnType::GetName($Field->Type), 'Nullable' => (int)$Field->Nullable));
    249251    }
    250252  }
    251253
    252   function UninstallModel(ModelDesc $ModelDesc, AppModule $Module)
    253   {
    254     $this->Database->delete('ModuleModel', '(Name='.$ModelDesc->Name.') AND (Module='.$Module->Id.')');
     254  function UninstallModel(ModelDesc $ModelDesc, Module $Module)
     255  {
     256    $DbResult = $this->Database->select('Model', 'Id', '(Name="'.$ModelDesc->Name.'") AND (Module='.$Module->Id.')');
     257    if ($DbResult->num_rows > 0)
     258    {
     259      $DbRow = $DbResult->fetch_assoc();
     260      $ModelId = $DbRow['Id'];
     261      $DbResult = $this->Database->delete('ModelField', '(Model='.$ModelId.')');
     262    }
     263    $this->Database->delete('Model', '(Id='.$ModelId.')');
    255264    $this->RemoveModelDatabase($ModelDesc);
    256265  }
    257266
    258   function InstallModule(AppModule $Module)
    259   {
    260     echo('Install module '.$Module->Name);
     267  function InstallModule(Module $Module)
     268  {
    261269    $this->Database->insert('Module', array(
     270      'Id' => $Module->Id,
    262271      'Name' => $Module->Name,
    263272      'Title' => $Module->Title,
     
    269278      'InstalledVersion' => $Module->InstalledVersion
    270279    ));
    271     $Module->Id = $this->Database->insert_id;
    272   }
    273 
    274   function UninstallModule(AppModule $Module)
    275   {
    276     echo('Uninstall module '.$Module->Name);
    277     $this->Database->delete('Module', 'Name='.$Module->Name);
    278   }
    279 
    280   function ModuleChange(AppModule $Module): void
     280  }
     281
     282  function UninstallModule(Module $Module)
     283  {
     284    $this->Database->delete('Module', 'Name="'.$Module->Name.'"');
     285  }
     286
     287  function ModuleChange(Module $Module): void
    281288  {
    282289    if ($Module->Installed) $Installed = 1;
     
    293300    while ($Module = $DbResult->fetch_array())
    294301    {
    295       //echo($Module['Name'].',');
    296302      include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
    297303      $ModuleClassName = 'Module'.$Module['Name'];
     
    534540}
    535541
    536 class Module extends Model
     542class ModelField extends Model
    537543{
    538   static function GetDesc(): ModelDesc
     544  static function GetModelDesc(): ModelDesc
    539545  {
    540546    $Desc = new ModelDesc(self::GetClassName());
    541547    $Desc->AddString('Name');
    542     $Desc->AddString('Title');
    543     $Desc->AddString('Creator');
    544     $Desc->AddString('Version');
    545     $Desc->AddString('License');
    546     $Desc->AddBoolean('Installed');
    547     $Desc->AddString('InstalledVersion');
    548     $Desc->AddString('Description');
    549     return $Desc;
    550   }
    551 }
    552 
    553 class ModuleModel extends Model
    554 {
    555   static function GetDesc(): ModelDesc
    556   {
    557     $Desc = new ModelDesc(self::GetClassName());
    558     $Desc->AddString('Name');
    559     $Desc->AddReference('Module', Module::GetClassName());
    560     return $Desc;
    561   }
    562 }
    563 
    564 class ModuleModelField extends Model
    565 {
    566   static function GetDesc(): ModelDesc
    567   {
    568     $Desc = new ModelDesc(self::GetClassName());
    569     $Desc->AddString('Name');
    570     $Desc->AddReference('Model', ModuleModel::GetClassName());
     548    $Desc->AddReference('Model', Model::GetClassName());
    571549    $Desc->AddString('Type');
    572550    $Desc->AddBoolean('Nullable');
     
    577555class ModuleDependency extends Model
    578556{
    579   static function GetDesc(): ModelDesc
     557  static function GetModelDesc(): ModelDesc
    580558  {
    581559    $Desc = new ModelDesc(self::GetClassName());
  • trunk/Packages/Common/Modules/Setup.php

    r897 r899  
    11<?php
     2
     3class 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
     63  function InsertSampleData(): void
     64  {
     65  }
     66}
    267
    368class PageSetup extends Page
     
    111176        {
    112177          $Output .= '<h3>Instalace systém</h3>';
     178          global $DatabaseRevision;
     179
     180          $this->Database->query("CREATE TABLE IF NOT EXISTS `".$this->UpdateManager->VersionTable."` (".
     181            '`Id` int(11) NOT NULL AUTO_INCREMENT, '.
     182            '`Revision` int(11) NOT NULL, '.
     183            'PRIMARY KEY (`Id`) '.
     184            ') ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
     185          $DbResult = $this->Database->select($this->UpdateManager->VersionTable, 'Id');
     186          if ($DbResult->num_rows == 0)
     187          {
     188            $this->Database->query("INSERT INTO `".$this->UpdateManager->VersionTable.
     189              "` (`Id`, `Revision`) VALUES (1, ".$DatabaseRevision.");");
     190          }
    113191          $this->System->ModuleManager->InstallAll(array(ModuleCondition::System));
    114192          $this->System->ModuleManager->LoadModules();
     
    121199          $Output .= '<h3>Odinstalace vše</h3>';
    122200          $this->System->ModuleManager->UninstallAll(array(ModuleCondition::System));
     201          $this->Database->query('DROP TABLE IF EXISTS `'.$this->UpdateManager->VersionTable.'`');
    123202          $Output .= $this->ControlPanel();
    124203        }
     
    296375  }
    297376}
    298 
    299 class ModuleSetup extends AppModule
    300 {
    301   public UpdateManager $UpdateManager;
    302 
    303   function __construct(System $System)
    304   {
    305     global $DatabaseRevision;
    306 
    307     parent::__construct($System);
    308     $this->Name = 'Setup';
    309     $this->Version = '1.0';
    310     $this->Creator = 'Chronos';
    311     $this->License = 'GNU/GPLv3';
    312     $this->Description = 'Base setup module';
    313     $this->Dependencies = array();
    314     $this->Revision = 1;
    315     $this->Type = ModuleType::System;
    316 
    317     // Check database persistence structure
    318     $this->UpdateManager = new UpdateManager();
    319     $this->UpdateManager->Database = &$this->Database;
    320     $this->UpdateManager->Revision = $DatabaseRevision;
    321     $this->UpdateManager->InstallMethod = 'FullInstall';
    322   }
    323 
    324   static function Cast(AppModule $AppModule): ModuleSetup
    325   {
    326     if ($AppModule instanceof ModuleSetup)
    327     {
    328       return $AppModule;
    329     }
    330     throw new Exception('Expected ModuleSetup type but '.gettype($AppModule));
    331   }
    332 
    333   function DoStart(): void
    334   {
    335     Core::Cast($this->System)->RegisterPage([''], 'PageSetupRedirect');
    336     Core::Cast($this->System)->RegisterPage(['setup'], 'PageSetup');
    337   }
    338 
    339   function DoStop(): void
    340   {
    341     unset($this->UpdateManager);
    342     Core::Cast($this->System)->UnregisterPage(['']);
    343     Core::Cast($this->System)->UnregisterPage(['setup']);
    344   }
    345 
    346   function CheckState(): bool
    347   {
    348     return $this->Database->Connected() and $this->UpdateManager->IsInstalled() and
    349       $this->UpdateManager->IsUpToDate();
    350   }
    351 
    352   function DoUpgrade(): string
    353   {
    354     $Updates = new Updates();
    355     $this->UpdateManager->Trace = $Updates->Get();
    356     $Output = $this->UpdateManager->Upgrade();
    357     return $Output;
    358   }
    359 }
Note: See TracChangeset for help on using the changeset viewer.