<?php

class UsersPrefValueMod extends DbMod{
  var $VarTableUsersPrefValueTypes = array('integer', 'integer', 'integer', 'text');

	function UsersPrefValueMod($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->VarTableUsersPrefValue .' 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->VarTableUsersPrefValue .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableUsersPrefValue .' 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)){
      $VarSql = 'INSERT INTO '. $this->VarTableUsersPrefValue .'
                (usersprefvalue_id, users_id, userspref_id, usersprefvalue_value)
                VALUES (?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableUsersPrefValueTypes, MDB2_PREPARE_MANIP);
      $data = array(
        '',
        SiteLibs::ConvertSqlIntegerInsert($values['users_id']),
        SiteLibs::ConvertSqlIntegerInsert($values['userspref_id']),
        SiteLibs::ConvertTextToSql($values['usersprefvalue_value'])
      );
      //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)){
      $this->Del($values['users_id'], 'users_id');
      //$ArrVal = array();
      //$this->Add($values);
      foreach ($values as $key => $value) {
        if('UsersPrefId' == substr($key, 0, 11)){
          $ArrAdd = array(
            'users_id' => $values['users_id'],
            'userspref_id' => substr($key, 11),
            'usersprefvalue_value' => $values[$key]
          );
          //SiteLibs::GetVarDump($ArrAdd);
          $this->Add($ArrAdd);
        }
      }
      //array_walk($values, 'UsersPrefValueMod::GetUsersPrefIdFromForm');

//      $VarSql = "UPDATE ". $this->VarTableUsersPrefValue ." SET
//                    userspref_defaultvalue = '". SiteLibs::ConvertTextToSql($values['UiName']) ."'
//                  WHERE users_id = '".  $values['users_id'] ."' AND userspref_var = 'UiName'";
//      //echo "VarSql: ". $VarSql ."<br />";
//      //$res =& $this->ArrMsSp->SP_DbConnect->query($VarSql);
//      //SiteLibs::GetVarDump($res);
//      if (PEAR::isError($res)) {
//        die($res->getMessage());
//      }
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
//
  function Del($VarId = '', $VarFiledName = 'usersprefvalue_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableUsersPrefValue ." WHERE ". $VarFiledName ." ='". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarResult =& $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
}
?>