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

/**
 * Notice_view
 */


class Notice_view extends CI_Controller
{

    private $CI;

    function __construct()
    {
        $this->CI = & get_instance();

    }

	function main()
	{

		if ( ! $this->CI->input->post_get('token')) {
			$this->CI->apilib->make_error("토큰 값이 넘어오지 않았습니다");
        }
		$token = $this->CI->input->post_get('token');


        $app_id = (int) trim($this->CI->input->post_get('app_id'));
        if (empty($app_id)) {
			$this->CI->apilib->make_error("어플 아이디가 넘어오지 않았습니다");
        }

        $notice_id = (int) trim($this->CI->input->get('notice_id'));
        if (empty($notice_id)) {
			$this->CI->apilib->make_error("공지사항 고유 아이디가 넘어오지 않았습니다");
        }

		$this->CI->load->model(array('Notice_model', 'Upload_file_model'));

        $where = array(
			'app_id' => $app_id,
			'notice_id' => $notice_id,
		);
		$result = $this->CI->Notice_model->get_one('', '', $where);
		if ( ! element('notice_id', $result)) {
			$this->CI->apilib->make_error("존재하지 않는 공지사항입니다");
		}
		$uploadwhere = array(
			'origin_table' => 'notice',
			'origin_id' => element('notice_id', $result),
		);
		$file = $this->CI->Upload_file_model->get('', '', $uploadwhere, '', '', 'ufi_id', 'ASC');
		$files = array();
		if ($file && is_array($file)) {
			foreach ($file as $key => $value) {
				$files[] = site_url('uploads/files/' . element('ufi_filename', $value));
			}
		}

		$arr = array(
			'result' => 'ok',
			'token' => $token,
			'app_id' => $app_id,
			'notice_id' => html_escape(element('notice_id', $result)),
			'user_id' => html_escape(element('user_id', $result)),
			'user_name' => html_escape(element('user_name', $result)),
			'notice_title' => html_escape(element('notice_title', $result)),
			'notice_content' => nl2br(html_escape(element('notice_content', $result))),
			'files' => $files,
			'created_at' => html_escape(element('created_at', $result)),
			'updated_at' => html_escape(element('updated_at', $result)),
		);
		return $arr;

	}


}
