<?php

class UsersSettingsValueMod extends DbMod{
  var $VarTableUsersSettingsValueTypes = array('integer', 'integer', 'text', 'text');

	function UsersSettingsValueMod($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->VarTableUsersSettingsValue .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    $ArrSqlArray['SqlAssoc']['select'] = 'a.userssettingsvalue_var, userssettingsvalue_value';
    $ArrSqlArray['SqlAssoc']['from'] = $this->VarTableUsersSettingsValue .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAssoc']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAssoc']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAssoc']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlRow']['select'] = 'a.*';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableUsersSettingsValue .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableUsersSettingsValue .' 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->VarTableUsersSettingsValue .'
                (userssettingsvalue_id, users_id, userssettingsvalue_var, userssettingsvalue_value)
                VALUES (?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableUsersSettingsValueTypes, MDB2_PREPARE_MANIP);
      $data = array(
        '',
        SiteLibs::ConvertSqlIntegerInsert($values['users_id']),
        SiteLibs::ConvertTextToSql($values['userssettingsvalue_var']),
        SiteLibs::ConvertTextToSql($values['userssettingsvalue_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 = array(), $VarUsersId = ''){
    $this->Del($VarUsersId, 'users_id');
    if(true === is_array($values) && 0 < count($values) && true === is_numeric($VarUsersId)){
      //$ArrVal = array();
      //$this->Add($values);
      foreach ($values as $key => $value) {
        //SiteLibs::GetVarDump($value);
        //if (true === isset($value['name']) && true === isset($value['value'])){
          $ArrAdd = array(
            'users_id' => $VarUsersId,
            'userssettingsvalue_var' => $value['name'],
            'userssettingsvalue_value' => $value['value']
          );
          //SiteLibs::GetVarDump($ArrAdd);
          $this->Add($ArrAdd);
        //}
      }
      //array_walk($values, 'UsersSettingsValueMod::GetUsersSettingsIdFromForm');
//      $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf['dbuser']['DbDsn']);
//      if (PEAR::isError($mdb2)) {
//        die($mdb2->getMessage());
//      }
//      SiteLibs::DbSetCharset($mdb2, 'cp1251');
//      //SiteLibs::GetVarDump($values);
//
//      $VarSql = "UPDATE ". $this->VarTableUsersSettingsValue ." SET
//                    userssettings_defaultvalue = '". serialize($values['UiName']) ."'
//                  WHERE users_id = '".  $values['users_id'] ."' AND userssettings_var = 'UiName'";
//      //echo "VarSql: ". $VarSql ."<br />";
//      //$res =& $mdb2->query($VarSql);
//      //SiteLibs::GetVarDump($res);
//      if (PEAR::isError($res)) {
//        die($res->getMessage());
//      }
//      $mdb2->disconnect();
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'userssettingsvalue_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableUsersSettingsValue ." 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']);
    }
	}
  
  function GetPref($VarUsersId = ''){
    $ArrUsersPref = $this->GetFromDb('SqlAll', 'GetAll', "a.users_id = '". $VarUsersId ."'");
    return $ArrUsersPref;
  }
}
?>