Changeset 898


Ignore:
Timestamp:
Feb 4, 2021, 11:55:44 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Initialization of modular system base tables.
Location:
trunk/Packages/Common
Files:
4 edited

Legend:

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

    r897 r898  
    7171    $this->Version = '';
    7272    $this->Creator = '';
     73    $this->Title = '';
    7374    $this->Description = '';
    7475    $this->Dependencies = array();
     
    8586  {
    8687    if ($this->Installed) return;
     88    echo('Install mod '.$this->Name.'<br/>');
    8789    $List = array();
    8890    $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled));
    8991    $this->Manager->PerformList($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
    90     $this->DoBeforeInstall();
    91     $this->InstallModels();
    92     $this->DoInstall();
    9392    $this->Installed = true;
    9493    $this->Enabled = true; // Automatically enable installed module
    9594    $this->InstalledVersion = $this->Version;
    9695    $this->Manager->Modules[$this->Name] = $this;
     96    $this->DoBeforeInstall();
     97    if ($this->Manager->OnInstallModule != null)
     98    {
     99      call_user_func($this->Manager->OnInstallModule, $this);
     100    }
     101    $this->InstallModels();
     102    $this->DoInstall();
    97103  }
    98104
     
    107113    $this->DoUninstall();
    108114    $this->UninstallModels();
     115    if ($this->Manager->OnUninstallModule != null)
     116    {
     117      call_user_func($this->Manager->OnUninstallModule, $this);
     118    }
    109119    $this->DoAfterUninstall();
    110120  }
     
    214224      foreach ($this->GetModels() as $Model)
    215225      {
    216         call_user_func($this->Manager->OnInstallModel, $Model::GetDesc());
     226        call_user_func($this->Manager->OnInstallModel, $Model::GetDesc(), $this);
    217227      }
    218228    }
     
    225235      foreach (array_reverse($this->GetModels()) as $Model)
    226236      {
    227         call_user_func($this->Manager->OnUninstallModel, $Model::GetDesc());
     237        call_user_func($this->Manager->OnUninstallModel, $Model::GetDesc(), $this);
    228238      }
    229239    }
     
    241251  public $OnInstallModel;
    242252  public $OnUninstsallModel;
     253  public $OnInstallModule;
     254  public $OnUninstsallModule;
    243255
    244256  function __construct(System $System)
     
    250262    $this->OnInstallModel = null;
    251263    $this->OnUninstallModel = null;
     264    $this->OnInstallModule = null;
     265    $this->OnUninstallModule = null;
    252266  }
    253267
     
    456470  }
    457471
     472  function GetUniqueModuleId()
     473  {
     474    $Id = 1;
     475    foreach ($this->Modules as $Module)
     476    {
     477      if ($Module->Id >= $Id) $Id = $Module->Id + 1;
     478    }
     479    return $Id;
     480  }
     481
    458482  function LoadModule(string $FileName): AppModule
    459483  {
     
    461485    $ModuleName = 'Module'.pathinfo($FileName, PATHINFO_FILENAME);
    462486    $Module = new $ModuleName($this->System);
     487    $Module->Id = $this->GetUniqueModuleId();
    463488    $this->RegisterModule($Module);
    464489    return $Module;
  • trunk/Packages/Common/Base.php

    r894 r898  
    150150  const Enum = 8;
    151151  const BigInt = 9;
     152
     153  static function GetName(int $Index)
     154  {
     155    $Text = array('Integer', 'String', 'Float', 'Text', 'DateTime', 'Reference', 'Boolean', 'Date', 'Enum', 'BigInt');
     156    return $Text[$Index];
     157  }
    152158}
    153159
  • trunk/Packages/Common/Database.php

    r897 r898  
    129129  }
    130130
    131   function delete(string $Table, string $Condition)
     131  function delete(string $Table, string $Condition): void
    132132  {
    133133    $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
    134134  }
    135135
    136   function insert(string $Table, array $Data)
     136  function insert(string $Table, array $Data): void
    137137  {
    138138    $this->query($this->GetInsert($Table, $Data));
  • trunk/Packages/Common/Modules/ModuleManager.php

    r897 r898  
    2222    $this->Manager->OnInstallModel = array($this, 'InstallModel');
    2323    $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
     24    $this->Manager->OnInstallModule = array($this, 'InstallModule');
     25    $this->Manager->OnUninstallModule = array($this, 'UninstallModule');
    2426
    2527    Core::Cast($this->System)->RegisterPage(['modules'], 'PageModules');
     
    140142  function DoBeforeInstall(): void
    141143  {
     144    $this->AddModelDatabase(Module::GetDesc());
     145    $this->Manager->OnInstallModule = array($this, 'InstallModule');
     146    $this->Manager->OnUninstallModule = array($this, 'UninstallModule');
     147    $this->AddModelDatabase(ModuleModel::GetDesc());
    142148    $this->Manager->OnInstallModel = array($this, 'InstallModel');
    143149    $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
     
    148154    $this->Manager->OnInstallModel = null;
    149155    $this->Manager->OnUninstallModel = null;
     156    $this->RemoveModelDatabase(ModuleModel::GetDesc());
     157    $this->Manager->OnInstallModule = null;
     158    $this->Manager->OnUninstallModule = null;
     159    $this->RemoveModelDatabase(Module::GetDesc());
    150160  }
    151161
    152162  function GetModels(): array
    153163  {
    154     return array(Module::GetClassName(), ModuleModel::GetClassName(), ModuleModelField::GetClassName(),
    155       ModuleDependency::GetClassName());
    156   }
    157 
    158   function InstallModel(ModelDesc $ModelDesc)
     164    return array(ModuleModelField::GetClassName(), ModuleDependency::GetClassName());
     165  }
     166
     167  function AddModelDatabase(ModelDesc $ModelDesc)
    159168  {
    160169    $Query = "CREATE TABLE IF NOT EXISTS `".$ModelDesc->Name."` (\n";
     
    212221  }
    213222
    214   function UninstallModel(ModelDesc $ModelDesc)
     223  function RemoveModelDatabase(ModelDesc $ModelDesc)
    215224  {
    216225    /*$Query = '';
     
    227236*/
    228237    $this->Database->query('SET foreign_key_checks = 0; DROP TABLE IF EXISTS `'.$ModelDesc->Name.'`');
     238  }
     239
     240  function InstallModel(ModelDesc $ModelDesc, AppModule $Module)
     241  {
     242    $this->AddModelDatabase($ModelDesc);
     243    $this->Database->insert('ModuleModel', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id));
     244    $ModelId = $this->Database->insert_id;
     245    foreach ($ModelDesc->Columns as $Field)
     246    {
     247      $this->Database->insert('ModuleModelField', array('Name' => $Field->Name, 'Model' => $ModelId,
     248        'Type' => ModelColumnType::GetName($Field->Type), 'Nullable' => (int)$Field->Nullable));
     249    }
     250  }
     251
     252  function UninstallModel(ModelDesc $ModelDesc, AppModule $Module)
     253  {
     254    $this->Database->delete('ModuleModel', '(Name='.$ModelDesc->Name.') AND (Module='.$Module->Id.')');
     255    $this->RemoveModelDatabase($ModelDesc);
     256  }
     257
     258  function InstallModule(AppModule $Module)
     259  {
     260    echo('Install module '.$Module->Name);
     261    $this->Database->insert('Module', array(
     262      'Name' => $Module->Name,
     263      'Title' => $Module->Title,
     264      'Creator' => $Module->Creator,
     265      'Version' => $Module->Version,
     266      'License' => $Module->License,
     267      'Installed' => (int)$Module->Installed,
     268      'Description' => $Module->Description,
     269      'InstalledVersion' => $Module->InstalledVersion
     270    ));
     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);
    229278  }
    230279
     
    519568    $Desc = new ModelDesc(self::GetClassName());
    520569    $Desc->AddString('Name');
    521     $Desc->AddReference('Module', ModuleModel::GetClassName());
     570    $Desc->AddReference('Model', ModuleModel::GetClassName());
    522571    $Desc->AddString('Type');
    523572    $Desc->AddBoolean('Nullable');
Note: See TracChangeset for help on using the changeset viewer.