<?php

class log {
	private $log_id;
	private $log_log_category_logc_id; //EZ CSAK AZ ID
	private $log_category; //EZ A LOG_CATEGORY OBJEKTUM
	private $log_date;
	private $log_user_id; //EZ CSAK AZ ID
	private $log_user; //EZ A USER OBJEKTUM
	private $log_text;


	public static function register($_category_name, $_text, $_date = null, $_user_id = null) {
		global $sql, $user;
		$category_id = $sql->single_variable("SELECT logc_id FROM log_category WHERE logc_name = '" . $_category_name . "';");

		if (is_object($user)) {
			//ha a user változó objektum, akkor megvizsgáljuk, hogy coach vagy parent
			if (get_class($user) == 'user') $function_name = 'get_ua_id';
			elseif(get_class($user) == 'user_kid') $function_name = 'get_uk_id';
			elseif(get_class($user) == 'camp_user') $function_name = 'get_cu_id';
			elseif(get_class($user) == 'user_camp_leader') $function_name = 'get_ucl_id';
		}

		$sql->insert_into('log', array(
				'log_log_category_logc_id' => $category_id,
				'log_user_id' => (!$_user_id?$user->$function_name():$_user_id),
				'log_date' => (!$_date?date("Y-m-d H:i:s"):$_date),
				'log_text' => $_text
			)
		);
	}

	public function set_log_id($_id) {
		$this->log_id = $_id;
	}

	public function set_log_log_category_logc_id($_log_category_logc_id) {
		$this->log_log_category_logc_id = $_log_category_logc_id;
	}

	public function set_log_category($_category) {
		$this->log_category = $_category;
	}

	public function set_log_date($_date) {
		$this->log_date = $_date;
	}

	public function set_log_user_id($_user_id) {
		$this->log_user_id = $_user_id;
	}

	public function set_log_user($_user) {
		$this->log_user = $_user;
	}

	public function set_log_text($_text) {
		$this->log_text = $_text;
	}

	public function set_log_table($_table) {
		$this->log_table = $_table;
	}

	public function set_log_field($_field) {
		$this->log_field = $_field;
	}

	public function set_log_selector($_selector) {
		$this->log_selector = $_selector;
	}

	public function get_log_id() {
		return $this->log_id;
	}

	public function get_log_log_category_logc_id() {
		return $this->log_log_category_logc_id;
	}

	public function get_log_category() {
		return $this->log_category;
	}

	public function get_log_date() {
		return $this->log_date;
	}

	public function get_log_user_id() {
		return $this->log_user_id;
	}

	public function get_log_user() {
		return $this->log_user;
	}

	public function get_log_text() {
		global $sql;
		//ha nincs TABLE beállítva, akkor visszaadja az üzenetet
		if (!$this->get_log_category()->get_logc_table()) return $this->log_text;
		else return $sql->single_variable("SELECT " . $this->get_log_category()->get_logc_field() . " FROM " . $this->get_log_category()->get_logc_table() . " WHERE " . $this->get_log_category()->get_logc_selector() . " = " . $this->log_text);
	}

	public function get_log_table() {
		return $this->log_table;
	}

	public function get_log_field() {
		return $this->log_field;
	}

	public function get_log_selector() {
		return $this->log_selector;
	}

	public function get_log_img() {
		if (strstr($this->get_log_category()->get_logc_name(), 'new')) return 'tick';
		elseif (strstr($this->get_log_category()->get_logc_name(), 'delete')) return 'delete';
		elseif (strstr($this->get_log_category()->get_logc_name(), 'update')) return 'edit';
		elseif (in_array($this->get_log_category()->get_logc_name(), array('training_close', 'training_open'))) return 'lock';
		elseif (in_array($this->get_log_category()->get_logc_name(), array('admin_login', 'admin_logout', 'kid_login', 'kid_logout', 'coach_login', 'coach_logout', 'camp_user_login', 'camp_user_logout', 'camp_leader_login', 'camp_leader_logout'))) return 'login';
	}

	public function set_log_data_by_id($_log_id) {
		global $sql, $user;
		$log_data_assoc_array = $sql->assoc_array("select * from log where log_id = " . $_log_id);
		$log_data_array = $log_data_assoc_array[0];
		foreach ($log_data_array as $field => $value) {
			$function_name = "set_" . $field;
			$this->$function_name($value);
			if ($field == 'log_log_category_logc_id') {
				//beállítja a log_category objektumot
				$log_cat = new log_category();
				$log_cat->set_logc_data_by_id($value);
				$this->set_log_category($log_cat);
			}

			if ($field == 'log_user_id') {
				//beállítja a log_user objektumot
				if ($this->get_log_category()->get_logc_type() == 1) $new_user = new user();
				elseif($this->get_log_category()->get_logc_type() == 2) $new_user = new user_kid();
				elseif($this->get_log_category()->get_logc_type() == 3) $new_user = new camp_user();
				elseif($this->get_log_category()->get_logc_type() == 4) $new_user = new user_camp_leader();
				$new_user->set_user_data_by_id($value);
				$this->set_log_user($new_user);
			}
		}
	}


}

?>
