<?php 
/**
 * Input_kegiatan Page Controller
 * @category  Controller
 */
class Input_kegiatanController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "input_kegiatan";
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function index($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("input_kegiatan.tps", 
			"kegiatan.tps AS kegiatan_tps", 
			"input_kegiatan.nama_kegiatan", 
			"input_kegiatan.foto_kegiatan");
		$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				input_kegiatan.id LIKE ? OR 
				input_kegiatan.tps LIKE ? OR 
				input_kegiatan.nama_kegiatan LIKE ? OR 
				input_kegiatan.foto_kegiatan LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "input_kegiatan/search.php";
		}
		$db->join("kegiatan", "input_kegiatan.tps = kegiatan.tps", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("input_kegiatan.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		if(!empty($request->input_kegiatan_tps)){
			$val = $request->input_kegiatan_tps;
			$db->where("input_kegiatan.tps", $val , "=");
		}
		$tc = $db->withTotalCount();
		$records = $db->get($tablename, $pagination, $fields);
		$records_count = count($records);
		$total_records = intval($tc->totalCount);
		$page_limit = $pagination[1];
		$total_pages = ceil($total_records / $page_limit);
		$data = new stdClass;
		$data->records = $records;
		$data->record_count = $records_count;
		$data->total_records = $total_records;
		$data->total_page = $total_pages;
		if($db->getLastError()){
			$this->set_page_error();
		}
		$page_title = $this->view->page_title = "Input Kegiatan";
		$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
		$this->view->report_title = $page_title;
		$this->view->report_layout = "report_layout.php";
		$this->view->report_paper_size = "A4";
		$this->view->report_orientation = "portrait";
		$this->render_view("input_kegiatan/list.php", $data); //render the full page
	}
// No View Function Generated Because No Field is Defined as the Primary Key on the Database Table
// No Edit Function Generated Because No Field is Defined as the Primary Key
}
