<?php
	define('thisscript','memberdownline');

	require_once 'globalad.php';	
	require_once '../class/User.php';	
	$user_obj = new User();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	if (empty($useraccess['manage_member_downline'])){
		redirecting('index.php');
	}
	
	/* sanitize post, get, request */
	$_POST['replayedit'] = ((empty($_POST['replayedit']))?"":$_POST['replayedit']);
	$_POST['do'] = ((empty($_POST['do']))?"":$_POST['do']);
	$_GET['getlist'] = ((empty($_GET['getlist']))?"":$_GET['getlist']);
	$_GET['keyword'] = ((empty($_GET['keyword']))?array():$_GET['keyword']);
	$_GET['field'] = ((empty($_GET['field']))?array():$_GET['field']);
	$_POST['cek'] = ((empty($_POST['cek']))?array():$_POST['cek']);
	$_POST['does'] = ((empty($_POST['does']))?"":$_POST['does']);
	$_GET['error'] = ((empty($_GET['error']))?"":$_GET['error']);		
	$_GET['actions'] = ((empty($_GET['actions']))?"":$_GET['actions']);
	$_POST['show_table'] = ((empty($_POST['show_table']))?"":$_POST['show_table']); 
	$_POST['action'] = ((empty($_POST['action']))?"":$_POST['action']); 
	/* end sanitize */
	
	if (empty($general['showperpageadmin'])){
		$general['showperpageadmin'] = 20;
	}
	
	if($_POST['action'] == 'getDownline'){
		$id = $_POST['id'];
		
		$next_downline = '';
		if(!empty($id )){
			
			$next_downline_db = $db->fetch_all("SELECT u.userid, u.firstname, u.lastname, (SELECT count( userid ) AS total_downline FROM user WHERE parentid = u.userid) total_downline FROM user u WHERE parentid = '".$db->clean($id)."'");
			if(sizeof($next_downline_db) > 0){
				foreach($next_downline_db as $row){
					$next_downline .= '<li>
										'.($row['total_downline'] > 0?'<a href="#" class="toggle" userid="'.$row['userid'].'"></a>&nbsp;&nbsp;':'').'
										<a href="adminuserlistdetail.php?id='.$row['userid'].'">'.$row['firstname'].' '.$row['lastname'].'</a>
									</li>';
				}
			}
			
			if(!empty($next_downline)){
				$next_downline = '<div class="tree-wrapper"><ul>'.$next_downline.'</ul></div>';
			}
		}
		
		echo json_encode(array("downline" => $next_downline));
		exit;
	}
	
	$pagelink = '';
	$listr = '';
	$listaction = '';
	$show_table_list = false;
	
	$first_level_downline = '';
	$first_level_downline_db = $db->fetch_all("SELECT u.userid, u.firstname, u.lastname, (SELECT count( userid ) AS total_downline FROM user WHERE parentid = u.userid) total_downline FROM user u WHERE parentid = '".$db->clean($userid)."'");
	if(sizeof($first_level_downline_db) > 0){
		foreach($first_level_downline_db as $row){
			$first_level_downline .= '<li>
										'.($row['total_downline'] > 0?'<a href="#" class="toggle" userid="'.$row['userid'].'"></a>&nbsp;&nbsp;':'').'
										<a href="adminuserlistdetail.php?id='.$row['userid'].'">'.$row['firstname'].' '.$row['lastname'].'</a>
									</li>';
		}
	}
	
	if(!empty($first_level_downline)){
		$first_level_downline = '<div class="tree-wrapper"><ul>'.$first_level_downline.'</ul></div>';
	}
	
	require_once 'incl/global_template.php';
		
	$theme = gettemplate('membertree');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>