<?php 
/**
 * Delete_profile_accept_reject Page Controller
 * @category  Controller
 */
class Delete_profile_accept_rejectController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "delete_profile_accept_reject";
	}
	/**
     * 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("delete_profile_accept_reject.id", 
			"delete_profile_accept_reject.profile_name", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"delete_profile_accept_reject.remark", 
			"delete_profile_accept_reject.stage", 
			"delete_profile_accept_reject.status");
		$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 = "(
				delete_profile_accept_reject.id LIKE ? OR 
				delete_profile_accept_reject.userid LIKE ? OR 
				delete_profile_accept_reject.profile_name LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				delete_profile_accept_reject.profile_id LIKE ? OR 
				delete_profile_accept_reject.app_userid LIKE ? OR 
				delete_profile_accept_reject.remark LIKE ? OR 
				delete_profile_accept_reject.stage LIKE ? OR 
				delete_profile_accept_reject.status LIKE ? OR 
				delete_profile.id LIKE ? OR 
				delete_profile.portfolio_id LIKE ? OR 
				delete_profile.userid LIKE ? OR 
				delete_profile.timestamp LIKE ? OR 
				delete_profile.status LIKE ? OR 
				delete_profile.reason 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 ?
			)";
			$search_params = array(
				"%$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 = "delete_profile_accept_reject/search.php";
		}
		$db->join("delete_profile", "delete_profile_accept_reject.profile_id = delete_profile.id", "INNER");
		$db->join("user_info", "delete_profile.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("delete_profile_accept_reject.id", ORDER_TYPE);
		}
		$db->where("delete_profile_accept_reject.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 = "Delete Profile Accept Reject";
		$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("delete_profile_accept_reject/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("delete_profile_accept_reject.id", 
			"delete_profile_accept_reject.userid", 
			"delete_profile_accept_reject.profile_id", 
			"delete_profile_accept_reject.app_userid", 
			"delete_profile_accept_reject.remark", 
			"delete_profile_accept_reject.stage", 
			"delete_profile_accept_reject.status", 
			"delete_profile_accept_reject.profile_name", 
			"delete_profile.id AS delete_profile_id", 
			"delete_profile.portfolio_id AS delete_profile_portfolio_id", 
			"delete_profile.userid AS delete_profile_userid", 
			"delete_profile.timestamp AS delete_profile_timestamp", 
			"delete_profile.status AS delete_profile_status", 
			"delete_profile.reason AS delete_profile_reason", 
			"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");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("delete_profile_accept_reject.id", $rec_id);; //select record based on primary key
		}
		$db->join("delete_profile", "delete_profile_accept_reject.profile_id = delete_profile.id", "INNER ");
		$db->join("user_info", "delete_profile_accept_reject.app_userid = user_info.id", "LEFT ");  
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Delete Profile Accept Reject";
		$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("delete_profile_accept_reject/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("delete_profile_accept_reject");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Delete Profile Accept Reject";
		$this->render_view("delete_profile_accept_reject/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","status","remark","stage");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'status' => 'required',
				'remark' => 'required',
				'stage' => 'required',
			);
			$this->sanitize_array = array(
				'status' => 'sanitize_string',
				'remark' => 'sanitize_string',
				'stage' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			if($this->validated()){
				$db->where("delete_profile_accept_reject.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("delete_profile_accept_reject", "profile_id");
    if($modeldata['status']=="ACCEPT"){
    $db->where("sequence", $modeldata['stage']+1);
    $app_user_id = $db->getValue("autorization", "IFNULL(user,0)");
//query the database and return a single row
$fields = array('portfolio_id');
$db->where("id", $rec_id);
$portfolio_id = $db->getOne("delete_profile", $fields)['portfolio_id'];
    $db->where("id", $portfolio_id);
    $profile = $db->getValue("portfolio", "name");
    if($app_user_id!=0){
    $table_data = array(
    "profile_id" => $rec_id,
    "profile_name" => $profile,
    "app_userid" => "$app_user_id",
    "stage" => $modeldata['stage']+1
    );
    $db->insert("delete_profile_accept_reject", $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("delete_profile", $table_data);
    }else{
//query the database and return a single row
$fields = array('portfolio_id','id');
$db->where("id", $rec_id);
$arr = $db->getOne("delete_profile", $fields);
        $db->where("id", $arr['portfolio_id']);
    $bool = $db->delete("portfolio");
        $db->where("pid", $arr['portfolio_id']);
    $bool = $db->delete("portfolio_configurations");
    //MAKE READY
    $table_data = array(
    "status" => "DELETED AS PER THE PROTOCOL OF AUTHORIZATION." 
    );
    $db->where("id", $rec_id);
    $bool = $db->update("delete_profile", $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" => "DELETE PROFILE REJECTED BY $userfullname($username) " 
    );
    $db->where("id", $rec_id);
    $bool = $db->update("delete_profile", $table_data);
}
		# End of after update statement
					$this->set_flash_msg("Record updated successfully", "success");
					return $this->redirect("delete_profile_accept_reject");
				}
				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("delete_profile_accept_reject");
					}
				}
			}
		}
		$db->where("delete_profile_accept_reject.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Delete Profile Accept Reject";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("delete_profile_accept_reject/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("delete_profile_accept_reject.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("delete_profile_accept_reject");
	}
	/**
     * 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("delete_profile_accept_reject.id", 
			"delete_profile_accept_reject.profile_name", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"delete_profile_accept_reject.remark", 
			"delete_profile_accept_reject.stage", 
			"delete_profile_accept_reject.status");
		$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 = "(
				delete_profile_accept_reject.id LIKE ? OR 
				delete_profile_accept_reject.userid LIKE ? OR 
				delete_profile_accept_reject.profile_id LIKE ? OR 
				delete_profile_accept_reject.profile_name LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				delete_profile_accept_reject.app_userid LIKE ? OR 
				delete_profile_accept_reject.remark LIKE ? OR 
				delete_profile_accept_reject.stage LIKE ? OR 
				delete_profile_accept_reject.status LIKE ? OR 
				delete_profile.id LIKE ? OR 
				delete_profile.portfolio_id LIKE ? OR 
				delete_profile.userid LIKE ? OR 
				delete_profile.timestamp LIKE ? OR 
				delete_profile.status LIKE ? OR 
				delete_profile.reason 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 ?
			)";
			$search_params = array(
				"%$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 = "delete_profile_accept_reject/search.php";
		}
		$db->join("delete_profile", "delete_profile_accept_reject.profile_id = delete_profile.id", "INNER");
		$db->join("user_info", "delete_profile_accept_reject.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("delete_profile_accept_reject.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 = "Delete Profile Accept Reject";
		$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("delete_profile_accept_reject/list2.php", $data); //render the full page
	}
}
