<?php 
/**
 * Mhs Page Controller
 * @category  Controller
 */
class MhsController extends BaseController{
	function __construct(){
		parent::__construct();
		$this->tablename = "mhs";
	}
	/**
     * 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("id", 
			"nopend", 
			"npm", 
			"nama", 
			"jk", 
			"prodi", 
			"rombel", 
			"alamat", 
			"rt", 
			"lurah", 
			"camat", 
			"kota", 
			"kodepos", 
			"telp", 
			"hp", 
			"temp", 
			"tgl", 
			"agama", 
			"wn", 
			"jjng", 
			"nasek", 
			"kotsek", 
			"jur", 
			"nis", 
			"noij", 
			"tempij", 
			"tglij", 
			"thnl", 
			"ibu", 
			"nik");
		$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 = "(
				mhs.id LIKE ? OR 
				mhs.nopend LIKE ? OR 
				mhs.npm LIKE ? OR 
				mhs.nama LIKE ? OR 
				mhs.jk LIKE ? OR 
				mhs.prodi LIKE ? OR 
				mhs.rombel LIKE ? OR 
				mhs.alamat LIKE ? OR 
				mhs.rt LIKE ? OR 
				mhs.lurah LIKE ? OR 
				mhs.camat LIKE ? OR 
				mhs.kota LIKE ? OR 
				mhs.kodepos LIKE ? OR 
				mhs.telp LIKE ? OR 
				mhs.hp LIKE ? OR 
				mhs.temp LIKE ? OR 
				mhs.tgl LIKE ? OR 
				mhs.agama LIKE ? OR 
				mhs.wn LIKE ? OR 
				mhs.jjng LIKE ? OR 
				mhs.nasek LIKE ? OR 
				mhs.kotsek LIKE ? OR 
				mhs.jur LIKE ? OR 
				mhs.nis LIKE ? OR 
				mhs.noij LIKE ? OR 
				mhs.tempij LIKE ? OR 
				mhs.tglij LIKE ? OR 
				mhs.thnl LIKE ? OR 
				mhs.ibu LIKE ? OR 
				mhs.nik LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "mhs/search.php";
		}
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("mhs.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		$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 = "Mhs";
		$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("mhs/list.php", $data); //render the full page
	}
	/**
     * View record detail 
	 * @param $rec_id (select record by table primary key) 
     * @param $value value (select record by value of field name(rec_id))
     * @return BaseView
     */
	function view($rec_id = null, $value = null){
		$request = $this->request;
		$db = $this->GetModel();
		$rec_id = $this->rec_id = urldecode($rec_id);
		$tablename = $this->tablename;
		$fields = array("id", 
			"nopend", 
			"npm", 
			"nama", 
			"jk", 
			"prodi", 
			"rombel", 
			"alamat", 
			"rt", 
			"lurah", 
			"camat", 
			"kota", 
			"kodepos", 
			"telp", 
			"hp", 
			"temp", 
			"tgl", 
			"agama", 
			"wn", 
			"jjng", 
			"nasek", 
			"kotsek", 
			"jur", 
			"nis", 
			"noij", 
			"tempij", 
			"tglij", 
			"thnl", 
			"ibu", 
			"nik");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("mhs.id", $rec_id);; //select record based on primary key
		}
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Mhs";
		$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";
		}
		else{
			if($db->getLastError()){
				$this->set_page_error();
			}
			else{
				$this->set_page_error("No record found");
			}
		}
		return $this->render_view("mhs/view.php", $record);
	}
	/**
     * Insert new record to the database table
	 * @param $formdata array() from $_POST
     * @return BaseView
     */
	function add($formdata = null){
		if($formdata){
			$db = $this->GetModel();
			$tablename = $this->tablename;
			$request = $this->request;
			//fillable fields
			$fields = $this->fields = array("nopend","npm","nama","jk","prodi","rombel","alamat","rt","lurah","camat","kota","kodepos","telp","hp","temp","tgl","agama","wn","jjng","nasek","kotsek","jur","nis","noij","tempij","tglij","thnl","ibu","nik");
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'nopend' => 'required',
				'npm' => 'required',
				'nama' => 'required',
				'jk' => 'required',
				'prodi' => 'required',
				'rombel' => 'required',
				'alamat' => 'required',
				'rt' => 'required',
				'lurah' => 'required',
				'camat' => 'required',
				'kota' => 'required',
				'kodepos' => 'required',
				'telp' => 'required',
				'hp' => 'required',
				'temp' => 'required',
				'tgl' => 'required',
				'agama' => 'required',
				'wn' => 'required',
				'jjng' => 'required',
				'nasek' => 'required',
				'kotsek' => 'required',
				'jur' => 'required',
				'nis' => 'required',
				'noij' => 'required',
				'tempij' => 'required',
				'tglij' => 'required',
				'thnl' => 'required',
				'ibu' => 'required',
				'nik' => 'required',
			);
			$this->sanitize_array = array(
				'nopend' => 'sanitize_string',
				'npm' => 'sanitize_string',
				'nama' => 'sanitize_string',
				'jk' => 'sanitize_string',
				'prodi' => 'sanitize_string',
				'rombel' => 'sanitize_string',
				'alamat' => 'sanitize_string',
				'rt' => 'sanitize_string',
				'lurah' => 'sanitize_string',
				'camat' => 'sanitize_string',
				'kota' => 'sanitize_string',
				'kodepos' => 'sanitize_string',
				'telp' => 'sanitize_string',
				'hp' => 'sanitize_string',
				'temp' => 'sanitize_string',
				'tgl' => 'sanitize_string',
				'agama' => 'sanitize_string',
				'wn' => 'sanitize_string',
				'jjng' => 'sanitize_string',
				'nasek' => 'sanitize_string',
				'kotsek' => 'sanitize_string',
				'jur' => 'sanitize_string',
				'nis' => 'sanitize_string',
				'noij' => 'sanitize_string',
				'tempij' => 'sanitize_string',
				'tglij' => 'sanitize_string',
				'thnl' => 'sanitize_string',
				'ibu' => 'sanitize_string',
				'nik' => 'sanitize_string',
			);
			$this->filter_vals = true; //set whether to remove empty fields
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$rec_id = $this->rec_id = $db->insert($tablename, $modeldata);
				if($rec_id){
					$this->set_flash_msg("Record added successfully", "success");
					return	$this->redirect("mhs");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Mhs";
		$this->render_view("mhs/add.php");
	}
	/**
     * Update table record with formdata
	 * @param $rec_id (select record by table primary key)
	 * @param $formdata array() from $_POST
     * @return array
     */
	function edit($rec_id = null, $formdata = null){
		$request = $this->request;
		$db = $this->GetModel();
		$this->rec_id = $rec_id;
		$tablename = $this->tablename;
		 //editable fields
		$fields = $this->fields = array("id","nopend","npm","nama","jk","prodi","rombel","alamat","rt","lurah","camat","kota","kodepos","telp","hp","temp","tgl","agama","wn","jjng","nasek","kotsek","jur","nis","noij","tempij","tglij","thnl","ibu","nik");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'nopend' => 'required',
				'npm' => 'required',
				'nama' => 'required',
				'jk' => 'required',
				'prodi' => 'required',
				'rombel' => 'required',
				'alamat' => 'required',
				'rt' => 'required',
				'lurah' => 'required',
				'camat' => 'required',
				'kota' => 'required',
				'kodepos' => 'required',
				'telp' => 'required',
				'hp' => 'required',
				'temp' => 'required',
				'tgl' => 'required',
				'agama' => 'required',
				'wn' => 'required',
				'jjng' => 'required',
				'nasek' => 'required',
				'kotsek' => 'required',
				'jur' => 'required',
				'nis' => 'required',
				'noij' => 'required',
				'tempij' => 'required',
				'tglij' => 'required',
				'thnl' => 'required',
				'ibu' => 'required',
				'nik' => 'required',
			);
			$this->sanitize_array = array(
				'nopend' => 'sanitize_string',
				'npm' => 'sanitize_string',
				'nama' => 'sanitize_string',
				'jk' => 'sanitize_string',
				'prodi' => 'sanitize_string',
				'rombel' => 'sanitize_string',
				'alamat' => 'sanitize_string',
				'rt' => 'sanitize_string',
				'lurah' => 'sanitize_string',
				'camat' => 'sanitize_string',
				'kota' => 'sanitize_string',
				'kodepos' => 'sanitize_string',
				'telp' => 'sanitize_string',
				'hp' => 'sanitize_string',
				'temp' => 'sanitize_string',
				'tgl' => 'sanitize_string',
				'agama' => 'sanitize_string',
				'wn' => 'sanitize_string',
				'jjng' => 'sanitize_string',
				'nasek' => 'sanitize_string',
				'kotsek' => 'sanitize_string',
				'jur' => 'sanitize_string',
				'nis' => 'sanitize_string',
				'noij' => 'sanitize_string',
				'tempij' => 'sanitize_string',
				'tglij' => 'sanitize_string',
				'thnl' => 'sanitize_string',
				'ibu' => 'sanitize_string',
				'nik' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("mhs.id", $rec_id);;
				$bool = $db->update($tablename, $modeldata);
				$numRows = $db->getRowCount(); //number of affected rows. 0 = no record field updated
				if($bool && $numRows){
					$this->set_flash_msg("Record updated successfully", "success");
					return $this->redirect("mhs");
				}
				else{
					if($db->getLastError()){
						$this->set_page_error();
					}
					elseif(!$numRows){
						//not an error, but no record was updated
						$page_error = "No record updated";
						$this->set_page_error($page_error);
						$this->set_flash_msg($page_error, "warning");
						return	$this->redirect("mhs");
					}
				}
			}
		}
		$db->where("mhs.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Mhs";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("mhs/edit.php", $data);
	}
	/**
     * Update single field
	 * @param $rec_id (select record by table primary key)
	 * @param $formdata array() from $_POST
     * @return array
     */
	function editfield($rec_id = null, $formdata = null){
		$db = $this->GetModel();
		$this->rec_id = $rec_id;
		$tablename = $this->tablename;
		//editable fields
		$fields = $this->fields = array("id","nopend","npm","nama","jk","prodi","rombel","alamat","rt","lurah","camat","kota","kodepos","telp","hp","temp","tgl","agama","wn","jjng","nasek","kotsek","jur","nis","noij","tempij","tglij","thnl","ibu","nik");
		$page_error = null;
		if($formdata){
			$postdata = array();
			$fieldname = $formdata['name'];
			$fieldvalue = $formdata['value'];
			$postdata[$fieldname] = $fieldvalue;
			$postdata = $this->format_request_data($postdata);
			$this->rules_array = array(
				'nopend' => 'required',
				'npm' => 'required',
				'nama' => 'required',
				'jk' => 'required',
				'prodi' => 'required',
				'rombel' => 'required',
				'alamat' => 'required',
				'rt' => 'required',
				'lurah' => 'required',
				'camat' => 'required',
				'kota' => 'required',
				'kodepos' => 'required',
				'telp' => 'required',
				'hp' => 'required',
				'temp' => 'required',
				'tgl' => 'required',
				'agama' => 'required',
				'wn' => 'required',
				'jjng' => 'required',
				'nasek' => 'required',
				'kotsek' => 'required',
				'jur' => 'required',
				'nis' => 'required',
				'noij' => 'required',
				'tempij' => 'required',
				'tglij' => 'required',
				'thnl' => 'required',
				'ibu' => 'required',
				'nik' => 'required',
			);
			$this->sanitize_array = array(
				'nopend' => 'sanitize_string',
				'npm' => 'sanitize_string',
				'nama' => 'sanitize_string',
				'jk' => 'sanitize_string',
				'prodi' => 'sanitize_string',
				'rombel' => 'sanitize_string',
				'alamat' => 'sanitize_string',
				'rt' => 'sanitize_string',
				'lurah' => 'sanitize_string',
				'camat' => 'sanitize_string',
				'kota' => 'sanitize_string',
				'kodepos' => 'sanitize_string',
				'telp' => 'sanitize_string',
				'hp' => 'sanitize_string',
				'temp' => 'sanitize_string',
				'tgl' => 'sanitize_string',
				'agama' => 'sanitize_string',
				'wn' => 'sanitize_string',
				'jjng' => 'sanitize_string',
				'nasek' => 'sanitize_string',
				'kotsek' => 'sanitize_string',
				'jur' => 'sanitize_string',
				'nis' => 'sanitize_string',
				'noij' => 'sanitize_string',
				'tempij' => 'sanitize_string',
				'tglij' => 'sanitize_string',
				'thnl' => 'sanitize_string',
				'ibu' => 'sanitize_string',
				'nik' => 'sanitize_string',
			);
			$this->filter_rules = true; //filter validation rules by excluding fields not in the formdata
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("mhs.id", $rec_id);;
				$bool = $db->update($tablename, $modeldata);
				$numRows = $db->getRowCount();
				if($bool && $numRows){
					return render_json(
						array(
							'num_rows' =>$numRows,
							'rec_id' =>$rec_id,
						)
					);
				}
				else{
					if($db->getLastError()){
						$page_error = $db->getLastError();
					}
					elseif(!$numRows){
						$page_error = "No record updated";
					}
					render_error($page_error);
				}
			}
			else{
				render_error($this->view->page_error);
			}
		}
		return null;
	}
	/**
     * Delete record from the database
	 * Support multi delete by separating record id by comma.
     * @return BaseView
     */
	function delete($rec_id = null){
		Csrf::cross_check();
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$this->rec_id = $rec_id;
		//form multiple delete, split record id separated by comma into array
		$arr_rec_id = array_map('trim', explode(",", $rec_id));
		$db->where("mhs.id", $arr_rec_id, "in");
		$bool = $db->delete($tablename);
		if($bool){
			$this->set_flash_msg("Record deleted successfully", "success");
		}
		elseif($db->getLastError()){
			$page_error = $db->getLastError();
			$this->set_flash_msg($page_error, "danger");
		}
		return	$this->redirect("mhs");
	}
}
