<?php
	if( !$this->user->is_logged ) {
		$this->redirect('home');
	}
	
	$D->codgroup = '';
	if ($this->param('g')) $D->codgroup = $this->param('g');

	if (empty($D->codgroup)) $this->redirect($C->SITE_URL.'dashboard/groups');	

	/*************************************************************************/
	// needed before proceeding
	require_once('_all-required-language.php');
	
	$this->load_langfile('global/global.php');	
	$this->load_langfile('inside/dashboard.php');

	/*************************************************************************/
	
	
	require_once('_all-required-dashboard.php');
	

	/*************************************************************************/
	
	$r = $this->db2->query("SELECT * FROM groups WHERE code='".$D->codgroup."' AND idcreator=".$this->user->id." LIMIT 1");
	
	if (!$obj = $this->db2->fetch_object($r)) {
		$this->redirect($C->SITE_URL.'dashboard/groups');
	} else {
		$D->idgroup = $obj->idgroup;
		$D->codegroup = $obj->code;
		$D->namegroup = stripslashes($obj->name_group);
		$D->urlgroup = $obj->url;
	}

	
	$D->cadsearch = '';
	if ($this->param('q')) $D->cadsearch = $this->db1->e($this->param('q'));
	
	$D->cadShort = 0;
	if (!empty($D->cadsearch) && strlen($D->cadsearch)<=3) $D->cadShort = 1;

	$D->doSearch = 0;

	if (!empty($D->cadsearch) && $D->cadShort==0) {

		$listUsers = $this->db2->fetch_all("
		SELECT * FROM users 
		WHERE 
		(iduser NOT IN (SELECT iduser FROM groups_members WHERE idgroup=".$D->idgroup.")) 
		AND active=1 
		AND (firstname LIKE '%".$D->cadsearch."%' OR lastname LIKE '%".$D->cadsearch."%' OR username LIKE '%".$D->cadsearch."%') 
		ORDER BY num_friends DESC
		");
		
		$D->doSearch = 1;
	
		$D->htmlUsers = '';
		
		foreach($listUsers as $obj) {
			$D->isThisUserVerified = $obj->verified==1?TRUE:FALSE;
			$D->f_name = (empty($obj->firstname) || empty($obj->lastname))?stripslashes($obj->username):(stripslashes($obj->firstname).' '.stripslashes($obj->lastname));
			$D->f_codeuser = $obj->code;
			$D->f_iduser = $obj->iduser;
			$D->f_avatar = $obj->avatar;
			$D->f_numfriends = $obj->num_friends;
			$D->f_username = $obj->username;
			$D->htmlUsers .= $this->load_template('__one-user-to-group.php', FALSE);
		}
		
		unset($listUsers, $obj);
		
	}
	
	/*************************************************************************/

	$D->optionactive = 12;
	$D->tab_active = 1;
	$this->load_template('dashboard-groups-addmembers.php');
?>