<?php

    $action_list_query = "
        SELECT
            object_id,
            TIMESTAMP(object_date) AS object_date,
            object_type,
            expire_date,
            IF(expire_date <= NOW(), 1, 0) AS expired
        FROM
            ((SELECT
                pr_training_tr_id AS object_id,
                    TIMESTAMP(tr_date) AS object_date,
                    IF(pr_training_tr_id IS NOT NULL, 'training', NULL) AS object_type,
                    NULL AS expire_date
            FROM
                presence
            JOIN training ON (tr_id = pr_training_tr_id
                AND tr_locked = 1)
            WHERE
                pr_user_kid_uk_id = {$user->get_uk_id()}
                    AND tr_deleted = 0) UNION (SELECT
                mod_id,
                    TIMESTAMP(mi_date),
                    IF(mod_id IS NOT NULL, 'money_deposit', NULL) AS object_type,
                    mod_expire_date AS expire_date
            FROM
                money_deposit
            JOIN money_income ON mi_id = mod_money_income_mi_id
            WHERE
                mod_user_kid_uk_id = {$user->get_uk_id()}
                    AND mod_deleted = 0)) actions
        ORDER BY object_date ASC;
    ";

    $action_assoc_array = $sql->assoc_array($action_list_query);

    $actions = array();
    $de_array = array();
    foreach ($action_assoc_array as $action) {
        if ($action['object_type'] == 'training') {
            $new_training = new training();
            $new_training->set_training_data_by_id($action['object_id']);
            //$actions[] = $new_training;
            $new_diary_entry = new diary_entry($action['object_id'], $action['object_date'], $action['object_type'], 0, 0, 0, 0, $new_training, null);
        }
        elseif ($action['object_type'] == 'money_deposit') {
            $new_mod = new money_deposit();
            $new_mod->set_mod_data_by_id($action['object_id']);
            //$actions[] = $new_mod;
            $new_diary_entry = new diary_entry($action['object_id'], $action['object_date'], $action['object_type'], 0, 0, 0, $action['expired'], null, $new_mod);
        }

        $de_array[] = $new_diary_entry;
    }

    //itt csak hivatkozással adjuk át a tömböt, a calculate_balance kiszámolja, belerakja és visszadja
    $user->calculate_balance($de_array, $user, false);
    //lekérjük az áthozatot, és felvesszük entryként
    $balance_transfer = $user->get_uk_balance_transfer();
    if (null == $balance_transfer) {
        $balance_transfer = 0;
    }

    $de_array = array_reverse($de_array);
    //$de_array[] = $first_entry;
    //var_dump($de_array);
    //var_dump($de_array);
    //$smarty->assign('actions', $actions);
    $smarty->assign('actions', $de_array);
    $smarty->assign('balance_transfer', $balance_transfer);

    //get news
    $news_assoc_array = $sql->assoc_array('SELECT * FROM news WHERE n_deleted = 0 ORDER BY n_date DESC;');

    $news_array = array();
    foreach ($news_assoc_array as $key => $news) {
        $new_news = new news();
        $new_news->set_news_data_by_id($news['n_id']);
        $news_array[] = $new_news;
    }

    $smarty->assign('news_array',$news_array);
    $smarty->assign('user',$user);

    //$smarty->assign('balance', $balance);
    $smarty->display('user_diary.tpl');



?>
