<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.

	This file is part of COLLABORATION SUITE of POSH http://sourceforge.net/projects/posh/.

	POSH is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version

	POSH is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Posh.  If not, see <http://www.gnu.org/licenses/>.
*/
# ***************************************
# Get users search result
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : éàèù
# ***************************************

$folder="";
$not_access=1;
$granted="I";
$pagename="portal/json_getorganigram.php";
//includes
require_once('includes.php');
require_once('../db_layer/'.__DBTYPE.'/organigram.php');


if(!isset($_SESSION['user_id']))
	exit();
$selectedUser = isset($_GET['uid'])?$_GET['uid']:0;
if($selectedUser == 0)
	exit();
$manager = array();
$managed = array();
//$associate = array();
//$branch_manager = array();


$DB = new connection(__SERVER,__LOGIN,__PASS,__DB);
$DB2 = new connection(__SERVER,__LOGIN,__PASS,__DB);
//$userData = array();
$jsonManager = array();
$jsonManaged = array();
$managed_id = "";
$manager_id = "";

$listManager = "";
$listManaged = "";

$tmp_managed = array();
$tmp_manager = array();
$managerHighLevel = 0;

$DB->getResults($organigram_getUserBranch,$DB->quote("%,".$selectedUser.",%"));
$i = 0;
if ($DB->nbResults()>0) {
	while ($row = $DB->fetch(0)){
		$tmp = preg_split('/,'.$selectedUser.',/',$row['branch']);
		if(!empty($tmp[0])) {
			if($tmp[0][0]==",") {
				$tmp[0] = substr( $tmp[0],1,strlen($tmp[0]));
			}
			
			if(!in_array($tmp[0],$tmp_manager) && !in_array($tmp[0],$jsonManager)) {
				$listManager .= $tmp[0].',';
				$jsonManager[$i] = array_reverse(explode(",",$tmp[0]));
				$tmp_manager[] = $tmp[0];
				$managerHighLevel = ($managerHighLevel<count($jsonManager[$i]))?count($jsonManager[$i]):$managerHighLevel;
				$i++;
			}
		}
		
		if(!empty($tmp[1])) {
			if($tmp[1][strlen($tmp[1])-1]==",") {
				$tmp[1] = substr( $tmp[1],0,(strlen($tmp[1])-1));
			}
			if(!in_array($tmp[1],$tmp_managed)) {
				$listManaged .= ",".$tmp[1];
				$jsonManaged[] = explode(",",$tmp[1]);
				$tmp_managed[] = $tmp[1];
			}
		}
		
		$manager[] = $row['manager'];
		if($row['managed']!=$selectedUser)
			$managed[] = $row['managed'];
	}
	$DB->freeResults();
		
	if( substr( $listManager,0,1 )==',' ) {
		$listManager = substr( $listManager,1,strlen($listManager) );
	}

	$listManager = preg_replace('/,,/',',',$listManager);
	$listManager .= $selectedUser;

	$listManaged = substr  ( $listManaged,1,strlen($listManaged)-1 );

	$manager_id = $listManager;
	$managed_id = $listManaged;

	// Get managed data
	if(!empty($managed_id)) {
		$DB->getResults($organigram_getGroupUserData,$managed_id);
		while ($row = $DB->fetch(0)){
			$picture = '';
			$picture .= (empty($row['picture']))
						?
						'<img src="../images/nopicture.gif" style="width:35px;height:35px;" border="0"/>'
						:
						'<img src="'.$row['picture'].'" id="img'.$row['id'].'" style="width:35px;height:35px;" border="0"/>';
			$long_name = $row['long_name'];
			//$userData[$row['id']] = array('long_name'=>$long_name,'picture'=>$picture);
				
			// JSON
			$data[$row['id']]['id'] = $row['id'];
			$data[$row['id']]['long_name'] = $long_name;
			$data[$row['id']]['picture'] = $picture;		
		}
		$DB->freeResults();
	}

	// Get manager data
	$DB->getResults($organigram_getGroupUserData,$manager_id);
	while ($row = $DB->fetch(0)){
		if($row['id']==$selectedUser) {
			$picture = (empty($row['picture']))
						?
						'<img src="../images/nopicture.gif" style="width:35px;height:35px;" border="0"/>'
						:
						'<img src="'.$row['picture'].'" id="img'.$row['id'].'" style="width:35px;height:35px;" border="0"/>';
				
			$long_name = $row['long_name'];
		} else {
			$picture = '';
			$picture .= (empty($row['picture']))
						?
						'<img src="../images/nopicture.gif" style="width:35px;height:35px;" border="0"/>'
						:
						'<img src="'.$row['picture'].'" id="img'.$row['id'].'" style="width:35px;height:35px;" border="0"/>';
			$long_name = ''.$row['long_name'].'';
		}

		//$userData[$row['id']] = array('long_name'=>$long_name,'picture'=>$picture);
		
		// JSON
		$data[$row['id']]['id'] = $row['id'];
		$data[$row['id']]['long_name'] = $long_name;
		$data[$row['id']]['picture'] = $picture;
		$data[$row['id']]['email'] = $row['email'];
	}
	$DB->freeResults();
		
	$DB->getResults($organigram_getSelectedUserBranch,$DB->escape($selectedUser),$DB->escape($selectedUser));
		
	$DB->close();
	$DB2->close();
	$response['response'] = 1;
	$response['mgrhighlevel'] = $managerHighLevel;
	array_multisort($jsonManager,SORT_DESC);
	$response['manager'] = json_encode($jsonManager);
	$response['managed'] = json_encode($jsonManaged);
	$response['tab'] = json_encode($data);

	
} else {
	$response['response'] = 0;
}	
echo json_encode($response);
?>