<?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/xml_myposition.php";
//includes
require_once('includes.php');
require_once('../db_layer/'.__DBTYPE.'/organigram.php');
require_once('../../../xml.inc.php');

$file=new xmlFile();

$file->header("myposition");
$seluser = isset($_GET['u'])?$_GET['u']:0;
$DB->getResults($xml_getMyPosition,
	$DB->quote(','.$seluser.',%,'.$_SESSION['user_id'].','),
	$DB->quote(','.$_SESSION['user_id'].',%,'.$seluser.','),
	$DB->quote(','.$_SESSION['user_id'].','.$seluser.','),
	$DB->quote(','.$seluser.','.$_SESSION['user_id'].',')
	);
if ($DB->nbResults()!=0) {
    $row=$DB->fetch(0);
    echo "<data><![CDATA[".$row["branch"]."]]></data>";

}else {
	$data = array();
	$DB->getResults($xml_getMaxBranch,
		$DB->escape($seluser)
		);
		$row=$DB->fetch(0);
		$branch = explode(",",$row['branch']);
	$DB->freeResults();
		for($i=1;$i<count($branch)-2;$i++) {
			$DB->getResults($xml_searchMyPosition,
				$DB->quote(','.$branch[$i].'%,'.$_SESSION['user_id'].',%')
				);
			$row=$DB->fetch(0);
			if( isset($row['branch']) ) {
				$branch2 = $row['branch'];
				echo "<data><![CDATA[".$branch2."]]></data>";
				$DB->freeResults();

				$branch2 = substr  ( $branch2,1,strlen($branch2)-2 );

				$DB->getResults($organigram_getGroupUserData,$branch2);
				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'];
					// JSON
					$data[$row['id']]['id'] = $row['id'];
					$data[$row['id']]['long_name'] = $long_name;
					$data[$row['id']]['picture'] = $picture;		
				}
				$DB->freeResults();
				echo "<userinfo><![CDATA[".json_encode($data)."]]></userinfo>";
				break;
			}
		}
	

		
}
$DB->freeResults();
$file->footer("myposition");

$DB->close();
?>