Ignore:
Timestamp:
Jan 12, 2021, 10:29:50 PM (4 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/Modules/System/System.php

    r894 r895  
    11<?php
     2
     3include_once(dirname(__FILE__).'/SystemModels.php');
    24
    35class PageModules extends Page
     
    109111    $this->Description = 'Base system module';
    110112    $this->Dependencies = array();
     113    $this->SystemModule = true;
    111114  }
    112115
     
    115118    return array(UnitOfMeasure::GetClassName(), ActionIcon::GetClassName(), ActionGroup::GetClassName(),
    116119      ActionType::GetClassName(), Action::GetClassName(), Language::GetClassName(), Country::GetClassName());
     120  }
     121
     122  function DoBeforeInstall(): void
     123  {
     124    $this->Manager->OnInstallModel = array($this, 'InstallModel');
     125    $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
     126  }
     127
     128  function DoAfterUninstall(): void
     129  {
     130    $this->Manager->OnInstallModel = null;
     131    $this->Manager->OnUninstallModel = null;
    117132  }
    118133
     
    184199  function DoStart(): void
    185200  {
     201    $this->Manager->OnInstallModel = array($this, 'InstallModel');
     202    $this->Manager->OnUninstallModel = array($this, 'UninstallModel');
     203
    186204    $this->System->RegisterPage(['module'], 'PageModules');
    187205    $this->System->FormManager->RegisterClass('Action', array(
     
    404422    //$this->Manager->OnModuleChange = array($this, 'ModuleChange');
    405423    //$this->LoadFromDatabase();
     424  }
     425
     426  function InstallModel(ModelDesc $ModelDesc)
     427  {
     428    $Query = "CREATE TABLE IF NOT EXISTS `".$ModelDesc->Name."` (\n";
     429    $Query .= '  `'.$ModelDesc->PrimaryKey.'` int(11) NOT NULL AUTO_INCREMENT,'."\n";
     430    foreach ($ModelDesc->Columns as $Column)
     431    {
     432      $Query .= "  `".$Column->Name."` ";
     433      if ($Column->Type == ModelColumnType::Integer) $Query .= 'int(11)';
     434      else if ($Column->Type == ModelColumnType::String) $Query .= 'varchar(255)';
     435      else if ($Column->Type == ModelColumnType::Float) $Query .= 'varchar(255)';
     436      else if ($Column->Type == ModelColumnType::Text) $Query .= 'text';
     437      else if ($Column->Type == ModelColumnType::DateTime) $Query .= 'datetime';
     438      else if ($Column->Type == ModelColumnType::Reference) $Query .= 'int(11)';
     439      else if ($Column->Type == ModelColumnType::Boolean) $Query .= 'tinyint(1)';
     440      else if ($Column->Type == ModelColumnType::Date) $Query .= 'date';
     441      else if ($Column->Type == ModelColumnType::BigInt) $Query .= 'bigint(20)';
     442      else if ($Column->Type == ModelColumnType::Enum)
     443      {
     444        $Query .= 'enum("'.implode('", "', $Column->States).'")';
     445      }
     446
     447      if ($Column->Nullable) $Query .= '';
     448        else $Query .= ' NOT NULL';
     449
     450      $Query .= ' COLLATE utf8_general_ci';
     451
     452      if ($Column->HasDefault)
     453      {
     454        if ($Column->Default == null)
     455          $Query .= ' DEFAULT NULL';
     456        else $Query .= ' DEFAULT '.$Column->GetDefault();
     457      }
     458      $Query .= ",\n";
     459    }
     460    $Query .= '  PRIMARY KEY (`'.$ModelDesc->PrimaryKey.'`)';
     461    foreach ($ModelDesc->Columns as $Column)
     462    {
     463      if ($Column->Type == ModelColumnType::Reference)
     464        $Query .= ','."\n".'  KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
     465      else if ($Column->Unique)
     466        $Query .= ','."\n".'  UNIQUE KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
     467    }
     468    $Query .= "\n";
     469
     470    if ($ModelDesc->Memory) $Engine = 'MEMORY';
     471      else $Engine = 'InnoDB';
     472    $Query .= ') ENGINE='.$Engine.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;';
     473    $I = 1;
     474    foreach ($ModelDesc->Columns as $Column)
     475    {
     476      if ($Column->Type == ModelColumnType::Reference)
     477        $Query .= "ALTER TABLE `".$ModelDesc->Name."` ".
     478        "ADD CONSTRAINT `".$ModelDesc->Name."_ibfk_".$I."` FOREIGN KEY (`".$Column->Name."`) REFERENCES `".$Column->RefTable."` (`Id`);";
     479      $I++;
     480    }
     481    $this->Database->query($Query);
     482  }
     483
     484  function UninstallModel(ModelDesc $ModelDesc)
     485  {
     486    $this->Database->query('DROP TABLE IF EXISTS `'.$ModelDesc->Name.'`');
    406487  }
    407488
     
    544625  }
    545626}
    546 
    547 class UnitOfMeasure extends Model
    548 {
    549   static function GetDesc(): ModelDesc
    550   {
    551     $Desc = new ModelDesc(self::GetClassName());
    552     $Desc->AddString('Name');
    553     $Desc->AddString('Unit');
    554     return $Desc;
    555   }
    556 }
    557 
    558 class Action extends Model
    559 {
    560   static function GetDesc(): ModelDesc
    561   {
    562     $Desc = new ModelDesc(self::GetClassName());
    563     $Desc->AddString('Title');
    564     $Desc->AddString('URL');
    565     $Desc->AddReference('Icon', ActionIcon::GetClassName());
    566     $Desc->AddReference('Type', ActionType::GetClassName());
    567     $Desc->AddReference('Group', ActionGroup::GetClassName());
    568     $Desc->AddReference('PermissionOperation', PermissionOperation::GetClassName());
    569     $Desc->AddBoolean('Enable');
    570     return $Desc;
    571   }
    572 }
    573 
    574 class ActionIcon extends Model
    575 {
    576   static function GetDesc(): ModelDesc
    577   {
    578     $Desc = new ModelDesc(self::GetClassName());
    579     $Desc->AddString('Name');
    580     return $Desc;
    581   }
    582 }
    583 
    584 class ActionGroup extends Model
    585 {
    586   static function GetDesc(): ModelDesc
    587   {
    588     $Desc = new ModelDesc(self::GetClassName());
    589     $Desc->AddString('Name');
    590     return $Desc;
    591   }
    592 }
    593 
    594 class ActionType extends Model
    595 {
    596   static function GetDesc(): ModelDesc
    597   {
    598     $Desc = new ModelDesc(self::GetClassName());
    599     $Desc->AddString('Name');
    600     return $Desc;
    601   }
    602 }
    603 
    604 class Language extends Model
    605 {
    606   static function GetDesc(): ModelDesc
    607   {
    608     $Desc = new ModelDesc(self::GetClassName());
    609     $Desc->AddString('Name');
    610     return $Desc;
    611   }
    612 }
    613 
    614 class Country extends Model
    615 {
    616   static function GetDesc(): ModelDesc
    617   {
    618     $Desc = new ModelDesc(self::GetClassName());
    619     $Desc->AddString('Name');
    620     return $Desc;
    621   }
    622 }
    623 
Note: See TracChangeset for help on using the changeset viewer.