<?php

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

  function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'i.*, inet_aton(i.ip_name) AS ip_name_sort, s.svt_id, s.svt_name, s.svt_buxname';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableIp .' AS i';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableSvt .' AS s ON i.svt_id = s.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlCount'] = $ArrSqlArray['SqlAll'];
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 1 <= count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableIp .'
                (ip_id, ip_name, svt_id, ip_note)
                VALUES (?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['ip_name'],
        $values['svt_id'],
        $values['ip_note']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableIpTypes, MDB2_PREPARE_MANIP);
//      var_dump($sth);
      //SiteLibs::GetVarDump($sth);
      if(PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableIp, 'ip_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->VarTableIp ." SET
                  ip_name = ?,
                  svt_id = ?,
                  ip_note = ?
                  WHERE ip_id = '".  $values['ip_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['ip_name'],
        $values['svt_id'],
        $values['ip_note']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      if(PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['ip_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'ip_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableIp ." 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['svt_id'] = NULL;
      if(true === isset($values['FrmAgreeSvtCheckboxes']) && '' != $values['FrmAgreeSvtCheckboxes']){
        $values['svt_id'] = current($values['FrmAgreeSvtCheckboxes']);
      }
      if(false === isset($values['ip_note'])){
        $values['ip_note'] = '';
      }
    }
    return $values;
  }
}
?>