<?php

/*
    PAGE CLASS
    url alapjan lekeri a template-et
    http://cegledcoach.hu/PAGE/SUBPAGE/ID

*/


class page {

    private $page = null;
    private $subpage = null;
    private $id = null;

    function __construct() {
        if (isset($_GET['page'])) $this->set_page($_GET['page']);
        if (isset($_GET['subpage'])) $this->set_subpage($_GET['subpage']);
        if (isset($_GET['id'])) $this->set_id($_GET['id']);
    }

    public function get_page_nav() {
        global $smarty, $sql, $user;
        //nem kell if, hanem page alapján beillesztük az id-t
        if ($this->get_page() == 'admin') {
            if ($user) {
                $menus_query = "
                    SELECT
                        *
                    FROM
                        subpage
                            LEFT JOIN
                        authority ON a_name = spage_url
                            JOIN
                        user_authority ON (ua_authority_a_id = a_id
                            OR ua_authority_a_id = 1)
                    WHERE
                        spage_page_id = 1
                            AND ua_user_kid_uk_id = " . $user->get_ua_id() ." ORDER BY spage_id ASC;
                ";
            }
            else {
                $menus_query = "SELECT * FROM subpage WHERE spage_page_id = 1;";
            }
            $menu_assoc_array = $sql->assoc_array($menus_query);
            $smarty->assign('menus', $menu_assoc_array);
        }
        elseif ($this->get_page() == 'view' || $this->get_page() == 'preview') {
            $menus_query = "SELECT * FROM subpage WHERE spage_page_id = 2;";
            $menu_assoc_array = $sql->assoc_array($menus_query);
            $smarty->assign('menus', $menu_assoc_array);
        }
        elseif ($this->get_page() == 'coach' || $this->get_page() == 'coach_preview') {
            $menus_query = "SELECT * FROM subpage WHERE spage_page_id = 3;";
            $menu_assoc_array = $sql->assoc_array($menus_query);
            $smarty->assign('menus', $menu_assoc_array);
        }
        elseif ($this->get_page() == 'tabor') {
            $menus_query = "SELECT * FROM subpage WHERE spage_page_id = 4;";
            $menu_assoc_array = $sql->assoc_array($menus_query);
            $smarty->assign('menus', $menu_assoc_array);
        }
        elseif ($this->get_page() == 'taborvezeto') {
            $menus_query = "SELECT * FROM subpage WHERE spage_page_id = 5;";
            $menu_assoc_array = $sql->assoc_array($menus_query);
            $smarty->assign('menus', $menu_assoc_array);
        }

        $smarty->assign('page', $this->get_page());
        $smarty->display('nav.tpl');
    }



    function findInStructure($id, &$array) {
        if (array_key_exists($id, $array)) {
            return $id;
        }

        foreach ($array as $key => $submenu) {
            if (in_array($id, $submenu)) {
                return $key;
            }
        }
    }

    public function get_page_content() {
        global $sql, $user, $smarty, $structure;
        //var_dump($user);
        ini_set('include_path', '_include/');
        //ini_set('include_path', '/var/www/badminton_coach/_include');
        if ((!empty($user) && $user->is_logged_in()) || ($this->get_subpage() == "export_balance")) {
            if ($this->is_page()) {
                //TODO: mi van ha nincs page? átirányítás v 404?
                //page alapján betölti a tpl-t

                if ($this->is_subpage() && $this->get_page() == 'admin') {
                    if ('create' == $this->get_subpage()) {
                        $toFind = $this->get_id();
                    } elseif (strpos($this->get_subpage(), 'delete') !== false) {
                        //delete_ utáni rész
                        $toFind = substr($this->get_subpage(), 7);
                    }
                    else {
                        $toFind = $this->get_subpage();
                    }
                    $subPage = $this->findInStructure($toFind, $structure);
                    if ('exception' != $subPage && !$user->has_authority_by_name($subPage)) {
                        include('include_access_denied.php');
                        return;
                    }
                }

                switch ($this->get_page()) {
                    case 'admin':
                        # ADMIN OLDALAK
                        switch ($this->get_subpage()) {
                            case 'members':
                                # TAGOK KEZELÉSE
                                $tpl = "edit";
                                include('include_members.php');
                                break;

                            case 'edit_member':
                                # TAG SZERKESZTÉSE
                                $tpl = "edit";
                                include('include_members.php');
                                break;
                            case 'delete_member':
                                # TAG TÖRLÉSE
                                # TODO: error kezelés
                                include('include_delete_member.php');
                                break;
                            case 'delete_parent':
                                # SZÜLŐ TÖRLÉSE
                                # TODO: error kezelés
                                include('include_delete_parent.php');
                                break;
                            case 'parents':
                                # SZÜLŐK SZERKESZTÉSE
                                include('include_parents.php');
                                break;
                            case 'coaches':
                                # EDZŐK SZERKESZTÉSE
                                include('include_coaches.php');
                                break;
                            case 'trainings':
                                # EDZÉSEK
                                # itt az edzések listája jelenik meg az aktuális hónapban
                                $tpl = "edit";
                                include('include_trainings.php');
                                break;
                            case 'training_templates':
                                # EDZÉS SABLONOK
                                include('include_training_templates.php');
                                break;
                            case 'delete_training_template':
                                # EDZÉS SABLON TÖRLÉS
                                include('include_delete_training_template.php');
                                break;
                            case 'shirts':
                                # PÓLÓK
                                include('include_shirts.php');
                                break;
                            case 'cities':
                                # PÓLÓK
                                include('include_cities.php');
                                break;
                            case 'edit_training':
                                # TAG SZERKESZTÉSE
                                $tpl = "edit";
                                include('include_trainings.php');
                                break;
                            case 'delete_training':
                                # EDZÉS TÖRLÉS
                                include('include_delete_training.php');
                                break;
                            case 'create':
                                # LÉTREHOZÓS OLDALAK
                                include('include_create.php');
                                break;
                            case 'presence':
                                # JELENLÉT
                                $tpl = "presence";
                                include('include_presence.php');
                                break;
                            case 'schools':
                                # ISKOLÁK
                                include('include_schools.php');
                                break;
                            case 'training_types':
                                # EDZÉS TÍPUSOK
                                include('include_training_types.php');
                                break;
                            case 'regions':
                                # DIÁKOLIMPIA KÖRZETEK
                                include('include_regions.php');
                                break;
                            case 'log':
                                # NAPLÓ
                                include('include_log.php');
                                break;
                            case 'money_deposit':
                                # BEFIZETÉSEK
                                include('include_money_deposit.php');
                                break;
                            case 'lease':
                                # BÉRLETTÍPUSOK
                                include('include_lease.php');
                                break;
                            case 'lock_training':
                                # EDZÉS ZÁROLÁS, FELOLDÁS
                                include('include_lock_training.php');
                                break;
                            case 'view_deposit':
                                # BEFIZETÉSEK / USER
                                include('include_view_deposit.php');
                                break;
                            case 'money_expense':
                                # KIADÁSOK
                                include('include_money_expense.php');
                                break;
                            case 'money_expense_category':
                                # KIADÁS KATEGÓRIÁK
                                include('include_money_expense_category.php');
                                break;
                            case 'money_income_category':
                                # BEVÉTEL KATEGÓRIÁK
                                include('include_money_income_category.php');
                                break;
                            case 'money_income':
                                # BEVÉTELEK
                                include('include_money_income.php');
                                break;
                            case 'money_update':
                                # FRISSÍTÉS
                                include('include_money_update.php');
                            case 'settings':
                                # BEÁLLÍTÁSOK
                                include('include_settings.php');
                                break;
                            case 'balance_list':
                                # BEÁLLÍTÁSOK
                                include('include_balance_list.php');
                                break;
                            case 'stats':
                                # STATISZTIKÁK
                                include('include_stats.php');
                                break;
                            case 'news':
                                # INFORMÁCIÓS FAL
                                include('include_information.php');
                                break;
                            case 'expire_check':
                                # CRON FUTTATÁSA
                                include('include_expire_check.php');
                                break;
                            case 'userlist':
                                # GYEREK ADATLISTA
                                include('include_userlist.php');
                                break;
                            case 'export_users':
                                # GYEREKLISTA EXPORTÁLÁSA
                                include('include_export_users.php');
                                break;
                            case 'traininglist':
                                # EDZÉS ADATLISTA
                                include('include_traininglist.php');
                                break;
                            case 'export_trainings':
                                # EDZÉSLISTA EXPORTÁLÁSA
                                include('include_export_trainings.php');
                                break;
                            case 'export_balance':
                                # EGYENLEG LISTA EXPORTÁLÁSA
                                include('include_export_balance.php');
                                break;
                            case 'download_report':
                                # EGYENLEG LISTA LETÖLTÉSE
                                include('include_download_report.php');
                                break;
                             case 'set_credit_to_zero':
                                # HITELALKALOM NULLÁZÁS
                                include('include_set_credit_to_zero.php');
                                break;
                            case 'delete_training_type':
                                # EDZÉS TÍPUS TÖRLÉS
                                include('include_delete_training_type.php');
                                break;
                            case 'delete_coach':
                                # EDZŐ TÖRLÉS
                                include('include_delete_coach.php');
                                break;
                            case 'delete_shirt':
                                # PÓLÓ TÖRLÉS
                                include('include_delete_shirt.php');
                                break;
                            case 'delete_city':
                                # VÁROS TÖRLÉS
                                include('include_delete_city.php');
                                break;
                            case 'delete_region':
                                # KÖRZET TÖRLÉS
                                include('include_delete_region.php');
                                break;
                            case 'delete_school':
                                # ISKOLA TÖRLÉS
                                include('include_delete_school.php');
                                break;
                            case 'delete_money_deposit':
                                # BEFIZETÉS TÖRLÉS
                                include('include_delete_money_deposit.php');
                                break;
                            case 'delete_money_expense_category':
                                # BEFIZETÉS KATEGÓRIA TÖRLÉS
                                include('include_delete_money_expense_category.php');
                                break;
                            case 'delete_money_expense':
                                # BEFIZETÉS TÖRLÉS
                                include('include_delete_money_expense.php');
                                break;
                            case 'delete_money_income_category':
                                # BEVÉTEL KATEGÓRIA TÖRLÉS
                                include('include_delete_money_income_category.php');
                                break;
                            case 'delete_money_income':
                                # BEVÉTEL TÖRLÉS
                                include('include_delete_money_income.php');
                                break;
                            case 'delete_news':
                                # HÍR TÖRLÉS
                                include('include_delete_news.php');
                                break;
                            case 'delete_lease':
                                # BÉRLETTÍPUS TÖRLÉS
                                include('include_delete_lease.php');
                                break;
                            case 'logout':
                                # kijelentkezés
                                $from = "admin";
                                include('include_logout.php');
                                break;
                            default:
                                # code...
                                break;
                        }
                        break;
                    case 'ajax':
                        if ($this->is_subpage()) {
                            include('ajax/'.$this->get_subpage());
                        }
                        break;
                    case 'view':
                        # SZÜLŐ FELÜLET
                        switch ($this->get_subpage()) {
                            case 'logout':
                                # kijelentkezés
                                $from = "view";
                                include('include_logout.php');
                                break;
                            case 'overview':
                                # áttekintő oldal adatokkal
                                include('include_overview.php');
                                break;
                            case 'diary':
                                # napló, edzéslista
                                include('include_diary.php');
                                break;
                            case 'information':
                                # információk
                                include('include_information_wall.php');
                                break;
                            default:
                                include('include_diary.php');
                                break;
                        }
                        break;
                    case 'preview':
                        # SZÜLŐI OLDAL ELŐNÉZET
                        switch ($this->get_subpage()) {
                            case 'overview':
                                # áttekintő oldal adatokkal
                                include('include_overview.php');
                                break;
                            case 'diary':
                                # napló, edzéslista
                                include('include_diary.php');
                                break;
                            case 'information':
                                # információk
                                include('include_information_wall.php');
                                break;
                            default:
                                include('include_diary.php');
                                break;
                        }
                        break;
                    case 'coach':
                        # EDZŐI NÉZET
                        switch ($this->get_subpage()) {
                            case 'diary':
                                # napló, edzéslista
                                include('include_coach_diary.php');
                                break;
                            case 'logout':
                                # kijelentkezés
                                $from = "coach";
                                include('include_logout.php');
                                break;
                            default:
                                include('include_coach_diary.php');
                                break;
                        }
                        break;
                    case 'coach_preview':
                        # EDZŐI ELŐNÉZET
                        switch ($this->get_subpage()) {
                            case 'diary':
                                # napló, edzéslista
                                include('include_coach_diary.php');
                                break;
                            default:
                                include('include_coach_diary.php');
                                break;
                        }
                        break;
                }
            }
        }
        else {
            //nincs bejelentkezve
            $smarty->assign("page", $this->get_page());
            if ('tabor' == $this->get_page() && $this->is_subpage() && 'regisztracio' == $this->get_subpage()) {
                if (isset($_COOKIE['bc_reg_error'])) {
                    $smarty->assign('error_code', $_COOKIE['bc_reg_error']);
                    setcookie('bc_reg_error', null, time()-60*60*3, '/');
                }
                $smarty->display("register.tpl");
            }
            else {
                if (isset($_COOKIE['bc_reg_error'])) {
                    $smarty->assign('error_code', $_COOKIE['bc_reg_error']);
                    setcookie('bc_reg_error', null, time()-60*60*2, '/');
                }
                $smarty->display("login.tpl");
            }
        }

    }


    private function set_page($_page) {
        $this->page = $_page;
    }

    private function set_subpage($_subpage) {
        $this->subpage = $_subpage;
    }

    private function set_id($_id) {
        $this->id = $_id;
    }

    public function get_page() {
        return $this->page;
    }

    public function get_subpage() {
        return $this->subpage;
    }

    public function get_id() {
        return $this->id;
    }

    public function is_page() {
        return !empty($this->page);
    }

    public function is_subpage() {
        return !empty($this->subpage);
    }

    public function is_id() {
        return !empty($this->id);
    }

}




?>
