<?php

class SecurityHandler extends Handler {

	var $currentuser;
	var $member;		//class Member
	var $role;			// class Role

	function __construct($name='SecurityHandler') {
			parent::__construct($name,false);
			$this->currentuser = $this->createMember();
			$this->role = $this->createRole();
			$this->openRole(1);
			$this->currentuser->role = $this->role;
	}

	function getCurrentUser($request=array()){
		return $this->currentuser;
	}

	function changeDateLastActivity() { 
		if(!empty($this->currentuser->id)) $this->DB->update('member',Array('date_last_activity' => 'now()'),Array('id' => $this->currentuser->id));
	}

	function openRole($id) {
		$this->role->id=$id;
		$this->DB->initObject($this->role);
	}

	function isAuthorized() {
		if(empty($this->currentuser->id)) return 0;
		else {
			$selected = $this->DB->SelectArray('member',array('id'),array('username' => $this->currentuser->username,'password' => $this->currentuser->password,'canceled' => 0,'type' => 2));
			if(!empty($selected)) return 1;
			else return 0;
		}
	}

	function authorizeMember($request){
		global $Server;
		$this->currentuser = $this->createMember();
		$this->parseRequest($request,$this->currentuser);
		$selected=$this->DB->SelectArray('member',array('id'),array('username' => $this->currentuser->username,'password' => $this->currentuser->password,'canceled' => 0,'type' => 2));
		if(!empty($selected)) {
			$this->DB->begin();
			$this->currentuser->id = $selected[0]['id'];
			$this->DB->update('member',array('date_last_activity' => 'now()','date_last_login' => 'now()'),array('id' => $this->currentuser->id));
			$this->DB->initObject($this->currentuser);
			$this->setLogEvent(1,1,1,'Log In',$this->currentuser->id);
			$_SESSION['FileManager_isLoggedIn'] = TRUE;
			$_SESSION['FileManager_UserName'] = $this->currentuser->username;
			$this->DB->commit();
			//$Server->clearAll();
		}
		else $this->currentuser->_error='1';
		return $this->currentuser;
	}

	function openAdmin($request){
		$member = $this->createMember();
		$this->parseRequest($request,$member);
		$tmp = $this->createMember();
		$tmp->id = $member->id;
		$this->DB->initObject($tmp);
		if(empty($tmp->role->id)){
			$member->_error='1';
			return $member;
		}
		if($tmp->type==1){
			$member->_error='98';
			return $member;
		}
		$this->DB->initObject($member);
		return $member;
	}

	function authorizeMemberByCookie($request){
		global $Server;
		$this->parseRequest($request,$this->currentuser);
		$this->cleanError($this->currentuser);
		$selected=$this->DB->SelectArray("member",array('id','activated','keep_loggedin','canceled'),array('id'=>$this->currentuser->id,'passw'=>$this->currentuser->passw));
		if (!empty($selected)) {
			if($selected[0]['canceled']==1) $this->currentuser->_error='109';
			else if($selected[0]['keep_loggedin']!=1) $this->currentuser->_error='44';
			else {
				if($selected[0]['activated']==1) {
					$this->currentuser->id=$selected[0]['id'];
					$this->DB->update('member',Array('date_last_login'=>date('Y-m-d H:i:s',time())),Array('id'=>$this->currentuser->id));
					$this->DB->initObject($this->currentuser);
				}
				else $this->currentuser->_error='2';
			}
		}
		else $this->currentuser->_error='1';
		//$Server->clearAll();
		if(!empty($this->currentuser->_error)) $this->currentuser->id='';
		return $this->currentuser;
	}

	function logoutMember($request){
		global $Server;
		$this->DB->begin();
		if(!empty($this->currentuser->id)) {
			$this->DB->update('module', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('layout', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('menu', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('template', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('presetgroup', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('preset', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('package', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('enginepacket', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('subdomain', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('importedfeed', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('city', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
			$this->DB->update('feed', array('access_flag' => 0), array('access_flag' => $this->currentuser->id));
		}
		if(!empty($this->currentuser->id)) {
			$this->setLogEvent(1, 1, 2, 'Log Out', $this->currentuser->id);
			if($this->currentuser->role->id == 3) $this->setLogEvent(3, 3, 4, 'UnLock all objects');
			else $this->setLogEvent(3, 3, 4, 'UnLock all own objects');
		}
		$this->currentuser = $this->createMember();
		$this->role = $this->createRole();
		$this->openRole(1);
		$Server->clearAll();
		$this->currentuser->role = $this->role;
		$this->DB->commit();
		return $this->currentuser;
	}

	function checkPerm($module_id,$minperm) {
		$error = '';
		foreach($this->currentuser->role->moduleRights->data as $k=>$v) {
			if($v->module->id==$module_id) {
				if (intval($v->perm) >= intval($minperm)) return $error;
//				if ((intval($v->perm) & intval($minperm))!=0) return $error;
				else return '5';
			}
		}
		return intval($minperm) > 2? '5' : '';
		//return $error;
	}

	function registerAdmin($request = array()) {
		$member = $this->createRegMember();
		$this->parseRequest($request,$member);
		$member->id = '';
		if($this->currentuser->role->id!=3) {
			$member->_error='79';
			return $member;
		}
		$selected=$this->DB->SelectArray('member','id',array('username' => $member->username));
		if(!empty($selected)) $member->_error='6';
		else {
			if(empty($member->role->id)) $member->role->id = 2;
			if(empty($member->type)) $member->type = 2;
			$member->date_signed = 'now()';
			$member->date_modified = 'now()';
			$this->DB->begin();
			$this->setLogEvent(1,1,11,'Add user "'.$member->username.'"');
			$this->DB->processObject($member);
			$this->DB->commit();
		}
		return $member;
	}

	function registerMember($request){
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		if(!$this->checkImages($member)) return $member;
		$member->passw=md5($member->passw);
		$member->id='';
		$selected=$this->DB->SelectAny("select id from member where email1='".$member->email1."'");
		if(!empty($selected) && !eregi('^RNJDT',$member->username)) {
			$member->_error='122';
		}
		else {
			$selected=$this->DB->SelectArray("member",'id',array('username'=>$member->username));
			if (!empty($selected)) {
				$member->_error='6';
			}
			else {
				$member->activate_code = md5($member->username.$member->username.$member->passw);
				$member->level->id=6;
				$member->rating=1;
				$member->date_signed=date('Y-m-d H:i:s',time());
				$this->DB->processObject($member);
				if(!empty($member->invitedby)) {
					$selected=$this->DB->SelectArray("member",'id',array('username'=>$member->invitedby));
					if (!empty($selected)) {
						$invmember=$this->createRegMember();
						$invmember->id=$selected[0]['id'];
						$this->DB->incrementField('member','rating',$invmember->id,$invmember,50);
					}
				}
			}
		}
		return 	$member;
	}

	function updateMemberProfile($request){
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		if($member->id!=$this->currentuser->id) {
			$member->_error='70';
			return $member;
		}
		$this->DB->begin();
		if($member->canceled==1) $member->date_canceled=date('Y-m-d H:i:s',time());
		$selected=$this->DB->SelectArray('member','id,username',array('id' => $member->id));
		if(!empty($selected)) {
			if($selected[0]['username']==$member->username) $this->DB->processObject($member,'save',array(),array(),TRUE);
			else {
				$selected=$this->DB->SelectArray("member",'id',array('username'=>$member->username));
				if(!empty($selected)) $member->_error='6';
				else $this->DB->processObject($member,'save',array(),array(),TRUE);
			}
			if(empty($member->_error)) $this->DB->initObject($this->currentuser);
			$this->DB->rollback();
		}
		else $member->_error='7';
		return $member;
	}

	/*function deleteRelatedUserInfo(&$member){
		// Delete MemberContent		
		$selected=$this->DB->SelectArray("membercontent",'id',array('member'=>$member->id));
		$deleteimages['MemberContent']=$selected;
		$this->DB->delete('membercontent',array('member'=>$member->id));
	}*/

	/*function checkDelete(&$member){
		$selected=$this->DB->SelectArray('module','current_object',array('id'=>16));
		if(!empty($selected[0]['current_object'])){
			$selected=$this->DB->SelectArray("image",'member',array('id'=>$selected[0]['current_object']));
			if(!empty($selected) && $selected[0]['member']==$member->id) {
				$member->_error='112';
			} 
		}
		$selected=$this->DB->SelectArray('module','current_object',array('id'=>4));
		if(!empty($selected[0]['current_object'])){
			$selected=$this->DB->SelectArray("aaquestion",'member',array('id'=>$selected[0]['current_object']));
			if(!empty($selected) && $selected[0]['member']==$member->id) {
				$member->_error='113';
			}
		}
		$selected=$this->DB->SelectArray('module','current_object',array('id'=>15));
		if(!empty($selected[0]['current_object'])){
			$selected=$this->DB->SelectArray("humor",'member',array('id'=>$selected[0]['current_object']));
			if(!empty($selected) && $selected[0]['member']==$member->id) {
				$member->_error='114';
			} 
		}
		$selected=$this->DB->SelectArray('zone z, adcampaign a','*',array('z.adcampaign'=>'a.id','z.type'=>1,'a.member'=>$member->id));
		if(!empty($selected)){
			$member->_error='117';
		}
	}*/

	function logDelete($member){
		$terminatelog=new TerminateLog();
		$terminatelog->username=$member->username;
		$terminatelog->user_level=$member->level->name;
		$terminatelog->first_name=$member->first_name;
		$terminatelog->last_name=$member->last_name;
		$terminatelog->email1=$member->email1;
		$terminatelog->admin_id=$this->currentuser->id;
		$terminatelog->admin_name=$this->currentuser->username;
		$terminatelog->date=date('Y-m-d H:i:s',time());	
		$this->DB->processObject($terminatelog);
	}

	function deleteAdmin($request = array()) {
		$member = $this->createMember();
		$this->parseRequest($request, $member);
		$this->DB->initObject($member);
		if($member->id == $this->currentuser->id){
			$member->_error = '118';
			return $member;
		}
		if(!empty($member->_error)) return $member;
		$member->canceled = 1;
		$this->DB->begin();
		$this->setLogEvent(1, 1, 13, 'Delete user "'.$member->username.'" ['.$member->id.']', $member->id);
		$this->DB->processObject($member, 'save', array(), array(), TRUE);
		$this->DB->update('module',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('layout',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('menu',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('template',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('presetgroup',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('preset',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('package',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->update('enginepacket',array('access_flag' => 0),array('access_flag' => $member->id));
		$this->DB->commit();
		return $member;
	}

	function deleteMember($request) {
		$member=$this->createMember();
		$this->parseRequest($request,$member);
		$this->DB->initObject($member);
		if($member->level->level_type==1){
			$member->_error='115';
			return $member;
		}
		$this->checkDelete($member);
		if(!empty($member->_error)) return $member;
		$this->deleteRelatedUserInfo($member);
		$this->DB->processObject($member,'delete');
		$this->logDelete($member);
		$newforumhandler=new NewForumHandler();
		$newforumhandler->deleteUser($member);
//		$forumhandler=new ForumHandler();
//		$forumhandler->deleteUser($member);
		return $member;
	}

	function openAdminProfileAdmin($request){
		$admin=$this->createRegMember();
		$this->parseRequest($request,$admin);
		$this->DB->initObject($admin);
		if(empty($admin->role->id)){
			$admin->_error='1';
			return $admin;
		}
		if($this->currentuser->role->id!=3 && $admin->id != $this->currentuser->id) {
			$admin->_error='5';
			return $admin;
		}
		return $admin;
	}

	function openMemberProfileAdmin($request){
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		$tmp=$this->createRegMember();
		$tmp->id=$member->id;
		$this->DB->initObject($tmp);
		if(empty($tmp->role->id)){
			$member->_error='1';
			return $member;
		}
		if($tmp->type==1){
			$member->_error='98';
			return $member;
		}
		$this->DB->initObject($member);
		return $member;
	}

	function getRightsList($request){
		$var=array(
			'0'=>'NA',
			'2'=>'VR',
			'6'=>'VRP',
			'14'=>'VRP,A',
			'22'=>'VRP,M',
			'30'=>'VRP,A,M',
			'62'=>'VRP,A,M,D',
			'126'=>'VRP,A,M,D,CAT',
			'254'=>'VRP,A,M,D,CAT,C',
			'510'=>'VRP,A,M,D,CAT,C,G'
			);
		return $var;
	}

	function updateAdminProfileAdmin($request){
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		$tmp=$this->createRegMember();
		$tmp->id=$member->id;
		$this->DB->begin();
		$this->DB->initObject($tmp);
		if($tmp->role->id==3 && $this->currentuser->role->id!=3) {
			$member->_error='79';
			return $member;
		}
		if($this->currentuser->role->id!=3 && $tmp->id != $this->currentuser->id) {
			$member->_error='79';
			return $member;
		}
		$selected=$this->DB->SelectArray("member",'id,username',array('id'=>$member->id));
		if(!empty($selected)) {
			if($selected[0]['username']==$member->username) $this->DB->processObject($member);
			else {
				$selected=$this->DB->SelectArray("member",'id',array('username'=>$member->username));
				if(!empty($selected)) $member->_error='6';
				else $this->DB->processObject($member);
			}
			if(empty($member->_error)) $this->DB->initObject($this->currentuser);
			if($member->id == $this->currentuser->id) 
				$this->setLogEvent(1,1,12,'Own profile of the user "'.$member->username.'" is updated',$this->currentuser->id);
			else $this->setLogEvent(1,1,12,'The profile of user "'.$member->username.'" is updated',$member->id);
			$this->DB->commit();
		}
		else $member->_error='7';
		return $member;
	}

	function updateMemberProfileAdmin($request){
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		$tmp=$this->createRegMember();
		$tmp->id=$member->id;
		$this->DB->initObject($tmp);
		if($tmp->type==1) {
			$member->_error='78';
			return $member;
		}
		if(!empty($member->role->id) && $tmp->role->id!=$member->role->id){
			$member->_error='97';
			return $member;
		}
		$member->status_set_by_id=$this->Security->currentuser->id;
		$selected=$this->DB->SelectArray("member",'id,username',array('id'=>$member->id));
		if(!empty($selected)) {
			if($selected[0]['username']==$member->username) {
				$this->DB->processObject($member);
			}
			else {
				$selected=$this->DB->SelectArray("member",'id',array('username'=>$member->username));
				if(!empty($selected)) $member->_error='6';
				else $this->DB->processObject($member);
			}
		}
		else $member->_error='7';
		return $member;
	}

	function changeMemberLevel($request) {
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		$tmp=$this->createMember();
		$tmp->id=$member->id;
		$this->DB->initObject($tmp);
		if($tmp->level->level_type==1) {
			$member->_error='77';
			return $member;
		}
		if($tmp->level->id==7 && $member->level->id!=7){
			$this->DB->delete('yplisting',array('member'=>$member->id,'listingtype'=>2));
		}
		if($tmp->level->id==8 && $member->level->id!=8){
			$this->DB->delete('yplisting',array('member'=>$member->id,'listingtype'=>3));
			$this->DB->delete('bdlisting',array('member'=>$member->id,'listingtype'=>3));
		}
		$this->DB->processObject($member);
		$this->DB->update('upgradelevelorder',array('closed'=>1),array('member'=>$member->id,'tolevel'=>$member->level->id));
		$this->DB->initObject($member);
		return $member;
	}

	function selectLevelsAdmin($request){
		$levels=array();
		if(empty($request['fields'])) $request['fields']='id,name,level_type';
		if(empty($request['sortColumn'])) { $request['sortColumn']='id'; $request['sortType']='asc';}
		if (empty($request['conditions']['level_type'])) $request['conditions']['level_type']='2';
		$request['curPage']=$this->DB->SelectArray(Array('level'),$request['fields'],$request['conditions'],$this->getOrderBy($request));
		if (!empty($request['curPage']))foreach($request['curPage'] as $k=>$v) $levels[$v['id']]=$v['name'];
		return $levels;
	}

	function changeAdminLevel($request) {
		$member=$this->createRegMember();
		$this->parseRequest($request,$member);
		$tmp=$this->createMember();
		$tmp->id=$member->id;
		$this->DB->initObject($tmp);
		if($tmp->level->id==1 and $this->currentuser->level->id!=1) {
			$member->_error='80';
			return $member;
		}
		$this->DB->processObject($member);
		return $member;
	}

	function saveModulesRights($request){
		$selected=$this->DB->SelectArray('level','id,name',array(),'id asc');
		foreach($selected as $k=>$v){
			$level=$this->createLevel();
			$level->id=$v['id'];
			$level->moduleRights->data=$this->getModuleRightsForLevel($request,$v['id']);
			$this->DB->processObject($level);
			/*
			print "<li>obj";printObject($level);
			foreach($level->moduleRights->data as $t=>$n){
				print "<li>data";printObject($n);
			}
			*/
		}
		return array();
	}

	function getModuleRightsForLevel($arr,$levelid){
		$modulerightsarr=array();
		foreach ($arr as $k=>$v){
			$moduleright=new ModuleRights();
			$moduleright->module=$k;
			foreach($v['perms'] as $l=>$m){
				if($l==$levelid) $moduleright->perm=$m;
			}
			$modulerightsarr[]=$moduleright;
		}
		return $modulerightsarr;
	}

	function getModulesRightsAdmin($request) {
		$selected=$this->DB->SelectArray('level','id,name',array(),'id asc');
		foreach($selected as $k=>$v){
			$level=$this->createLevel();
			$level->id=$v['id'];
			$this->DB->initObject($level);
			$levels[$v['id']]=$level;
		}
		$selected=$this->DB->SelectArray('module','id,name',array());
		foreach($selected as $k=>$v){
			if (!in_array($v['id'],$this->notincludemodule)) {
				$modulerights[$v['id']]['name']=$v['name'];
				$modulerights[$v['id']]['perms']=$this->getPermsForModule($v['id'],$levels);
			}
		}
		return $modulerights;
	}

	function getPermsForModule($moduleid,$levels){
		$perms=array();
		foreach($levels as $k=>$v){
			foreach($v->moduleRights->data as $l=>$m) {
				if($m->module->id==$moduleid) $perms[$k]=$m->perm;
			}
		}
		return $perms;
	}

	function rememberPassword($request) {
		$this->member=$this->createMember();
		$this->parseRequest($request,$this->member);
		$selected=$this->DB->SelectArray("member",'id,pass_hint,pass_answ,email1,first_name,last_name',array('username'=>$this->member->username));
		if (empty($selected)) {
			$this->member->_error='1';
		}
		else {
			if(empty($this->member->pass_hint)){
				$this->member->pass_hint=$selected[0]['pass_hint'];
			}
			else {
				if ($this->member->pass_answ!=$selected[0]['pass_answ']) $this->member->_error='8';
				else {
					 $this->member->id=$selected[0]['id'];
					 $this->member->email1=$selected[0]['email1'];
					 $this->member->first_name=$selected[0]['first_name'];
					 $this->member->last_name=$selected[0]['last_name'];
					 $this->member->passw=substr(md5($this->member->id.$this->member->username.$this->member->id),-8);
					 $tmppassw=$this->member->passw;
					 $this->member->passw=md5($tmppassw);
					 $this->DB->processObject($this->member);
					 $forumhandler=new NewForumHandler();
					 $forumhandler->updateUser($this->member);
					 $this->member->passw=$tmppassw;
					//printObject($this->member);
				}
			}
		}
		return $this->member;
	}

	function selectAdministratorsListAdmin($request){
		if(empty($request['fields'])) $request['fields'] = 'm.id, m.username, m.date_last_login, m.date_last_activity, r.id as role_id, r.name as role_name, mt.id as type_id, mt.name as type_name';
		if(empty($request['sortColumn'])) {
			$request['sortColumn'] = 'ascii(username)';
			$request['sortType'] = 'asc';
		}
		else if($request['sortColumn'] == 'username') {
			$request['sortColumn'] = 'ascii(username)';
			$request['sortType'] = 'asc';
		}
		$request['conditions']['m.role'] = array('=' => 'r.id');
		$request['conditions']['m.type'] = array('=' => 'mt.id');
		//$request['conditions']['m.type'] = array('!=' => 1);
		$request['conditions']['m.canceled'] = array('!=' => 1);
		if(!empty($request['conditions']['username'])) $request['conditions']['username'] = array('like' => '%'.$request['conditions']['username'].'%');
		if(!empty($request['conditions']['first_name'])) $request['conditions']['first_name'] = array('like' => '%'.$request['conditions']['first_name'].'%');
		if(!empty($request['conditions']['r.name'])){
			$request['conditions']['r.name'] = array('=' => $request['conditions']['r.name']);
			unset($request['conditions']['r.name']);
		}
		foreach($request['conditions'] as $k => $v) $rels[] = 'and';
		if(empty($request['curPageNumber'])) $request['curPageNumber'] = 1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty'] = 10;
		$selected = $this->DB->SelectArray( Array('member m', 'role r', 'membertype mt'), 'count(*) as qty', $request['conditions'], 0, 0, 0, array(), 'CND_COMPLEX', $rels);
		$request['recordsTotal'] = $selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal'] / $request['pageRecordsQty']);
		$request['curPage'] = $this->DB->SelectArray(Array('member m', 'role r', 'membertype mt'), $request['fields'], $request['conditions'], $this->getOrderBy($request), $this->getLimit($request), 0, array(), 'CND_COMPLEX', $rels);
		return $request;
	}

	function selectMembersListAdmin($request){
		if(empty($request['fields'])) $request['fields']='m.id,m.username,m.date_last_login,m.date_last_activity,r.id as role_id,r.name as role_name';
		if(empty($request['sortColumn'])) { $request['sortColumn']='ascii(username)'; $request['sortType']='asc';}
		else if($request['sortColumn']=='username') { $request['sortColumn']='ascii(username)'; $request['sortType']='asc';}
		//$request['conditions']['canceled']=array('='=>0);
		$request['conditions']['m.role']=array('='=>'r.id');
		$request['conditions']['m.type']=array('!='=>1);
		$request['conditions']['m.canceled'] = array('!=' => 1);
		if(!empty($request['conditions']['username'])) $request['conditions']['username']=array('like'=>'%'.$request['conditions']['username'].'%');
		if(!empty($request['conditions']['first_name'])) $request['conditions']['first_name']=array('like'=>'%'.$request['conditions']['first_name'].'%');
		if(!empty($request['conditions']['r.name'])){
			$request['conditions']['r.name']=array('='=>$request['conditions']['r.name']);
			unset($request['conditions']['r.name']);
		}
		foreach($request['conditions'] as $k=>$v) $rels[]='and';
		if(empty($request['curPageNumber'])) $request['curPageNumber']='1';
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty']='10';
		$selected=$this->DB->SelectArray(Array('member m','role r'),'count(*) as qty',$request['conditions'],0,0,0,array(),'CND_COMPLEX',$rels);
		$request['recordsTotal']=$selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal']/$request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray(Array('member m','role r'),$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request),0,array(),'CND_COMPLEX',$rels);
		return $request;
	}

	function createMember(){
		$member = new Member();
		$member->role =$this->createRole();
		return $member;
	}

	function createRegMember(){
		$member = new Member();
		//$member->country = new Country();
		//$member->state = new State();
		$member->role=$this->createRole();
		return $member;
	}

	function createRole(){
		$level = new Role();
		$level->moduleRights = new ObjectsList();
		$moduleRights = new ModuleRight();
		$moduleRights->module=new Module();
		$level->moduleRights->type = $moduleRights;
		return $level;
	}
}
?>