<?php

    $action_list_query = "
    SELECT
    object_id,
    timestamp(object_date) as object_date,
    object_type
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
    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
    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, $new_training);
        }
        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, 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);
    //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');



?>
