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

/**
 * Contents_category model class
 */

class Contents_category_model extends MY_Model
{

    /**
     * 테이블명
     */
    public $_table = 'contents_category';

    /**
     * 사용되는 테이블의 프라이머리키
     */
    public $primary_key = 'cat_id'; // 사용되는 테이블의 프라이머리키

    function __construct()
    {
        parent::__construct();
    }


    public function get_admin_list($limit = '', $offset = '', $where = '', $like = '', $findex = '', $forder = '', $sfield = '', $skeyword = '', $sop = 'OR')
    {
        $result = $this->_get_list_common('', '', $limit, $offset, $where, $like, $findex, $forder, $sfield, $skeyword, $sop);
		return $result;
    }


	public function select_max_sort($app_id = '')
	{
		$this->db->where('app_id', $app_id);
		$this->db->select_max('cat_sort');
		$qry = $this->db->get($this->_table);
		$result = $qry->row_array();
		return $result['cat_sort'];
	}


	public function cat_list($app_id = '')
	{
		$this->db->where('app_id', $app_id);
		$this->db->where('cat_parent', '0');
		$this->db->order_by('cat_sort', 'desc');
		$qry = $this->db->get($this->_table);
		$result = $qry->result_array();
		$data = array();
		if ($result) {
			foreach ($result as $value) {
				$data[element('cat_id', $value)] = $value;
			}
		}
		return $data;
	}

	public function get_hierarchical_name($app_id, $category_id) {

		$this->db->where('app_id', $app_id);
		$this->db->where('cat_id', $category_id);
		$this->db->from('contents_category');
		$qry = $this->db->get();
		$result = $qry->row_array();
		$depth = element('cat_depth', $result); 

		if ($depth == '4') {
			$this->db->select('t1.cat_id AS lev1_id, t1.cat_name AS lev1_name, t1.cat_code AS lev1_code, t2.cat_id AS lev2_id, t2.cat_name as lev2_name, t2.cat_code as lev2_code, t3.cat_id AS lev3_id, t3.cat_name as lev3_name, t3.cat_code as lev3_code, t4.cat_id AS lev4_id, t4.cat_name as lev4_name, t4.cat_code as lev4_code');
			$this->db->from('contents_category as t1');
			$this->db->join('contents_category as t2', 't2.cat_parent = t1.cat_id AND t2.app_id = t1.app_id', 'left');
			$this->db->join('contents_category as t3', 't3.cat_parent = t2.cat_id AND t3.app_id = t2.app_id', 'left');
			$this->db->join('contents_category as t4', 't4.cat_parent = t3.cat_id AND t4.app_id = t3.app_id', 'left');
			$this->db->where('t4.cat_id', $category_id);
		}
		else if ($depth == '3') {
			$this->db->select('t1.cat_id AS lev1_id, t1.cat_name AS lev1_name, t1.cat_code AS lev1_code, t2.cat_id AS lev2_id, t2.cat_name as lev2_name, t2.cat_code as lev2_code, t3.cat_id AS lev3_id, t3.cat_name as lev3_name, t3.cat_code as lev3_code');
			$this->db->from('contents_category as t1');
			$this->db->join('contents_category as t2', 't2.cat_parent = t1.cat_id AND t2.app_id = t1.app_id', 'left');
			$this->db->join('contents_category as t3', 't3.cat_parent = t2.cat_id AND t3.app_id = t2.app_id', 'left');
			$this->db->where('t3.cat_id', $category_id);
		}
		else if ($depth == '2') {
			$this->db->select('t1.cat_id AS lev1_id, t1.cat_name AS lev1_name, t1.cat_code AS lev1_code, t2.cat_id AS lev2_id, t2.cat_name as lev2_name, t2.cat_code as lev2_code');
			$this->db->from('contents_category as t1');
			$this->db->join('contents_category as t2', 't2.cat_parent = t1.cat_id AND t2.app_id = t1.app_id', 'left');
			$this->db->where('t2.cat_id', $category_id);
		}
		else if ($depth == '1') {
			$this->db->select('t1.cat_id AS lev1_id, t1.cat_name AS lev1_name, t1.cat_code AS lev1_code');
			$this->db->from('contents_category as t1');
			$this->db->where('t1.cat_id', $category_id);
		}
		$qry = $this->db->get();
		$result = $qry->row_array();

		$id = array();
		if (element('lev1_id', $result)) $id['1'] = element('lev1_id', $result);
		if (element('lev2_id', $result)) $id['2'] = element('lev2_id', $result);
		if (element('lev3_id', $result)) $id['3'] = element('lev3_id', $result);
		if (element('lev4_id', $result)) $id['4'] = element('lev4_id', $result);

		$name = array();
		if (element('lev1_name', $result)) $name['1'] = element('lev1_name', $result);
		if (element('lev2_name', $result)) $name['2'] = element('lev2_name', $result);
		if (element('lev3_name', $result)) $name['3'] = element('lev3_name', $result);
		if (element('lev4_name', $result)) $name['4'] = element('lev4_name', $result);

		$code = array();
		if (element('lev1_code', $result)) $code['1'] = element('lev1_code', $result);
		if (element('lev2_code', $result)) $code['2'] = element('lev2_code', $result);
		if (element('lev3_code', $result)) $code['3'] = element('lev3_code', $result);
		if (element('lev4_code', $result)) $code['4'] = element('lev4_code', $result);

		$return = array('id' => $id, 'name' => $name, 'code' => $code);

		return $return;
	}
}