<?php 
/**
 * Portfolio_configurations Page Controller
 * @category  Controller
 */
class Portfolio_configurationsController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "portfolio_configurations";
	}
	/**
     * 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("portfolio_configurations.id", 
			"portfolio.name AS portfolio_name", 
			"portfolio_configurations.pid", 
			"portfolio_configurations.col_name", 
			"portfolio_configurations.value", 
			"portfolio_configurations.allowance_or_deductions", 
			"portfolio_configurations.calculation_type", 
			"portfolio_configurations.prct_value_1", 
			"portfolio_configurations.prct_value_2", 
			"portfolio_configurations.prct_value_operation", 
			"portfolio_configurations.position", 
			"portfolio_configurations.show_col", 
			"portfolio_configurations.minimum_value");
		$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 = "(
				portfolio_configurations.id LIKE ? OR 
				portfolio.name LIKE ? OR 
				portfolio_configurations.pid LIKE ? OR 
				portfolio_configurations.col_name LIKE ? OR 
				portfolio_configurations.value LIKE ? OR 
				portfolio_configurations.allowance_or_deductions LIKE ? OR 
				portfolio_configurations.calculation_type LIKE ? OR 
				portfolio_configurations.prct_value_1 LIKE ? OR 
				portfolio_configurations.prct_value_2 LIKE ? OR 
				portfolio_configurations.prct_value_operation LIKE ? OR 
				portfolio.id LIKE ? OR 
				portfolio.pid LIKE ? OR 
				portfolio_configurations.position LIKE ? OR 
				portfolio_configurations.show_col LIKE ? OR 
				portfolio_configurations.minimum_value LIKE ?
			)";
			$search_params = array(
				"%$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 = "portfolio_configurations/search.php";
		}
		$db->join("portfolio", "portfolio_configurations.pid = portfolio.id", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("portfolio_configurations.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		if(!empty($request->portfolio_configurations_pid)){
			$val = $request->portfolio_configurations_pid;
			$db->where("portfolio_configurations.pid", $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 = "Portfolio Configurations";
		$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("portfolio_configurations/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("portfolio_configurations.id", 
			"portfolio_configurations.pid", 
			"portfolio_configurations.col_name", 
			"portfolio_configurations.value", 
			"portfolio_configurations.allowance_or_deductions", 
			"portfolio_configurations.calculation_type", 
			"portfolio_configurations.prct_value_1", 
			"portfolio_configurations.prct_value_2", 
			"portfolio_configurations.prct_value_operation", 
			"portfolio.id AS portfolio_id", 
			"portfolio.name AS portfolio_name", 
			"portfolio.pid AS portfolio_pid", 
			"portfolio_configurations.position", 
			"portfolio_configurations.show_col", 
			"portfolio_configurations.minimum_value");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("portfolio_configurations.id", $rec_id);; //select record based on primary key
		}
		$db->join("portfolio", "portfolio_configurations.pid = portfolio.id", "INNER ");  
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Portfolio Configurations";
		$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("portfolio_configurations/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("pid","col_name","value","allowance_or_deductions","calculation_type","prct_value_1","prct_value_2","prct_value_operation","position","show_col","minimum_value");
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'pid' => 'required',
				'col_name' => 'required',
				'value' => 'required',
				'allowance_or_deductions' => 'required',
				'calculation_type' => 'required',
				'position' => 'required|numeric',
				'show_col' => 'required',
				'minimum_value' => 'numeric',
			);
			$this->sanitize_array = array(
				'pid' => 'sanitize_string',
				'col_name' => 'sanitize_string',
				'value' => 'sanitize_string',
				'allowance_or_deductions' => 'sanitize_string',
				'calculation_type' => 'sanitize_string',
				'prct_value_1' => 'sanitize_string',
				'prct_value_2' => 'sanitize_string',
				'prct_value_operation' => 'sanitize_string',
				'position' => 'sanitize_string',
				'show_col' => 'sanitize_string',
				'minimum_value' => 'sanitize_string',
			);
			$this->filter_vals = true; //set whether to remove empty fields
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
		# Statement to execute before adding record
//query the database and return a single field value
$db->where("pid", $modeldata['pid']);
$db->where("col_name", $modeldata['col_name']);
$value = $db->getValue("portfolio_configurations", "col_name");
if($value==$modeldata['col_name']){
            $this->set_flash_msg("Same Col Name Exits", "danger");
            return  $this->redirect("portfolio_configurations");
        exit();
}
		# End of before add statement
				$rec_id = $this->rec_id = $db->insert($tablename, $modeldata);
				if($rec_id){
					$this->set_flash_msg("Record added successfully", "success");
					return	$this->redirect("portfolio_configurations");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Portfolio Configurations";
		$this->render_view("portfolio_configurations/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","pid","col_name","value","allowance_or_deductions","calculation_type","prct_value_1","prct_value_2","prct_value_operation","position","show_col","minimum_value");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'pid' => 'required',
				'col_name' => 'required',
				'value' => 'required',
				'allowance_or_deductions' => 'required',
				'calculation_type' => 'required',
				'position' => 'required|numeric',
				'show_col' => 'required',
				'minimum_value' => 'numeric',
			);
			$this->sanitize_array = array(
				'pid' => 'sanitize_string',
				'col_name' => 'sanitize_string',
				'value' => 'sanitize_string',
				'allowance_or_deductions' => 'sanitize_string',
				'calculation_type' => 'sanitize_string',
				'prct_value_1' => 'sanitize_string',
				'prct_value_2' => 'sanitize_string',
				'prct_value_operation' => 'sanitize_string',
				'position' => 'sanitize_string',
				'show_col' => 'sanitize_string',
				'minimum_value' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("portfolio_configurations.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("portfolio_configurations");
				}
				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("portfolio_configurations");
					}
				}
			}
		}
		$db->where("portfolio_configurations.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Portfolio Configurations";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("portfolio_configurations/edit.php", $data);
	}
	/**
     * 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("portfolio_configurations.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("portfolio_configurations");
	}
	/**
     * Insert new record to the database table
	 * @param $formdata array() from $_POST
     * @return BaseView
     */
	function add2($formdata = null){
		if($formdata){
			$db = $this->GetModel();
			$tablename = $this->tablename;
			$request = $this->request;
			//fillable fields
			$fields = $this->fields = array("pid","col_name","value","allowance_or_deductions","calculation_type","prct_value_1","prct_value_2","prct_value_operation","position","show_col","minimum_value");
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'pid' => 'required',
				'col_name' => 'required',
				'value' => 'required|numeric',
				'allowance_or_deductions' => 'required',
				'calculation_type' => 'required',
				'prct_value_1' => 'required',
				'prct_value_2' => 'required',
				'prct_value_operation' => 'required',
				'position' => 'required|numeric',
				'show_col' => 'required',
				'minimum_value' => 'required',
			);
			$this->sanitize_array = array(
				'pid' => 'sanitize_string',
				'col_name' => 'sanitize_string',
				'value' => 'sanitize_string',
				'allowance_or_deductions' => 'sanitize_string',
				'calculation_type' => 'sanitize_string',
				'prct_value_1' => 'sanitize_string',
				'prct_value_2' => 'sanitize_string',
				'prct_value_operation' => 'sanitize_string',
				'position' => 'sanitize_string',
				'show_col' => 'sanitize_string',
				'minimum_value' => '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("portfolio_configurations");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Portfolio Configurations";
		$this->render_view("portfolio_configurations/add2.php");
	}
	/**
     * Update table record with formdata
	 * @param $rec_id (select record by table primary key)
	 * @param $formdata array() from $_POST
     * @return array
     */
	function edit2($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","pid","col_name","value","allowance_or_deductions","calculation_type","prct_value_1","prct_value_2","prct_value_operation","position");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'pid' => 'required',
				'col_name' => 'required',
				'value' => 'required',
				'allowance_or_deductions' => 'required',
				'calculation_type' => 'required',
				'position' => 'required|numeric',
			);
			$this->sanitize_array = array(
				'pid' => 'sanitize_string',
				'col_name' => 'sanitize_string',
				'value' => 'sanitize_string',
				'allowance_or_deductions' => 'sanitize_string',
				'calculation_type' => 'sanitize_string',
				'prct_value_1' => 'sanitize_string',
				'prct_value_2' => 'sanitize_string',
				'prct_value_operation' => 'sanitize_string',
				'position' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("portfolio_configurations.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("portfolio_configurations");
				}
				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("portfolio_configurations");
					}
				}
			}
		}
		$db->where("portfolio_configurations.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Portfolio Configurations";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("portfolio_configurations/edit2.php", $data);
	}
}
