<?php

class CountryMod extends DbMod{
  var $VarTableCountryTypes = array('integer', 'text', 'text');

	function CountryMod($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->VarTableCountry .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlOne']['select'] = 'a.country_id';
    $ArrSqlArray['SqlOne']['from'] = $this->VarTableCountry .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlOne']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlOne']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlOne']['limit'] = $VarLimit;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableCountry .' AS a';
    $ArrSqlArray['SqlCount']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

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

  function Del($VarId = '', $VarFiledName = 'country_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableCountry ." 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)){
//      if(false === isset($values['deliverytype_defaultvalue']) || 1 != $values['deliverytype_defaultvalue']){
//        $values['deliverytype_defaultvalue'] = 0;
//      }
    }
    return $values;
  }
}
?>