<?php

/*
    TRAINING osztály
    Edzések



*/

class training {
    private $tr_id;
    private $tr_date;
    private $tr_training_type_trt_id;
    private $tr_user_coach_uc_id;
    private $tr_duration;
    private $tr_note;
    private $tr_locked;
    private $tr_deleted;
    private $coaches = array();

    private $helpers = array();



    public function set_tr_id($_tr_id) {
        $this->tr_id = $_tr_id;
    }

    public function set_tr_date($_tr_date) {
        $this->tr_date = $_tr_date;
    }

    public function set_tr_training_type_trt_id($_tr_training_type_trt_id) {
        $this->tr_training_type_trt_id = $_tr_training_type_trt_id;
    }

    public function set_tr_duration($_tr_duration) {
        $this->tr_duration = $_tr_duration;
    }

    public function set_tr_note($_tr_note) {
        $this->tr_note = $_tr_note;
    }

    public function set_tr_locked($_tr_locked) {
        $this->tr_locked = $_tr_locked;
    }

    public function set_tr_deleted($_tr_deleted) {
        $this->tr_deleted = $_tr_deleted;
    }

    public function get_tr_id() {
        return $this->tr_id;
    }

    public function get_tr_date($_formatted = false) {
        return !$_formatted ? $this->tr_date : date("Y. F d. H:i", strtotime($this->tr_date));
    }

    public function get_tr_date_day() {
        $day = date("d", strtotime($this->tr_date));
        if (substr($day, 0, 1) == '0') return substr($day, 1, 1);
        return date("d", strtotime($this->tr_date));
    }

    public function get_tr_date_day_of_week() {
        return date("w", strtotime($this->tr_date));
    }

    public function get_tr_date_time() {
        return date("H:i", strtotime($this->tr_date));
    }

    public function get_tr_training_type_trt_id() {
        return $this->tr_training_type_trt_id;
    }

    public function get_tr_duration() {
        return $this->tr_duration;
    }

    public function get_tr_note() {
        return $this->tr_note;
    }

    public function get_tr_locked() {
        return $this->tr_locked;
    }

    public function get_tr_deleted() {
        return $this->tr_deleted;
    }

    public function get_tr_type_name_by_id() {
        global $sql;
        return $sql->single_variable("SELECT trt_name FROM training_type WHERE trt_id = " . $this->get_tr_training_type_trt_id());
    }

    public function set_tr_coaches() {
        //beállítja a tr_coaches array-be a coach-okat
        global $sql;
        $coach_ids = $sql->assoc_array("SELECT trc_coach_uc_id FROM training_coach JOIN user_coach ON ua_id = trc_coach_uc_id WHERE ua_deleted = 0 AND trc_helper = 0 AND trc_training_tr_id = " . $this->get_tr_id());
        $this->tr_coaches = array();
        foreach ($coach_ids as $trc) {
            $this->tr_coaches[] = $trc['trc_coach_uc_id'];
        }

        $helper_ids = $sql->assoc_array("SELECT trc_coach_uc_id FROM training_coach JOIN user_coach ON ua_id = trc_coach_uc_id WHERE ua_deleted = 0 AND trc_helper = 1 AND trc_training_tr_id = " . $this->get_tr_id());
        $this->tr_helpers = array();
        foreach ($helper_ids as $trc) {
            $this->tr_helpers[] = $trc['trc_coach_uc_id'];
        }
    }

    public function set_training_data_by_id($_tr_id) {
        global $sql;
        $training_data_assoc_array = $sql->assoc_array("select * from training where tr_id = " . $_tr_id);
        $training_data_array = $training_data_assoc_array[0];
        foreach ($training_data_array as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value);
        }
        $this->set_tr_coaches();
    }

    public function is_coach() {
        //megadja, hogy van-e beállítva edző az edzéshez
        //TRUE HA IGEN, FALSE EGYÉBKÉNT
        return !empty($this->tr_coaches);
    }

    public function is_helper() {
        //megadja, hogy van-e beállítva segédedző az edzéshez
        //TRUE HA IGEN, FALSE EGYÉBKÉNT
        return !empty($this->tr_helpers);
    }

    public function get_tr_coaches_name() {
        //tömböt ad vissza az edzők neveivel
        global $sql;
        $coach_names = array();
        foreach ($this->tr_coaches as $ua_id) {
            $coach_names[] = $sql->single_variable('SELECT ua_name FROM user_coach WHERE ua_id = ' . $ua_id);
        }
        return $coach_names;
    }

    public function get_tr_helpers_name() {
        //tömböt ad vissza a segédedzők neveivel
        global $sql;
        $helper_names = array();
        foreach ($this->tr_helpers as $ua_id) {
            $helper_names[] = $sql->single_variable('SELECT ua_name FROM user_coach WHERE ua_id = ' . $ua_id);
        }
        return $helper_names;
    }

    public static function create_training($_training_value_array, $_every_week) {
        global $sql;
        //megnézzük, hogy minden hétre be kell-e rakni
        //ha igen, akkor az adott hónapban, a hét minden azonos napjára létrehozza
        if ($_every_week) {
            //megnézzük milyen nap van a megadott dátumon
            $day_of_week = date('N', strtotime($_training_value_array['tr_date']));
            //kigyűjtük a hónap további ilyen napjait
            $last_day = date("t", strtotime($_training_value_array['tr_date']));

            for ($actual_day=date('d', strtotime($_training_value_array['tr_date'])); $actual_day <= $last_day; $actual_day=$actual_day+7) {
                $new_tr_id = $sql->insert_into('training',
                    array(
                        'tr_date' => date('Y-m', strtotime($_training_value_array['tr_date'])) . '-' . $actual_day . ' ' . date('H:i', strtotime($_training_value_array['tr_date'])),
                        'tr_training_type_trt_id' => $_training_value_array['tr_training_type_trt_id'],
                        'tr_duration' => $_training_value_array['tr_duration'],
                        'tr_note' => $_training_value_array['tr_note']
                        )
                    );
                log::register('new_training', $new_tr_id);
                //itt rakjuk be a coach-okat
                if (isset($_training_value_array['coaches'])) {
                    foreach ($_training_value_array['coaches'] as $coach_id) {
                        # beilleszt minden edzőt ehhez az edzéshez
                        $sql->insert_into('training_coach', array('trc_training_tr_id' => $new_tr_id, 'trc_coach_uc_id' => $coach_id));
                    }
                }
                //itt rakjuk be a segédedzőket
                if (isset($_training_value_array['helpets'])) {
                    foreach ($_training_value_array['helpets'] as $coach_id) {
                        # beilleszt minden edzőt ehhez az edzéshez
                        $sql->insert_into('training_coach', array('trc_training_tr_id' => $new_tr_id, 'trc_coach_uc_id' => $coach_id, 'trc_helper' => 1));
                    }
                }
            }
            //var_dump($day_array);
        }
        else {
            //coaches
            if (isset($_training_value_array['coaches'])) {
                $coaches = $_training_value_array['coaches'];
                unset($_training_value_array['coaches']);
            }
            //helpers
            if (isset($_training_value_array['helpers'])) {
                $helpers = $_training_value_array['helpers'];
                unset($_training_value_array['helpers']);
            }

            if ($_training_value_array['tr_note'] == "") {
                $_training_value_array['tr_note'] = null;
            }
            $new_tr_id = $sql->insert_into('training', $_training_value_array);
            log::register('new_training', $new_tr_id);
            if (isset($coaches)) {
                foreach ($coaches as $coach_id) {
                    # beilleszt minden edzőt ehhez az edzéshez
                    $new_trc_id = $sql->insert_into('training_coach', array('trc_training_tr_id' => $new_tr_id, 'trc_coach_uc_id' => $coach_id));
                }
            }
            if (isset($helpers)) {
                foreach ($helpers as $coach_id) {
                    # beilleszt minden edzőt ehhez az edzéshez
                    $sql->insert_into('training_coach', array('trc_training_tr_id' => $new_tr_id, 'trc_coach_uc_id' => $coach_id, 'trc_helper' => 1));
                }
            }
        }

        return $new_tr_id;

    }

    public static function update_training($_training_value_array, $_tr_id) {
        global $sql;

        $sql->execute_query("DELETE FROM training_coach WHERE trc_training_tr_id = '" . $_tr_id ."';");
        if (isset($_training_value_array['coaches'])) {
            //coaches handler -> kitoroljuk mindet és újra insertaljuk
            foreach ($_training_value_array['coaches'] as $coach_id) {
                $sql->insert_into('training_coach', array('trc_training_tr_id' => $_tr_id, 'trc_coach_uc_id' => $coach_id));
            }

            unset($_training_value_array['coaches']);
        }
        if (isset($_training_value_array['helpers'])) {
            //helper handler -> kitoroljuk mindet és újra insertaljuk
            foreach ($_training_value_array['helpers'] as $coach_id) {
                $sql->insert_into('training_coach', array('trc_training_tr_id' => $_tr_id, 'trc_coach_uc_id' => $coach_id, 'trc_helper' => 1));
            }

            unset($_training_value_array['helpers']);
        }
        if ($_training_value_array['tr_note'] == "") {
            $_training_value_array['tr_note'] = null;
        }
        $sql->update_table('training', $_training_value_array, array('tr_id' => $_tr_id));
    }
}


?>
