<?php

/*
    USER CLASS
    ősosztály
    USER COACH adatszerkezet
    az öszes user rendelkezik ezekkel a tulajdonságokkal

*/


class user {
    private $user_id;
    private $user_name;
    private $user_last_name;
    private $user_first_name;
    private $user_password;
    private $user_last_login;
    private $user_admin;
    private $logged_in;
    private $user_type;
    private $authorities = array();
    private $user_deleted;

    public function set_ua_id($_uid) {
        $this->user_id = $_uid;
    }

    public function set_ua_name($_uname) {
        $this->user_name = $_uname;
    }

    public function set_ua_last_name($_u_last_name) {
        $this->user_last_name = $_u_last_name;
    }

    public function set_ua_first_name($_u_first_name) {
        $this->user_first_name = $_u_first_name;
    }

    public function set_ua_password($_u_pass) {
        $this->user_password = $_u_pass;
    }

    public function set_ua_last_login($_u_last_login) {
        $this->user_last_login = $_u_last_login;
    }

    public function set_ua_admin($_u_admin) {
        $this->user_admin = $_u_admin;
    }

    public function set_ua_deleted($_u_deleted) {
        $this->user_deleted = $_u_deleted;
    }

    public function get_ua_id() {
        return $this->user_id;
    }

    public function get_ua_name() {
        return $this->user_name;
    }

    public function get_ua_last_name() {
        return $this->user_last_name;
    }

    public function get_ua_first_name() {
        return $this->user_first_name;
    }

    public function get_ua_password() {
        return $this->user_password;
    }

    public function get_ua_admin() {
        return $this->user_admin;
    }

    public function get_ua_deleted() {
        return $this->user_deleted;
    }

    public function is_logged_in() {
        //leellenőrzi cookie alapján h be vagyunk-e jelentkezve
        //JAVÍTVA: adja vissza az adattag igazságértékét
        return $this->logged_in;
    }

    public function set_login($_login) {
        //bool-t kap paraméterül
        $this->logged_in = $_login;
    }

    public function add_ua_authority($_a_id) {
        $this->authorities[] = $_a_id;
    }

    public function remove_ua_authority($_a_id) {
        if (($key = array_search($del_val, $messages)) !== false) {
            unset($messages[$key]);
        }
    }

    public function set_user_data_by_id($_ua_id) {
        global $sql, $user;
        $user_data_assoc_array = $sql->assoc_array("select * from user_coach where ua_id = " . $_ua_id);
        $user_data_array = $user_data_assoc_array[0];
        foreach ($user_data_array as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value); //alapadatok beállítása
            $this->set_ua_type(1); //coach típus beállítása
            $this->set_login(true);
        }

        $authorities_aa = $sql->assoc_array('SELECT * FROM user_authority WHERE ua_user_kid_uk_id = ' . $_ua_id);

        foreach($authorities_aa as $key => $authority) {
            $this->add_ua_authority($authority['ua_authority_a_id']);
        }
    }


    public function is_coach_at_training($_training_id) {
        global $sql;
        //kap egy training id-t, és megmondja, hogy az user be van-e jelölve edzőként azon az edzésen
        $query = "SELECT * FROM training_coach WHERE trc_coach_uc_id = '" . $this->get_ua_id() . "' AND trc_training_tr_id = '" . $_training_id ."' AND trc_helper = 0;";
        return $sql->num_of_rows($query);
    }

    public function is_helper_at_training($_training_id) {
        global $sql;
        //kap egy training id-t, és megmondja, hogy az user be van-e jelölve segédedzőként azon az edzésen
        $query = "SELECT * FROM training_coach WHERE trc_coach_uc_id = '" . $this->get_ua_id() . "' AND trc_training_tr_id = '" . $_training_id ."' AND trc_helper = 1;";
        return $sql->num_of_rows($query);
    }

    public function update_login_time($_ua_id = null) {
        global $sql;
        //az adott user_id-n updateli a login_time-ot
        $sql->update_table('user_coach', array('ua_last_login' => date('Y-m-d')), array('ua_id' => (empty($_ua_id)?$this->get_ua_id():$_ua_id)));
    }

    public function set_ua_type($_type) {
        $this->user_type = $_type;
    }

    public function get_training_count_in_month($_year, $_month) {
        global $sql;
        return $sql->single_variable('select count(distinct trc_id) from training_coach join training on tr_id = trc_training_tr_id where year(tr_date) = '.$_year.' and month(tr_date) = '.$_month.' and trc_coach_uc_id = '.$this->get_ua_id().' and tr_deleted = 0;');
    }

    public function has_authority($a_id) {
        global $sql;
        return $sql->num_of_rows('SELECT * FROM user_authority WHERE ua_user_kid_uk_id = ' . $this->get_ua_id() . ' AND ua_authority_a_id = ' . $a_id. ';');
    }

    public function has_authority_by_name($a_name) {
        global $sql;
        return $sql->num_of_rows("SELECT * FROM user_authority JOIN authority ON a_id = ua_authority_a_id WHERE ua_user_kid_uk_id = " . $this->get_ua_id() . " AND (a_name = '" . $a_name. "' OR a_name = 'admin');");
    }

    public function get_authorities() {
        return $this->authorities;
    }

    public static function user_can_edit_training($user_id, $training_id) {
        global $sql;
        return $sql->num_of_rows("SELECT * FROM training_coach WHERE trc_coach_uc_id = " . $user_id ." AND trc_training_tr_id = " . $training_id);
    }

    public static function create_user($_name, $_password, $_authorities = array()) {
        global $sql;
        $new_user_id = $sql->insert_into('user_coach', array(
            'ua_name' => $_name,
            'ua_password' => $_password
            )
        );

        if (is_array($_authorities) && !empty($_authorities)) {
            foreach ($_authorities as $key => $authority_id) {
                $sql->insert_into('user_authority', array(
                    'ua_user_kid_uk_id' => $new_user_id,
                    'ua_authority_a_id' => $authority_id,
                ));
            }
        }

        return $new_user_id;
    }

    public static function update_user($_name, $_password, $_admin, $_ua_id, $_authorities = array()) {
        global $sql;
        if ($_password != "-1") {
            $sql->update_table('user_coach',
                array(
                    'ua_name' => $_name,
                    'ua_admin' => ($_admin?1:0),
                    'ua_password' => $_password
                ),
                array(
                    'ua_id' => $_ua_id
                )
            );
        }
        else {
            $sql->update_table('user_coach',
                array(
                    'ua_name' => $_name,
                    'ua_admin' => ($_admin?1:0),
                ),
                array(
                    'ua_id' => $_ua_id
                )
            );
        }

        $sql->execute_query('DELETE FROM user_authority WHERE ua_user_kid_uk_id = ' . $_ua_id);
        if (is_array($_authorities) && !empty($_authorities)) {
            foreach ($_authorities as $key => $authority_id) {
                $sql->insert_into('user_authority', array(
                    'ua_user_kid_uk_id' => $_ua_id,
                    'ua_authority_a_id' => $authority_id,
                ));
            }
        }

        return true;
    }

}


?>
