HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /var/www/html/nt/application/controllers/admin/Module.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Module extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('admin/mindustrial','admin/mmodule', 'mcommon'));
		$this->load->helper(array('sms', 'email'));
	}
	
	public function index() {
		// Load the model
		// Fetch industrial parks data
		$data['industrial_parks'] = $this->mindustrial->get_all_parks();
		$data['content'] = 'admin/module/list';
		$this->load->view('admin/layouts/index', $data);
	}

    public function getmoduleData() {
        // Call the model method to fetch module data
        $moduleData = $this->mmodule->getmoduleData();

        // Convert status code to text
        foreach ($moduleData as &$module) {
            $module['status_text'] = ($module['status'] == 1) ? 'Vacant' : 'Active Agreement';
        }

        // Send JSON response
        $this->output->set_content_type('application/json')->set_output(json_encode($moduleData));
    }
        
		public function list() {
		// Get search keyword and status filter from the URL parameters
		$search_keyword = $this->input->get('searchorders');
		$status = $this->input->get('status');
		// Pass the retrieved data and search criteria to the view
		$data['search_keyword'] = $search_keyword;
		$data['status'] = $status;
		// Fetch industrial parks based on search keyword and status filter
		$data['industrial_parks'] = $this->mindustrial->search_industrial_parks($search_keyword, $status);
		$data['content'] = 'admin/industrial_park/list';
		$this->load->view('admin/layouts/index', $data);
	}
	
	public function add() {
 // Retrieve industrial parks from the database where is_active=1
        $data['industrialParks'] = $this->mmodule->get_active_parks();
        $data['uom_list'] = $this->mcommon->getActiveUomList('M');
 		$data['content'] = 'admin/module/add';
		$this->load->view('admin/layouts/index', $data);
    }

    public function saveModuleData() {
        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('industrialPark', 'Industrial Park', 'required');
        $this->form_validation->set_rules('moduleRefNo', 'module / Ref No.', 'required');
        $this->form_validation->set_rules('totalArea', 'Total Area', 'required|numeric');
    
        if ($this->form_validation->run() == FALSE) {
            // Form validation failed, return validation errors or redirect to the form page with error messages
            $this->session->set_flashdata('error', 'There was an error in saving the module data. Please check your inputs.');
            redirect('admin/module/add');
        } else {
            // Form validation passed, proceed to save data into database
    
            // Save module header details
            $moduleHeaderData = array(
                'industrial_park_id' => $this->input->post('industrialPark'),
                'module_ref_no' => $this->input->post('moduleRefNo'),
                'total_area' => $this->input->post('totalArea'),
                'uom_id' => $this->input->post('uom'),
                'description' => $this->input->post('description'),
                'status' => 1, // Assuming default status is 1 (Vacant)
                'created_by' => $this->session->userdata('user_id') // Assuming user ID is stored in session
            );
            $moduleHeaderId = $this->mmodule->saveModule($moduleHeaderData);
            $this->session->set_flashdata('success', 'module data saved successfully!');
            redirect('admin/module');       
         }
    }
    
    public function edit($moduleHeaderId) {        
        // Fetch module data based on module_header_id
        $moduleDetails = $this->mmodule->getmoduleDetails($moduleHeaderId);
        $data['industrialParks'] = $this->mmodule->get_active_parks();
        $data['uom_list'] = $this->mcommon->getActiveUomList('M');
        // Check if module data exists
        if ($moduleDetails) {
            // Load any additional models if needed
            
            // Pass module data to the view
            $data['moduleData'] = $moduleDetails;
            /*echo '<pre>';
            print_r($moduleDetails);
            die;*/
            $data['content'] = 'admin/module/edit'; // Adjust the view path as per your directory structure
            $this->load->view('admin/layouts/index', $data);
        } else {
            redirect('admin/module');       
        }
    }
    
	public function updatemoduleData() {
        $this->load->library('form_validation');
    
        // Validation rules for module header
        $this->form_validation->set_rules('industrialPark', 'Industrial Park', 'required');
        $this->form_validation->set_rules('moduleRefNo', 'module / Ref No.', 'required');
        $this->form_validation->set_rules('totalArea', 'Total Area', 'required|numeric');
    
    
        if ($this->form_validation->run() == FALSE) {
            // Form validation failed, return validation errors or redirect to the form page with error messages
            $this->session->set_flashdata('error', 'There was an error in updating the module data. Please check your inputs.');
            redirect('admin/module/edit/' . $this->input->post('module_id'));
        } else {
            // Form validation passed, proceed to update data in the database
    
            // Update module header details
            $moduleHeaderId = $this->input->post('module_id');
            $moduleHeaderData = array(
                'industrial_park_id' => $this->input->post('industrialPark'),
                'module_ref_no' => $this->input->post('moduleRefNo'),
                'total_area' => $this->input->post('totalArea'),
                'uom_id' => $this->input->post('uom'),
                'description' => $this->input->post('description'),
                'updated_by' => $this->session->userdata('user_id')

            );
            $this->mmodule->updatemodule($moduleHeaderId, $moduleHeaderData);
    
            $this->session->set_flashdata('success', 'module data updated successfully!');
            redirect('admin/module');
        }
    }
    

    public function getmoduleDetails() {
          // Retrieve module_header_id from the GET request
         $moduleHeaderId = $this->input->get('module_id');
        // Call the model method to fetch module details
        $moduleDetails = $this->mmodule->getmoduleDetails($moduleHeaderId);

        // Send the module details as JSON response
        $this->output->set_content_type('application/json')->set_output(json_encode($moduleDetails));
    }
    

}