<?php

class Infoprojects_model extends CI_Model
{

	var $insert_id=false;
	var $top_num=false;
	var $p_id=false;
	var $order_by=false;

    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

	function init()
	{
		$this->top_num=false;
		$this->p_id=false;
		$this->order_by=" ORDER BY ip.p_id ASC ";
	}
	
	function detail($p_id)
	{
		$this->init();
		$this->top_num=1;
		$this->p_id=" AND ip.p_id=".$this->HT->escape($p_id);
		return $this->get_list();
	}
	
	function all()
	{
		$this->init();
		return $this->get_list();
	}
	
    function get_list()
    {
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "      ip.p_id \n"
				."	  ,ip.p_type \n"
				."	  ,ip.p_state \n"
				."	  ,ip.p_title \n"
				."	  ,ip.p_manager \n"
				."	  ,ip.p_bonus \n"
				."	  ,ip.p_memo \n"
				."	  ,ip.p_requirer \n"
				."	  ,ip.p_solution \n"
				."	  ,ip.p_datetime \n"
				." FROM infoprojects ip \n"
			 ." WHERE  1 = 1 \n ";
		$this->p_id ? $sql.=$this->p_id : false;
		$this->order_by ? $sql.=$this->order_by : false;
		$query=$this->HT->query($sql);
		if($this->top_num==1)
		{
			if ($query->num_rows() > 0)
			{
				$row = $query->row();
				return $row;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return $query->result();
		}    
    }
	
	function add($p_title,$p_type,$p_manager,$p_bonus,$p_memo,$p_requirer,$p_solution,$p_state)
	{
		$sql="	INSERT INTO [infoprojects] \n"
				."	   ([p_title] \n"
				."	   ,[p_type] \n"
				."	   ,[p_manager] \n"
				."	   ,[p_bonus] \n"
				."	   ,[p_memo] \n"
				."	   ,[p_requirer] \n"
				."	   ,[p_solution] \n"
				."	   ,[p_state] \n"
				."	   ,[p_datetime]) \n"
				." VALUES \n"
				."	   (N?,N?,N?,?,N?,?,N?,GetDate()) \n";
		$query = $this->HT->query($sql,array($p_title,$p_type,$p_manager,$p_bonus,$p_memo,$p_requirer,$p_solution,$p_state));
		$this->insert_id = $this->HT->last_id('infoprojects');
		return $query;
	}
	
	function update($p_id,$p_title,$p_type,$p_manager,$p_bonus,$p_memo,$p_requirer,$p_solution,$p_state)
	{
		$sql="	UPDATE [infoprojects] SET \n"
				."	   [p_title]=N? \n"
				."	   ,[p_type]=N?\n"
				."	   ,[p_manager]=? \n"
				."	   ,[p_bonus]=N? \n"
				."	   ,[p_memo]=N? \n"
				."	   ,[p_requirer]=? \n"
				."	   ,[p_solution]=N? \n"
				."	   ,[p_state]=N? \n"
				."	   ,[p_datetime]=GetDate() \n"
				." WHERE \n"
				."	   [p_id]=? \n";
		$query = $this->HT->query($sql,array($p_title,$p_type,$p_manager,$p_bonus,$p_memo,$p_requirer,$p_solution,$p_state,$p_id));
		return $query;
	}
	
}