<?php

class OrganizMod extends DbMod{
  var $VarTableOrganizTypes = array('integer', 'text', 'text', 'text', 'text', 'text', 'text', 'text');

	function OrganizMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'o.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableOrganiz .' AS o';
    $ArrSqlArray['SqlAll']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableOrganiz .' AS o';
    $ArrSqlArray['SqlCount']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;  	
	}
  
  function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableOrganiz .'
                (organiz_id, organiz_name, organiz_note, organiz_address, organiz_tel, organiz_fax, organiz_person, organiz_persontel)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['organiz_name'],
        $values['organiz_note'],
        $values['organiz_address'],
        $values['organiz_tel'],
        $values['organiz_fax'],
        $values['organiz_person'],
        $values['organiz_persontel']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableOrganizTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableOrganiz, 'organiz_id');
      if (PEAR::isError($VarId)) {
        return array('error' => $VarId->getMessage());
      }
      return $VarId;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Edit($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = "UPDATE ". $this->VarTableOrganiz ." SET 
                  organiz_name = ?,
                  organiz_note = ?,
                  organiz_address = ?,
                  organiz_tel = ?,
                  organiz_fax = ?,
                  organiz_person = ?,
                  organiz_persontel = ?
                WHERE organiz_id = '". $values['organiz_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
                  $values['organiz_name'],
                  $values['organiz_note'],
                  $values['organiz_address'],
                  $values['organiz_tel'],
                  $values['organiz_fax'],
                  $values['organiz_person'],
                  $values['organiz_persontel']
                );
      $sth =& $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['organiz_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'organiz_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableOrganiz ." WHERE ". $VarFiledName ." = '". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarQueryResult =& $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function PrepareData($values){
//    if(true === is_array($values) && 0 < count($values)){
//      
//    }
    return $values;
  }
}
?>