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

class Message extends MY_Controller {

	public function index()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			redirect('message/inbox', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function compose()
	{
		save_current_url(base_url() . 'message/compose/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_private_message') . ' - ' . $this->lang->line('hf_compose_message');
			
			$this->load->view('message_compose_2', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function compose_submit()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			if($_POST) {
				$this->lang->load('account_general', get_language());
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_private_message') . ' - ' . $this->lang->line('hf_compose_message');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->user_admin_compose_message();
					
				if($rs) {
					redirect('message/sent/', 'refresh');
				}
				else {
					$this->load->view('message_compose_2', $_POST);
				}
			}
			else {
				redirect('message/compose/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function inbox()
	{
		save_current_url(base_url() . 'message/inbox/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_private_message') . ' - ' . $this->lang->line('hf_inbox_message');
			
			$this->load->view('message_inbox', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function sent()
	{
		save_current_url(base_url() . 'message/sent/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_message') . ' - ' . $this->lang->line('hf_message_sent');
			
			$this->load->view('message_sent', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getlist($type = NULL)
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			if(strtolower($type) == 'sent') {
				$message_type = MESSAGE_INBOX;
			}
			else {
				$message_type = MESSAGE_SENT;
			}
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
								$this->db->dbprefix('member_messages') . '.message_id',
								$this->db->dbprefix('members') . '.username',								
								$this->db->dbprefix('member_messages') . '.subject', 
								$this->db->dbprefix('member_messages') . '.message',
								$this->db->dbprefix('member_messages') . '.admin_read', 
								$this->db->dbprefix('member_messages') . '.admin_reply', 
								$this->db->dbprefix('member_messages') . '.created_date', 
								$this->db->dbprefix('member_messages') . '.member_id'
							);
			$jColumns = $this->db->dbprefix('member_messages') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_messages';
			$jTable = 'members';
			//
			
			$this->db->where('message_type', $message_type);
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			$this->load->helper('text');
			
			if($message_type == MESSAGE_SENT) {
				foreach($rResult->result_array() as $aRow)
				{
					$row = array();
					
					$row[] = ($aRow['admin_read'] == 1) ? '<b>' . date('Y-m-d H:i:s', $aRow['created_date']) . '</b>' : date('Y-m-d H:i:s', $aRow['created_date']);
					$row[] = ($aRow['admin_read'] == 1) ? '<b>' . $aRow['username'] . '</b>' : $aRow['username'];
					
					if($aRow['admin_reply'] == 1) {
						$row[] = ($aRow['admin_read'] == 1) ? '<b>' . character_limiter($aRow['subject'], 30) . '</b>' : '&deg; ' . character_limiter($aRow['subject'], 30);
					}
					else {
						$row[] = ($aRow['admin_read'] == 1) ? '<b>' . character_limiter($aRow['subject'], 30) . '</b>' : character_limiter($aRow['subject'], 30);
					}
					
					$row[] = ($aRow['admin_read'] == 1) ? '<b>' . character_limiter($aRow['message'], 30) . '</b>' : character_limiter($aRow['message'], 30);
					$row[] = '<a href="' . base_url() . 'message/view/' . MESSAGE_INBOX . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_view') . '</a> | <a href="' . base_url() . 'message/reply/' . MESSAGE_INBOX . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_reply') . '</a> | <a href="' . base_url() . 'message/edit/' . MESSAGE_INBOX . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_edit') . '</a>';
					
					$output['aaData'][] = $row;
				}
			}
			else {
				foreach($rResult->result_array() as $aRow)
				{
					$row = array();
					
					$row[] = date('Y-m-d H:i:s', $aRow['created_date']);
					$row[] = $aRow['username'];
					
					if($aRow['admin_reply'] == 1) {
						$row[] = '&deg; ' . character_limiter($aRow['subject'], 30);
					}
					else {
						$row[] = character_limiter($aRow['subject'], 30);
					}
					
					$row[] = character_limiter($aRow['message'], 30);
					$row[] = '<a href="' . base_url() . 'message/view/' . MESSAGE_SENT . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_view') . '</a> | <a href="' . base_url() . 'message/reply/' . MESSAGE_SENT . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_reply') . '</a> | <a href="' . base_url() . 'message/edit/' . MESSAGE_SENT . '/' . $aRow['message_id'] . '/">' . $this->lang->line('message_edit') . '</a>';
					
					$output['aaData'][] = $row;
				}
			}
		
			echo json_encode($output);
		}	
    }
	
	public function submit()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			if($_POST) {
				$this->lang->load('account_general', get_language());
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_private_message') . ' - ' . $this->lang->line('hf_new_message');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->user_compose_message();
					
				if($rs) {
					redirect('message/sent/', 'refresh');
				}
				else {
					$this->load->view('message_compose', $_POST);
				}
			}
			else {
				redirect('message/inbox/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function view($message_type = MESSAGE_INBOX, $message_id = NULL)
	{
		save_current_url(base_url() . 'message/view/' . $message_type . '/' . $message_id . '/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
	
			$this->load->model('user_model');
			$data = $this->user_model->get_member_message_details_by_read($message_id);
			$data['selected_tab'] = ($message_type == MESSAGE_SENT) ? MESSAGE_SENT : MESSAGE_INBOX;
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_message') . ' - ' . (($message_type == MESSAGE_SENT) ? $this->lang->line('hf_message_sent') : $this->lang->line('hf_inbox_message'));
		
			$this->load->view('message_view', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function reply($message_type = MESSAGE_INBOX, $message_id = NULL)
	{
		save_current_url(base_url() . 'message/reply/' . $message_type . '/' . $message_id . '/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
	
			$this->load->model('user_model');
			$data = $this->user_model->get_member_message_details_by_reply($message_id);
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_message') . ' - ' . (($message_type == MESSAGE_SENT) ? $this->lang->line('hf_message_sent') : $this->lang->line('hf_inbox_message'));
			$data['is_reply'] = 1;
			
			$this->load->view('message_compose', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function edit($message_type = MESSAGE_INBOX, $message_id = NULL)
	{
		save_current_url(base_url() . 'message/edit/' . $message_type . '/' . $message_id . '/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
	
			$this->load->model('user_model');
			$data = $this->user_model->get_member_message_details_by_edit($message_id);
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_message') . ' - ' . (($message_type == MESSAGE_SENT) ? $this->lang->line('hf_message_sent') : $this->lang->line('hf_inbox_message'));
			$data['is_reply'] = 2;
			
			$this->load->view('message_compose', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
}

/* End of file message.php */
/* Location: ./application/controllers/message.php */