<?php

class ProgrammPrefValueMod extends DbMod{
  var $VarTableProgrammPrefValueTypes = array('integer', 'integer', 'text', 'integer');

	function ProgrammPrefValueMod($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->VarTableProgrammPrefValue .' 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->VarTableProgrammPrefValue .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableProgrammPrefValue .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 0 < count($values) && true === isset($_SESSION['SessionAuth']['organiz_id']) && true === is_numeric($_SESSION['SessionAuth']['organiz_id'])){
      $VarSql = 'INSERT INTO '. $this->VarTableProgrammPrefValue .'
                (programmprefvalue_id, programmpref_id, programmprefvalue_value, organiz_id)
                VALUES (?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableProgrammPrefValueTypes, MDB2_PREPARE_MANIP);
      if (true === isset($values['programmprefvalue_value']) && '' != $values['programmprefvalue_value']){
        $VarProgrammPrefValueValue = SiteLibs::ConvertTextToSql($values['programmprefvalue_value']);
      }
      else{
        $VarProgrammPrefValueValue = '';
      }
      $data = array(
        '',
        SiteLibs::ConvertSqlIntegerInsert($values['programmpref_id']),
        $VarProgrammPrefValueValue,
        $_SESSION['SessionAuth']['organiz_id']
      );
      //SiteLibs::GetVarDump($data);
      $res = $sth->execute($data);
      if (PEAR::isError($res)) {
        die($res->getMessage());
      }
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Edit($values){
    if(true === is_array($values) && 0 < count($values) && true === isset($_SESSION['SessionAuth']['organiz_id']) && true === is_numeric($_SESSION['SessionAuth']['organiz_id'])){
      //$this->Del('%');
      //$ArrVal = array();
      //$this->Add($values);
      $VarResult = $this->ArrMsSp->SP_DbConnect->exec('DELETE FROM '. $this->VarTableProgrammPrefValue .' WHERE organiz_id = "'. $_SESSION['SessionAuth']['organiz_id'] .'"');
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      foreach ($values as $key => $value) {
        if('ProgrammPrefId' == substr($key, 0, 9) && true === isset($values[$key]) && '' != $values[$key]){
          $ArrAdd = array(
            'programmpref_id' => substr($key, 9),
            'programmprefvalue_value' => $values[$key]
          );
          //SiteLibs::GetVarDump($ArrAdd);
          $this->Add($ArrAdd);
        }
      }
      //array_walk($values, 'ProgrammPrefValueMod::GetProgrammPrefIdFromForm');
      
      //SiteLibs::GetVarDump($values);

      
      //$mdb2->disconnect();
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
//
  function Del($VarId = '', $VarFiledName = 'programmprefvalue_id'){
    if('' != $VarId){
      $sql = "DELETE FROM ". $this->VarTableProgrammPrefValue ." WHERE ". $VarFiledName ." = '". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarResult =& $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      //$db->disconnect();
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
}
?>