<?php

//BÉRLET OSZTÁLY

class lease {
    private $l_id;
    private $l_name;
    private $l_sum;
    private $l_deleted;
    private $l_expire_type;
    private $l_expire_date;
    private $l_expire_deleted;
    private $l_training_types = array(); //array of objects

    /**
     * @return mixed
     */
    public function get_l_id()
    {
        return $this->l_id;
    }

    /**
     * @param mixed $l_id
     *
     * @return self
     */
    public function set_l_id($l_id)
    {
        $this->l_id = $l_id;

        return $this;
    }

     /**
     * @return mixed
     */
    public function get_l_deleted()
    {
        return $this->l_deleted;
    }

    /**
     * @param mixed $l_deleted
     *
     * @return self
     */
    public function set_l_deleted($l_deleted)
    {
        $this->l_deleted = $l_deleted;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_l_name()
    {
        return $this->l_name;
    }

    /**
     * @param mixed $l_name
     *
     * @return self
     */
    public function set_l_name($l_name)
    {
        $this->l_name = $l_name;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_l_sum()
    {
        return $this->l_sum;
    }

    /**
     * @param mixed $l_sum
     *
     * @return self
     */
    public function set_l_sum($l_sum)
    {
        $this->l_sum = $l_sum;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_l_expire_type()
    {
        return $this->l_expire_type;
    }

    /**
     * @param mixed $l_expire_type
     *
     * @return self
     */
    public function set_l_expire_type($l_expire_type)
    {
        $this->l_expire_type = $l_expire_type;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_l_expire_date()
    {
        return $this->l_expire_date;
    }

    /**
     * @param mixed $l_expire_date
     *
     * @return self
     */
    public function set_l_expire_date($l_expire_date)
    {
        $this->l_expire_date = $l_expire_date;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_l_expire_deleted()
    {
        return $this->l_expire_deleted;
    }

    /**
     * @param mixed $l_expire_deleted
     *
     * @return self
     */
    public function set_l_expire_deleted($l_expire_deleted)
    {
        $this->l_expire_deleted = $l_expire_deleted;

        return $this;
    }

    public static function create_lease($_values) {
        global $sql;

        if (isset($_values['l_expire_date'])) {
            $expDate = $_values['l_expire_date'];
        }
        else {
            $expDate = 'null';
        }

        $l_id = $sql->insert_into('lease', array(
            'l_name' => $_values['l_name'],
            'l_sum' => $_values['l_sum'],
            'l_expire_type' => $_values['l_expire_type'],
            'l_expire_date' => $expDate,
        ));

        if (isset($_values['l_training_types']) && is_array($_values['l_training_types'])) {
            foreach ($_values['l_training_types'] as $key => $trt_id) {
                $sql->insert_into('lease_training_type', array(
                    'ltt_training_type_trt_id' => $trt_id,
                    'ltt_lease_l_id' => $l_id,
                ));
            }
        }

        return $l_id;
    }

    public static function update_lease($_values, $_l_id) {
        global $sql;

        if (isset($_values['l_expire_date'])) {
            $expDate = $_values['l_expire_date'];
        }
        else {
            $expDate = 'null';
        }

        $sql->update_table('lease', array(
            'l_name' => $_values['l_name'],
            'l_sum' => $_values['l_sum'],
            'l_expire_type' => $_values['l_expire_type'],
            'l_expire_date' => $expDate,
        ), array(
            'l_id' => $_l_id,
        ));

        $sql->execute_query('DELETE FROM lease_training_type WHERE ltt_lease_l_id = ' . $_l_id);
        if (isset($_values['l_training_types']) && is_array($_values['l_training_types'])) {
            foreach ($_values['l_training_types'] as $key => $trt_id) {
                $sql->insert_into('lease_training_type', array(
                    'ltt_training_type_trt_id' => $trt_id,
                    'ltt_lease_l_id' => $_l_id,
                ));
            }
        }
    }

    public function set_l_data_by_id($_l_id) {
        global $sql;
        $lease_data_assoc_array = $sql->assoc_array("select * from lease where l_id = " . $_l_id);
        $l_data_array = $lease_data_assoc_array[0];
        foreach ($l_data_array as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value); //alapadatok beállítása
        }

        $l_trt_assoc_array = $sql->assoc_array('SELECT * FROM lease_training_type WHERE ltt_lease_l_id = '.$_l_id);
        foreach ($l_trt_assoc_array as $key => $l_trt) {
            $trt = new training_type();
            $trt->set_trt_data_by_id($l_trt['ltt_training_type_trt_id']);

            $this->l_training_types[] = $trt;
        }
    }

    public function has_training($_trt_id) {
        foreach ($this->l_training_types as $trt) {
            if ($trt->get_trt_id() == $_trt_id) {
                return true;
            }
        }

        return false;
    }
}


?>
