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

/**
 * Contents
 */


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

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

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

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

		$cat1_id = $this->CI->input->get('cat1_id');
		$cat2_id = $this->CI->input->get('cat2_id');
		$cat3_id = $this->CI->input->get('cat3_id');
		$cat4_id = $this->CI->input->get('cat4_id');
		$tag = $this->CI->input->get('tag');
		$sort = $this->CI->input->get('sort');
		$is_bookmark = $this->CI->input->get('is_bookmark') == 'Y' ? '1' : '';
		if ($sort == 'H') {
			$data = $this->CI->Contents_model->get_api_popular_list($app_id, $is_bookmark, element('user_id', $sessionuser), $tag, $cat1_id, $cat2_id);
		} else {
			$data = $this->CI->Contents_model->get_api_list($app_id, $is_bookmark, element('user_id', $sessionuser), $tag, $cat1_id, $cat2_id);
		}
		$result = array();
		$count = 0;
		if ($data) {
			foreach ($data as $key => $value) {
				$result[$key]['con_id'] = html_escape(element('con_id', $value));
				$result[$key]['con_sort'] = html_escape(element('con_sort', $value));
				$result[$key]['cat1_id'] = html_escape(element('cat1_id', $value));
				$result[$key]['cat2_id'] = html_escape(element('cat2_id', $value));
				$result[$key]['cat3_id'] = html_escape(element('cat3_id', $value));
				$result[$key]['cat4_id'] = html_escape(element('cat4_id', $value));
				$result[$key]['cat1_code'] = element('cat1_id', $value) ? html_escape(element('cat_code', element(element('cat1_id', $value), $category))) : null;
				$result[$key]['cat2_code'] = element('cat2_id', $value) ? html_escape(element('cat_code', element(element('cat2_id', $value), $category))) : null;
				$result[$key]['cat3_code'] = element('cat3_id', $value) ? html_escape(element('cat_code', element(element('cat3_id', $value), $category))) : null;
				$result[$key]['cat4_code'] = element('cat4_id', $value) ? html_escape(element('cat_code', element(element('cat4_id', $value), $category))) : null;
				$result[$key]['cat1_name'] = element('cat1_id', $value) ? html_escape(element('cat_name', element(element('cat1_id', $value), $category))) : null;
				$result[$key]['cat2_name'] = element('cat2_id', $value) ? html_escape(element('cat_name', element(element('cat2_id', $value), $category))) : null;
				$result[$key]['cat3_name'] = element('cat3_id', $value) ? html_escape(element('cat_name', element(element('cat3_id', $value), $category))) : null;
				$result[$key]['cat4_name'] = element('cat4_id', $value) ? html_escape(element('cat_name', element(element('cat4_id', $value), $category))) : null;
				$result[$key]['user_id'] = html_escape(element('user_id', $value));
				$result[$key]['user_name'] = html_escape(element('user_name', $value));
				$result[$key]['con_title'] = html_escape(element('con_title', $value));
				$result[$key]['con_content'] = html_escape('<meta name="viewport" content="width=320"><style>img, table {max-width:100% !important;}</style>'. element('con_content', $value));
				$result[$key]['con_published_at'] = html_escape(element('con_published_at', $value));
				$cwhere = array(
					'app_id' => $app_id,
					'con_id' => element('con_id', $value),
				);
				$result[$key]['comment_count'] = $this->CI->Contents_comment_model->count_by($cwhere);
				$result[$key]['like_count'] = element('con_like', $value);
				$result[$key]['share_count'] = element('con_share_count', $value);
				$result[$key]['share_url'] = site_url('contents/view/'.element('con_id', $value));
				$result[$key]['cnt'] = element('cnt', $value);
				$filedata = array();
				$files = $this->CI->Upload_file_model->get_files('contents', element('con_id', $value));
				if ($files) {
					foreach ($files as $fkey => $fval) {
						$filedata[$fkey]['url'] = base_url('uploads/files/' . element('ufi_filename', $fval));
					}
				}
				$result[$key]['files'] = $filedata;
				$count++;
			}
		}

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

	}


}
