<?php

class objects
{
  public $debug = false;
  public $info = array();
  public $key_value = false;
  public $set_info = array();
  public $fields = false;
  public $table = false;
   
  protected function __construct( $key=0,$array=array() ){
    /*if ( empty($key) ){
      throw new Exseption('Клиническая ошибка');
    }*/
    $this -> key_value = $key;
    $this -> info = $array;
  }
  
  public function get_info(){
	
    return $this -> info;
  }
  
	public function __get($k){
	
    return $this -> get($k);
  }
  public function get($k){
   
    if ( isset($this -> info[$k]) ){
      return $this -> info[$k];
    } 
    throw new Exception(sprintf('ошибка: нет свойства %s',$k));
  }
  
  
  public function set($k,$v){
      /// перенесено в методы Insert/update
 /* global $MAIN_DB;
     $f  = $this -> fields;
   
    if ( method_exists($this -> fields,$k) ){
      $v =call_user_func( array($f,$k),$v,$this);
    }
    if ( $v===false){
      return false;
    }*/
    $this -> set_info[$k] = $v;
    //$this -> info[$k] = $v;
    return TRUE;
  }
  
  public function __set($k,$v){
    return $this -> set($k,$v);
  }  
  
  public function set_debug($flag){
    $this -> debug = $flag;
  }
  
  
  public function update(){
  global $MAIN_DB;
    if ( sizeof($this -> set_info)==0 ){
      throw new Exception(E_EMPTY_ARRAY);
    }
    $sql = array();
    $f  = $this -> fields;
    foreach( $this -> set_info as $k=>$v ){
      if ( method_exists($this -> fields,$k) ){
        $v =call_user_func( array($f,$k),$v,$this,$this -> set_info);
      }
      if ( false===$v){
        continue;
      }
      $sql[]="`$k`='".sys_in_sql($v)."'";
      $this -> info[$k] = $v;
    }
    $sql = "
      UPDATE ".$MAIN_DB -> prefix( $this -> table )." 
      SET ".implode(',',$sql)." 
      WHERE ".$this -> key."='".$this -> key_value."'";
    if ( !empty($this -> debug) ){
      HL::ajaxDebug($sql);
    }
    $MAIN_DB -> query($sql);
    $this -> set_info = array();
    $this -> onChange( 'update' );
    return $this -> key_value;
  }
	
  public function insert(){
  global $MAIN_DB;
    
    if ( sizeof($this -> set_info)==0 ){
      throw new Exception(E_EMPTY_ARRAY);
    }
      
    $sql = array();
    $f  = $this -> fields;
  
    foreach( $this -> set_info as $k=>$v ){
     if ( method_exists($this -> fields,$k) ){
        $v =call_user_func( array($f,$k),$v,$this, $this -> set_info);
      }
      if ( false===$v){
        echo $v;
        continue;
      }
      $sql[]="`$k`='".sys_in_sql($v)."'";
      $this -> info[$k] =$v;
    }
    $sql = "INSERT INTO ".$MAIN_DB -> prefix(  $this -> table )." SET ".implode(',',$sql)."";
    if ( !empty($this -> debug) ){
      HL::ajaxDebug($sql);
    }
    $MAIN_DB -> query($sql);
    $id = $MAIN_DB -> insert_id();
    $this -> info[$this -> key] =$id;

    $this -> key_value = $id;
    $this -> set_info = array();
    $this -> onChange( 'insert' );
    return  $id;
  }
  
  
  
  public function delete(){
  global $MAIN_DB;
    $sql = "DELETE FROM ".$MAIN_DB -> prefix($this -> table)." WHERE ".$this -> key."='".$this -> key_value."'";
    if ( !empty($this -> debug) ){
      HL::ajaxDebug($sql);
    }

    $MAIN_DB -> query($sql);
    $this -> onChange( 'delete' );
    return TRUE;
  }
  


	
	public function onChange($act){}
	static function query($a){}
}
?>
