<?php

class Configuracoes_model extends CI_Model 
{
	/*function get_profile()
	{
		$this->db->select('*');
		$this->db->where('id',1);
		$query = $this->db->get( 'blg_profile' );
		
		if ($query->num_rows() === 1) return $query->row();
		return NULL;
	}*/
	
	function get_version()
    {
        $query = $this->db->get('migrations');
        if ($query->num_rows() === 1) return $query->row();
		return NULL;
    }
    
	function get_sqls()
	{
	    $this->db->where('a.status',1);
	    $this->db->order_by("a.id_sql", "desc");
	    $this->db->join('cad_cadastro as b', 'b.id_cadastro = a.id_cadastro');
	    $query = $this->db->get('mig_sql as a');
	    if ($query->num_rows() > 0)
			return $query->result();
		return NULL;
	}
	
	function get_sql($id_sql)
	{
	    $this->db->where('a.id_sql',$id_sql);
	    $this->db->order_by("a.id_sql", "desc");
	    $this->db->join('cad_cadastro as b', 'b.id_cadastro = a.id_cadastro');
	    $query = $this->db->get('mig_sql as a');
	    if ($query->num_rows() === 1)
			return $query->row();
		return NULL;
	}
	
	function get_tipo_slideshow()
	{
		$this->db->select('tipo_slideshow');
		$query = $this->db->get( 'blg_profile' );
		
		if ($query->num_rows() === 1) return $query->row();
		return NULL;
	}
	
	function get_tema($id_user)
	{
		$this->db->where('id_user',$id_user);
		$query = $this->db->get( 'blg_tema' );
		
		if ($query->num_rows() === 1) return $query->row();
		return NULL;
	}
	
	//#######################   U P D A T E    #######################
	function update_sql($id, $data)
	{
	    $this->db->where( 'id_sql', $id );
	    if ($this->db->update( 'mig_sql', $data )) 
        {
            return TRUE;
        } 
        else
        {
            return FALSE;
        }
	}
	
    function update_profile($id, $data)
	{
	    $this->db->where( 'id', $id );
	    if ($this->db->update( 'blg_profile', $data )) 
        {
            return TRUE;
        } 
        else
        {
            return FALSE;
        }
	}
	
	function update_tema($id, $data)
	{
        $this->db->where( 'id_user', $id );
		$query = $this->db->get( 'blg_tema' );
		if ($query->num_rows() === 1)
		{
		   $this->db->where( 'id_user', $id );
    	    if ($this->db->update( 'blg_tema', $data )) 
            {
                return TRUE;
            } 
            else
            {
                return FALSE;
            } 
		}
		else
		{
		    if ($this->db->insert ('blg_tema', $data))
			    return TRUE;
		    else
			    return FALSE;
		}
	}
	
	//#######################   I N S E R T     #######################
	
	function insere_sql($dados) {
		if ($this->db->insert ('mig_sql', $dados))
			return $this->db->insert_id();
		else
			return FALSE;
	}
	
	function insere_contato($dados) {
		if ($this->db->insert ('blg_contato', $dados))
			return $this->db->insert_id();
		else
			return FALSE;
	}
	
	function insere_lp_contato($dados) {
		if ($this->db->insert ('lp_contato', $dados))
			return $this->db->insert_id();
		else
			return FALSE;
	}
	
	function get_lp_contato($id)
	{
		$this->db->where('id',$id);
		$query = $this->db->get( 'lp_contato' );
		
		if ($query->num_rows() === 1) return $query->row();
		return NULL;
	}
	
	//#######################   D E L E T E     #######################
	
	function delete_sql($id_sql)
	{
        $this->db->where('id_sql',$id_sql);
	    if ($this->db->delete('mig_sql'))
			return TRUE;
		else
			return FALSE;
	}
}