<?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/>.
*/
# ***************************************
# add or remove a manager (for a user)
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : éàèù
# ***************************************
/**
    script : scr_add_modify_manager.php

**/
$act=(isset($_REQUEST["act"]))?$_REQUEST["act"]:exit();
$id=(isset($_REQUEST["manager"]))?$_REQUEST["manager"]:exit();

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

$newBranch = '';
$data = array();
if ($act == 'add') {
	$tmp_user_id = 0;
	$DB2 = new connection(__SERVER,__LOGIN,__PASS,__DB);

	$DB->getResults($organigram_checkIfManager,$DB->quote(",".$id.",%,".$_SESSION['user_id'].","));
	if($DB->nbResults()<=0) {
		// Copy manager
		$DB->getResults($organigram_getAllManagerBranch,$DB->quote("%,".$id.","));//,$DB->escape($id));
		if($DB->nbResults()>0) {
			while ($row = $DB->fetch(0)){
				$newBranch = $row['branch'].$_SESSION['user_id'].",";
				$DB2->execute($scrmanager_addNewManager,$DB2->escape($_SESSION['user_id']),$DB2->escape($id),$DB2->quote($newBranch));
			}

		}

		// Copy managed
		$DB->getResults($organigram_getAllManagedBranch,$DB->quote(",".$_SESSION['user_id'].",%"));
		if($DB->nbResults()>0) {
			while ($row = $DB->fetch(0)){
				$newBranch = ",".$id.$row['branch'];
				$DB2->execute($scrmanager_addNewManager,$DB2->escape($_SESSION['user_id']),$DB2->escape($id),$DB2->quote($newBranch));
			}

		}
		$DB->execute($scrmanager_addNewManager,$DB->escape($_SESSION['user_id']),$DB->escape($id),$DB->quote(",".$id.",".$_SESSION['user_id'].","));
		$data['status'] = 1;
	} else {
		$data['status'] = 0;
		$data['err'] = lg('errManager');
	}
    
}
else if ($act == 'del') {
    $DB->execute($scrmanager_deleteManager,$DB->escape($_SESSION['user_id']),$DB->escape($id));
	$DB->execute($organigram_delAllManagerBranch,
				$DB->quote(",".$id.",".$_SESSION['user_id'].","),
				$DB->escape($_SESSION['user_id']),
				$DB->quote("%,".$id.",".$_SESSION['user_id'].",%")
				);
    $data['status'] = 1;
}

echo json_encode($data);
$DB->close();
?>