<?php

require_once('MDB2.php');

class FeaturesSectionMod{
	var $ArrMsSp = '';
  var $VarTableFeaturesSectionTypes = array('integer', 'text');
	var $VarTableFeaturesSection = '';
//  var $VarTableFeaturesSectionVal = '';

	function FeaturesSectionMod($ArrMsSp){
		$this->ArrMsSp = $ArrMsSp;
    $this->VarTableFeaturesSection = $this->ArrMsSp->Sp_DbConf['db']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['db']['DbTbl_FeaturesSection'];
//    $this->VarTableFeaturesSectionVal = $this->ArrMsSp->Sp_DbConf['db']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['db']['DbTbl_FeaturesSectionVal'];
		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->VarTableFeaturesSection .' 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->VarTableFeaturesSection .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableFeaturesSection .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlAllLeft']['select'] = 'a.*, b.featuressectionval_id';
    $ArrSqlArray['SqlAllLeft']['from'] = $this->VarTableFeaturesSection .' AS a';
    $ArrSqlArray['SqlAllLeft']['leftjoin'] = $this->VarTableFeaturesSectionVal .' AS b ON a.featuressection_id = b.featuressection_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAllLeft']['leftjoin'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrSqlArray['SqlReportByStatus']['select'] = 'b.*, count(b.featuressection_id) AS vartotal';
    $ArrSqlArray['SqlReportByStatus']['from'] = $this->VarTableFeaturesSectionVal .' AS a, '. $this->VarTableFeaturesSection .' AS b';
    $ArrSqlArray['SqlReportByStatus']['where'] = 'a.featuressection_id = b.featuressection_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlReportByStatus']['where'] .= ' AND '. $VarWhere;
    }
    $ArrSqlArray['SqlReportByStatus']['group'] = 'b.featuressection_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlReportByStatus']['order'] = $VarOrder;
    }
    ///
    $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->VarTableFeaturesSection .'
                (featuressection_id, featuressection_name)
                VALUES (?, ?)';
      $sth = $mdb2->prepare($VarSql, $this->VarTableFeaturesSectionTypes, MDB2_PREPARE_MANIP);
      $data = array(
        '',
        SiteLibs::ConvertTextToSql($values['featuressection_name'])
      );
      //SiteLibs::GetVarDump($data);
      $res = $sth->execute($data);
      if (PEAR::isError($res)) {
        die($res->getMessage());
      }
      $VarLastId = $mdb2->lastInsertID($this->VarTableFeaturesSection, 'featuressection_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->VarTableFeaturesSection ." SET
                    featuressection_name = '". SiteLibs::ConvertTextToSql($values['featuressection_name']) ."'
                 WHERE featuressection_id ='".  $values['featuressection_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 = 'featuressection_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->VarTableFeaturesSection ." 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' => 'Неверный входной параметр');
    }
	}
}
?>