<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class contactus_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_list($row) {
        $this->db->order_by('co_id', 'DESC');
        $this->db->limit(10, $row);
        $result = $this->db->get('d_contactus');
        return $result->result_array();
    }

    // get total number of users
    function getNumUsersAdmin() {
        $query = $this->db->query("select * from d_contactus");
        return $query->num_rows();
    }

    function delete($id) {
        $this->db->where('co_id', $id);
        $this->db->delete('d_contactus');
        return TRUE;
    }

    function is_valid_page($id) {
        $this->db->where('co_id', $id);
        $this->db->from('d_contactus');
        if ($this->db->count_all_results() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function get_page($id) {
        $this->db->where('co_id', $id);
        $result = $this->db->get('d_contactus');
        if ($result->num_rows() > 0) {
            return $result->row_array();
        } else {
            return FALSE;
        }
    }

    function showmsg($id) {
        // To Set Readed
        $this->db->set('co_Readed', 1);
        $this->db->where('co_id', $id);
        $this->db->update('d_contactus');
        // to Get The Measge Content
        $this->db->where('co_id', $id);
        $result = $this->db->get('d_contactus');
        if ($result->num_rows() > 0) {
            return $result->row_array();
        } else {
            return FALSE;
        }
    }

    function operation() {
        if (!empty($_POST['check_nf'])) {
            $products = $_POST['check_nf'];
            for ($i = 0; $i <= count($products) - 1; $i++) {
                switch ($_POST['operation']) {
                    case "delete":
                        $this->db->where('co_id', $products[$i]);
                        $result = $this->db->get('d_contactus');
                        $r = $result->row();
                        $this->db->where('co_id', $products[$i]);
                        $this->db->delete('d_contactus');
                        $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_del_all'));
                        break;
                }
            }
        }
//        if (isset($_POST['check_nf2'])) {
//            $products = $_POST['check_nf2'];
//            switch ($_POST['operation']) {
//                case "delete":
//                    $result = $this->db->get('d_contactus');
//                    $r = $result->result_array();
//                    foreach ($r as $rm):
//                        $this->db->delete('d_contactus');
//                    endforeach;
//                    $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_del_all'));
//                    break;
//            }
//        }
    }

    /* Front In Function  */

    function send() {
        $this->db->set('co_Name', $this->input->post('TxtName'));
        $this->db->set('co_Title', $this->input->post('TxtTitle'));
        $this->db->set('co_Email', $this->input->post('TxtEmail'));
        $this->db->set('co_Country', $this->input->post('TxtCountry'));
        $this->db->set('co_Content', $this->input->post('TxtContent'));
        $this->db->set('co_Date', time());
        $this->db->insert('d_contactus');
        return TRUE;
    }

    function add_email() {
        $this->db->set('ML_Email', $this->input->post('TxtEmail'));
        $this->db->set('ML_Date', time());
        $this->db->insert('d_mailinglist');
    }

    function count_unread()
    {
        if($this->auth->get_group($this->auth->get_user_id()) == 2){
           $this->db->where('co_user_id',$this->auth->get_user_id());
        }
        $this->db->where("`co_readed` = '0'");
        return $this->db->get('contactus')->num_rows();
    }
}