Ignore:
Timestamp:
Jan 12, 2021, 10:29:50 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Setup is now AppModule and it is installed and stated as first module.
  • Modified: Improved modular system.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Setup.php

    r891 r895  
    11<?php
    22
    3 class PageSetup extends Page
     3class PageSetupModules extends Page
    44{
    5   public UpdateManager $UpdateManager;
    6   public array $ConfigDefinition;
    7   public array $Config;
    8   public int $DatabaseRevision;
    9   public int $Revision;
    10   public string $ConfigDir;
    115  public array $YesNo;
    126
     
    148  {
    159    parent::__construct($System);
    16     $this->FullTitle = T('Application setup');
    17     $this->ShortTitle = T('Application setup');
    18     //$this->ParentClass = 'PagePortal';
    19     $this->ConfigDir = dirname(dirname(dirname(__FILE__))).'/Config';
     10    $this->FullTitle = T('Modules');
     11    $this->ShortTitle = T('Modules');
     12    $this->ParentClass = 'PageSetup';
    2013    $this->YesNo = array(false => T('No'), true => T('Yes'));
    2114  }
    2215
    23   function LoginPanel(): string
    24   {
    25     $Output = '<h3>Přihlášení k instalaci</h3>'.
    26       '<form action="?" method="post">'.
    27       '<table>'.
    28       '<tr><td>Systémové heslo:</td><td> <input type="password" name="SystemPassword" value=""/></td></tr>'.
    29       '</table>'.
    30       '<input type="submit" name="login" value="'.T('Login').'"/>'.
    31       '</form>';
    32     return $Output;
    33   }
    34 
    35   function ControlPanel(): string
    36   {
    37     $Output = '<h3>'.T('Instance management').'</h3>';
    38 
    39     $Output .= 'Je připojení k databázi: '.$this->YesNo[$this->UpdateManager->Database->Connected()].'<br/>';
    40     if ($this->UpdateManager->Database->Connected())
    41     {
    42       $Output .= 'Je instalováno: '.$this->YesNo[$this->UpdateManager->IsInstalled()].'<br/>';
    43       if ($this->UpdateManager->IsInstalled())
    44         $Output .= 'Je aktuální: '.$this->YesNo[$this->UpdateManager->IsUpToDate()].'<br/>'.
    45         'Verze databáze: '.$this->UpdateManager->GetDbVersion().'<br/>';
    46       $Output .= 'Verze databáze kódu: '.$this->UpdateManager->Revision.'<br/>';
    47       if ($this->UpdateManager->IsInstalled())
    48       {
    49         if (!$this->UpdateManager->IsUpToDate())
    50           $Output .= '<a href="?action=upgrade">'.T('Upgrade').'</a> ';
    51         $Output .= '<a href="?action=insert_sample_data">Vložit vzorová data</a> ';
    52         $Output .= '<a href="?action=reload_modules">Obnovit seznam modulů</a> ';
    53         $Output .= '<a href="?action=uninstall">Odinstalovat</a> ';
    54         $Output .= '<a href="?action=modules">Správa modulů</a> ';
    55         $Output .= '<a href="?action=models">Přegenerovat modely</a> ';
    56       } else $Output .= '<a href="?action=install">Instalovat</a> ';
    57     }
    58     $Output .= '<a href="?action=configure">Nastavit</a> ';
    59     $Output .= '<a href="?action=logout">Odhlásit</a> ';
    60     $Output .= '<a href="'.$this->System->Link('/').'">'.T('Go to main page').'</a> ';
    61     $Output .= '';
    62     return $Output;
    63   }
    64 
    6516  function Show(): string
    66   {
    67     global $ConfigDefinition, $DatabaseRevision, $Config, $Updates;
    68 
    69     $this->UpdateManager = $this->System->Setup->UpdateManager;
    70     $DefaultConfig = new DefaultConfig();
    71     $this->ConfigDefinition = $DefaultConfig->Get();
    72     $this->DatabaseRevision = $DatabaseRevision;
    73     $this->Config = &$Config;
    74 
    75     $Output = '';
    76     if (isset($this->Config))
    77     {
    78       if (!array_key_exists('SystemPassword', $_SESSION)) $_SESSION['SystemPassword'] = '';
    79       if (array_key_exists('login', $_POST)) $_SESSION['SystemPassword'] = $_POST['SystemPassword'];
    80       if (sha1($_SESSION['SystemPassword']) != $this->Config['SystemPassword'])
    81       {
    82         $Output .= $this->LoginPanel();
    83       } else
    84       {
    85         if (array_key_exists('action', $_GET)) $Action = $_GET['action'];
    86           else $Action = '';
    87         if ($Action == 'logout')
    88         {
    89           $_SESSION['SystemPassword'] = '';
    90           $Output .= 'Odhlášen';
    91           $Output .= $this->LoginPanel();
    92         } else
    93         if ($Action == 'models')
    94         {
    95           $this->System->FormManager->UpdateSQLMeta();
    96         } else
    97         if ($Action == 'upgrade')
    98         {
    99           $Output .= '<h3>Povýšení</h3>';
    100           try {
    101             $Output .= $this->System->Setup->Upgrade();
    102           } catch (Exception $E) {
    103             $Output .= $this->SystemMessage('Chyba aktualizace',
    104               'Došlo k chybě v SQL dotazu při aktualizaci: <br/>'.$E->getMessage());
    105           }
    106           $Output .= $this->ControlPanel();
    107         } else
    108         if ($Action == 'install')
    109         {
    110           $Output .= '<h3>Instalace</h3>';
    111           $this->System->Setup->Install();
    112           $this->System->ModuleManager->LoadModules();
    113           $this->System->ModuleManager->SaveState();
    114           //$Output .= $this->System->Setup->Upgrade();
    115           $Output .= $this->ControlPanel();
    116         } else
    117         if ($Action == 'uninstall')
    118         {
    119           $Output .= '<h3>Odinstalace</h3>';
    120           $this->System->Setup->Uninstall();
    121           $Output .= $this->ControlPanel();
    122         } else
    123         if ($Action == 'reload_modules')
    124         {
    125           $Output .= '<h3>Znovunačtení seznamu modulů</h3>';
    126           $this->System->ModuleManager->LoadModules();
    127           $this->System->ModuleManager->SaveState();
    128           $Output .= $this->ControlPanel();
    129         } else
    130         if ($Action == 'insert_sample_data')
    131         {
    132           $Output .= '<h3>Vložení vzorových dat</h3>';
    133           $this->System->Setup->InsertSampleData();
    134           $Output .= $this->ControlPanel();
    135         } else
    136         if ($Action == 'modules')
    137         {
    138           $Output .= $this->ShowModules();
    139         } else
    140         if ($Action == 'configure_save')
    141         {
    142            $Output .= $this->ConfigSave($this->Config);
    143            $Output .= $this->ControlPanel();
    144         } else
    145         if ($Action == 'configure')
    146         {
    147           $Output .= $this->PrepareConfig($this->Config);
    148         } else
    149         {
    150           $Output .= $this->ControlPanel();
    151         }
    152       }
    153     } else
    154     {
    155       if (array_key_exists('configure_save', $_POST))
    156       {
    157         $Output .= $this->ConfigSave(array());
    158         $Output .= 'Pokračujte k přihlášení <a href="">zde</a>';
    159       } else {
    160         $Output .= $this->PrepareConfig(array());
    161       }
    162     }
    163     return $Output;
    164   }
    165 
    166   function ShowModules(): string
    16717  {
    16818    $Output = '';
     
    23787      $Table->Table->Cells[] = array($Module->Name,
    23888        $Module->Creator, $Module->Version,
    239         $Module->License, $this->YesNo[$Module->Installed],
     89        $Module->License,  $this->YesNo[$Module->Installed],
    24090        $this->YesNo[$Module->Enabled], $Module->Description,
    24191        $Dependencies, $Actions);
     
    24595    $Output .= $Pageing->Show();
    24696    //$Output .= '<p><a href="?A=SaveToDb">Uložit do databáze</a></p>';
     97    return $Output;
     98  }
     99}
     100
     101class PageSetup extends Page
     102{
     103  public UpdateManager $UpdateManager;
     104  public array $ConfigDefinition;
     105  public array $Config;
     106  public int $DatabaseRevision;
     107  public int $Revision;
     108  public string $ConfigDir;
     109  public array $YesNo;
     110
     111  function __construct(System $System)
     112  {
     113    parent::__construct($System);
     114    $this->FullTitle = T('Application setup');
     115    $this->ShortTitle = T('Application setup');
     116    //$this->ParentClass = 'PagePortal';
     117    $this->ConfigDir = dirname(dirname(dirname(__FILE__))).'/Config';
     118    $this->YesNo = array(false => T('No'), true => T('Yes'));
     119  }
     120
     121  function LoginPanel(): string
     122  {
     123    $Output = '<h3>Přihlášení k instalaci</h3>'.
     124      '<form action="?" method="post">'.
     125      '<table>'.
     126      '<tr><td>Systémové heslo:</td><td> <input type="password" name="SystemPassword" value=""/></td></tr>'.
     127      '</table>'.
     128      '<input type="submit" name="login" value="'.T('Login').'"/>'.
     129      '</form>';
     130    return $Output;
     131  }
     132
     133  function ControlPanel(): string
     134  {
     135    $Output = '<h3>'.T('Instance management').'</h3>';
     136
     137    $Output .= 'Je připojení k databázi: '.$this->YesNo[$this->UpdateManager->Database->Connected()].'<br/>';
     138    if ($this->UpdateManager->Database->Connected())
     139    {
     140      $Output .= 'Je instalováno: '.$this->YesNo[$this->UpdateManager->IsInstalled()].'<br/>';
     141      if ($this->UpdateManager->IsInstalled())
     142        $Output .= 'Je aktuální: '.$this->YesNo[$this->UpdateManager->IsUpToDate()].'<br/>'.
     143        'Verze databáze: '.$this->UpdateManager->GetDbVersion().'<br/>';
     144      $Output .= 'Verze databáze kódu: '.$this->UpdateManager->Revision.'<br/>';
     145      if ($this->UpdateManager->IsInstalled())
     146      {
     147        if (!$this->UpdateManager->IsUpToDate())
     148          $Output .= '<a href="?action=upgrade">'.T('Upgrade').'</a> ';
     149        $Output .= '<a href="?action=insert_sample_data">Vložit vzorová data</a> ';
     150        $Output .= '<a href="?action=reload_modules">Obnovit seznam modulů</a> ';
     151        $Output .= '<a href="?action=uninstall">Odinstalovat</a> ';
     152        $Output .= '<a href="'.$this->System->Link('/setup/modules/').'">Správa modulů</a> ';
     153        $Output .= '<a href="?action=models">Přegenerovat modely</a> ';
     154      } else $Output .= '<a href="?action=install">Instalovat</a> ';
     155    }
     156    $Output .= '<a href="?action=configure">Nastavit</a> ';
     157    $Output .= '<a href="?action=logout">Odhlásit</a> ';
     158    $Output .= '<a href="'.$this->System->Link('/').'">'.T('Go to main page').'</a> ';
     159    $Output .= '';
     160    return $Output;
     161  }
     162
     163  function Show(): string
     164  {
     165    global $ConfigDefinition, $DatabaseRevision, $Config, $Updates;
     166
     167    $this->UpdateManager = ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager;
     168    $DefaultConfig = new DefaultConfig();
     169    $this->ConfigDefinition = $DefaultConfig->Get();
     170    $this->DatabaseRevision = $DatabaseRevision;
     171    $this->Config = &$Config;
     172
     173    $Output = '';
     174    if (isset($this->Config))
     175    {
     176      if (!array_key_exists('SystemPassword', $_SESSION)) $_SESSION['SystemPassword'] = '';
     177      if (array_key_exists('login', $_POST)) $_SESSION['SystemPassword'] = $_POST['SystemPassword'];
     178      if (sha1($_SESSION['SystemPassword']) != $this->Config['SystemPassword'])
     179      {
     180        $Output .= $this->LoginPanel();
     181      } else
     182      {
     183        if (array_key_exists('action', $_GET)) $Action = $_GET['action'];
     184          else $Action = '';
     185        if ($Action == 'logout')
     186        {
     187          $_SESSION['SystemPassword'] = '';
     188          $Output .= 'Odhlášen';
     189          $Output .= $this->LoginPanel();
     190        }
     191        else if ($Action == 'models')
     192        {
     193          $this->System->FormManager->UpdateSQLMeta();
     194        }
     195        else if ($Action == 'upgrade')
     196        {
     197          $Output .= '<h3>Povýšení</h3>';
     198          try
     199          {
     200            $Output .= ModuleSetup::Cast($this->System->GetModule('Setup'))->Upgrade();
     201          } catch (Exception $E)
     202          {
     203            $Output .= $this->SystemMessage('Chyba aktualizace',
     204              'Došlo k chybě v SQL dotazu při aktualizaci: <br/>'.$E->getMessage());
     205          }
     206          $Output .= $this->ControlPanel();
     207        }
     208        else if ($Action == 'install')
     209        {
     210          $Output .= '<h3>Instalace</h3>';
     211          ModuleSetup::Cast($this->System->GetModule('Setup'))->Install();
     212          $this->System->ModuleManager->LoadModules();
     213          $this->System->ModuleManager->SaveState();
     214          //$Output .= ModuleSetup::Cast($this->System->GetModule('Setup'))->Upgrade();
     215          $Output .= $this->ControlPanel();
     216        }
     217        else if ($Action == 'uninstall')
     218        {
     219          $Output .= '<h3>Odinstalace</h3>';
     220          ModuleSetup::Cast($this->System->GetModule('Setup'))->Uninstall();
     221          $Output .= $this->ControlPanel();
     222        }
     223        else if ($Action == 'reload_modules')
     224        {
     225          $Output .= '<h3>Znovunačtení seznamu modulů</h3>';
     226          $this->System->ModuleManager->LoadModules();
     227          $this->System->ModuleManager->SaveState();
     228          $Output .= $this->ControlPanel();
     229        }
     230        else if ($Action == 'insert_sample_data')
     231        {
     232          $Output .= '<h3>Vložení vzorových dat</h3>';
     233          ModuleSetup::Cast($this->System->GetModule('Setup'))->InsertSampleData();
     234          $Output .= $this->ControlPanel();
     235        }
     236        else if ($Action == 'configure_save')
     237        {
     238           $Output .= $this->ConfigSave($this->Config);
     239           $Output .= $this->ControlPanel();
     240        }
     241        else if ($Action == 'configure')
     242        {
     243          $Output .= $this->PrepareConfig($this->Config);
     244        }
     245        else
     246        {
     247          $Output .= $this->ControlPanel();
     248        }
     249      }
     250    } else
     251    {
     252      if (array_key_exists('configure_save', $_POST))
     253      {
     254        $Output .= $this->ConfigSave(array());
     255        $Output .= 'Pokračujte k přihlášení <a href="">zde</a>';
     256      } else {
     257        $Output .= $this->PrepareConfig(array());
     258      }
     259    }
    247260    return $Output;
    248261  }
     
    256269      $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože soubor "'.$this->ConfigDir.'/Config.php" není povolen pro zápis!';
    257270    $Output .= '<h3>Nastavení systému</h3>'.
    258         '<form action="?action=configure_save" method="post">'.
    259         '<table>';
     271      '<form action="?action=configure_save" method="post">'.
     272      '<table>';
    260273    foreach ($this->ConfigDefinition as $Def)
    261274    {
     
    279292    }
    280293    $Output .= '</td></tr>'.
    281         '<tr><td colspan="2"><input type="submit" name="configure_save" value="'.T('Save').'"/></td></tr>'.
    282         '</table>'.
    283         '</form>';
     294      '<tr><td colspan="2"><input type="submit" name="configure_save" value="'.T('Save').'"/></td></tr>'.
     295      '</table>'.
     296      '</form>';
    284297    return $Output;
    285298  }
     
    365378    if (!$this->Database->Connected()) $Output .= T('Can\'t connect to database').'<br>';
    366379    else {
    367       if (!$this->System->Setup->UpdateManager->IsInstalled())
     380      if (!ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager->IsInstalled())
    368381        $Output .= T('System requires database initialization').'<br>';
    369382      else
    370       if (!$this->System->Setup->UpdateManager->IsUpToDate())
     383      if (!ModuleSetup::Cast($this->System->GetModule('Setup'))->UpdateManager->IsUpToDate())
    371384        $Output .= T('System requires database upgrade').'<br>';
    372385    }
     
    376389}
    377390
    378 class Setup extends Model
     391class ModuleSetup extends AppModule
    379392{
    380393  public UpdateManager $UpdateManager;
    381394
    382   function Start()
     395  function __construct(System $System)
    383396  {
    384397    global $DatabaseRevision;
    385398
    386     $this->System->RegisterPage([''], 'PageSetupRedirect');
    387     $this->System->RegisterPage(['setup'], 'PageSetup');
     399    parent::__construct($System);
     400    $this->Name = 'Setup';
     401    $this->Version = '1.0';
     402    $this->Creator = 'Chronos';
     403    $this->License = 'GNU/GPLv3';
     404    $this->Description = 'Base setup module';
     405    $this->Dependencies = array();
     406    $this->Revision = 1;
     407    $this->SystemModule = true;
    388408
    389409    // Check database persistence structure
     
    394414  }
    395415
    396   function Stop()
     416  static function Cast(AppModule $AppModule): ModuleSetup
     417  {
     418    if ($AppModule instanceof ModuleSetup)
     419    {
     420      return $AppModule;
     421    }
     422    throw new Exception('Expected ModuleSetup type but '.gettype($AppModule));
     423  }
     424
     425  function DoStart(): void
     426  {
     427    $this->System->RegisterPage([''], 'PageSetupRedirect');
     428    $this->System->RegisterPage(['setup'], 'PageSetup');
     429    $this->System->RegisterPage(['setup', 'modules'], 'PageSetupModules');
     430  }
     431
     432  function DoStop(): void
    397433  {
    398434    unset($this->UpdateManager);
    399435    $this->System->UnregisterPage(['']);
    400436    $this->System->UnregisterPage(['setup']);
     437    $this->System->UnregisterPage(['setup', 'modules']);
    401438  }
    402439
     
    407444  }
    408445
    409   function Install()
     446  function DoInstall(): void
    410447  {
    411448    global $DatabaseRevision;
     
    444481  }
    445482
    446   function Uninstall()
     483  function DoUninstall(): void
    447484  {
    448485    $this->System->ModuleManager->UninstallAll();
     
    459496  }
    460497
    461   function Upgrade(): string
     498  function DoUpgrade(): string
    462499  {
    463500    $Updates = new Updates();
     
    466503    return $Output;
    467504  }
     505
     506  function InsertSampleData(): void
     507  {
     508  }
    468509}
Note: See TracChangeset for help on using the changeset viewer.