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

class Announcement 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('announcement/add', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function add()
	{
		save_current_url(base_url() . 'announcement/add/');
		
		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_announcement') . ' - ' .$this->lang->line('hf_add_new_announcement');
			
			$this->load->view('announcement_add_new', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	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_announcement') . ' - ' .$this->lang->line('hf_add_new_announcement');
			
				$this->load->model('user_model');
			
				$rs = $this->user_model->add_announcement();
					
				if($rs) {
					redirect('announcement/add/', 'refresh');
				}
				else {
					$this->load->view('announcement_add_new', $_POST);
				}
			}
			else {
				redirect('announcement/add/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function edit($announcement_id = NULL)
	{
		save_current_url(base_url() . 'announcement/edit/' . $announcement_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_announcement_details($announcement_id);
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_announcement') . ' - ' .$this->lang->line('hf_edit_announcement');
			
			$this->load->view('announcement_update', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update($announcement_id = NULL)
	{
		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_announcement') . ' - ' .$this->lang->line('hf_edit_announcement');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_announcement_details($announcement_id);
				
				if($rs) {
					redirect('announcement/edit/' . $announcement_id . '/', 'refresh');
				}
				else {
					$this->load->view('announcement_update', $_POST);
				}
			}
			else {
				redirect('announcement/edit/' . $announcement_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function delete($announcement_id = 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());
	
			$this->load->model('user_model');
			$this->user_model->delete_announcement($announcement_id);
			
			redirect('announcement/listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing()
	{
		save_current_url(base_url() . 'announcement/listing/');
		
		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_announcement') . ' - ' .$this->lang->line('hf_announcement_list');
			
			$this->load->view('announcement_list', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getlist()
    {
		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());
			
			/* 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(
								'announcement_id',
								'title_en',
								'title_zh_cn',
								'title_zh_hk',
								'status',
								'created_date'
							);
			
			// DB table to use
			$sTable = 'announcements';
			//
			
			$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);
			$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()
			);
			
			$lang = strtolower(str_replace('-', '_', get_language()));
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['created_date']);
                $row[] = strip_tags($aRow['title_' . $lang]);
                $row[] = $aRow['status'] == 1 ? '<span class="label label-success">' . $this->lang->line('announcement_published') . '</span>' : '<span class="label label-fail">' . $this->lang->line('announcement_unpublished') . '</span>';
                $row[] = '<a href="' . base_url() . 'announcement/edit/' . $aRow['announcement_id'] . '/">' . $this->lang->line('announcement_edit') . '</a> | <a href="' . base_url() . 'announcement/delete/' . $aRow['announcement_id'] . '/" onclick="return confirm(\'' . $this->lang->line('announcement_confirm') . '\');">' . $this->lang->line('announcement_delete') . '</a>';

                $output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function imageupload()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->helper('form');
			
			$this->lang->load('account_general', get_language());

			$resp = array(
				'resp' => 'error',
				'img' => '',
			);

			$config = array();
			$config['upload_path'] = './../uploads/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['encrypt_name'] = true;

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload())
			{
				$resp['msg'] = $config['upload_path'];
			}
			else
			{
				$img = $this->upload->data();
				$resp['status'] = 'success';
				$resp['img'] = str_replace('admin1388/', '', base_url()) . 'uploads/' . $img['file_name'];
				$resp['data'] = $this->upload->data();
			}

			echo json_encode($resp);
		}
    }
}

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