<?php

namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use App\Libraries\MyEncrypt;

//error_reporting(0);



class Genealogyview extends BaseController {
/**
* 
* Start 
* Create Date: 22 sep, 2016
* This controller to manage infobox setting page.
* Created karthiga.
* @see http://zhyip.com/admin/infoboxsettings/
* Last Updated: 22 sep, 2016
* End
* 
*/

public function __construct() {
	    $this->db = db_connect();
		$myencrypt = new MyEncrypt(true);
		$this->common_model = model('CommonModel');
		$session = \Config\Services::session();
		//$validation = \Config\Services::validation();
		//$uri = service('uri');
		

		// user login only
		if($session->get('user_login') || $session->get('admin_login')) {

			// form validation library
			// $this->load->library('form_validation');
			// $this->load->model('userdash_model');
			// $this->load->model('finance_model');
			$this->finance_model = model('finance_model');
			$this->userdash_model = model('userdash_model');

			
		}else{
			throw new \CodeIgniter\Router\Exceptions\RedirectException('login');
		} 
	}



public function index()
{
	$session = \Config\Services::session();
		if(!($session->get('user_login') || $session->get('admin_login')))
		{			
			return redirect()->to('login');
		}
	$this->view();




}

public function view($id='')
{



//$this->uri = service('uri');
 //$this->uri = current_url(true);

// $id=$this->uri->getSegment(10);
$session = \Config\Services::session();
		if(!($session->get('user_login') || $session->get('admin_login')))
		{			
			return redirect()->to('login');
		}

$this->db = db_connect();
$this->common_model = model('CommonModel');

//print_r($id);
//exit();

if($id!=='') {
$userid=$id;

}   else {
$userid = '2';
}

$table= "uusers_matrix";

$condition="uusersid = '".$userid."'";
$user_Det=$this->common_model->GetRow($table,$condition);
//print_r($condition);
//exit();
//$dowid_left=$user_Det->leftId;
//$dowid_right=$user_Det->rightId;

$this->data['val'] = $this->user_ta($userid,$table);

//echo $this->data['val'];
//exit();

echo view('user/genealogyview',$this->data);





}

function getname($id)
 {

$this->db = db_connect();
$this->common_model = model('CommonModel');


$condition = "uusersid = '".$id."'";
$member = $this->common_model->GetRow('uusers',$condition);

return $member->email;
}

function rankimage($id)
 {
$this->db = db_connect();
$this->common_model = model('CommonModel');


$img ='';

$condition="uusersid = '".$id."'";
//$SelectColumn='ProfileImage';

//$imgdetail = $this->common_model->GetRowcol($condition,'uusers', $SelectColumn);

// if($imgdetail->ProfileImage!='')
// {

// 	$img = base_url().$imgdetail->ProfileImage;
// }
// else
// {

$img = base_url().'/assets/img/blankuser.png';
// $img = base_url().'assets/img/avatars/no-photo.jpg';
//}

return $img;
}

function tooltips($getunames,$table)
{

$this->db = db_connect();
$this->common_model = model('CommonModel');


$condition="username = '".$getunames."'";
$exe = $this->common_model->GetRow('uusers',$condition);

$member_id=$exe->uusersid;

$direct_id=$exe->referral_id;

$join_date=$exe->createdate;

$condition="uusersid = '".$member_id."'";

$exe1 = $this->common_model->GetRow($table,$condition);	
//$lef_id=$exe1->leftId;
//$righ_id=$exe1->rightId;

if($lef_id!=0)
$count_left = $this->getlist_left($lef_id,1,$table);    
else 
$count_left = 0; 

if($righ_id!=0)
$count_right = $this->getlist_left($righ_id,1,$table);
else 
$count_right = 0;


// $data= '<div class="tip" style="background-color:#8e0000;color:#fff; width: 325px; -webkit-box-shadow: 0px 0px 19px  #2b2b2b;-moz-box-shadow: 0px 0px 19px #2b2b2b;box-shadow: 0px 0px 19px #2b2b2b; ">
// 		<table width="100%" cellpadding="0" style="color:#fff;" cellspacing="0">
// 		<tr><td nowrap="nowrap">Name</td><td> : </td><td width="40%">'.ucfirst($exe->username).'</td></tr>
// 		<tr><td><bR></td></tr>
// 		<tr><td nowrap="nowrap">Member package</td><td> : </td><td width="40%">'.$exe2->PackageName.'</td></tr>
// 		<tr><td><bR></td></tr>
// 		<tr><td nowrap="nowrap">Account status</td><td> : </td><td>'.ucfirst($exe->MemberStatus).'</td></tr>
// 		<tr><td><bR></td></tr>
// 		<tr><td nowrap="nowrap">Date of Join</td><td> : </td><td>'.ucfirst($exe->DateAdded).'</td></tr>

// 		</table>
// 		</div>';


return $data;

}

function getlist_left($n,$count_left,$table)
{


$this->db = db_connect();

$condition="direct_id = '".$n."'";

$exe=$this->common_model->GetRow($table,$condition);

if($exe>0) 
{
$count_left=$num_rows+$count_left;
}


$condition="referral_id = '".$n."'";
$users = $this->common_model->GetResults($table,$condition);

foreach ($users as $user) 
{
$id=$user->uusersid;
if($id!='0')
{
$count_left =$this->getlist_left($id,$count_left,$table);  
}
}

return $count_left;

}

function display($id,$pos,$table)
{
//var $tmp = array();
if(!empty($id))
{

$this->db = db_connect();

$condition="username = '".$id."'";
$exe2 = $this->common_model->GetRow('uusers',$condition);
//$sql_query_fetch=mysql_fetch_array(mysql_query("select * from members_table where username = '$id'"));
//$member_id=$sql_query_fetch['member_id'];
$member_id=$exe2->uusersid;

$condition="uusersid = '".$member_id."'";

$exe = $this->common_model->GetRow($table,$condition);

//$sql=mysql_query("select * from downlines where member_id=$member_id");
//$exe=mysql_fetch_array($sql);	
if($pos=="left")
{
$id=$exe->leftId;
}
else if($pos='right')
{
$id=$exe->rightId;
}	
$this->tmp = array_merge($this->tmp,array($id));
}


if($id > 0)
{

$condition="uusersid = '".$id."'";
$exe3 = $this->common_model->GetRow('uusers',$condition);
$name=$exe3->username;
return $name;
}
else
{
$dd="";
return $dd; 
}

}

public function count_downlines($userid,$count,$table)
{
//static $count;

$this->db = db_connect();
$this->common_model = model('CommonModel');


$mmcondition = "spillover_id= '".$userid."'";
$check_rows = $this->common_model->GetRowCount($table,$mmcondition);
$fet = $this->common_model->GetResults($table,$mmcondition);


if($check_rows)
{
for($i=0; $i<$check_rows; $i++)
{


$use = $fet[$i]->uusersid;

$count++;

$mncondition = "spillover_id = '".$use."'";

$check_rows_use = $this->common_model->GetRowCount($table,$mncondition);

if($check_rows_use > 0)
{

$this->count_downlines($use,$count,$table);
}
}


}

if($count=='')
{
$count = 0;
}
return $count;
}

public function user_ta1($userid,$table)
{


	$color='black';

$this->db = db_connect();
$this->common_model = model('CommonModel');

$gendata="";
$umcondition = "spillover_id= '".$userid."'";
$select_count = $this->common_model->GetRowCount($table,$umcondition);
$select = $this->common_model->GetResults($table,$umcondition);



if($select_count>0)
{
$gendata.='<ul class="collapse">';

for($j=0; $j<$select_count; $j++)
{
$uncondition = "spillover_id= '".$select[$j]->uusersid."'";
$uocondition = "uusersid= '".$select[$j]->uusersid."'";
$inner_fata_count = $this->common_model->GetRowCount($table,$uocondition);
$inner_fata = $this->common_model->GetRow($table,$uocondition);
//$rank_condition="uusersid='".$inner_fata->uusersid."'";
//$rank=$this->common_model->GetRow($rank_condition,'arm_members');

//$rank_id=$rank->rank;


//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank_id);


//print_r($table);
if($inner_fata_count>0)
{
$gendata.= $this->call_from($select[$j]->uusersid,$table);
}
else
{

/*if ($inner_fata_count >= 2) {
$noreferral= $this->Generalsetting_model->Getsite('tworeferral');
}
elseif($inner_fata_count == 1) {
$noreferral= $this->Generalsetting_model->Getsite('onereferral');
}else{
$noreferral= $this->Generalsetting_model->Getsite('noreferral');
}*/


$gendata.='<li id="beer" style="cursor: s-resize;"><img style="border:5px solid '.$color.';"  src='.$this->rankimage($inner_fata->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($inner_fata->uusersid)).' ('.$this->count_downlines($inner_fata->uusersid,'',$table).') </h1>
	</li>'; 

}

}

$gendata.='</ul>';
}

return $gendata;

}


public function call_from($userid,$table)
{


$color='black';
$this->db = db_connect();
$this->common_model = model('CommonModel');
$gendatagof ="";
$cmcondition ="uusersid = '".$userid."'";
$select_high =$this->common_model->GetRow($table,$cmcondition);

//print_r($table);


$ncondition = "spillover_id='".$userid."'";

$mcount = $this->common_model->GetRowCount($table,$ncondition);
/* if ($mcount >= 2) {
$noreferral= $this->Generalsetting_model->Getsite('tworeferral');
}
elseif($mcount == 1) {
$noreferral= $this->Generalsetting_model->Getsite('onereferral');
}else{
$noreferral= $this->Generalsetting_model->Getsite('noreferral');
}*/


// $member_id=$select_high->uusersid;

//$cocondition ="uusersid = '".$member_id."'";

//$rank=$this->common_model->GetRow($cocondition,'arm_members');

//$rank_id=$rank->rank;


//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank_id);


$gendatagof.= '<li id="beer" class="collapsed" style="cursor: s-resize;"><div class="node"
	data-toggle="tooltip" title=" Email 		: '.($this->getname($select_high->uusersid)).' &#xa; Invitation code 	: '.($this->getspname($select_high->uusersid,$table)).'&#xa; Member Since 	: '.$this->getdate($select_high->uusersid,$table).'">
	<img style="border:5px solid '.$color.';" src='.$this->rankimage($select_high->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($select_high->uusersid)).'  ('.$this->count_downlines($select_high->uusersid,'',$table).')</h1>
	</div><ul>';
	$cncondition = "spillover_id = '".$userid."'";


$run_count = $this->common_model->GetRowCount($table,$cncondition);
$run = $this->common_model->GetResults($table,$cncondition);
for($i=0; $i<$run_count; $i++)
{
$cocondition ="uusersid = '".$run[$i]->uusersid."'";
$inner_fata = $this->common_model->GetRow($table,$cocondition);
$inner_fata_count = $this->common_model->GetRowCount($table,$cocondition);
//print_r($inner_fata_count);


$ncondition = "spillover_id='".$run[$i]->uusersid."'";

$mcount = $this->common_model->GetRowCount($table,$ncondition);



/*if ($mcount >= 2) {
$noreferral= $this->Generalsetting_model->Getsite('tworeferral');
}
elseif($mcount == 1) {
$noreferral= $this->Generalsetting_model->Getsite('onereferral');
}else{
$noreferral= $this->Generalsetting_model->Getsite('noreferral');
}*/


if($inner_fata_count >0)
{

//$rank_condition="uusersid='".$inner_fata->uusersid."'";
//$rank=$this->common_model->GetRow($rank_condition,'arm_members');

//$rank_id=$rank->rank;

//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank_id);

$gendatagof.= '<li id="beer" class="collapsed"><div class="node"
	data-toggle="tooltip" title=" Email 		: '.($this->getname($inner_fata->uusersid)).' &#xa; Invitation Code 	: '.($this->getspname($inner_fata->uusersid,$table)).'&#xa; Member Since 	: '.$this->getdate($inner_fata->uusersid,$table).'">
	<img style="border:5px solid '.$color.';" src='.$this->rankimage($inner_fata->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($inner_fata->uusersid)).' ('.$this->count_downlines($inner_fata->uusersid,'',$table).') </h1>
	</div>';
	$gendatagof.= $this->user_ta1($inner_fata->uusersid,$table);
	$gendatagof.= '</li>';
}
else
{

//$rank_condition="uusersid='".$inner_fata[$i]->uusersid."'";

//$rank=$this->common_model->GetRow($rank_condition,'arm_members');

// $rank_id=$rank->rank;

//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank_id);


$gendatagof.='<li id="beer" class="expanded collapse" style="cursor: s-resize;"><div class="node"
	data-toggle="tooltip" title=" Email		: '.($this->getname($inner_fata[$i]->uusersid)).' &#xa; Invitation Code	: '.($this->getspname($inner_fata[$i]->uusersid,$table)).'&#xa; Member Since 	: '.$this->getdate($inner_fata[$i]->uusersid,$table).'">
	<img style="border:5px solid '.$color.';" src='.$this->rankimage($inner_fata[$i]->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($inner_fata[$i]->uusersid)).' ('.$this->count_downlines($inner_fata[$i]->uusersid,'',$table).')</h1>
	</div></li>'; 
}
}

$gendatagof.= '</ul></li>';

return $gendatagof;
}



function getspname($id,$table)
{
$this->db = db_connect();

$condition ="uusersid = '".$id."'";

$member = $this->common_model->GetRow('uusers',$condition);
if($member->referral_id!==0)
{


return $member->invitation_code;
}
}
function getdate($id,$table)
{
$this->db = db_connect();
$this->common_model=model('CommonModel');
$member = $this->common_model->GetCustomer($id,$table);
$date=date_create($member->cr_date);	
return date_format($date,"d M Y");
}


public function user_ta($userid,$table)
{
//print_r($table,$userid);
//exit();

$this->db = db_connect();
$this->common_model = model('CommonModel');

$img1 = base_url().'/assets/img/01-us.gif';
$img2 = base_url().'/assets/img/02-us.gif';
$img3 = base_url().'/assets/img/03-us.gif';

$img4= base_url().'/assets/img/register.png';

$img5= base_url().'/assets/img/blankuser.png';

$color='black';

$gendata='';

$mcondition = "uusersid='".$userid."'";

$select_high = $this->common_model->GetRow($table,$mcondition);

//print_r($select_high);
//exit();


//$rank_condition="uusersid='".$select_high->uusersid."'";

//$rank_id = $this->common_model->GetRow($rank_condition, 'arm_members');
//print_r($select_high);
//$rank=$rank_id->rank;

//$rank=$select_high->rank;

//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank);

//print_r($rankcolor);
//exit();


$ncondition = "spillover_id='".$userid."'";

$mcount = $this->common_model->GetRowCount($table,$ncondition);
//print_r($mcount);
//exit();


/*if ($mcount >= 2) {
$noreferral= $this->Generalsetting_model->Getsite('tworeferral');
}
elseif($mcount == 1) {
$noreferral= $this->Generalsetting_model->Getsite('onereferral');
}else{
$noreferral= $this->Generalsetting_model->Getsite('noreferral');
}*/


$gendata.= '<ul id="org" >';

if($userid!="")
{



	$gendata.='<li id="beer" class="node expanded collapse" style="cursor: s-resize;"><div class="node"
	data-toggle="tooltip" title=" Email 		: '.($this->getname($select_high->uusersid)).' &#xa; Invitation Code 	: '.($this->getspname($select_high->uusersid,$table)).'&#xa; Member Since 	: '.$this->getdate($select_high->uusersid,$table).'">
	<img style="border:5px solid  '.$color.';" src='.$this->rankimage($select_high->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($select_high->uusersid)).' ('.$this->count_downlines($select_high->uusersid,0,$table).') </h1></div>';
}

//print_r($gendata);
//exit();

$ncondition = "spillover_id='".$userid."'";

$mcount = $this->common_model->GetRowCount($table,$ncondition);

$select_member = $this->common_model->GetResults($table,$ncondition);
 //print_r($mcount);
 //exit();



if($mcount>0)
{ 
$gendata.='<ul class="collapse">';

for($i=0; $i<$mcount; $i++)
{

$incondition = "spillover_id='".$select_member[$i]->uusersid."'";

//print_r($select_member[$i]->uusersid);
//exit();
$inner_fata_count = $this->common_model->GetRowCount($table,$incondition);
$inner_fata = $this->common_model->GetRow($table,$incondition);




/*if ($inner_fata_count >= 2) {
$noreferral= $this->Generalsetting_model->Getsite('tworeferral');
}
elseif($inner_fata_count == 1) {
$noreferral= $this->Generalsetting_model->Getsite('onereferral');
}else{
$noreferral= $this->Generalsetting_model->Getsite('noreferral');
}*/

if($inner_fata_count>0)
{

$gendata.= $this->call_from($select_member[$i]->uusersid,$table);

//print_r($gendata);
//exit();
}
else
{

//$rank1=$select_member[$i]->uusersid;

//$mcondition = "uusersid='".$rank1."'";

//  $rank_id = $this->common_model->GetRow($mcondition, 'arm_members');

//print_r($select_high);

// $rank=$rank_id->rank;

//$rankcolor=$this->Ranksetting_model->Getcareerdata($rank);



$gendata.='<li id="beer" class="expanded collapse" style="cursor: s-resize;"><div class="node"
	data-toggle="tooltip" title=" Email 		: '.($this->getname($select_member[$i]->uusersid)).' &#xa; Invitation Code 	: '.($this->getspname($select_member[$i]->uusersid,$table)).'&#xa; Member Since 	: '.$this->getdate($select_member[$i]->uusersid,$table).'">
	<img style="border:5px solid '.$color.';" src='.$this->rankimage($select_member[$i]->uusersid).' style=margin-bottom:-42px; />
	<h1>'.ucwords($this->getname($select_member[$i]->uusersid)).' ('.$this->count_downlines($select_member[$i]->uusersid,'',$table).')</h1>
	</div></li>'; 
	
}

}

$gendata.='</ul>';

}
$gendata.='</li></ul>';
//$gendata.='</table>';

return $gendata;

}





}

