<?php

class SvtStatusMod extends DbMod{
  var $VarTableSvtStatusTypes = array('integer', 'text', 'integer');
  
  function SvtStatusMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableSvtStatus .' AS a';
    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->VarTableSvtStatus .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlAllLeft']['select'] = 'a.*, b.svtstatusval_id';
    $ArrSqlArray['SqlAllLeft']['from'] = $this->VarTableSvtStatus .' AS a';
    $ArrSqlArray['SqlAllLeft']['leftjoin'] = $this->VarTableSvtStatusVal .' AS b ON a.svtstatus_id = b.svtstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAllLeft']['leftjoin'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrSqlArray['SqlReportByStatus']['select'] = 'b.*, count(b.svtstatus_id) AS vartotal';
    $ArrSqlArray['SqlReportByStatus']['from'] = $this->VarTableSvtStatusVal .' AS a, '. $this->VarTableSvtStatus .' AS b';
    $ArrSqlArray['SqlReportByStatus']['where'] = 'a.svtstatus_id = b.svtstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlReportByStatus']['where'] .= ' AND '. $VarWhere;
    }
    $ArrSqlArray['SqlReportByStatus']['group'] = 'b.svtstatus_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlReportByStatus']['order'] = $VarOrder;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableSvtStatus .'
                (svtstatus_id, svtstatus_name, svtstatus_main)
                VALUES (?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtStatusTypes, MDB2_PREPARE_MANIP);
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['svtstatus_name'],
        $values['svtstatus_main']
      );
      //SiteLibs::GetVarDump($data);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableSvtStatus, 'svtstatus_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->VarTableSvtStatus ." SET
                  svtstatus_name = ?,
                  svtstatus_main = ?
                  WHERE svtstatus_id = '".  $values['svtstatus_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['svtstatus_name'],
        $values['svtstatus_main']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['svtstatus_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'svtstatus_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableSvtStatus ." 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['svtstatus_name'] = SiteLibs::ConvertTextToSql($values['svtstatus_name']);
      if (true === isset($values['svtstatus_main'])){
        $values['svtstatus_main'] = 1;
      }
      else {
        $values['svtstatus_main'] = 0;
      }
    }
    return $values;
  }
  
  function UnChecked(){
    $VarSql = "UPDATE ". $this->VarTableSvtStatus ." SET svtstatus_main = ?";
    $ArrData = array(0);
    $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
    $VarQueryResult = $sth->execute($ArrData);
    if(PEAR::isError($VarQueryResult)) {
      return array('error' => $VarQueryResult->getMessage());
    }
    return true;
  }
}
?>