<?php

require_once('../init.php');
$GLOBALS['smarty']->assign('page','lieux');
$GLOBALS['template'] = 'lieux/lieux_list.tpl';
$GLOBALS['smarty']->assign('secondbar','lieux/lieux_secondbar.tpl');
$jsFiles[] = PUBLIC_HTML_ADMIN.'js/lieux.js';

if($id>0 && $action) {
    $infos = getLieuDatas($id);
    switch($action) {
        // AJAX GET DATAS
        case "getDatas": die(json_encode($infos)); break;
        // EDIT
        case "edit": {
            $datas = getLieuDatasFromRequest();
            die( strval( updateLieu($id,$datas) ) );
        } break;
        // DELETE
        case "delete" : die( strval( deleteLieu($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}
switch($action) {
    // ADD
    case "add": {
        $datas = getLieuDatasFromRequest();
        die( strval( addLieu($datas) ) );
    } break;
    // SELECT LIST
    case "select_list": {
        $GLOBALS['smarty'] -> assign('lieux_list', getLieuxList());
        die( $GLOBALS['smarty']->fetch("lieux/lieux_select_list.tpl") );
    } break;
    // DEFAULT - LIST
    default: {
        // SEARCH
        $search = getSearch("lieux_search");
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbLieuxInList($search));
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits['start'] = intval($_REQUEST['startListAt']);
            $limits['end'] = $limits['start'] + $limits['nbItemsByPage'];
            if($limits['end'] > $limits['max']) $limits['end'] = $limits['max'];
        }
        $limits['rest'] = $limits['max'] - $limits['end'];
        $GLOBALS['smarty'] -> assign('list_limits',$limits);
        
        // LIST
        $order = getListOrder('lieux_list_order', 'lieux_list_sens', $allowLieuxListOrder, 'nom');
        $list = getLieuxList($order["order"], $order["sens"], $search, $limits);
        $GLOBALS['smarty'] -> assign('list',$list);
        
        // LIST PART
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits["list"] = $GLOBALS['smarty']->fetch("lieux/lieux_list_builder.tpl");
            die(json_encode($limits));
        }
        
        // DISPLAY
        display();
    }
}