<?PHP
class users extends engine {
	var $call_sql_class = array('roles', 'users', 'clients', 'client_details', 'permissions', 'agent_services', 'service_company');
	var $call_class = array('Form', 'File');

	function roles(){

		$this->roles->Select();
		return $this;
	}

	function get_users_data(){

		$this->roles->dataSeek();
		$this->roles->Select("WHERE level <= ".$_SESSION['UserData']['level']." ");

		if($_GET['search'])
			$search = " AND (p2.first_name LIKE '%".$_GET['search']."%' OR p2.last_name LIKE '%".$_GET['search']."%' OR CONCAT(p2.first_name, ' ', p2.last_name) LIKE '%".$_GET['search']."%' OR p1.name LIKE '%".$_GET['search']."%') ";

		$this->users->users_join_clients_details_roles(
			"WHERE p4.level <= ".$_SESSION['UserData']['level']." ".
			$search.
			"ORDER BY p1.name ASC ");
		return $this;
	}

	function dashboard(){

		$this->users->users_and_roles("where p2.level <= ".$_SESSION['UserData']['level']." ".
			"ORDER BY id DESC ");
		return $this;
	}

	function count_not_accepted_companys($row){

		$c = 0;

		if($row['service_company']){

			foreach(unserialize($row['service_company']) as $k => $v){

				if(!$v['status']) $c++;
			}
		}
		$row['count_service_company_status'] = $c;
		return $row;
	}


	//update

	function insert_role(){

		$this->roles->Insert($_POST);
		$Insertid = $this->roles->Insertid;
		$this->roles->Select("WHERE level = ".$_POST['level']." AND id != ".$Insertid);
		while($row = $this->roles->Assoc()){

			$this->permissions->Select("WHERE role_id = ".$row['id']);
			while($row2 = $this->permissions->Assoc()){

				$this->permissions->Insert(array('role_id' => $Insertid, 'table_name' => $row2['table_name'], 'table_id' => $row2['table_id'], 'created_at' => 'NOW()', 'allow_access' => $row2['allow_access']));
			}
		}
	}

	function update_role(){

		$this->roles->Update($_POST, "WHERE id = ".$_GET['id']);
	}

	function delete_role(){

		$this->permissions->Delete("WHERE role_id = ".$_GET['role_id']);
		$this->clients->clients_join_details_users_roles("WHERE p4.id = ".$_GET['role_id'], true);

		if(is_array($this->clients->Rows)){

			foreach($this->clients->Rows as $v){

				$this->users->Delete("WHERE id = ".$v['user_id']);
				$this->clients->Delete("WHERE id = ".$v['id']);
				$this->client_details->Delete("WHERE id = ".$v['client_details_id']);
			}
		}
		$this->roles->Delete("WHERE id = ".$_GET['role_id']);
	}




	function insert_user(){

		$_POST['created_at'] = 'NOW()';
		$_POST['password'] = md5($_POST['password']);

		$this->roles->Select("WHERE id = ".$_POST['role_id'], true);
		$_POST['type'] = $this->roles->Rows[0]['name'];

		$this->users->Insert($_POST);

		$_POST['user_id'] = $this->users->Insertid;
		$this->clients->Insert($_POST);

		$_POST['client_id'] = $this->clients->Insertid;
		$this->client_details->Insert($_POST);
	}

	function update_user(){

		if(!$_POST['password']){

			unset($_POST['password']);
		}
		else{

			$_POST['password'] = md5($_POST['password']);
		}
		$_POST['is_enabled'] = $_POST['is_enabled'] ? 1 : 0;

		$this->roles->Select("WHERE id = ".$_POST['role_id'], true);
		$_POST['type'] = $this->roles->Rows[0]['name'];

		$this->users->Update($_POST, "WHERE id = ".$_POST['id']);
		$this->clients->Update($_POST, "WHERE user_id = ".$_POST['id']);

		$this->clients->Select("WHERE user_id = ".$_POST['id'], true);
		$this->client_details->Update($_POST, "WHERE client_id = ".$this->clients->Rows[0]['id']);
	}

	function delete_user(){

		$this->users->Delete("WHERE id = ".$_GET['id']);

		$this->clients->Select("WHERE user_id = ".$_GET['id'], true);
		if($this->clients->Numrows > 0){

			$client_id = $this->clients->Rows[0]['id'];
			$this->client_details->Delete("WHERE client_id = ".$client_id);
			$this->clients->Delete("WHERE id = ".$client_id);
		}
	}

	function accept_agent(){

		$this->clients->Update(array('is_temporary' => '0'), "WHERE user_id = ".$_GET['id']);
	}

	function block_agent(){

		$this->clients->Update(array('is_temporary' => '1'), "WHERE user_id = ".$_GET['id']);
	}

	function accept_agent_company(){

		$this->clients->Select("WHERE id = ".$_GET['id'], true);
		$service_company = unserialize($this->clients->Rows[0]['service_company']);
		foreach($service_company as $k => $v){

			if($v['id'] == $_GET['company_id']){

				$service_company[$k]['status'] = 'accepted';
			}
		}
		$this->clients->Update(array('service_company' => serialize($service_company)), "WHERE id = ".$_GET['id']);
	}

	function decline_agent_company(){

		$this->clients->Select("WHERE id = ".$_GET['id'], true);
		$service_company = unserialize($this->clients->Rows[0]['service_company']);
		foreach($service_company as $k => $v){

			if($v['id'] == $_GET['company_id']){

				$service_company[$k]['status'] = 'declined';
			}
		}
		$this->clients->Update(array('service_company' => serialize($service_company)), "WHERE id = ".$_GET['id']);
	}
}
?>