Changeset 341


Ignore:
Timestamp:
Jan 17, 2012, 8:40:51 AM (13 years ago)
Author:
chronos
Message:
  • Přesunuto: Jednotky code a error.
  • Přidáno: Třída ModularSystem pro správu modulů systému.
  • Přidáno: Synchronizace seznamu modulů na disku a v databázi.
Location:
trunk
Files:
5 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Module.php

    r340 r341  
    11<?php
     2
     3include_once(dirname(__FILE__).'/../Model.php');
    24
    35class Module
     
    911  var $Description;
    1012  var $Dependencies;
     13  var $Models;
     14  var $Database;
     15  var $Installed;
     16 
     17  function __construct($Database)
     18  {
     19    $this->Database = &$Database;   
     20  }
    1121 
    1222  function Install()
    13   { 
     23  {
     24    DebugLog('Installing module '.$this->Name.'...');
     25    parent::Install();
     26    foreach($this->Models as $ModelName)
     27    {
     28      $Model = new $ModelName();
     29      $Query = 'CREATE TABLE IF NOT EXISTS `'.$ModelName.'` (
     30  `Id` int(11) NOT NULL AUTO_INCREMENT,';
     31      foreach($Model->Properties as $Property)
     32      {
     33        if($Property['Type'] == PropertyDateTime)
     34          $Query .= '`'.$Property['Name'].'` DATETIME NOT NULL,';
     35        else if($Property['Type'] == PropertyString)
     36          $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,';
     37        else if($Property['Type'] == PropertyText)
     38          $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci NOT NULL,';
     39        else if($Property['Type'] == PropertyInteger)
     40          $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,';
     41        else if($Property['Type'] == PropertyFloat)
     42          $Query .= '`'.$Property['Name'].'` FLOAT NOT NULL,';
     43        else if($Property['Type'] == PropertyOneToMany)
     44          $Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
     45            'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
     46        else if($Property['Type'] == PropertyManyToOne)
     47          $Query .= '';
     48        else if($Property['Type'] == PropertyManyToMany)
     49          ; // Create many-to-many table
     50          //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
     51          //  'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
     52      }
     53      $Query .= 'PRIMARY KEY (`Id`),'.
     54        ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;';         
     55      $this->Database->query($Query);
     56    }
     57    unset($Model);
    1458  }
    1559 
    1660  function UnInstall()
    1761  {
     62    DebugLog('Uninstalling module '.$this->Name.'...');
     63    parent::UnInstall();
     64    foreach($this->Models as $ModelName)
     65    {
     66      $Model = new $ModelName();
     67      if($Model['Type'] == PropertyManyToMany)
     68        ; // Delete many-to-many table
     69      $this->Database->query('DROP TABLE IF EXISTS `'.$ModelName.'`');
     70      unset($Model);
     71    }
     72  }
     73}
     74
     75class ModularSystem
     76{
     77  var $Database;
     78  var $Modules = array();
     79 
     80  function __construct($Database)
     81  {
     82    $this->Database = &$Database;
     83    $DbResult = $this->Database->query('SELECT * FROM Module WHERE Installed=1');
     84    while($Module = $DbResult->fetch_array())
     85    {
     86      include_once('Modules/'.$File.'/'.$File.'.php');
     87      $this->Modules[] = new $Module['Name']($this->Database);
     88    }
     89  }
     90 
     91  function Install()
     92  {
     93    DebugLog('Installing modular system core...');
     94    $this->Database->query('CREATE TABLE IF NOT EXISTS `Module` (
     95  `Id` int(11) NOT NULL AUTO_INCREMENT,
     96  `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     97  `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     98  `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     99  `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,
     100  `Installed` int(11) NOT NULL,
     101  PRIMARY KEY (`Id`)
     102) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;');
     103    foreach($this->Modules as $Module)
     104    {
     105      $Module->Install();
     106    }
     107  }
     108 
     109  function UnInstall()
     110  {
     111    DebugLog('Uninstalling modular system core...');
     112    foreach($this->Modules as $Module)
     113      $Module->UnInstall();
     114    $this->Database->query('DROP TABLE IF EXISTS `Module`');
     115  }
     116 
     117  function ReloadList()
     118  {
     119    // Load list of modules from database
     120    $Modules = array();
     121    $DbResult = $this->Database->query('SELECT * FROM Module');
     122    while($DbRow = $DbResult->fetch_assoc())
     123      $Modules[$DbRow['Name']] = $DbRow;
     124   
     125    // Load list of modules on disk
     126    $ModulesOnDisk = array();
     127    $Files = scandir('Modules');
     128    foreach($Files as $File)
     129    if(is_dir('Modules/'.$File) and ($File != '.') and ($File != '..'))
     130    {
     131      DebugLog($File.',');       
     132      $ModulesOnDisk[] = $File;
     133    }   
     134
     135    // Add new
     136    foreach($ModulesOnDisk as $ModuleName)
     137    if(!array_key_exists($ModuleName, $Modules))
     138    {
     139      DebugLog('Adding module '.$ModuleName.' to list');
     140      include_once('Modules/'.$ModuleName.'/'.$ModuleName.'.php');
     141      $ModuleClassName = 'Module'.$ModuleName;
     142      if(class_exists($ModuleClassName))
     143      {
     144        $Module = new $ModuleClassName($this->Database);       
     145        $this->Database->insert('Module', array('Name' => $Module->Name,
     146          'Version' => $Module->Version, 'Creator' => $Module->Creator,
     147          'Description' => $Module->Description, 'License' => $Module->License,
     148          'Installed' => 0));
     149        unset($Module);
     150      } else throw new Exception('Missing class '.$ModuleClassName.' in module '.$ModuleName);
     151    }
     152   
     153    // Remove missing
     154    foreach($Modules as $Module)
     155    if($Module['Installed'] == 0)
     156    {
     157      DebugLog('Removing module '.$ModuleName.' from list');
     158      $this->Database->query('DELETE FROM Module WHERE Id = '.$Module['Id']);
     159    }
    18160  }
    19161}
  • trunk/Modules/Project/Project.php

    r340 r341  
    22
    33include_once(dirname(__FILE__).'/../Module.php');
     4include_once(dirname(__FILE__).'/../../Model.php');
    45
    5 class Project extends Module
     6class Project extends Model
     7{
     8  function __construct($Database)
     9  {
     10    parent::__construct($Database);
     11    $this->Name = 'Project';
     12    $this->AddPropertyDateTime('TimeSchedule');
     13    $this->AddPropertyOneToMany('UserAssignedTo', 'User');
     14    $this->AddPropertyManyToOne('Comments', 'ProjectComment', 'Project');
     15    $this->AddPropertyText('Description');
     16  }
     17}
     18
     19class ProjectComment extends Model
    620{
    721  function __construct()
    822  {
     23    parent::__construct();
     24    $this->Name = 'ProjectComment';
     25    $this->AddPropertyOneToMany('Project', 'Project');
     26    $this->AddPropertyText('Text');     
     27  }
     28}
     29
     30class ModuleProject extends Module
     31{
     32  function __construct($Database)
     33  {
     34    parent::__construct($Database);
    935    $this->Name = 'Project';
    1036    $this->Version = '1.0';
     
    1339    $this->Description = 'Project and task management';
    1440    $this->Dependencies = array('User');
     41    $this->Models = array('Project', 'ProjectComment');
    1542  }
    1643 
    1744  function Install()
    1845  {
    19      $Query = 'CREATE TABLE IF NOT EXISTS `Project` (
    20   `Id` int(11) NOT NULL AUTO_INCREMENT,
    21   `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    22   `TimeCreate` datetime NOT NULL,
    23   `UserCreate` int(11) NOT NULL,
    24   `Description` text COLLATE utf8_czech_ci NOT NULL,
    25   `UserAssignedTo` int(11) DEFAULT NULL,
    26   `TimeSchedule` datetime DEFAULT NULL,
    27   PRIMARY KEY (`Id`),
    28   KEY `UserCreate` (`UserCreate`),
    29   KEY `UserAssignedTo` (`UserAssignedTo`)
    30 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;
    31 
    32 CREATE TABLE IF NOT EXISTS `ProjectComment` (
    33   `Id` int(11) NOT NULL AUTO_INCREMENT,
    34   `Project` int(11) NOT NULL,
    35   `Text` text COLLATE utf8_czech_ci NOT NULL,
    36   `TimeCreate` datetime NOT NULL,
    37   `UserCreate` int(11) NOT NULL,
    38   PRIMARY KEY (`Id`),
    39   KEY `Project` (`Project`),
    40   KEY `UserCreate` (`UserCreate`)
    41 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;';         
    4246  }
    4347 
     
    5054}
    5155
     56
    5257?>
  • trunk/Modules/User/user.php

    r340 r341  
    266266}
    267267
     268class ModuleUser extends Module
     269{
     270  function __construct($Database)
     271  {
     272    parent::__construct($Database);
     273    $this->Name = 'User';
     274    $this->Version = '1.0';
     275    $this->Creator = 'Chronos';
     276    $this->License = 'GNU/GPL';
     277    $this->Description = 'User management';
     278    $this->Dependencies = array();
     279    //$this->Models = array('User', 'UserOnline');
     280  }
     281}
     282
     283
    268284?>
  • trunk/global.php

    r340 r341  
    77 
    88include_once('database.php');
    9 //include('error.php');
    10 include_once('code.php');
     9include_once('Common/Error.php');
     10include_once('Common/Code.php');
    1111include_once('module.php');
    1212include_once('forms.php');
     
    1919include_once('finance/finance.php');
    2020include_once('Modules/Module.php');
     21include_once('Model.php');
    2122include_once('Modules/User/user.php');
    2223include_once('Modules/Project/Project.php');
     
    221222  $System->AddModule(new Bill());
    222223  $System->AddModule(new Finance());
    223   $System->Modules['Finance']->LoadMonthParameters(0);
     224  $System->Modules['Finance']->LoadMonthParameters(0);
     225 
     226  $System->ModularSystem = new ModularSystem($Database);
     227  $System->ModularSystem->ReloadList();
    224228}
    225229
     
    521525}
    522526
     527function DebugLog($Text)
     528{
     529  global $Config;
     530 
     531  if($Config['Web']['ShowDebug'])
     532  {
     533    if(isset($_SERVER['REMOTE_ADDR'])) $Ending = '<br/>'."\n";
     534      else $Ending = "\n";
     535      echo($Text.$Ending);
     536  }
     537}
     538
    523539GlobalInit();
    524540
Note: See TracChangeset for help on using the changeset viewer.