<?php

require_once('MDB2.php');

class FeaturesKeyMod{
	var $ArrMsSp = '';
  var $VarTableFeaturesKeyTypes = array('integer', 'integer', 'text');
	var $VarTableFeaturesKey = '';
//  var $VarTableFeaturesKeyVal = '';

	function FeaturesKeyMod($ArrMsSp){
		$this->ArrMsSp = $ArrMsSp;
    $this->VarTableFeaturesKey = $this->ArrMsSp->Sp_DbConf['db']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['db']['DbTbl_FeaturesKey'];
//    $this->VarTableFeaturesKeyVal = $this->ArrMsSp->Sp_DbConf['db']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['db']['DbTbl_FeaturesKeyVal'];
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    //SiteLibs::GetVarDump($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
 		//echo "here<br />";
    $db =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
    if (PEAR::isError($db)) {
      die($db->getMessage());
    }
    SiteLibs::DbSetCharset($db);
    //SiteLibs::GetVarDump($db);
    $db->setFetchMode(MDB2_FETCHMODE_ASSOC);
    $db->loadModule('Extended');
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableFeaturesKey .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlRow']['select'] = 'a.*';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableFeaturesKey .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableFeaturesKey .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlAssoc']['select'] = 'a.featureskey_id, a.featureskey_name';
    $ArrSqlArray['SqlAssoc']['from'] = $this->VarTableFeaturesKey .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAssoc']['where'] = $VarWhere;
    }
    ///
    $sql = SiteLibs::BuildSqlFromArray($ArrSqlArray[$VarSqlName]);
    //echo $sql. "<br />";
    switch ($VarSqlGet) {
      case 'GetAssoc':
        $ArrResult =& $db->extended->getAssoc($sql);
      break;
      case 'GetOne':
        $ArrResult =& $db->extended->getOne($sql);
      break;
      case 'GetCol':
        $ArrResult =& $db->extended->getCol($sql);
      break;
      case 'GetRow':
        $ArrResult =& $db->extended->getRow($sql);
      break;
      case 'GetAll':
      default:
        $ArrResult =& $db->extended->getAll($sql);
      break;
    }
    if (PEAR::isError($ArrResult)) {
      die($ArrResult->getMessage());
    }
    $db->disconnect();
  	return $ArrResult;
	}

   function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($mdb2)) {
        die($mdb2->getMessage());
      }
      SiteLibs::DbSetCharset($mdb2);
      $VarSql = 'INSERT INTO '. $this->VarTableFeaturesKey .'
                (featureskey_id, featuressection_id, featureskey_name)
                VALUES (?, ?, ?)';
      $sth = $mdb2->prepare($VarSql, $this->VarTableFeaturesKeyTypes, MDB2_PREPARE_MANIP);
      $data = array(
        '',
        $values['featuressection_id'],
        SiteLibs::ConvertTextToSql($values['featureskey_name'])
      );
      //SiteLibs::GetVarDump($data);
      $res = $sth->execute($data);
      if (PEAR::isError($res)) {
        die($res->getMessage());
      }
      $VarLastId = $mdb2->lastInsertID($this->VarTableFeaturesKey, 'featureskey_id');
      if (PEAR::isError($VarLastId)) {
        die($VarLastId->getMessage());
      }
      $mdb2->disconnect();
      return $VarLastId;
    }
    else{
      $ArrResult['error'] = 'Неверный входной параметр!';
    }
  }

  function Edit($values){
    if(true === is_array($values) && 0 < count($values)){
      $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($mdb2)) {
        die($mdb2->getMessage());
      }
      SiteLibs::DbSetCharset($mdb2);
      $VarSql = "UPDATE ". $this->VarTableFeaturesKey ." SET
                  featuressection_id = '". SiteLibs::ConvertTextToSql($values['featuressection_id']) ."',
                  featureskey_name = '". SiteLibs::ConvertTextToSql($values['featureskey_name']) ."'
                 WHERE featureskey_id ='".  $values['featureskey_id'] ."'";
      //echo "VarSql: ". $VarSql ."<br />";
      $res =& $mdb2->query($VarSql);
      if (PEAR::isError($res)) {
          die($res->getMessage());
      }
      $mdb2->disconnect();
      return true;
    }
    else{
      $ArrResult['error'] = 'Неверный входной параметр!';
    }
  }

  function Del($VarId = '', $VarFiledName = 'featureskey_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $db =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($db)) {
        die($db->getMessage());
      }
      SiteLibs::DbSetCharset($db);
      $sql = "DELETE FROM ". $this->VarTableFeaturesKey ." WHERE ". $VarFiledName ." ='". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarResult =& $db->exec($sql);
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      $db->disconnect();
      return true;
    }
    else {
      return array('error' => 'Неверный входной параметр');
    }
	}
}
?>