<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Message_Model extends CI_Model {

    public function get_reciever_person_id($email_address) {
        $this->db->select('person_id');
        $this->db->from('tbl_person');
        $this->db->where('person_email', $email_address);
        $query_result = $this->db->get();
        $result = $query_result->row();
        if ($result) {
            return $result->person_id;
        }
    }

    public function check_person_linked($data) {

        $sql = "SELECT * FROM tbl_person_link WHERE (person_id='$data' OR requested_person_id='$data') AND add_link_request_status=1 ";
        $result = $this->db->query($sql)->result();
//      echo '<pre>';
        // print_r($result);
        // exit();

        return $result;
    }

    public function recieve_email_by_person_id($check_person_id, $requested_person_id) {
        $check_person = implode(",", $check_person_id);

        // print_r($check_person);

        $check_requested_person_id = implode(",", $requested_person_id);
        //print_r($check_requested_person_id);
        $sql = "SELECT * FROM tbl_person WHERE (person_id IN ($check_person)) OR (person_id IN ($check_requested_person_id))";
        $result = $this->db->query($sql)->result();
//     echo'<pre>';
//     print_r($result);
//     exit();
        return $result;

        //}
    }

    public function save_message_body_draft_value($message_id, $message_body, $user_id) {
        $data['sender_id'] = $user_id;

        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['message_status'] = 4;
        $data['message_datetime'] = $message_datetime;
        $data['message_body'] = $message_body;
        if ($message_id == "") {

            $this->db->insert('tbl_message', $data);
            return $this->db->insert_id();
        } else {

            $this->db->set('message_body', $message_body);
            $this->db->where('message_id', $message_id);
            $this->db->where('sender_id', $user_id);
            $this->db->update('tbl_message');
            return $message_id;
        }
    }

    public function for_only_attachment_generate_message_id($user_id) {
        $data['sender_id'] = $user_id;

        $data['message_status'] = 4;        
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['message_datetime'] = $message_datetime;        
        $this->db->insert('tbl_message', $data);
        $last_id = $this->db->insert_id();
        return $last_id;
    }

    public function save_sender_draft_attachment_value_after_message_body($message_id, $file_name) {


        $data['message_sender_attachment'] = $file_name;

        $data['message_id'] = $message_id;
        $data['message_attachment_original_name'] = $this->input->post('message_attachment_original_name', TRUE);
        //$data['message_id'] = $mesage_tbl_id;
        $data['message_attachment_type'] = 1;
        $this->db->insert('tbl_message_attachment', $data);
        $msg_attach_id = $this->db->insert_id();
        return $message_id . "," . $msg_attach_id;
    }

//    
//    
//    public function save_sender_draft_attachment_value($mesage_tbl_id, $file_name) {
//        $data['message_sender_attachment'] = $file_name;
//        $data['message_id']=$this->input->post('message_id', TRUE);
//        $data['message_attachment_original_name'] = $this->input->post('message_attachment_original_name', TRUE);
//        $data['message_id'] = $mesage_tbl_id;
//        $this->db->insert('tbl_message_attachment', $data);
//        $last_id = $this->db->insert_id();
//        return $last_id;
//    }



    public function insert_email_info_into_table_by_message_id($message_id, $user_id, $receiver_id_array) {
        
        $message_datetime = date('Y/m/d H:i:s', (time()));
       
        $this->db->set('message_status', 4);
        $this->db->set('message_datetime', $message_datetime);
        $this->db->set('sender_id', $user_id);
        $this->db->set('receiver_id', $receiver_id_array);
        $this->db->where('message_id', $message_id);
        $this->db->where('sender_id', $user_id);
        $this->db->update('tbl_message');
        return $message_id;
    }

    public function insert_email_info_into_table_without_message_id($user_id, $receiver_id) {

        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['sender_id'] = $user_id;
        $data['receiver_id'] = $receiver_id; //[$i];
        $data['message_datetime'] = $message_datetime;
        $data['message_status'] = 4;
        $this->db->insert('tbl_message', $data);
        $last_id = $this->db->insert_id();
        return $last_id;
    }

    public function select_all_message_from_tbl_message_attachment($message_id) {
        $sql = "SELECT * FROM  	tbl_message_attachment WHERE message_id='$message_id' ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function delete_draft_message_attachemnt($message_attachmen_id, $message_id) {
        $sql = "DELETE FROM tbl_message_attachment WHERE  message_attachmen_id='$message_attachmen_id'  AND message_id='$message_id'  ";
        $this->db->query($sql);
    }

    public function delete_draft_message($message_id, $message_id_array) {
        if (count($message_id_array) > 1) {
            $sql = "DELETE FROM tbl_message WHERE  message_id IN ($message_id) AND  message_status=4 ";
            $this->db->query($sql);
        } else if (count($message_id_array) == 1) {
            $sql = "DELETE FROM tbl_message WHERE  message_id ='$message_id' AND  message_status=4 ";
            $result = $this->db->query($sql);
        }
    }

    public function select_all_person() {
        $sql = "SELECT * FROM tbl_person";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_all_data_from_message_tbl_for_sender($msg_id) {


        $user_id = $this->session->userdata('user_id');
        // echo $message_id;
        $m_id = explode(",", $msg_id);
        if (count($m_id) > 1) {
            $sql = "SELECT * FROM tbl_message WHERE message_id IN($msg_id) AND sender_id='$user_id '  AND message_status=1  ORDER BY message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($m_id) == 1) {
            $sql = "SELECT * FROM tbl_message WHERE message_id='$msg_id' AND   sender_id=' $user_id '  AND message_status=1  ORDER BY message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public $id_max_all_sent = '';

    public function select_sent_msg($messgae_id) {

        $user_id = $this->session->userdata('user_id');
        $preview_rcv_reply_message = $this->select_msg_for_sent_reply($messgae_id);
        if ($preview_rcv_reply_message) {
            $id = explode(",", $preview_rcv_reply_message);


            if ($id[count($id) - 1] == '') {
                array_pop($id);
            }

            $id1 = min($id);

            $preview_rcv_reply_message_1 = $this->message_model->select_msg_for_reply_message($id1);
            $id_all = explode(',', $preview_rcv_reply_message_1);

            if ($id_all[count($id_all) - 1] == '') {
                array_pop($id_all);
            }
            for ($i = 0; $i <= count($id_all); $i++) {
                $id_max = max($id_all);

                $result2 = $this->select_rcv_id_exit_or_not($id_max);


                if ($result2) {

                    $id_2 = explode(",", $this->id_max_all_sent);

                    if (!in_array($id_max, $id_2)) {
                        $sql1 = "SELECT * FROM tbl_message WHERE message_id = '$id_max' AND ( sender_id = '$user_id' )  AND message_status=1  ORDER BY message_datetime DESC";
                        $result1 = $this->db->query($sql1)->row();
                        $this->id_max_all_sent = $id_max . ",";

                        return $result1;
                    }
                } else {
                    if (($key = array_search($id_max, $id_all)) !== false) {
                        unset($id_all[$key]);
                    }
                    $id_max = max($id_all);
                }
            }
        } else {

            $sql1 = "SELECT * FROM tbl_message WHERE message_id = '$messgae_id' AND  (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%' OR sender_id = '$user_id' ) AND message_status=1  ORDER BY message_datetime DESC";
            $result1 = $this->db->query($sql1)->row();

            return $result1;
        }
    }

    function select_sent_id_exit_or_not($id_max) {
        $user_id = $this->session->userdata('user_id');
        $sql2 = "SELECT * FROM tbl_message WHERE message_id = '$id_max' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%' OR sender_id = '$user_id' ) AND message_status=1  ORDER BY message_datetime DESC";
        $result2 = $this->db->query($sql2)->row();
        if ($result2) {
            return $id_max;
        }
    }

    public function select_msg_for_sent_reply($message_id1) {
        $id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE  message_id='$message_id1' OR message_reply_parent_id='$message_id1'  ";
        $result = $this->db->query($sql)->row();
        if ($result) {
            $id = $result->message_reply_parent_id . ',';

            $id.=$this->select_msg_for_reply($result->message_reply_parent_id);

            return $id;
        }
    }

    public function select_all_data_from_message_tbl_attachment_for_sender($message_id) {
        //print_r($message_id);

        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$message_id' AND message_attachment_type= 1";
        $result = $this->db->query($sql)->result();
        // print_r($result);
        return $result;
    }

    public function select_message_details_by_message_id($message_id) {
        $sender_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id' AND sender_id='$sender_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function preview_message_sender_info($id) {


        $sql = "SELECT * FROM tbl_person WHERE person_id = '$id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function preview_message_all_sender_info($id) {

        //print_r($id);
        //for($i=0;$i<count($id);$i++){
        $sql = "SELECT * FROM tbl_person WHERE person_id = '$id'";
        $result = $this->db->query($sql)->result();
        return $result;
        //}
    }

    public function preview_message_receiver_id_info($receiver_id) {
        $receiver_id_array = explode(",", $receiver_id);
        if (count($receiver_id_array) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id'";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($receiver_id_array) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id)";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public function preview_message_all_receiver_id_info($receiver_id) {
        $receiver_id_array = explode(",", $receiver_id);
        if (count($receiver_id_array) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id'";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($receiver_id_array) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id)";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public function preview_message_receiver_id_single_info($receiver_id) {
        $receiver_id_array = explode(",", $receiver_id);
        if (count($receiver_id_array) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id' LIMIT 1";
            $result = $this->db->query($sql)->result();
        } else if (count($receiver_id_array) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id) LIMIT 1";
            $result = $this->db->query($sql)->result();
        }



        return $result;
    }

    public function preview_message_all_receiver_id_single_info($receiver_id) {
        // for($i=0;$i<count($receiver_id);$i++){
        $receiver_id_array = explode(",", $receiver_id);
        if (count($receiver_id_array) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id' LIMIT 1";
            $result = $this->db->query($sql)->result();
        } else if (count($receiver_id_array) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id)LIMIT 1";
            $result = $this->db->query($sql)->result();
        }

        return $result;
        //}
    }

    public function select_all_data_from_message_tbl_for_draft() {
        $sender_id = $this->session->userdata('user_id');

        $sql = "SELECT * FROM tbl_message WHERE message_status=4 AND sender_id='$sender_id' ORDER BY message_datetime DESC";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_all_data_from_message_tbl_attachment_for_draft($message_id) {
        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$message_id' AND message_attachment_type= 1";
        $result = $this->db->query($sql)->result();

        return $result;
    }

    public function send_draft_message($message_id, $new_file_path, $message_attachmen_id) {
        $user_id = $this->session->userdata('user_id');
        $message_body = $this->input->post('message_body', TRUE);
        $message_datetime = date('Y/m/d H:i:s', (time()));

        $this->db->set('message_body', $message_body);
        $this->db->set('message_status', 1);
        $this->db->set('message_datetime', $message_datetime);
        $this->db->where('message_id', $message_id);
        $this->db->where('sender_id', $user_id);
        $this->db->update('tbl_message');
        $this->db->set('message_receiver_attachment', $new_file_path);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_attachmen_id', $message_attachmen_id);
        $this->db->where('message_attachment_type', 1);

        $this->db->update('tbl_message_attachment');
    }

    public function send_draft_message_no_attachment($message_id) {
        $user_id = $this->session->userdata('user_id');
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $message_body = $this->input->post('message_body', TRUE);
        $this->db->set('message_body', $message_body);
        $this->db->set('message_status', 1);
        $this->db->set('message_datetime', $message_datetime);
        $this->db->where('message_id', $message_id);
        $this->db->where('sender_id', $user_id);
        $this->db->update('tbl_message');
    }

    public function get_parent_msg_id() {
        // $id = '';
        $sql = "SELECT * FROM  tbl_message_reply ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_receive_delete_message_all() {
        $user_id = $this->session->userdata('user_id');

        $sql1 = "SELECT * FROM tbl_message_detete WHERE (person_id LIKE '%,$user_id' OR person_id LIKE '$user_id,%' OR person_id LIKE '$user_id' OR person_id LIKE '%,$user_id,%')";
        $result1 = $this->db->query($sql1)->result();

        return $result1;
    }

    public function select_sent_delete_message_all() {
        $user_id = $this->session->userdata('user_id');

        $sql1 = "SELECT * FROM tbl_message_detete WHERE (person_id LIKE '%,$user_id' OR person_id LIKE '$user_id,%' OR person_id LIKE '$user_id' OR person_id LIKE '%,$user_id,%')";
        $result1 = $this->db->query($sql1)->result();

        return $result1;
    }

    public function select_reply_exits($message_id) {
        $sql1 = "SELECT * FROM tbl_message_reply WHERE message_id='$message_id' OR message_reply_parent_id='$message_id'";
        $result1 = $this->db->query($sql1)->result();

        return $result1;
    }

    public function select_receive_message_all($message_id) {
        $user_id = $this->session->userdata('user_id');
        // echo $message_id;
        $m_id = explode(",", $message_id);
        if (count($m_id) > 1) {
            $sql = "SELECT * FROM tbl_message WHERE message_id IN($message_id) AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%')AND sender_id != '$user_id'  AND message_status=1  ORDER BY message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($m_id) == 1) {
            $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%')AND sender_id != '$user_id'  AND message_status=1  ORDER BY message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public function select_all_msg_id() {
        $sql = "SELECT * FROM tbl_message";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public $id_max_all = '';

    public function select_receive_message($messgae_id) {

        // $id_max_all= array();
        $user_id = $this->session->userdata('user_id');
        $preview_rcv_reply_message = $this->select_msg_for_rcv_reply($messgae_id);
        if ($preview_rcv_reply_message) {
            $id = explode(",", $preview_rcv_reply_message);


            if ($id[count($id) - 1] == '') {
                array_pop($id);
            }

            $id1 = min($id);

            $preview_rcv_reply_message_1 = $this->message_model->select_msg_for_reply_message($id1);
            $id_all = explode(',', $preview_rcv_reply_message_1);

            if ($id_all[count($id_all) - 1] == '') {
                array_pop($id_all);
            }
            for ($i = 0; $i <= count($id_all); $i++) {
                $id_max = max($id_all);

                $result2 = $this->select_rcv_id_exit_or_not($id_max);
                // echo $result2;
                if ($result2) {
                    $id_2 = explode(",", $this->id_max_all);
                    //  print_r($id_2);

                    if (!in_array($result2, $id_2)) {

                        $sql1 = "SELECT * FROM tbl_message WHERE message_id = '$result2' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%')AND sender_id != '$user_id'  AND message_status=1  ORDER BY message_datetime DESC";
                        $result1 = $this->db->query($sql1)->row();
                        $this->id_max_all = $result2 . ",";

                        return $result1;
                    }
                } else {
                    if (($key = array_search($id_max, $id_all)) !== false) {
                        unset($id_all[$key]);
                        $id_max = max($id_all);
                    }
                }
            }
        } else {

            $sql1 = "SELECT * FROM tbl_message WHERE message_id = '$messgae_id' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%')AND sender_id != '$user_id'  AND message_status=1  ORDER BY message_datetime DESC";
            $result1 = $this->db->query($sql1)->row();

            return $result1;
        }
    }

    function select_rcv_id_exit_or_not($id_max) {
        $user_id = $this->session->userdata('user_id');
        $sql2 = "SELECT * FROM tbl_message WHERE message_id = '$id_max' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%' OR sender_id = '$user_id' ) AND message_status=1  ORDER BY message_datetime DESC";
        $result2 = $this->db->query($sql2)->row();
        if ($result2) {
            return $id_max;
        }
    }

    public function select_msg_for_rcv_reply($message_id1) {
        $id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE  message_id='$message_id1' OR message_reply_parent_id='$message_id1'  ";
        $result = $this->db->query($sql)->row();
        if ($result) {
            $id = $result->message_reply_parent_id . ',';

            $id.=$this->select_msg_for_reply($result->message_reply_parent_id);

            return $id;
        }
    }

    public function get_parent_msg_id_rcv($messgae_id) {
        $sql = "SELECT * FROM  tbl_message_reply WHERE message_id='$messgae_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_all_data_from_message_tbl_attachment_for_receiver($message_id) {

        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$message_id' AND message_attachment_type= 1";
        $result = $this->db->query($sql)->result();

        return $result;
    }

    public function select_all_person_receiver($id) {
        $sql = "SELECT * FROM tbl_person WHERE person_id='$id'";
        $result = $this->db->query($sql)->result();


        return $result;
    }

    public function viewer_id_for_msg_no_viwer_earlier($message_id1, $person_id) {



        $this->db->set('message_viewer_id', $person_id);
        $this->db->where('message_id', $message_id1);
        $this->db->where('message_status', 1);
        $this->db->update('tbl_message');
    }

    public function viewer_id_for_msg($message_id1, $viewer_id) {
        $this->db->set('message_viewer_id', $viewer_id);
        $this->db->where('message_id', $message_id1);
        $this->db->where('message_status', 1);
        $this->db->update('tbl_message');
    }

    public function select_rcv_message_details_by_message_id($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%' ) AND message_status=1 ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_rcv_message_details_by_message_id_for_reply($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id' AND (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%' OR sender_id='$user_id' ) AND message_status=1 ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function check_authentication_for_download($m_id, $message_attachmen_id) {
        $user_id = $this->session->userdata('user_id');

        $sql = "SELECT * FROM tbl_message WHERE message_id='$m_id' AND( (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%')  OR  sender_id=' $user_id')";
        $result = $this->db->query($sql)->result();
        // print_r($result);
        foreach ($result as $val) {
            $m_id_attach = $val->message_id;
        }
        $sql1 = "SELECT * FROM tbl_message_attachment WHERE message_id='$m_id_attach' AND message_attachmen_id ='$message_attachmen_id' AND message_attachment_type= 1 ";
        $result_at = $this->db->query($sql1)->result();
        //print_r($result_at);
        return $result_at;
    }

    public function check_authentication_for_download_send($m_id, $message_attachmen_id) {
        //$user_id = $this->session->userdata('user_id');
        // echo $m_id;
        $sql0 = "SELECT * FROM tbl_message WHERE message_id='$m_id' ";
        $temp_result = $this->db->query($sql0)->row();
        $user_id = $temp_result->sender_id;

        $sql = "SELECT * FROM tbl_message WHERE message_id='$m_id' AND ( sender_id=' $user_id' OR (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%') )";
        $result = $this->db->query($sql)->result();
        foreach ($result as $val) {
            $m_id_attach = $val->message_id;
        }
        $sql1 = "SELECT * FROM tbl_message_attachment WHERE message_id='$m_id_attach' AND message_attachmen_id ='$message_attachmen_id' AND message_attachment_type= 1";
        $result_at = $this->db->query($sql1)->result();
        //print_r($result_at);
        return $result_at;
    }

    public function download_message_details($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function download_message_details_send($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id'  ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_all_msg($message_id) {
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function download_message_attach_tbl_detail($message_id, $message_attachmen_id) {

        //print_r($message_id);

        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$message_id' AND message_attachmen_id = '$message_attachmen_id' AND message_attachment_type= 1";
        $result = $this->db->query($sql)->result();
        // print_r($result);
        return $result;
    }

    public function select_all_unread_data($message_id) {
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function update_for_viewer_id_unread_data($post_view_by, $message_id) {
        $this->db->set('message_viewer_id', $post_view_by);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_status', 1);
        $this->db->update('tbl_message');
    }

    public function update_for_no_viewer_id_read_data($person_id, $message_id) {

        $this->db->set('message_viewer_id', $person_id);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_status', 1);
        $this->db->update('tbl_message');
    }

    public function update_for_viewer_id_read_data($viewer_id_temp, $message_id) {
        $this->db->set('message_viewer_id', $viewer_id_temp);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_status', 1);
        $this->db->update('tbl_message');
    }

    public function cancel_upload_file($message_id, $message_attachmen_id) {

        $this->db->where('message_attachmen_id', $message_attachmen_id);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_attachment_type', 1);

        $this->db->delete('tbl_message_attachment');
    }

    public function cancel_upload_file_draft($message_id, $message_attachmen_id) {

        $this->db->where('message_attachmen_id', $message_attachmen_id);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_attachment_type', 1);
        $this->db->delete('tbl_message_attachment');
    }

    public function select_all_attachment_for_one_id($message_id) {
        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$message_id' AND message_attachment_type=1";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function insert_message_reply_tbl($insert_id, $message_id_old) {


        //echo $insert_id.$message_id_old;
        $sql = "SELECT * FROM tbl_message_reply WHERE message_id='$insert_id' ";
        $result = $this->db->query($sql)->result();
        if (!$result) {

            $data['message_id'] = $insert_id;
            $data['message_reply_parent_id'] = $message_id_old;
            $this->db->insert('tbl_message_reply', $data);
        }
    }

    public function select_msg_for_reply($message_id1) {
        $id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE  message_id='$message_id1'  ";
        $result = $this->db->query($sql)->row();
        if ($result) {
            $id = $result->message_reply_parent_id . ',';

            $id.=$this->select_msg_for_reply($result->message_reply_parent_id);

            return $id;
        }
    }

    public function select_msg_for_reply_message($id1) {
        //  echo $id1;

        $id = '';
        $parent_id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE message_reply_parent_id 	='$id1'";
        $result = $this->db->query($sql)->result();
        if ($result) {
            foreach ($result as $val) {
                $id .= $val->message_id . ",";
                $parent_id.= $val->message_reply_parent_id . ",";
                $id.=$this->select_msg_for_reply_message($val->message_id);
            }
            return $id . $parent_id;
        }
    }

    public function select_msg_for_reply_message_sent($id1) {
        //  echo $id1;

        $id = '';
        $parent_id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE message_reply_parent_id 	='$id1'";
        $result = $this->db->query($sql)->result();
        if ($result) {
            foreach ($result as $val) {
                $id .= $val->message_id . ",";
                $parent_id.= $val->message_reply_parent_id . ",";
                $id.=$this->select_msg_for_reply_message($val->message_id);
            }
            return $id . $parent_id;
        }
    }

    public function select_reply_message_for_delete($message_id) {
        $id = '';
        $sql = "SELECT * FROM  tbl_message_reply WHERE  message_id='$message_id'  ";
        $result = $this->db->query($sql)->row();
        if ($result) {
            $id = $result->message_reply_parent_id . ',';

            $id.=$this->select_msg_for_reply($result->message_reply_parent_id);

            return $id;
        }
    }

    public function select_previous_deleted_id() {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message_detete WHERE (person_id LIKE '%,$user_id' OR person_id LIKE '$user_id,%' OR person_id LIKE '$user_id' OR person_id LIKE '%,$user_id,%')  ";
        $result = $this->db->query($sql)->result();
        //print_r($result);
        return $result;
    }

    public function delete_single_msg($reply_msg_id) {
        //$user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message_detete WHERE message_id='$reply_msg_id'    ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function insert_deleted_id($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message_detete WHERE message_id='$message_id'  ";
        $result = $this->db->query($sql)->row();

        if ($result) {
            $this->db->set('person_id', $user_id);
            $this->db->where('message_id', $message_id);

            $this->db->update('tbl_message_detete');
        } else {

            $data['message_id'] = $message_id;
            $data['person_id'] = $user_id;
            $this->db->insert('tbl_message_detete', $data);
        }
        //$last_id = $this->db->insert_id();
        // return $last_id;
    }

    public function Select_from_delete_table($delete_msg_id) {
        $sql = "SELECT * FROM tbl_message WHERE message_detete_id='$delete_msg_id' ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function update_for_deleted_id($deleted_id_temp, $reply_msg_id) {
        $this->db->set('person_id', $deleted_id_temp);
        $this->db->where('message_id', $reply_msg_id);
        //  $this->db->where('message_status', 1);
        $this->db->update('tbl_message_detete');
    }

    public function select_msg_attach($reply_msg_id) {
        $sql = "SELECT * FROM tbl_message_attachment WHERE message_id='$reply_msg_id' AND message_attachment_type = 1";
        $result = $this->db->query($sql)->result();
        // print_r($result);
        return $result;
    }

    public function select_msg_table_data($reply_msg_id) {
        $sql = "SELECT * FROM tbl_message WHERE message_id='$reply_msg_id' ";
        $result = $this->db->query($sql)->row();
        // print_r($result);
        return $result;
    }

    public function select_message_reply_id($message_id1) {
        $sql = "SELECT * FROM  tbl_message_reply WHERE  message_id='$message_id1'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_reply($message_id) {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_message WHERE message_id='$message_id' AND( sender_id=' $user_id' OR (receiver_id LIKE '%,$user_id' OR receiver_id LIKE '$user_id,%' OR receiver_id LIKE '$user_id' OR receiver_id LIKE '%,$user_id,%') )AND message_status=1  ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function preview_message_reply_sender_info($id) {


        $sql = "SELECT * FROM tbl_person WHERE person_id = '$id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function preview_message_receiver_reply_id_info($receiver_id) {
        $receiver_id_new = explode(",", $receiver_id);
        if (count($receiver_id_new) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id'";
            $result = $this->db->query($sql)->result();
        }
        if (count($receiver_id_new) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id)";
            $result = $this->db->query($sql)->result();
        }

        return $result;
    }

    public function preview_message_receiver_reply_id_single_info($receiver_id) {

        $receiver_id_new = explode(",", $receiver_id);
        if (count($receiver_id_new) == 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id ='$receiver_id' LIMIT 1";
            $result = $this->db->query($sql)->result();
        }
        if (count($receiver_id_new) > 1) {
            $sql = "SELECT * FROM tbl_person WHERE person_id IN($receiver_id) LIMIT 1";
            $result = $this->db->query($sql)->result();
        }

        return $result;
    }

    //invitation message start
    //Devoloper:Afrin Jahan noumen
    // Staring date:1-02-2016
    public function select_course_invitation_count($course_id) {

        $reciver_id_totall = 0;
        $today_date = date('d/m/y');
        $sql = "SELECT * FROM tbl_invitation_message WHERE course_id='$course_id'  ";
        $result = $this->db->query($sql)->result();
        if ($result) {
            foreach ($result as $value) {
                $pre_time = date("d/m/y", strtotime($value->invitation_message_datetime));
                if ($pre_time == $today_date) {
                    $receiver_num = $value->invitation_receiver_email;
                    $receiver_num_array = explode(",", $receiver_num);
                    if ($receiver_num_array[count($receiver_num_array) - 1] == '') {
                        array_pop($receiver_num_array);
                    }
                    $reciver_id_totall = $reciver_id_totall + count($receiver_num_array);
                }
            }

            return $reciver_id_totall;
        }
    }

    public function select_person_invitation_count($person_id) {

        $reciver_id_totall = 0;
        $today_date = date('d/m/y');
        $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_sender_id='$person_id'  ";
        $result = $this->db->query($sql)->result();
        if ($result) {
            foreach ($result as $value) {
                $pre_time = date("d/m/y", strtotime($value->invitation_message_datetime));
                if ($pre_time == $today_date) {
                    $receiver_num = $value->invitation_receiver_email;
                    $receiver_num_array = explode(",", $receiver_num);
                    if ($receiver_num_array[count($receiver_num_array) - 1] == '') {
                        array_pop($receiver_num_array);
                    }
                    $reciver_id_totall = $reciver_id_totall + count($receiver_num_array);
                }
            }

            return $reciver_id_totall;
        }
    }

    public function select_course_name($course_id) {
        $sql = "SELECT * FROM tbl_course WHERE course_id='$course_id'  ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function set_course_to_message($course_id, $email_array, $person_id) {
//        print_r($email_array);
//        echo $person_id;
//        exit();
        //$result=array();
        $user_id = $this->session->userdata('user_id');
        $email = implode(",", $email_array);

        $course_name = $this->select_course_name($course_id);




        $data['course_id'] = $course_id;
        $data['invitation_message_subject'] = $this->input->post('subject', true);
        $data['invitation_message_body'] = $this->input->post('invitation_message', true);
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['invitation_message_type'] = 1;
        $data['invitation_message_sender_id'] = $user_id;
        $data['invitation_message_subject'] = "Course invitation for " . $course_name->course_name . " - TalhaTraining";



        $data['invitation_message_status'] = 1;

        $data['invitation_message_datetime'] = $message_datetime;
        $data['invitation_receiver_email'] = $email;
        if ($person_id == '') {
            $this->db->insert('tbl_invitation_message', $data);
            return $this->db->insert_id();
        } else {
            $data['course_invitation_permission_id'] = $person_id;
            $this->db->insert('tbl_invitation_message', $data);
            return $this->db->insert_id();
        }
    }

    public function set_course_to_group_message($course_id, $email_array, $person_id) {

        $user_id = $this->session->userdata('user_id');
        $email = implode(",", $email_array);
        $course_name = $this->select_course_name($course_id);
        $data['course_id'] = $course_id;
        $data['invitation_message_subject'] = "Course invitation for " . $course_name->course_name . " - TalhaTraining";
        $data['invitation_message_body'] = $this->input->post('invitation_message', true);
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['invitation_message_sender_id'] = $user_id;
        $data['invitation_message_type'] = 2;
        $data['invitation_message_status'] = 1;
        $data['invitation_message_datetime'] = $message_datetime;
        $data['invitation_receiver_email'] = $email;
        if ($person_id == '') {
            $this->db->insert('tbl_invitation_message', $data);
            return $this->db->insert_id();
        } else {
            $data['course_invitation_permission_id'] = $person_id;
            $this->db->insert('tbl_invitation_message', $data);
            return $this->db->insert_id();
        }
    }

    public function select_course_type($course_id) {
        $sql = "SELECT * FROM tbl_course WHERE course_id ='$course_id' ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_person_id_if_exists($email) {
        $sql = "SELECT * FROM tbl_person WHERE person_email ='$email' ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_person_email($id) {
        $sql = "SELECT * FROM tbl_person WHERE person_id ='$id' ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function check_person_course_permission($course_id) {
        $sql = "SELECT * "
                . " FROM "
                . "     tbl_course_person_permission as cpp "
                . " WHERE "
                . "      cpp.course_id = '$course_id'"
        ;
//        echo '<pre>';
//        print_r($sql);
//        exit();
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function seclect_course_creator_name($person_id) {

        $sql = "SELECT * FROM tbl_person WHERE  	person_id ='$person_id' ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_group_member($group_id) {
        $sql = "SELECT * FROM tbl_group_member WHERE  group_id ='$group_id' ";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_all_sent_invitation_msg() {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_sender_id='$user_id' ORDER BY invitation_message_datetime DESC";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_invitation_detail($invitation_message_id) {

        $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_id='$invitation_message_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_sender_info($person_id) {
        $sql = "SELECT * FROM tbl_person WHERE person_id='$person_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_all_rcv_invitation_msg() {
        $user_id = $this->session->userdata('user_id');
        $sql1 = "SELECT * FROM tbl_person WHERE person_id='$user_id'";
        $result1 = $this->db->query($sql1)->row();
        $email = $result1->person_email;
        // echo $email;
        $sql2 = "SELECT * FROM tbl_invitation_message";
        $result2 = $this->db->query($sql2)->result();
        if ($result2) {
            foreach ($result2 as $val) {
                if ($val->invitation_message_type == 1) {
                    $sql = "SELECT * FROM tbl_invitation_message WHERE (course_invitation_permission_id LIKE '%,$user_id' OR course_invitation_permission_id LIKE '$user_id,%' OR course_invitation_permission_id LIKE '$user_id' OR course_invitation_permission_id LIKE '%,$user_id,%') ORDER BY invitation_message_datetime DESC";
                    $result = $this->db->query($sql)->result();
                    //return  $result;
                } else if ($val->invitation_message_type == 2) {
                    $my_gorup_id = $this->select_my_all_group();
                    foreach ($my_gorup_id as $value) {
                        $group_id = $value->group_id;
                        $sql = "SELECT * FROM tbl_invitation_message WHERE (course_invitation_permission_id LIKE '%,$group_id' OR course_invitation_permission_id LIKE '$group_id,%' OR course_invitation_permission_id LIKE '$group_id' OR course_invitation_permission_id LIKE '%,$group_id,%') ORDER BY invitation_message_datetime DESC";
                        $result = $this->db->query($sql)->result();
                    }
                }
            }

            return $result;
        }
    }

    public function select_my_all_group() {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_group_member WHERE person_id='$user_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_course_creator_name($person_id) {
        $sql = "SELECT * FROM tbl_person WHERE person_id='$person_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_invitation_rcv_message_details_for_read_unread($invitation_message_id) {

        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_invitation_message  WHERE invitation_message_id='$invitation_message_id' AND invitation_message_status= 1 ";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function viewer_for_invitation_msg_no_viwer_earlier($invitation_message_id) {
        $user_id = $this->session->userdata('user_id');
        $this->db->set('invitation_viewer_id', $user_id);
        $this->db->where('invitation_message_id', $invitation_message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function viewer_invitation_msg_for_read_unread($invitation_message_id, $viewer_id) {
        $this->db->set('invitation_viewer_id', $viewer_id);
        $this->db->where('invitation_message_id', $invitation_message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function select_all_invitation_unread_data($message_id) {
        $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_id = '$message_id'";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function update_for_viewer_id_invitation_unread($post_view_by, $message_id) {
        $this->db->set('invitation_viewer_id', $post_view_by);
        $this->db->where('invitation_message_id', $message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function update_for_no_viewer_id_invitation_read_data($person_id, $message_id) {
        $this->db->set('invitation_viewer_id', $person_id);
        $this->db->where('invitation_message_id', $message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function update_for_viewer_id_invitation_read_data($viewer_id_temp, $message_id) {
        $this->db->set('invitation_viewer_id', $viewer_id_temp);
        $this->db->where('invitation_message_id', $message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function select_user_TT_user_or_not($person_id) {
        $sql = "SELECT * FROM tbl_resource_person WHERE person_id='$person_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_deleted_id_for_invitation_message_id($invitation_message_id) {

        $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_id = '$invitation_message_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function insert_invitation_deleted_id($person_id, $invitation_message_id) {
        $this->db->set('invitation_message_deleted_id', $person_id);
        $this->db->where('invitation_message_id', $invitation_message_id);
        $this->db->where('invitation_message_status', 1);
        $this->db->update('tbl_invitation_message');
    }

    public function deleted_invitation_id() {
        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_invitation_message WHERE (invitation_message_deleted_id LIKE '%,$user_id' OR invitation_message_deleted_id LIKE '$user_id,%' OR invitation_message_deleted_id LIKE '$user_id' OR invitation_message_deleted_id LIKE '%,$user_id,%')";
        $result = $this->db->query($sql)->result();
        return $result;
    }

    public function select_invitation_sent_message_all($message_id) {
        $user_id = $this->session->userdata('user_id');
        // echo $message_id;
        $m_id = explode(",", $message_id);
        if (count($m_id) > 1) {
            $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_id IN($message_id) AND invitation_message_sender_id = '$user_id'  AND  	invitation_message_status=1  ORDER BY invitation_message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($m_id) == 1) {
            $sql = "SELECT * FROM tbl_invitation_message WHERE 	invitation_message_id='$message_id' AND  invitation_message_sender_id = '$user_id'  AND  	invitation_message_status=1  ORDER BY invitation_message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public function select_invitation_rcv_message_all($message_id) {
        $user_id = $this->session->userdata('user_id');
        // echo $message_id;
        $m_id = explode(",", $message_id);
        if (count($m_id) > 1) {
            $sql = "SELECT * FROM tbl_invitation_message WHERE invitation_message_id IN($message_id) AND  invitation_message_status=1  ORDER BY invitation_message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        } else if (count($m_id) == 1) {
            $sql = "SELECT * FROM tbl_invitation_message WHERE 	invitation_message_id='$message_id' AND    invitation_message_status=1  ORDER BY invitation_message_datetime DESC";
            $result = $this->db->query($sql)->result();
            return $result;
        }
    }

    public function select_person_id_for_special_user($value) {
        $sql = "SELECT * FROM tbl_person WHERE person_email ='$value'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function whether_tt_user_or_not_email($email) {
        $sql = "SELECT * FROM tbl_person WHERE person_email ='$email'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function select_message_setting_option($person_id) {
        $sql = "SELECT * FROM tbl_person WHERE person_id ='$person_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    //invitation message end
    //message_setting start
    public function check_messgae_setting() {

        $user_id = $this->session->userdata('user_id');
        $sql = "SELECT * FROM tbl_person WHERE person_id='$user_id'";
        $result = $this->db->query($sql)->row();
        return $result;
    }

    public function save_message_setting_option($person_message_setting) {


        $user_id = $this->session->userdata('user_id');
        $this->db->set('person_message_setting', $person_message_setting);
        $this->db->where('person_id', $user_id);
        $this->db->update('tbl_person');
    }

    //message setting end


    /*
      -----------@@@RIPON@ Start Course Message Part (03-09-2016)----------------------------------
     */

    /**
     * @Function            : send_message_for_course_registration 
     * @Description         : send message for course registration
     * @Developed By        : MD Jaherul Islam
     * @Start DateTime      : 03-09-2016 05:39 PM  
     * @Parameter           : $notification_id[INTEGRE]
     * @Return              : NULL    
     * @Status              : Available        
     * */
    function send_message_for_course_registration($notification_info, $course_info) {
        $peson_id_arr = explode(',', $notification_info->notify_to);
        $course_name = mb_substr($course_info->course_name, 0, 40, 'utf-8') . '...';
        $person_info = $this->person_model->get_single_person_info_by_person_id($notification_info->notify_by);
        $person_name = $person_info->person_first_name . ' ' . $person_info->person_last_name;

        for ($i = 0; $i < count($peson_id_arr); $i++) {
            $data = Array();
            $data['message_type'] = 2;
            $data['message_type_id'] = $course_info->course_id;
            $data['sender_id'] = 1;
            $data['receiver_id'] = $peson_id_arr[$i];
            $data['message_body'] = ($notification_info->notify_by == $peson_id_arr[$i]) ? get_message('course_registered_notification', $course_name) : get_message('course_register_notification', $person_name, $course_name);
            $data['message_datetime'] = date('Y/m/d H:i:s', (time() - $this->session->userdata('time_offset')));
            $data['message_status'] = 1;
            $this->db->insert('tbl_message', $data);
        }
    }

    /*
      -----------@@@RIPON@ End Course Message Part (03-09-2016)----------------------------------
     */
}
