<?php

// Extended database class
// Date: 2012-06-12

class Database extends mysqli
{
  var $HostName = 'localhost';
  var $UserName;
  var $Password;
  var $Schema;
  var $Charset = 'utf8';
  var $Prefix = '';
  var $ShowSQLQuery = false;
  var $ShowSQLError = false;
  var $Config;

  function __construct()
  {
    $this->Config = new Config();
    $this->HostName = $this->Config->Database['Host'];
    $this->UserName = $this->Config->Database['User'];
    $this->Password = $this->Config->Database['Password'];
    $this->Schema = $this->Config->Database['Database'];
    $this->Charset = $this->Config->Database['Charset'];
    $this->ShowSQLQuery = $this->Config->Web['ShowSQLQuery'];
    $this->ShowSQLError = $this->Config->Web['ShowSQLError'];
    $this->open();
    
  }

  function open()
  {
    parent::connect($this->HostName, $this->UserName, $this->Password, $this->Schema);
    $this->charset($this->Charset);
  }

  function query($Query)
  {
    if($this->ShowSQLQuery)
    {
  echo $this->ShowSQLQuery;
      if(isset($_SERVER['REMOTE_ADDR'])) echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'); 
      else echo($Query."\n");
    }
    $Result = parent::query($Query);
    if(($this->error != '') and ($this->ShowSQLError))
    {
      if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
      echo('SQL Error: '.$this->error.' '.$Query."\n");
    }

    return($Result);  
  }

  function select($Table, $What = '*', $Condition = 1)
  {
    return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));  
  }

  function delete($Table, $Condition)
  {
    $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);  
  }
  
  function insert($Table, $Data)
  {
    $Name = '';
    $Values = '';
    foreach($Data as $Key => $Value)
    {
      $Name .= ',`'.$Key.'`';
      $Values .= ','.$Value;
    }
    $Name = substr($Name, 1);
    $Values = substr($Values, 1);
    $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 
  }
  
  function update($Table, $Condition, $Data)
  {
    $Values = '';
    foreach($Data as $Key => $Value)
    {
      $Values .= ', `'.$Key.'`='.$Value;
    }
    $Values = substr($Values, 2);  
    $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
  }
  
  function replace($Table, $Data)
  {
    $Name = '';
    $Values = '';
    foreach($Data as $Key => $Value)
    {
      $Name .= ',`'.$Key.'`';
      $Values .= ','.$Value;
    }
    $Name = substr($Name, 1);
    $Values = substr($Values, 1);
    //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
    $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    //echo($this->error().'<br>');
  }
  
  function charset($Charset)
  {
    $this->query('SET NAMES "'.$Charset.'"');
  }

  function TimeToMysqlDateTime($Time)
  {
    return(date('Y-m-d H:i:s', $Time));
  }

  function MysqlDateTimeToTime($Time)
  {
    $Parts = explode(' ', $Time);
    $DateParts = explode('-', $Parts[0]);
    $TimeParts = explode(':', $Parts[1]);
    $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
    return($Result);
  }

  function MysqlDateToTime($Time)
  {
    return($this->MysqlDateTimeToTime($Time.' 0:0:0'));
  }
}

?>
