Ignore:
Timestamp:
Oct 10, 2012, 9:29:20 AM (12 years ago)
Author:
chronos
Message:
  • Upraveno: Modulární systém přepracován tak, že základní modul System je zodpovědný za udržování stavu instalování modulů v databázi a je instalován samostatně jako první modul. Následně lze instalovat moduly dle závislostí.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Modular/Modules/System/System.php

    r405 r424  
    109109  function Install()
    110110  {
    111     // Do nothing, already installed by ModuleManager
    112111  }
    113112 
    114113  function UnInstall()
    115114  {
    116     // Do nothing, managed by ModuleManager
    117   } 
     115  }   
    118116}
    119117
     
    135133  function Install()
    136134  {
     135    if($this->IsInstalled()) return;
    137136    parent::Install();
     137    $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
     138  `Id` int(11) NOT NULL AUTO_INCREMENT,
     139  `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     140  `Description` datetime NOT NULL,
     141  PRIMARY KEY (`Id`)
     142) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
     143    $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModule` (
     144  `Id` int(11) NOT NULL AUTO_INCREMENT,
     145  `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     146  `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     147  `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     148  `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     149  `Installed` int(11) NOT NULL,
     150  `Description` text COLLATE utf8_czech_ci NOT NULL,
     151  PRIMARY KEY (`Id`)
     152) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
     153   
     154    $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModuleDependency` (
     155  `Id` int(11) NOT NULL AUTO_INCREMENT,
     156  `Module` int(11) NOT NULL,
     157  `DependencyModule` int(11) NOT NULL,
     158  PRIMARY KEY (`Id`),
     159  KEY (`Module`),
     160  KEY (`DependencyModule`),
     161  UNIQUE (`Module` , `DependencyModule`)
     162) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
     163    $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
     164    $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_2` FOREIGN KEY ( `DependencyModule` ) REFERENCES `SystemModule` (`Id`)');
     165   
     166    $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModel` (
     167  `Id` int(11) NOT NULL AUTO_INCREMENT,
     168  `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     169  `Module` int(11) NOT NULL,
     170  KEY (`Module`),
     171  PRIMARY KEY (`Id`)
     172) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
     173    $this->Database->query('ALTER TABLE `SystemModel` ADD CONSTRAINT `SystemModel_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
     174   
     175    $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModelProperty` (
     176  `Id` int(11) NOT NULL AUTO_INCREMENT,
     177  `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     178  `Type` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     179  `Model` int(11) NOT NULL,
     180  KEY (`Model`),
     181  PRIMARY KEY (`Id`)
     182) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
     183    $this->Database->query('ALTER TABLE `SystemModelProperty` ADD CONSTRAINT `SystemModelProperty_ibfk_1` FOREIGN KEY ( `Model` ) REFERENCES `SystemModel` (`Id`)');
    138184  }
    139185 
     
    141187  {
    142188    parent::UnInstall();
    143   }
    144 
    145   function Init()
    146   {
    147     $this->System->Pages['modules'] = 'PageModules';
    148   }
     189    if(!$this->IsInstalled()) return;
     190   
     191    // Delete tables with reverse order
     192    $this->Database->query('ALTER TABLE `SystemModelProperty` DROP FOREIGN KEY `SystemModelProperty_ibfk_1`');
     193    $this->Database->query('DROP TABLE IF EXISTS `SystemModelProperty`');
     194    $this->Database->query('ALTER TABLE `SystemModel` DROP FOREIGN KEY `SystemModel_ibfk_1`');
     195    $this->Database->query('DROP TABLE IF EXISTS `SystemModel`');
     196    $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_1`');
     197    $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_2`');
     198    $this->Database->query('DROP TABLE IF EXISTS `SystemModuleDependency`');
     199    $this->Database->query('DROP TABLE IF EXISTS `SystemModule`');
     200    $this->Database->query('DROP TABLE IF EXISTS `SystemVersion`');
     201  }
     202
     203  function Start()
     204  {
     205    parent::Start();
     206    $this->System->Pages['module'] = 'PageModules';
     207    $this->ModularSystem->OnModuleChange = array($this, 'ModuleChange');
     208    $this->LoadFromDatabase();
     209  }
     210 
     211  function Stop()
     212  {
     213    parent::Stop();
     214  }
     215 
     216  function IsInstalled()
     217  {
     218    $DbResult = $this->Database->query('SELECT table_name FROM information_schema.tables
     219WHERE table_schema = "'.$this->Database->Database.'" AND table_name = "SystemVersion";');   
     220    if($DbResult->num_rows > 0) return(true);
     221      else return(false);
     222  }
     223 
     224  function ModuleChange($Module)
     225  {
     226    if($this->IsInstalled())
     227    {
     228      if($Module->Installed)
     229      $this->Database->query('UPDATE `SystemModule` SET `Installed`=1 WHERE `Name`="'.$Module->Name.'"');
     230        else $this->Database->query('UPDATE `SystemModule` SET `Installed`=0 WHERE `Name`="'.$Module->Name.'"');
     231    }
     232  }
     233 
     234  function LoadFromDatabase()
     235  {
     236    //DebugLog('Loading modules...');
     237    $this->Modules = array();
     238    $Query = 'SELECT `Id`, `Name`,`Installed` FROM `SystemModule`';
     239    $DbResult = $this->Database->query($Query);
     240    while($Module = $DbResult->fetch_array())
     241    {
     242      //echo($Module['Name'].',');
     243      include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
     244      $ModuleClassName = 'Module'.$Module['Name'];
     245      $NewModule = new $ModuleClassName($this->Database, $this);     
     246      $NewModule->Id = $Module['Id'];
     247      $NewModule->Installed = $Module['Installed'];     
     248      $this->ModularSystem->RegisterModule($NewModule);
     249    }     
     250  }
     251 
     252  function SaveToDatabase()
     253  {
     254    $Modules = array();
     255    $DbResult = $this->Database->query('SELECT * FROM `SystemModule`');
     256    while($DbRow = $DbResult->fetch_assoc())
     257      $Modules[$DbRow['Name']] = $DbRow;
     258
     259    // Add missing
     260    foreach($this->ModularSystem->Modules as $Module)   
     261    {     
     262      if(!array_key_exists($Module->Name, $Modules))
     263      $this->Database->insert('SystemModule', array('Name' => $Module->Name,
     264        'Version' => $Module->Version, 'Creator' => $Module->Creator,
     265        'Description' => $Module->Description, 'License' => $Module->License,
     266        'Installed' => $Module->Installed));
     267     else $this->Database->update('SystemModule', 'Name = "'.$Module->Name.'"', array(
     268        'Version' => $Module->Version, 'Creator' => $Module->Creator,
     269        'Description' => $Module->Description, 'License' => $Module->License,
     270        'Installed' => $Module->Installed));
     271    }
     272   
     273    // Remove exceeding
     274    foreach($Modules as $Module)   
     275    if(!$this->ModularSystem->ModulePresent($Module['Name']))
     276    {
     277      DebugLog('Removing module '.$Module['Name'].' from list');
     278      $this->Database->query('DELETE FROM `SystemModule` WHERE `Id` = '.$Module['Id']);
     279    }   
     280   
     281    // Reload dependencies
     282    $DbDependency = array();
     283    $DbResult = $this->Database->query('SELECT * FROM `SystemModuleDependency`');
     284    while($DbRow = $DbResult->fetch_assoc())
     285      $DbDependency[$DbRow['Module']][] = $DbRow['DependencyModule'];
     286   
     287    foreach($this->ModularSystem->Modules as $Module)
     288    {
     289      // Add missing
     290      foreach($Module->Dependencies as $Dependency)
     291      {
     292        if(!array_key_exists($Module->Id, $DbDependency) or
     293        !in_array($this->ModularSystem->Modules[$Dependency]->Id, $DbDependency[$Module->Id]))
     294        $this->Database->insert('SystemModuleDependency', array('Module' => $Module->Id,
     295          'DependencyModule' => $this->ModularSystem->Modules[$Dependency]->Id));       
     296      }
     297     
     298      // Remove exceeding
     299      if(array_key_exists($Module->Id, $DbDependency))
     300      foreach($DbDependency[$Module->Id] as $Dep)
     301      {
     302        $DepModName = $this->ModularSystem->SearchModuleById($Dep);
     303        if(!in_array($DepModName, $Module->Dependencies))
     304        $this->Database->query('DELETE FROM `SystemModuleDependency` WHERE `Module` = '.
     305          $Module->Id.' AND DependencyModule='.$Dep);
     306      }     
     307    }   
     308  }
    149309}
    150310
Note: See TracChangeset for help on using the changeset viewer.