<?php
/*
@author: G J Jidni
*/
class Module_Setup_model extends CI_Model
{

	//constructor
	public function __construct()
	{
		parent::__construct();
	}

	function getModules ()
	{
		$sql = '(SELECT module_id, module_name, (CASE WHEN active_status = \'1\' THEN \'Active\' ELSE \'Inactive\' END) as active_status from tbl_module) AS abc';

		$getInstance = & get_instance();
		$getInstance->load->library('rapyd');
		$getInstance->rapyd->load('datagrid');
		$grid = new DataGrid('', $sql);
		$grid->order_by('module_id', 'asc');
		$grid->per_page = 20;
		$grid->column_orderby('Module Name', 'module_name', 'module_name', 'align=\'left\'');
		$grid->column_orderby('Active Status', 'active_status', 'active_status', 'align=\'left\'');
		$link_edit = anchor('administration/module_setup/editModule/<#module_id#>','Edit');
		$grid->column('Actions',$link_edit);
		
		$grid->build();
		return $grid->output;
	}
	
	function isModuleExist($moduleData)
	{
		$this->db->select('*');
		$this->db->from('tbl_module');
		if($moduleData['module_id'])
		{
			$this->db->where('module_id !=', $moduleData['module_id']);
		}
		$this->db->where('module_name', $moduleData['module_name']);
		$resultSet = $this->db->get();
		return $resultSet->num_rows();
		
	}
	
	function saveModule($moduleData)
	{	
		if($moduleData['module_id'])
		{
			$this->db->where('module_id',$moduleData['module_id']);
			$this->db->update('tbl_module',$moduleData);
		}
		else
		{
			$this->db->insert('tbl_module',$moduleData);
		}
	}
	
	function getModule($moduleId)
	{
		$this->db->select('*');
		$this->db->from('tbl_module');
		$this->db->where('module_id', $moduleId);
		$resultSet = $this->db->get();
		return $resultSet->row_array();
	}
	
	function getActiveModules()
	{
		$this->db->select('*');
		$this->db->from('tbl_module');
		$this->db->where('active_status',1);
		$resultSet = $this->db->get();
		return $resultSet->result();
	}
}
?>