<?php 
//namespace models;
//use helpers\session as Session;
//use helpers\cookie as Cookie;
//use \PDO; //uncomment for PDOStatement::fetchObjects

/**
* Profile Model : The Database object
* Handles the user's profile / inbox / edit / user images stuff
**/
class Profile_Model extends Model{
	/**
	* Constructor, expects a Database connection
	*/
	public function __construct(){
		parent::__construct();
	}

	/**
	* Returns user all parameters from user_information table
	* @param INT the id of user to look for
	* @return ARRAY or false BOOL if none found
	**/
	public function get_User($userId){
		$query = $this->_db->select("SELECT *
									FROM ".PREFIX."usr_i 
									WHERE user_id = :user_id"
									,array(':user_id' => $userId));
		// if there's NOT one result but only first row fetched (we only have one result)
		if($query){
			return $query[0];
		}else{
			return false;
		}
	}

	/**
	* Updates/edit users profile information everything that passed to this method
	* @param INT the id of user to look for
	* @param ARRAY updateArray with values
	* @return BOOL on successful update ele on false
	*/
	public function set_EditProfile($userId,$updateArray){
		$query_a = $this->_db->update(PREFIX."usr_i",
									$updateArray,
									array('user_id' => $userId));
		if(sizeof($query_a) == 1){
			// Session::set("feedback_positive", USERNAME_CHANGE_SUCCESSFUL);
			return true;
		}else{
			// Session::set("feedback_negative", ERROR_UNKNOWN);
			return false;
		}
	}

	/**
	* Retrieve all messages/conversations that was to current user with others, auto limit considering 
	* @param INT current page/ optional limit/ current user id
	* @return ARRAY of conversations/messages or BOOL false if no conversations found
	**/
	public function get_UserConvs($pageId=0,$limit=DISPLAY_USERS,$userId){
		$data['usr'] = $this->_db->select("SELECT SQL_CALC_FOUND_ROWS DISTINCT u.user_id, u.user_city, u.user_name, u.user_birth, u.user_interpersonal_stat, u.user_about, u.user_height ,u.user_img_selected, u.user_gender,p.con_id, p.user_one, p.user_two, p.status,p.time
									FROM ".PREFIX."usr_i u
									JOIN (SELECT DISTINCT * 
											FROM ".PREFIX."usr_con
											WHERE ".PREFIX."usr_con.user_one = :user_id OR ".PREFIX."usr_con.user_two = :user_id
											ORDER BY ".PREFIX."usr_con.time DESC
											)AS p ON u.user_id = p.user_one OR u.user_id = p.user_two 
									WHERE NOT u.user_id = :user_id
									GROUP BY u.user_id
									ORDER BY p.time DESC LIMIT ".$limit * ($pageId)." , ".$limit." "
									,array(':user_id' => $userId));
		// may be should be replaced data ['usr'] != NULL
		if($data){
			$data['add'] = $this->_db->select("SELECT FOUND_ROWS() AS `total_found`");
			return $data;
		}else{
			return false;
		}
	}

	/** 
	* Deletes user image by id from images table in DB, for security match both id's
	* @param INT user id 
	* @param INT image id to delete 
	* @return BOOL success or failure
	*/
	public function set_ImageDelete($userId,$imgId=''){
		if($user_img=='')
			return false;
		$query_a = $this->_db->delete(PREFIX."usr_img",
							array('user_img' => $imgId,
								'user_id' => $userId));
		if(sizeof($query_a) == 1){
			return true;
		}else{
			return false;
		}
	}

	/** 
	* Set selected avatar image for user as none in DB, unfortunately due to PDO i will update directly....
	* @param INT user id to unset avatar image
	* @return BOOL success or failure
	*/
	public function set_ImageSelectedNone($userId){
		$query_a = $this->_db->updateDirect(PREFIX."usr_i, ".PREFIX."usr_img
											SET
											".PREFIX."usr_i.user_img_selected = '',
											".PREFIX."usr_img.user_img_selected = '0'
											WHERE 
											".PREFIX."usr_img.user_id='".$userId."' AND ".PREFIX."usr_i.user_id='".$userId."'");
		if(sizeof($query_a) == 1){
			return true;
		}else{
			return false;
		}
	}

	/** 
	* Set selected avatar image in user information table and user images table
	* @param INT user id
	* @param INT image id to set
	* @param STRING image path 
	* @return BOOL success or failure
	*/
	public function set_ImageSelected($userId,$userImg='',$imgSelected=''){
		if($imgSelected=='' OR $userImg=='')
			return false;
		$query_a = $this->_db->updateDirect(PREFIX."usr_i, ".PREFIX."usr_img
											SET
											".PREFIX."usr_i.user_img_selected = '$imgSelected',
											".PREFIX."usr_img.user_img_selected = IF(".PREFIX."usr_img.user_img='$userImg','1','0')
											WHERE 
											".PREFIX."usr_img.user_id='".$userId."' AND ".PREFIX."usr_i.user_id='".$userId."'");
		if(sizeof($query_a) == 1){
			return true;
		}else{
			return false;
		}
	}

	/**
	* Get all user image paths and selected image path if such was set
	* @param INT user id 
	* @returns ARRAY with paths or BOOL false on none found
	*/
	public function get_UserImages($userId){
		$query = $this->_db->select("SELECT *
									FROM ".PREFIX."usr_img 
									WHERE user_id = :user_id"
									,array(':user_id' => $userId));
		// if there's NOT one result but only first row fetched (we only have one result)
		if($query){
			return $query;
		}else{
			return false;
		}
	}

	/** 
	* Inserts new image user have uploaded to server user images table
	* PLEASE NOTE THE IMAGE SHOULD BE CHECKED AND SET IN BOTH DB ADDRESES OR NOT 
	* I THINK IT SHOULD RETURN THE CONSTRUCTED PATHS....
	* @param INT user id 
	* @param STRING the new image path on server 
	* @return BOOL success or failure
	**/
	public function set_ConcatNewImage($userId,$imgPath=''){
		if($imgPath == '')
			return false;
		/* Add new image path to current user , this path should be checked by administrator */
		$query = $this->_db->insert(PREFIX."usr_img",
							array('user_id' => $userId,
									'user_img_path' => $imgPath));
		if($query >= 1){
			return true;
		}else{
			return false;
		}
	}

	// Unfortunately due to FUCKING PDO the CONCAT_WS(sparator , string 1 , string 2) not working
	// we return upon success or failure the user images paths 
	// public function set_ConcatNewImage($user_img_paths=''){
		// $user_add = $this->get_users_add();
		// $usedData = $user_add[0]->user_img_paths;
		// // $query_a = $this->_db->update(PREFIX."usr_".Session::get("user_gender")."_i",
		// $query_a = $this->_db->update(PREFIX."usr_i",
							// //array('user_img_paths' => "CONCAT_WS('|',user_img_paths,".$user_img_paths.")"),
							// array('user_img_paths' => '|'.$user_img_paths.$usedData),
							// array('user_id' => Session::get('user_id')));
		// if(sizeof($query_a) == 1){
			// return '|'.$user_img_paths.$usedData;
		// }else{
			// return $usedData;
		// }
	// }

}