<?php class holidays_model extends CI_Model{

	private $table_name	= 'feriados';			// EAP Holidays

	public function get_holidays($active=null)
	{
		$this->db->from($this->table_name);
		if($active != null) {$this->db->where('activo', $active);}
		$this->db->order_by('fecha');
		$query=$this->db->get();
		return $query->result_array();
		
	}

	public function get_holidays_dates()
	{
		$txtSelect = 'fecha';
		$this->db->select($txtSelect);
		$this->db->from($this->table_name);
		$this->db->order_by('fecha');
		$query=$this->db->get();
		
		$holidays = $query->result_array();
		
		// Esta funcion puede ser reemplazada por array_colum pero no es soportado en PHP < a 5.5
		$b = array_map(function($h)
				{
					return $h['fecha'];
				}, $holidays);
		return $b ;
	}

	
	public function set_holiday($id,$data,$mode=null)
	{
		if ($mode == 'insert') {
			$this->db->insert($this->table_name, $data);
			return $this->db->insert_id();
		} else {
			$this->db->where('id', $id);
			return $this->db->update($this->table_name, $data);
		}
		
	}

	public function get_holiday_log($id)
	{
		$txtSelect = '*';
		$this->db->select($txtSelect);
		$this->db->from('feriados_log');
		$this->db->where('id_feriado', $id);
		$this->db->order_by('fecha','desc');
		$query=$this->db->get();
		return $query->result_array();
	}	
	
	
	public function set_profile_log($data)
	{
		return $this->db->insert('feriados_log', $data);
	}	


	public function is_unique($fecha, $id)
	{
		$this->db->from($this->table_name);
		$this->db->where('id !=', $id);
		$this->db->where('fecha', $fecha);				
		$query=$this->db->get();
		if ($query->num_rows()>0) {return false;}else { return true;}
	}
	
}

