<?php
	/**
	 *	Class for handling users
	 *
	 *
	 */
	
	class User {
		
		public $userId = null;
		public $simpleUserInfo = null;
		
		function __construct($user_id=null,$show_inactive=false,$arr_location_id=array()) {
			 if ($user_id == 'all') {
				//get all users
				$show_inactive_sql = (!$show_inactive) ? 'AND u.user_active=1' : '';
				
				$sql = "SELECT *,
						u.user_id AS user_id,
						l.location_id AS location_id,
						l.location_name AS location_name,
						l2.location_id AS parent_location_id,
						l2.location_name AS parent_location_name
							FROM users u 
							LEFT JOIN user_user_type uut ON uut.user_id = u.user_id
							LEFT JOIN user_types ut ON ut.user_type_id = uut.user_type_id
							LEFT JOIN user_location ul ON ul.user_id = u.user_id
							LEFT JOIN location l ON l.location_id = ul.location_id
							LEFT JOIN location l2 ON l2.location_id = l.parent_id
						WHERE 1=1 {$show_inactive_sql} {$where}
						ORDER BY u.user_id ASC";
				$db = new Db();
				$this->userList = $db->select($sql,true);
				//dump($this->userList);
			}
			
			elseif ($user_id != null) {
				$this->userId = $user_id;
				$this->getUserData();
				$this->getUserTypes();
			}
		}
		
		function getUserData($user_id=null) {
		//dump($this);
			if ($user_id == null) $user_id = $this->userId;
			//dump($user_id);
			$sql  = "SELECT 
						u.*, 
						IF (u.name_prepend != '',CONCAT(u.name_prepend, ' ', u.user_name), u.user_name) AS user_name,
						IF (u.name_prepend != '',CONCAT(u.user_surname, ' ', u.name_append), u.user_surname) AS user_surname,
						ut.*, 
						DATE_FORMAT(last_login, '%d.%m.%Y') AS last_login_date, 
						DATE_FORMAT(last_login, '%H:%i') AS last_login_time 
					
					FROM users u 
						LEFT JOIN user_user_type uut ON uut.user_id = {$user_id}
						LEFT JOIN user_types ut ON uut.user_type_id=ut.user_type_id
					WHERE u.user_id = {$user_id}";
			//dump($sql);
			$db = new Db();
			$return = $db->select($sql,true,false);
			//dump($return);
			if ($return == array()) {
				//die("Uporabnik ne obstaja!");
			}
			$this->userTypeID = $return[0]['user_type_id'];
			$this->simpleUserInfo = $return[0];		//expecting there is only one user with user_id $user_id

			$this->user_type_id = $return[0]['user_type_id'];
			return $this;
			//dump($this);
		}
		
		public function getChildrenLocation($parent_id) {
			$sql = "";
		}
		
		public function prepareUser() {
			//get user types
			$sql = "SELECT user_type_id AS id, user_type_name AS value  FROM user_types ORDER BY user_type_id";
			$db_user_types = new Db();
			
			$arr_user = array(
				'tables' => array(
					'users' => array(
						'login_name'	=> 'VC',
						'user_mail'		=> 'VC',
						'password'		=> 'PASS',
						'user_name'		=> 'VC',
						'user_surname'	=> 'VC'
					),
					'user_user_type' => array(
						'user_id'		=> 'insert_id',
						'user_type_id'	=> 'SE;get_types'
					)
				),
				'hreadable' => array(
					'users' => array(
						'table_hreadable'=>'Uporabnik',
						'login_name'	=> 'Uporabniško ime',
						'user_mail'		=> 'E-mail',
						'password'		=> 'Geslo',
						'user_name'		=> 'Ime',
						'user_surname'	=> 'Priimek'
					),
					'user_user_type' => array(
						'table_hreadable'=>'Tip uporabnika',
						'user_id'		=> 'insert_id',
						'user_type_id'	=> 'Tip uporabnika'
					)
				),
				'input_data' => array('get_types' => $db_user_types->select($sql,true))
			);
			
			return $arr_user;
		}
		public function getUserTypes() {
			$db = new Db();
			$sql = "SELECT * FROM user_types ORDER BY user_type_name ASC";
			$this->allUserTypes = $db->select($sql);
		}
		
		public function addUser($arr_userdata) {
		
		}
	}
?>