<?php
	class Admin_modules extends Admin_Controller{

		public function __construct(){
			parent::__construct();
			$this->load->model('hc_modules_model', 'modules');
		}

		public function index(){
			$this->data["meta_title"] = "Modules";
			$this->data["page_name"] = "modules";
			$this->data["access_right"] = "view_right";
			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$this->data['subview'] = $this->config->item('admin_folder').'/access_denied';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}else{
				$this->data['subview'] = $this->config->item('admin_folder').'/admin_modules/index';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}
		}


		public function ajax_load_data(){
			if(isset($_POST) && !empty($_POST)){

				$search_params = array();
				$like_params = array();

				if(isset($_POST['search']['value']) && !empty($_POST['search']['value'])){
					$like_params = array(
						"hc_modules.module_group_name" => $_POST['search']['value'],
						"hc_modules.module_name" => $_POST['search']['value'],
						"hc_modules.module_unique_name" => $_POST['search']['value']
					);

					$this->db->group_start();
					foreach ($like_params as $key => $value) {
						$this->db->or_like($key, $value);
					}
					$this->db->group_end();
		        }

		        $search_params["is_active != "] = 2;
				$this->db->select("hc_modules.*");
				$this->db->where($search_params);
				$this->db->limit($_POST['length'], $_POST['start']);
				$result = $this->modules->get();

				$data = array();
	        	$no = $_POST['start'];

				foreach ($result as $key => $value) {
					$no++;
		            $row = array();
		            
		            $row[] = "<strong>".$no.".</strong>";
		            $row[] = $value->module_group_name;
		            $row[] = $value->module_name;
		            $row[] = $value->module_unique_name;

		            $a = "";
		            $a = btn_edit(base_url().$this->config->item('admin_folder').'/admin_modules/edit/'.$value->id);
	            	$a .= ' '.btn_delete($this->config->item('admin_folder').'/admin_modules/delete/'.$value->id);
	            	
	            	
	            	$row[] = $a;

		            $data[] = $row;
				}

				$output = array(
	                "draw" => $_POST['draw'],
	                "recordsTotal" => $this->modules->get_count($search_params),
	                "recordsFiltered" => $this->modules->get_search_count($search_params, $like_params),
	                "data" => $data,
	        	);
		        //output to json format
		        echo json_encode($output);

			}
		}

		public function edit($id = NULL){
			if($id){
				$this->data['modules'] = $this->modules->get_by(array("hc_modules.id" => $id), TRUE);
				$this->data["meta_title"] = "Edit module";

				//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Edit module";
				$logs_data["action_identifier"] = "modules_logs";

				$this->data["access_right"] = "edit_right";

			}else{
				$this->data['modules'] = $this->modules->get_new();
				$this->data["meta_title"] = "Add module";

				//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Add module";
				$logs_data["action_identifier"] = "modules_logs";

				$this->data["access_right"] = "create_right";
			}

			
			$this->data["page_name"] = "modules";
			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$this->data['subview'] = $this->config->item('admin_folder').'/access_denied';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}else{

				if(isset($_POST) && !empty($_POST)){
					$this->load->library('form_validation');
					// Set up the form
					$rules = $this->modules->modules_rules;
					
					$this->form_validation->set_rules($rules);

					if($this->form_validation->run() === TRUE){
						$data = $this->modules->array_from_post(array('module_group_name', 'module_name', 'module_page_name', 'module_unique_name', 'icon'));


						if(!$id){
							$data["created_at"] = Date('Y-m-d H:i:s');
							$data["created_by"] = isset($_SESSION["id"]) ? $_SESSION["id"] : 0;
						}

						$data["updated_at"] = Date('Y-m-d H:i:s');
						$data["updated_by"] = isset($_SESSION["id"]) ? $_SESSION["id"] : 0;

						$logs_data["request"] = json_encode($data, TRUE);

						$result_id = $this->modules->save($data, $id);
						$returnArr = array();

						if($result_id){

							$returnArr["status"] = TRUE;
							$returnArr["message"] = "Details saved successfully.";
							if($id){
								$returnArr["redirect_url"] = base_url().$this->config->item('admin_folder').'/admin_modules/index';
							}else{
								$returnArr["redirect_url"] = base_url().$this->config->item('admin_folder').'/admin_modules/edit/'.$result_id;
							}

						}else{
							$returnArr["status"] = FALSE;
							$returnArr["message"] = "Failed to perform this action.";
						}

						//--------------------------------------------Save Activity Logs-----------------------------
						$logs_data["response"] = json_encode($returnArr, TRUE);
						$log_id = $this->logs->add_log($logs_data);

	    				echo json_encode($returnArr);
		            	exit;

					}else{
						$returnArr["status"] = false;
						$returnArr["message"] = "Validation Failed.";
						$returnArr["errors"] = validation_errors();

						//--------------------------------------------Save Activity Logs-----------------------------
						$logs_data["response"] = json_encode($returnArr, TRUE);
						$log_id = $this->logs->add_log($logs_data);

		    			echo json_encode($returnArr);
		            	exit;
					}
				}else{

					$this->data["module_list"] = $this->modules->get();

					//Get the list of groups
					$this->db->group_by("module_group_name");
					$this->data["groups"] = $this->modules->get();

					$this->data['subview'] = $this->config->item('admin_folder').'/admin_modules/edit';
					$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
				}
			}
		}

		public function delete($id){

        	$returnArr = array();

        	$this->data["page_name"] = "modules";
			$this->data["access_right"] = "delete_right";
			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$returnArr["status"] = FALSE;
				$returnArr["message"] = "Access denied!";

				echo json_encode($returnArr, TRUE);
			}else{

	        	//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Delete Module";
				$logs_data["action_identifier"] = "module_logs";

	        	if($id){
	        		$update_val["is_active"] = 2;
					$result = $this->modules->save($update_val,$id);

					if($result){
						$returnArr["status"] = TRUE;
						$returnArr["message"] = "Row deleted successfully.";
						$returnArr["redirect"] = base_url().$this->config->item('admin_folder').'/admin_modules/index';
					}else{

						$returnArr["status"] = FALSE;
						$returnArr["message"] = "Failed to delete row.";
						$returnArr["redirect"] = base_url().$this->config->item('admin_folder').'/admin_modules/index';
					}
				}else{

			    	$returnArr["status"] = FALSE;
					$returnArr["message"] = "Validation Error";
					$returnArr["errors"] = "ID is required";
					
				}

				//--------------------------------------------Save Activity Logs-----------------------------
				$logs_data["response"] = json_encode($returnArr, TRUE);
				$log_id = $this->logs->add_log($logs_data);
				
				echo json_encode($returnArr, TRUE);
			}
		}
	}
?>