<?php

class user_group_filter_value {
	private $ugfv_id;
	private $ugfv_filter_id; //obj
	private $ugfv_group_id; //obj
	private $ugfv_value;





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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function set_ugfv_data_by_id($_sc_id) {
		global $sql;
		$ugfv_data_assoc_array = $sql->assoc_array("select * from user_group_filter_value where ugfv_id = " . $_sc_id);
		$ugfv_data_array = $ugfv_data_assoc_array[0];
		foreach ($ugfv_data_array as $field => $value) {
			$function_name = "set_" . $field;
			$this->$function_name($value); //alapadatok beállítása
			if ($field == 'ugfv_filter_id' && !empty($value)) {
				$filter = new user_group_filter();
				$filter->set_ugf_data_by_id($value);
				$this->set_ugfv_filter_id($filter);
			}
			if ($field == 'ugfv_group_id' && !empty($value)) {
				$group = new user_group();
				$group->set_ug_data_by_id($value);
				$this->set_ugfv_group_id($group);
			}
		}
	}

	public function apply($intersect = 0) {
		global $sql;
		//EZ CSAK BEILLESZT, A SZÜKSÉGES TÖRLÉSEKET ELŐTTE KELL MEGCSINÁLNI
		//$this->get_ugfv_group_id()->empty_group($ugc_id);
		//$this->get_ugfv_group_id()->empty_group($ugc_id);
		//lekéri az usereket a megadott értékek alapján, és ha még nincs ilyen user a csoportban ezzel a category-val, akkor beilleszti

        if (!$intersect) {
            $ugc_id = $sql->insert_into('user_group_category', array(
                'ugc_name' => $this->get_ugfv_value(),
                'ugc_user_group_ug_id' => $this->get_ugfv_group_id()->get_ug_id(),
                'ugc_ugfv_id' => $this->get_ugfv_id()
            ));
        }

		if ($this->get_ugfv_filter_id()->get_ugf_table()) {
			//ha meg van adva table, akkor az alapján rakjuk össze a query-t
			$table = $this->get_ugfv_filter_id()->get_ugf_table();
			$uk_field = 'uk_'.$this->get_ugfv_filter_id()->get_ugf_field();
			$value = $this->get_ugfv_filter_id()->get_ugf_value();
			$condition = $this->get_ugfv_filter_id()->get_ugf_condition();

			$filter_query = "
			SELECT
			    uk_id
			FROM
			    user_kid
			        JOIN
			    ".$table." ON ".$value." = ".$uk_field."
			WHERE ".($table!='user_kid_is_active'?"uk_is_active = 1 ":"")." AND uk_deleted = 0 AND
			    ".$uk_field." = ".$this->get_ugfv_value() . ($condition?" AND " .  $condition:"") .
			";";


		}

		else {
			$filter_query = "
			SELECT uk_id FROM user_kid
			WHERE uk_is_active = 1 AND uk_deleted = 0 AND uk_".$this->get_ugfv_filter_id()->get_ugf_field()." = '".$this->get_ugfv_value()."';
			";

		}
        //var_dump($filter_query);


		$uk_assoc_array = $sql->assoc_array($filter_query);

        $uk_ids = [];
        foreach ($uk_assoc_array as $uk_array) {
            $uk_ids[] = $uk_array['uk_id'];
        }

        $ugfv_list = $sql->assoc_array('SELECT * FROM user_group_kid WHERE ugk_ugfv_id in (SELECT ugfv_id FROM user_group_filter_value WHERE ugfv_group_id = ' . $this->get_ugfv_group_id()->get_ug_id(). ');');
        if ($intersect && count($ugfv_list)) {
            //ha már van vmi a metszetben
            //végigmegyünk a metszeten, és ha az aktuális elem nincs benne az uk_assoc_arrayben, akkor kivesszük a metszetből
            foreach ($ugfv_list as $index => $ugfv_array) {
                if (!in_array($ugfv_array['ugk_user_kid_uk_id'], $uk_ids)) {
                    $sql->execute_query('DELETE FROM user_group_kid WHERE ugk_id = ' . $ugfv_array['ugk_id']);
                    unset($ugfv_list[$index]);
                }
            }
        }

        else {
            foreach ($uk_assoc_array as $uk) {
    			$sql->insert_into('user_group_kid', array(
    				'ugk_user_kid_uk_id' => $uk['uk_id'],
    				'ugk_user_group_ug_id' => $this->get_ugfv_group_id()->get_ug_id(),
    				'ugk_category_ugc_id' => ($intersect?'null':$ugc_id),
    				'ugk_ugfv_id' => $this->get_ugfv_id(),
    			));
            }
        }
    }

    public function is_user_filterable($user) {
        global $sql;

        if ($this->get_ugfv_filter_id()->get_ugf_table()) {
            //ha meg van adva table, akkor az alapján rakjuk össze a query-t
            $table = $this->get_ugfv_filter_id()->get_ugf_table();
            $uk_field = 'uk_'.$this->get_ugfv_filter_id()->get_ugf_field();
            $value = $this->get_ugfv_filter_id()->get_ugf_value();
            $condition = $this->get_ugfv_filter_id()->get_ugf_condition();

            $filter_query = "
            SELECT
                uk_id
            FROM
                user_kid
                    JOIN
                ".$table." ON ".$value." = ".$uk_field."
            WHERE ".($table!='user_kid_is_active'?"uk_is_active = 1 ":"")." AND uk_id = ".$user->get_uk_id()." AND uk_deleted = 0 AND
                ".$uk_field." = ".$this->get_ugfv_value() . ($condition?" AND " .  $condition:"") .
            ";";


        }

        else {
            $filter_query = "
            SELECT uk_id FROM user_kid
            WHERE uk_is_active = 1 AND uk_deleted = 0 AND uk_id = ".$user->get_uk_id()." AND uk_".$this->get_ugfv_filter_id()->get_ugf_field()." = '".$this->get_ugfv_value()."';
            ";

        }
        //var_dump($filter_query);
        return $sql->num_of_rows($filter_query);
    }

    public function is_intersect() {
        global $sql;
        return empty($sql->single_variable('SELECT ugk_category_ugc_id FROM user_group_kid WHERE ugk_user_group_ug_id = ' . $this->get_ugfv_group_id()->get_ug_id(). ' AND ugk_ugfv_id = ' . $this->get_ugfv_id()));
    }
}


?>
