<?php
/**
 * department_model - Model for add/update/delete department
 *
 * @author $Author: dtong $
 * @version $Id: department_model.php,v 1.6 2011/02/11 09:09:23 dtong Exp $
 * @copyright Copyright (c) 2009, Tiller Software Co., Ltd.
*/

class department_model extends CI_Model {
	function __construct() {
		parent::__construct();
    }

	function get_alldepartment($department_enabled,$filter=FALSE) { // 1:enabled

		$department = $this->db->dbprefix('department');
		$sql   = "select id,name,shortname,description,enabled from $department";
		$where = " where id <>''";

		if ($filter!=FALSE)
	    //$where .= " and (name like '%$filter%' or shortname like '%$filter%' or description like '%$filter%' or enabled like '%$filter%')";
	    $where .= " and (name like '%$filter%')";
	    if ($department_enabled!=CMS_ADMIN_ALL){
	    $where .= " and enabled =" . $department_enabled;
	    }

	    $order_by = " Order by name";

		$res = $this->db->query($sql.$where.$order_by);
		if ( $res->num_rows() <= 0 )
			return FALSE;

		return $res->result();
		//return $res->result_array();
	}

	function get_department($departmentid, $department_enabled=1 ) {
		if ( !is_numeric($departmentid) )
			return FALSE;

	    $this->db->select('id,name,shortname,description,enabled');
	    $this->db->from('department');
	    $this->db->where('id',$departmentid);
		$res = $this->db->get();
		if ( $res->num_rows() != 1 )
			return FALSE;
		return $res->result();
	}

	function add_department($data) {

		$this->db->insert('department', $data);
		return TRUE;
	}

    function update_department($departmentid, $data) {

		$this->db->where('id',$departmentid);
		$this->db->update('department', $data);
		return TRUE;
	}

	function del_department($departmentid,$department_enabled) {

		$this->db->where('id',$departmentid);
		$this->db->update('department', array('enabled'=>$department_enabled));
		return TRUE;
	}


}
?>