<?php

//init
$previous_link = true;
$next_link = true;

$logc_list_query = "SELECT * FROM log_category ORDER BY logc_title ASC";
$logc_assoc_array = $sql->assoc_array($logc_list_query);
$log_categories = array();

//jött-e újabb POST
$update = false;
//lapozható-e legyen-e?
$fold = true;

//POST DATA HANDLER
//ha nem a gyerek belépéseit kérjük le ÉS létezik postolt logcategory és (nem létezik a cookie VAGY létezik de nem egyenlő az eddigivel)
$selected = "";
if (!isset($_REQUEST['login_kid']) && isset($_REQUEST['log_category']) && (!isset($_COOKIE['log_category']) || (isset($_COOKIE['log_category']) && $_COOKIE['log_category'] != $_REQUEST['log_category']))) {
    if ($_REQUEST['log_category'] != 'null') $condition = "WHERE log_log_category_logc_id = " . $_REQUEST['log_category'];
    //beállítjuk a cookie-t, rövid élettartammal
    setcookie("log_category", $_REQUEST['log_category'], time()+60*60, '/');
    $selected = $_REQUEST['log_category'];
    $update = true;
}

elseif (isset($_REQUEST['login_kid'])) {
    //lekérjük a gyerek befizetéseit + hideoljuk a lapozót
    $condition = "WHERE log_log_category_logc_id = 3 and log_user_id = " . $_REQUEST['login_kid'];
    $fold = false;
}
//COOKIE HANDLER - igazából ide kell a condition leírása
if (isset($_COOKIE['log_category']) && $_COOKIE['log_category'] != 'null' && !$update && !isset($_REQUEST['login_kid'])) {
    $condition = "WHERE log_log_category_logc_id = " . $_COOKIE['log_category'];
    $selected = $_COOKIE['log_category'];
}


foreach ($logc_assoc_array as $logc_array) {
    $new_logc = new log_category();
    $new_logc->set_logc_data_by_id($logc_array['logc_id']);
    $log_categories[] = $new_logc;
}


if ($this->is_id()) {
    # LOG LIST FILTERED
    //50esével megyünk
    $from = ($this->get_id() - 1) * 50;
    $log_query = "SELECT * FROM log " . (isset($condition)?$condition:"") . " ORDER BY log_date DESC " . ($fold?"LIMIT ".$from.",50;":"");
    $log_count = $sql->num_of_rows("SELECT * FROM log ".(isset($condition)?$condition:"")." ORDER BY log_date DESC;");
    //echo $log_count;
    if ($log_count <= $this->get_id()*50) {
        $next_link = false;
    }
    $smarty->assign('next_id', ($next_link?$this->get_id()+1:false));
    $smarty->assign('previous_id', ($this->get_id()>1?$this->get_id()-1:false));

    $log_assoc_array = $sql->assoc_array($log_query);
    $log_array = array();
    foreach ($log_assoc_array as $log_list_array) {
        $current_log = new log();
        $current_log->set_log_data_by_id($log_list_array['log_id']);
        $log_array[] = $current_log;
    }

    $smarty->assign('selected', $selected);
    $smarty->assign('fold', $fold);
    $smarty->assign('logc_array', $log_categories);
    $smarty->assign('log_array', $log_array);
    $smarty->display('log.tpl');
}
else {
    # LOG LIST
    header("Location: /admin/log/1");
    /*
    $log_query = "SELECT * FROM log ORDER BY log_date DESC LIMIT 50;";
    $previous_link = false;
    $smarty->assign('next_id', '2');
    */
}



?>
