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

/**
 * Notice
 */


class Notice 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("어플 아이디가 넘어오지 않았습니다");
        }

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

        $where = array(
			'app_id' => $app_id,
		);
		$data = $this->CI->Notice_model->get('', '', $where, '', '', 'notice_id', 'desc');
		$result = array();
		$count = 0;
		if ($data) {
			foreach ($data as $key => $value) {

				$uploadwhere = array(
					'origin_table' => 'notice',
					'origin_id' => element('notice_id', $value),
				);
				$file = $this->CI->Upload_file_model->get('', '', $uploadwhere, '', '', 'ufi_id', 'ASC');
				$files = array();
				if ($file && is_array($file)) {
					foreach ($file as $key2 => $val2) {
						$files[] = site_url('uploads/files/' . element('ufi_filename', $val2));
					}
				}

				
				$result[$key]['notice_id'] = html_escape(element('notice_id', $value));
				$result[$key]['user_id'] = html_escape(element('user_id', $value));
				$result[$key]['user_name'] = html_escape(element('user_name', $value));
				$result[$key]['notice_title'] = html_escape(element('notice_title', $value));
				$result[$key]['notice_content'] = html_escape(element('notice_content', $value));
				$result[$key]['files'] = $files;
				$result[$key]['created_at'] = html_escape(element('created_at', $value));
				$result[$key]['updated_at'] = html_escape(element('updated_at', $value));
				$count++;
			}
		}

		$arr = array(
			'result' => 'ok',
			'token' => $token,
			'app_id' => $app_id,
			'count' => $count,
			'list' => $result,
		);
		return $arr;

	}


}
