<?php 
/**
 * Accept_reject_profile_reposition Page Controller
 * @category  Controller
 */
class Accept_reject_profile_repositionController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "accept_reject_profile_reposition";
	}
	/**
     * 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("accept_reject_profile_reposition.id", 
			"accept_reject_profile_reposition.rep_id", 
			"accept_reject_profile_reposition.remark", 
			"accept_reject_profile_reposition.timestamp_up", 
			"accept_reject_profile_reposition.app_userid", 
			"accept_reject_profile_reposition.stage", 
			"accept_reject_profile_reposition.status_acc", 
			"user_profile_reposition.status AS user_profile_reposition_status", 
			"user_profile_reposition.effective_month AS user_profile_reposition_effective_month", 
			"user_profile_reposition.effective_year AS user_profile_reposition_effective_year", 
			"user_profile_reposition.timestamp AS user_profile_reposition_timestamp", 
			"user_profile_reposition.old_portfolio AS user_profile_reposition_old_portfolio", 
			"portfolio.name AS portfolio_name", 
			"user_info.user_full_name AS user_info_user_full_name");
		$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 = "(
				accept_reject_profile_reposition.id LIKE ? OR 
				accept_reject_profile_reposition.rep_id LIKE ? OR 
				accept_reject_profile_reposition.remark LIKE ? OR 
				accept_reject_profile_reposition.timestamp_up LIKE ? OR 
				accept_reject_profile_reposition.app_userid LIKE ? OR 
				accept_reject_profile_reposition.stage LIKE ? OR 
				accept_reject_profile_reposition.status_acc LIKE ? OR 
				user_profile_reposition.id LIKE ? OR 
				user_profile_reposition.empid LIKE ? OR 
				user_profile_reposition.status LIKE ? OR 
				user_profile_reposition.effective_month LIKE ? OR 
				user_profile_reposition.effective_year LIKE ? OR 
				user_profile_reposition.userid LIKE ? OR 
				user_profile_reposition.timestamp LIKE ? OR 
				user_profile_reposition.new_portfolio LIKE ? OR 
				user_profile_reposition.old_portfolio LIKE ? OR 
				portfolio.id LIKE ? OR 
				portfolio.name LIKE ? OR 
				portfolio.pid LIKE ? OR 
				user_info.id LIKE ? OR 
				user_info.username LIKE ? OR 
				user_info.password LIKE ? OR 
				user_info.email LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				user_info.user_role_id LIKE ? OR 
				user_info.portfolio_id 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%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "accept_reject_profile_reposition/search.php";
		}
		$db->join("user_profile_reposition", "accept_reject_profile_reposition.rep_id = user_profile_reposition.id", "LEFT");
		$db->join("portfolio", "user_profile_reposition.new_portfolio = portfolio.id", "INNER");
		$db->join("user_info", "user_profile_reposition.userid = user_info.id", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("accept_reject_profile_reposition.id", ORDER_TYPE);
		}
		$db->where("accept_reject_profile_reposition.app_userid='".USER_ID."'");
		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 = "Accept Reject Profile Reposition";
		$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("accept_reject_profile_reposition/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("accept_reject_profile_reposition.id", 
			"accept_reject_profile_reposition.rep_id", 
			"accept_reject_profile_reposition.remark", 
			"accept_reject_profile_reposition.timestamp_up", 
			"accept_reject_profile_reposition.app_userid", 
			"accept_reject_profile_reposition.stage", 
			"accept_reject_profile_reposition.status_acc", 
			"user_profile_reposition.id AS user_profile_reposition_id", 
			"user_profile_reposition.empid AS user_profile_reposition_empid", 
			"user_profile_reposition.status AS user_profile_reposition_status", 
			"user_profile_reposition.effective_month AS user_profile_reposition_effective_month", 
			"user_profile_reposition.effective_year AS user_profile_reposition_effective_year", 
			"user_profile_reposition.userid AS user_profile_reposition_userid", 
			"user_profile_reposition.timestamp AS user_profile_reposition_timestamp", 
			"user_profile_reposition.new_portfolio AS user_profile_reposition_new_portfolio", 
			"user_profile_reposition.old_portfolio AS user_profile_reposition_old_portfolio", 
			"portfolio.id AS portfolio_id", 
			"portfolio.name AS portfolio_name", 
			"portfolio.pid AS portfolio_pid", 
			"user_info.id AS user_info_id", 
			"user_info.username AS user_info_username", 
			"user_info.password AS user_info_password", 
			"user_info.email AS user_info_email", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"user_info.user_role_id AS user_info_user_role_id", 
			"user_info.portfolio_id AS user_info_portfolio_id", 
			"user_info.id AS user_info_id", 
			"user_info.username AS user_info_username", 
			"user_info.password AS user_info_password", 
			"user_info.email AS user_info_email", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"user_info.user_role_id AS user_info_user_role_id", 
			"user_info.portfolio_id AS user_info_portfolio_id");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("accept_reject_profile_reposition.id", $rec_id);; //select record based on primary key
		}
		$db->join("user_info", "accept_reject_profile_reposition.app_userid = user_info.id", "LEFT ");  
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Accept Reject Profile Reposition";
		$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("accept_reject_profile_reposition/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();
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
			);
			$this->sanitize_array = array(
			);
			$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("accept_reject_profile_reposition");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Accept Reject Profile Reposition";
		$this->render_view("accept_reject_profile_reposition/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","remark","status_acc","stage");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'remark' => 'required',
				'status_acc' => 'required',
				'stage' => 'required',
			);
			$this->sanitize_array = array(
				'remark' => 'sanitize_string',
				'status_acc' => 'sanitize_string',
				'stage' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("accept_reject_profile_reposition.id", $rec_id);;
				$bool = $db->update($tablename, $modeldata);
				$numRows = $db->getRowCount(); //number of affected rows. 0 = no record field updated
				if($bool && $numRows){
		# Statement to execute after adding record
$db->where("id", $rec_id);
    $rec_id = $db->getValue("accept_reject_profile_reposition", "rep_id");
if($modeldata['status_acc']=="ACCEPT"){
    $db->where("sequence", $modeldata['stage']+1);
    $app_user_id = $db->getValue("autorization", "IFNULL(user,0)");
    if($app_user_id!=0){
    $table_data = array(
    "rep_id" => $rec_id,
    "app_userid" => "$app_user_id",
    "stage" => $modeldata['stage']+1
    );
    $db->insert("accept_reject_profile_reposition", $table_data);
    $db->where("id", $app_user_id);
    $userfullname = $db->getValue("user_info", "user_full_name");
      $db->where("id", $app_user_id);
    $username = $db->getValue("user_info", "username");
    $table_data = array(
    "status" => "SENT TO $userfullname($username) FOR APPROVAL" 
    );
    $db->where("id", $rec_id);
    $bool = $db->update("user_profile_reposition", $table_data);
    }else{
        $table_data = array(
    "status" => "READY MIGRATION" 
    );
    $db->where("id", $rec_id);
    $bool = $db->update("user_profile_reposition", $table_data);
    }
}else{
    $db->where("id", USER_ID);
    $userfullname = $db->getValue("user_info", "user_full_name");
      $db->where("id", USER_ID);
    $username = $db->getValue("user_info", "username");
    $table_data = array(
    "status" => "APPROVAL REJECTED BY $userfullname($username) " 
    );
    $db->where("id", $rec_id);
    $bool = $db->update("user_profile_reposition", $table_data);
}
		# End of after update statement
					$this->set_flash_msg("Record updated successfully", "success");
					return $this->redirect("accept_reject_profile_reposition");
				}
				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("accept_reject_profile_reposition");
					}
				}
			}
		}
		$db->where("accept_reject_profile_reposition.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Accept Reject Profile Reposition";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("accept_reject_profile_reposition/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("accept_reject_profile_reposition.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("accept_reject_profile_reposition");
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function list2($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("accept_reject_profile_reposition.id", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"accept_reject_profile_reposition.timestamp_up", 
			"accept_reject_profile_reposition.remark", 
			"accept_reject_profile_reposition.stage", 
			"accept_reject_profile_reposition.status_acc");
		$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 = "(
				accept_reject_profile_reposition.id LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				accept_reject_profile_reposition.rep_id LIKE ? OR 
				accept_reject_profile_reposition.timestamp_up LIKE ? OR 
				accept_reject_profile_reposition.app_userid LIKE ? OR 
				accept_reject_profile_reposition.remark LIKE ? OR 
				accept_reject_profile_reposition.stage LIKE ? OR 
				accept_reject_profile_reposition.status_acc LIKE ? OR 
				user_info.id LIKE ? OR 
				user_info.username LIKE ? OR 
				user_info.password LIKE ? OR 
				user_info.email LIKE ? OR 
				user_info.user_role_id LIKE ? OR 
				user_info.portfolio_id LIKE ?
			)";
			$search_params = array(
				"%$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 = "accept_reject_profile_reposition/search.php";
		}
		$db->join("user_info", "accept_reject_profile_reposition.app_userid = user_info.id", "LEFT");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("accept_reject_profile_reposition.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 = "Accept Reject Profile Reposition";
		$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("accept_reject_profile_reposition/list2.php", $data); //render the full page
	}
}
