<?php

class DemandsCommentsMod extends DbMod{
  var $VarTableDemandsCommentsTypes = array('integer', 'integer', 'integer', 'text', 'date', 'integer', 'text');
  
  function DemandsCommentsMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}
	
	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*, e.users_fio';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableDemandsComments .' AS a';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $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->VarTableDemandsComments .' AS a, '. $this->VarTableSprStatus .' AS b, '. $this->VarTableSvt .' AS c, '. $this->VarTableSvtType .' AS d';
    $ArrSqlArray['SqlCount']['where'] = 'a.sprstatus_id = b.sprstatus_id AND a.svt_id = c.svt_id AND c.svttype_id = d.svttype_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

//  function Add($values){
//    if(true === isset($values) && true === is_array($values) && 1 <= count($values)){
//      $VarSql = 'INSERT INTO '. $this->VarTableDemandsComments .'
//                (demandscommentsparent_id, demands_id, demandscomments_comment, demandscomments_date, users_id, demandscomments_ip)
//                VALUES (?, ?, ?, ?, ?, ?)';
//      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtStatusTypes, MDB2_PREPARE_MANIP);
//      $values = $this->PrepareData($values);
//      if (true === isset($values['users_id']) && true === is_numeric($values['users_id'])){
//        $VarUsersId = $values['users_id'];
//      }
//      else{
//        $VarUsersId = 1;
//      }
//      $ArrData = array(
//        $values['demandscommentsparent_id'],
//        $values['demands_id'],
//        SiteLibs::ConvertTextToSql($values['demandscomments_comment']),
//        date('YmdHis'),
//        $VarUsersId,
//        $_SERVER['REMOTE_ADDR']
//      );
//      //SiteLibs::GetVarDump($data);
//      $VarQueryResult = $sth->execute($ArrData);
//      if(PEAR::isError($VarQueryResult)) {
//        return array('error' => $VarQueryResult->getMessage());
//      }
//      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableDemandsCommentsTypes, 'demandscomments_id');
//      if (PEAR::isError($VarId)) {
//        return array('error' => $VarId->getMessage());
//      }
//      return $VarId;
//    }
//    else{
//      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
//    }
//  }

  function Add($values){
    if(true === isset($values) && true === is_array($values) && 1 <= count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableDemandsComments .'
                (demandscomments_id, demandscommentsparent_id, demands_id, demandscomments_comment, demandscomments_date, users_id, demandscomments_ip)
                VALUES (?, ?, ?, ?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableDemandsCommentsTypes, MDB2_PREPARE_MANIP);
      if(PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['demandscommentsparent_id'],
        $values['demands_id'],
        $values['demandscomments_comment'],
        $values['demandscomments_date'],
        $values['users_id'],
        $values['demandscomments_ip']
      );
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableDemandsCommentsTypes, 'demandscomments_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) && 1 <= count($values)){
      $VarSql = "UPDATE ". $this->VarTableDemandsComments ." SET
                  demandscomments_comment = ?,
                  demandscomments_date = ?,
                  users_id = ?,
                  demandscomments_ip = ?
                  WHERE demandscomments_id = '".  $values['demandscomments_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['demandscomments_comment'],
        $values['demandscomments_date'],
        $values['users_id'],
        $values['demandscomments_ip']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['demandscomments_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'demandscomments_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableDemandsComments ." 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) && 1 <= count($values)){
      $values['users_id'] = NULL;
      if(true === isset($values['FrmAgreeUsersCheckboxes']) && '' != $values['FrmAgreeUsersCheckboxes']){
        $values['users_id'] = current($values['FrmAgreeUsersCheckboxes']);
      }
      $values['demandscomments_date'] = date('YmdHis');
      $values['demandscomments_ip'] = $_SERVER['REMOTE_ADDR'];
    }
    return $values;
  }
}
?>