<?php

class UsersRoleMod extends DbMod{
  var $VarTableUsersRoleTypes = array('integer', 'integer', 'integer');

	function UsersRoleMod($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->VarTableUsersRole .' 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['SqlId']['select'] = 'a.role_id';
    $ArrSqlArray['SqlId']['from'] = $this->VarTableUsersRole .' AS a';
    $ArrSqlArray['SqlId']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlId']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlRow']['select'] = 'a.*';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableUsersRole .' 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['role_id']) && 0 < count($values['role_id'])){
        $VarSql = 'INSERT INTO '. $this->VarTableUsersRole .'
                  (usersrole_id, users_id, role_id)
                  VALUES (?, ?, ?)';
        $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableUsersRoleTypes, MDB2_PREPARE_MANIP);
        foreach ($values['role_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 = 'usersrole_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableUsersRole ." 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']);
    }
	}
}
?>