<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_personal_attachment extends CI_Model {

	public function fetchPersonalAttachment($staff_id = null,$mask_id = null,$latest = false,$profiling_id = null)
	{

		//inner query
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__personal_attachment a');
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		$this->db->where('a.is_delete',0);
		$max = $this->db->get_compiled_select();

		//outer query
		$this->db->select('a.*,b.action_efective_date,c.type as action_type');
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		if($mask_id) $this->db->where('a.mask_id',$mask_id);
		if($latest) $this->db->where("a.profiling_id IN ($max)");
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);

		$this->db->where('a.is_delete',0);

		$this->db->join('user__profiling b','b.id = a.profiling_id','left');
		$this->db->join('_ref__action_type c','c.id = b.action_type_id','left');

		$query = $this->db->get('user__personal_attachment a');

		return ($query->num_rows() > 0)?$query->result() : null;
	}

	public function createPersonalAttachment($input)
	{
		$this->db->insert('user__personal_attachment',$input);
		return $this->db->insert_id();
	}

	public function editPersonalAttachment($input, $attachmentId = null, $mask_id = null)
	{
		$this->db->set($input);
		if($attachmentId) $this->db->where('id',$attachmentId);
		if($mask_id) $this->db->where('mask_id',$mask_id);

		$this->db->update('user__personal_attachment');


		return true;
	}
	
	public function get_personalData($id=0){
		$this->db->where('id',$id);
		return $this->db->get('user__personal_attachment')->result_array();
	}
}
?>