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

/**
 * Contents_view
 */


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

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

		$this->CI->load->model(array('Contents_model', 'Contents_category_model', 'Contents_comment_model', 'Contents_like_model', 'Upload_file_model', 'Contents_bookmark_model'));

		$sessionuser = $this->CI->User_model->get_by_token($app_id, $token);

        $where = array(
			'app_id' => $app_id,
			'con_id' => $con_id,
			'is_open' => '1',
			'is_del' => '0',
			'con_published_at <=' => cdate('Y-m-d H:i:s'),
		);
		$result = $this->CI->Contents_model->get_one('', '', $where);
		if ( ! element('con_id', $result)) {
			$this->CI->apilib->make_error("존재하지 않는 데이터입니다");
		}
		if ( ! element('con_published_at', $result)) {
			$this->CI->apilib->make_error("존재하지 않는 데이터입니다");
		}

        if ( ! $this->CI->session->userdata('con_hit_' . element('con_id', $result))) {

            $this->CI->session->set_userdata('con_hit_' . element('con_id', $result), '1');
			$this->CI->Contents_model->update_plus(element('con_id', $result), 'con_hit', '1');

        }


		$category = $this->CI->Contents_category_model->cat_list($app_id);

		$arr = array(
			'result' => 'ok',
			'token' => $token,
			'app_id' => $app_id,
			'con_id' => html_escape(element('con_id', $result)),
			'con_sort' => html_escape(element('con_sort', $result)),
			'cat1_id' => html_escape(element('cat1_id', $result)),
			'cat2_id' => html_escape(element('cat2_id', $result)),
			'cat3_id' => html_escape(element('cat3_id', $result)),
			'cat4_id' => html_escape(element('cat4_id', $result)),
			'user_id' => html_escape(element('user_id', $result)),
			'user_name' => html_escape(element('user_name', $result)),
			'con_title' => html_escape(element('con_title', $result)),
			'con_content' => nl2br(html_escape('<meta name="viewport" content="width=320"><style>img, table {max-width:100% !important;}</style>'. element('con_content', $result))),
			'con_published_at' => html_escape(element('con_published_at', $result)),
		);
		$arr['cat1_code'] = element('cat1_id', $result) ? html_escape(element('cat_code', element(element('cat1_id', $result), $category))) : null;
		$arr['cat2_code'] = element('cat2_id', $result) ? html_escape(element('cat_code', element(element('cat2_id', $result), $category))) : null;
		$arr['cat3_code'] = element('cat3_id', $result) ? html_escape(element('cat_code', element(element('cat3_id', $result), $category))) : null;
		$arr['cat4_code'] = element('cat4_id', $result) ? html_escape(element('cat_code', element(element('cat4_id', $result), $category))) : null;
		$arr['cat1_name'] = element('cat1_id', $result) ? html_escape(element('cat_name', element(element('cat1_id', $result), $category))) : null;
		$arr['cat2_name'] = element('cat2_id', $result) ? html_escape(element('cat_name', element(element('cat2_id', $result), $category))) : null;
		$arr['cat3_name'] = element('cat3_id', $result) ? html_escape(element('cat_name', element(element('cat3_id', $result), $category))) : null;
		$arr['cat4_name'] = element('cat4_id', $result) ? html_escape(element('cat_name', element(element('cat4_id', $result), $category))) : null;

		$cwhere = array(
			'app_id' => $app_id,
			'con_id' => element('con_id', $result),
        );
		$arr['comment_count'] = $this->CI->Contents_comment_model->count_by($cwhere);
		//$arr['like_count'] = $this->CI->Contents_like_model->count_by($cwhere);
        $arr['like_count'] = element('con_like', $result);
		$arr['share_count'] = element('con_share_count', $result);
		$arr['share_url'] = site_url('contents/view/'.element('con_id', $result));

		$filedata = array();
		$files = $this->CI->Upload_file_model->get_files('contents', element('con_id', $result));
		if ($files) {
			foreach ($files as $fkey => $fval) {
				$filedata[$fkey]['url'] = base_url('uploads/files/' . element('ufi_filename', $fval));
			}
		}
		$arr['files'] = $filedata;

		$where = array(
			'app_id' => $app_id, 
			'con_id' => $con_id, 
			'user_token' => $token
		);
		$like = $this->CI->Contents_like_model->get_one('', '', $where);
		$arr['i_like'] = (element('like_id', $like)) ? 1 : 0;

		if (element('user_id', $sessionuser)) {
			$where = array(
				'app_id' => $app_id, 
				'con_id' => $con_id, 
				'user_id' => element('user_id', $sessionuser)
			);
			$bookmark = $this->CI->Contents_bookmark_model->get_one('', '', $where);
			$arr['i_bookmark'] = (element('bookmark_id', $bookmark)) ? 1 : 0;
		} else {
			$arr['i_bookmark'] = 0;
		}

		return $arr;

	}


}
