<?php

define('PropertyDate', 'Date');
define('PropertyTime', 'Time');
define('PropertyDateTime', 'DateTime');
define('PropertyText', 'Text');
define('PropertyString', 'String');
define('PropertyBoolean', 'Boolean');
define('PropertyInteger', 'Integer');
define('PropertyFloat', 'Float');
define('PropertyOneToMany', 'OneToMany');
define('PropertyManyToOne', 'ManyToOne');
define('PropertyManyToMany', 'ManyToMany');

class Model 
{
  var $Database;    
  var $Name;
  var $Properties;
  var $System;
  
  function __construct($Database, $System)
  {
    $this->Database = &$Database;
    $this->System = &$System;
    $this->AddPropertyDateTime('TimeCreate');
    $this->AddPropertyOneToMany('UserCreate', 'User');
    $this->AddPropertyDateTime('TimeModify');
    $this->AddPropertyOneToMany('UserModify', 'User');
    $this->AddPropertyOneToMany('Module', 'Module');
  }
  
  function AddPropertyDateTime($Name)
  {
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDateTime); 
  }
  
  function AddPropertyDate($Name)
  {
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDate); 
  }
  
  function AddPropertyTime($Name)
  {
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyTime); 
  }
  
  function AddPropertyOneToMany($Name, $TargetModel)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyOneToMany, 'TargetModel' => $TargetModel); 
  }
  
  function AddPropertyText($Name)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText); 
  }

  function AddPropertyString($Name)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyString); 
  }

  function AddPropertyInteger($Name)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger); 
  }

    function AddPropertyFloat($Name)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyFloat); 
  }

  function AddPropertyBoolean($Name)
  {
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyBoolean); 
  }

  function AddPropertyManyToOne($Name, $TargetModel, $TargetColumn)
  {      
    $this->Properties[] = array('Name' => $Name, 'Type' => PropertyManyToOne, 
      'TargetModel' => $TargetModel, 'TargetColumn' => $TargetColumn); 
  }
  
  function AddPropertyManyToMany($TargetModel, $Relation, $ColumOwn, $ColumnTarget)
  {      
    $this->Properties[] = array('Type' => PropertyManyToMany, 
      'TargetModel' => $TargetModel, 'Relation' => $Relation, 'ColumnOwn' => $ColumnOwn,
      'ColumnTarget' => $ColumnTarget); 
  }
  
  function Install()
  {
    $Query = 'CREATE TABLE IF NOT EXISTS `'.$this->Name.'` ('.
      '`Id` int(11) NOT NULL AUTO_INCREMENT,';
    foreach($this->Properties as $Property)
    {
      if($Property['Type'] == PropertyDateTime) 
        $Query .= '`'.$Property['Name'].'` DATETIME NOT NULL,';
      else if($Property['Type'] == PropertyDate) 
        $Query .= '`'.$Property['Name'].'` DATE NOT NULL,';
      else if($Property['Type'] == PropertyTime) 
        $Query .= '`'.$Property['Name'].'` TIME NOT NULL,';
      else if($Property['Type'] == PropertyString) 
        $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,';
      else if($Property['Type'] == PropertyText) 
        $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci NOT NULL,';
      else if($Property['Type'] == PropertyInteger) 
        $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,';
      else if($Property['Type'] == PropertyBoolean) 
        $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,';
      else if($Property['Type'] == PropertyFloat) 
        $Query .= '`'.$Property['Name'].'` FLOAT NOT NULL,';
      else if($Property['Type'] == PropertyOneToMany) 
        $Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
          'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
      else if($Property['Type'] == PropertyManyToOne) 
        $Query .= '';
      else if($Property['Type'] == PropertyManyToMany) ;
        // Create many-to-many table
        //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
        //  'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
    }
    $Query .= 'PRIMARY KEY (`Id`)'.
      ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;';          
    $this->Database->query($Query);
    foreach($this->Properties as $Property)
    {
      if($Property['Type'] == PropertyOneToMany) 
        $this->Database->query('ALTER TABLE `'.$this->Name.'` ADD CONSTRAINT '.
          '`'.$this->Name.'_ibfk_'.$Property['Name'].'` FOREIGN KEY (`'.$Property['TargetModel'].'`) REFERENCES `'.$Property['TargetModel'].'` (`Id`);');
    }
  }
    
  function UnInstall()
  {
    foreach($Model->Properties as $Property)
    {
      if($Property['Type'] == PropertyManyToMany)
        ; // Delete many-to-many table
    }
    $this->Database->query('DROP TABLE IF EXISTS `'.$this->Name.'`');
  }
}

?>
