<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Super_Admin_Message extends TT_admin_login {
    public function __construct() {
        parent::__construct();
    }

     public function super_admin_special_send_email() {
        $data = array();
        $data['active_menu'] = 'email';
        $data['active_sub_menu'] = 'send_email';
        $data['main_content'] = $this->load->view('admin/message/special_send_email', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    public function super_admin_send_email(){
        $data = array();
        $data['active'] = 'send_email';
        $data['person_id']= $this->session->userdata("person");
        $decode_person_id = str_replace("%F2", "/", $data['person_id']);
        $person_id1 = $this->encrypt->decode($decode_person_id);
        if (!$person_id1) {
            redirect("super-admin/all-user");
        }
        $person_id_array=explode(",",$person_id1);
        if ($person_id_array[count($person_id_array) - 1] == '') {
                    array_pop($person_id_array);
                }
        $data['person_sup']=$this->super_admin_message_model->select_person_email($person_id_array);
        
        foreach($data['person_sup'] as $email_send){
            $this->send_email_to_user_for_security_reset_password($email_send->person_id);
        }

        redirect('super-admin/all-user');
    }
    
    function send_email_to_user_for_security_reset_password($person_id) {
        $user_info = $this->super_admin_model->get_single_user_info($person_id); 
        $this->sign_up_model->password_recovery_request_status($user_info->person_id);
        
        $p_id = $this->encryption->encrypt($person_id);
        $p_id = str_replace("%F2", "/", $p_id);
        
        $mdata = array();
        $mdata['person_id'] = $user_info->person_id;
        $mdata['person_name'] = $user_info->person_first_name;
        $mdata['from_address'] = 'no-replay@talhatraining.com';
        $mdata['company_name'] = 'Talha Training';
        $mdata['admin_name'] = 'Talha Training';
        $mdata['to_address'] = $user_info->person_email;
        $mdata['subject'] = 'For security purposes, your TalhaTraining account has been locked';
        $mdata['site_link'] = base_url() . 'recover-password/' . $p_id;
        $this->mailer_model->send_email($mdata, 'security_reset_password_link');
    }
    
    
    
    public function encypt_data(){
          $id=$this->input->post("person_id");
          $message_id= $this->encrypt->encode($id);
          echo str_replace("/","%F2", $message_id);
          
    }
    public function super_admin_send_email_temp($id){
          $sdata = array();
         $sdata['person'] = $id;
        $this->session->set_userdata($sdata);
        redirect("super-admin-send-email");
         
    }
    public function view_email() {
        $data = array();
        $data['active_menu'] = 'email';
        $data['active_sub_menu'] = 'view_email';
        $data['main_content'] = $this->load->view('admin/message/view_email', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    public function view_user_email() {
        $data = array();
        $data['active_menu'] = 'email';
        $data['active_sub_menu'] = 'view_user_email';
        $data['main_content'] = $this->load->view('admin/message/view_user_email', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    public function view_person_email_address() {
        $data = array();
        $data['active'] = 'view_email';
        $data['main_content'] = $this->load->view('admin/message/view_all_person_email', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    public function view_course_info() {
        $data = array();
        $data['active'] = 'view_email';
        $data['main_content'] = $this->load->view('admin/message/view_course_info', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    public function view_group_info() {
        $data = array();
        $data['active'] = 'view_email';
        $data['main_content'] = $this->load->view('admin/message/view_group_info', $data, true);
        $this->load->view('admin/admin_master',$data);
    }
    //message send to reciever start
    function save_backend_message(){
        
    }
     public function cancel_backened_upload() {
        $backend_message_id = $this->input->post("backend_message_id");
        $hidden_uploaded_file_path = $this->input->post("hidden_uploaded_file_path");
        $message_attachmen_id = $this->input->post("message_attachmen_id");

        if (unlink($hidden_uploaded_file_path)) {

            $this->super_admin_message_model->cancel_upload_file($backend_message_id, $message_attachmen_id);
        }
    }
    function save_message_file_backend(){
         $user_id = $this->session->userdata('person_id');
        $profile_url = $this->post_model->get_profile_url($user_id);
        $message_id = $this->input->post("backend_message_id");
        // echo $message_id;
        $total_size = $this->input->post("total_size");

        if ($message_id) {
            $fileName = $_FILES["message_files"]["name"]; // The file name
            if ($fileName) {
                $fileTmpLoc = $_FILES["message_files"]["tmp_name"];
                if (file_get_contents($fileTmpLoc)) { // File in the PHP tmp folder
                    $fileType = $_FILES["message_files"]["type"]; // The type of file it is
                    $fileSize = $_FILES["message_files"]["size"]; // File size in bytes
                    $fileErrorMsg = $_FILES["message_files"]["error"]; // 0 for false... and 1 for true
                    $new_file_name = $profile_url->url . '-' . date('h-i-s-a-m-d-Y', time());

                    if (!$fileTmpLoc) { // if file not chosen
                        echo "ERROR: Please browse for a file before clicking the upload button.";
                    } else {
                        if ($total_size < 26214400) {
                            $folder = 'content-library/' . $profile_url->url . '/assignment-file/';

                            $target_file = $folder . $fileName;

                            if (move_uploaded_file($fileTmpLoc, $target_file)) {
                                $file_explode = explode('.', $fileName);
                                $file_extention = $file_explode[count($file_explode) - 1];

                                rename("$target_file", "$folder$new_file_name.$file_extention");
                                $file_name = "$folder$new_file_name.$file_extention";
                                $new_file_path = "assignment-file/" . "$new_file_name.$file_extention";
                                $message_attachment_tbl_id = $this->super_admin_message_model->save_sender_draft_attachment_value_after_message_body($message_id, $new_file_path);

                                echo $message_attachment_tbl_id . "," . $file_name . "," . $fileName;
                            }
                        } else {
                            echo 'File size must less then 25 Mb';
                        }
                    }
                } else {
                    echo "null";
                }
            }
        } else if ($message_id == '') {
            $message_generate_id = $this->super_admin_message_model->for_only_attachment_generate_message_id($user_id);

            $fileName = $_FILES["message_files"]["name"];
            if ($fileName) {
                // echo $fileName;

                $fileTmpLoc = $_FILES["message_files"]["tmp_name"]; // File in the PHP tmp folder
                if (file_get_contents($fileTmpLoc)) {
                    $fileType = $_FILES["message_files"]["type"]; // The type of file it is
                    $fileSize = $_FILES["message_files"]["size"]; // File size in bytes
                    $fileErrorMsg = $_FILES["message_files"]["error"]; // 0 for false... and 1 for true
                    $new_file_name = $profile_url->url . '-' . date('h-i-s-a-m-d-Y', time());

                    if (!$fileTmpLoc) { // if file not chosen
                        echo "ERROR: Please browse for a file before clicking the upload button.";
                    } else {
                        if ($total_size < 26214400) {
                            $folder = 'content-library/' . $profile_url->url . '/assignment-file/';

                            $target_file = $folder . $fileName;

                            if (move_uploaded_file($fileTmpLoc, $target_file)) {

                                $file_explode = explode('.', $fileName);
                                $file_extention = $file_explode[count($file_explode) - 1];

                                rename("$target_file", "$folder$new_file_name.$file_extention");
                                // $sData['file_path'] = "$folder$new_file_name.$file_extention";
                                // $this->session->set_userdata($sData);
                                $file_name = "$folder$new_file_name.$file_extention";
                                $new_file_path = "assignment-file/" . "$new_file_name.$file_extention";
                                $message_attachment_tbl_id = $this->super_admin_message_model->save_sender_draft_attachment_value_after_message_body($message_generate_id, $new_file_path);
                                echo $message_attachment_tbl_id . "," . $file_name . "," . $fileName;
                            }
                        } else {
                            echo 'File size must less then 25 Mb';
                        }
                    }
                } else {
                    echo "null";
                }
            }
        }        
        
    }

}
