<?php
require_once("include/dbcommon.php"); // DataBase PHPRunner

// Require our Event class and datetime utilities
require 'fullcalendar/php/utils.php';



$sql = "
SELECT u.user_id, u.login, u.username , concat(c.name,'+', ifnull(d.name,'')) name, ((u.companies_company_id*10000) + ifnull(u.departments_dept_id,0)) id_companie
FROM users u
left join companies c on (u.companies_company_id = c.company_id)
left join departments d On (d.dept_id = u.departments_dept_id)
order by c.name, d.name,u.login
";


$resql = DB::Query($sql); 
// Accumulate an output array of event data arrays.
// $output_arrays = array();
$str = '[';

$group = '';
while ($data = $resql->fetchAssoc()){
 
    If ($group <> $data['name']) {
        if ($group <> '' ) {
            $str = substr($str,0,strlen($str)-1); // erase last ","
            $str .= '  ] },';
        }
        $str .= '{ "id": "'.$data['id_companie'].'", "title": "'.$data['name'].'", "children": [';
        $group = $data['name'];
        
    } 
    $str .= '{ "id": "'.$data['user_id'].'", "title": "'.$data['username'].'", "eventColor": "'.'#1D52B2'.'" },';
    
}
$str = substr($str,0,strlen($str)-1); // erase last ","
$str .= '  ] } ]';

echo $str;
// Send JSON to the client.
// $str = json_encode($output_arrays);
// echo json_encode($output_arrays);

?>