<?php

class UsersServicesMod extends DbMod{
  var $VarTableUsersServicesTypes = array('integer', 'integer', 'integer');

	function UsersServicesMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $UserId = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableUsersServices .' AS a';
    $ArrSqlArray['SqlAll']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlWebservicesId']['select'] = 'a.webservices_id';
    $ArrSqlArray['SqlWebservicesId']['from'] = $this->VarTableUsersServices .' AS a';
    $ArrSqlArray['SqlWebservicesId']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlWebservicesId']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlRow']['select'] = 'a.*';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableUsersServices .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;  	
	}

  function Edit($values){
    if(true === is_numeric($values['users_id']) && 0 < $values['users_id']){
      $this->Del($values['users_id'], 'users_id');
      if(true === is_array($values['webservices_id']) && 0 < count($values['webservices_id'])){
        $VarSql = 'INSERT INTO '. $this->VarTableUsersServices .'
                  (usersservices_id, users_id, webservices_id)
                  VALUES (?, ?, ?)';
        $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableUsersServicesTypes, MDB2_PREPARE_MANIP);
        foreach ($values['webservices_id'] as $key => $value) {
          $data = array(
            '',
            $values['users_id'],
            $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 Del($VarId = '', $VarFiledName = 'usersservices_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableUsersServices ." 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']);
    }
	}
}
?>