/** * Index * * @param array $options * @return object */ public function index($options) { $result = core\Util::makeObject(['nest' => null, 'category' => null, 'articles' => null, 'pageNavigation' => null, 'nextpage' => null]); $print = explode(',', $options['print_data']); // get nests if ($options['nest_id']) { $result->nest = core\Spawn::item(['table' => core\Spawn::getTableName('Nest'), 'where' => 'id=\'' . $options['nest_id'] . '\'', 'jsonField' => ['json']]); if (!isset($result->nest['srl'])) { return core\Util::makeObject(['state' => 'error', 'message' => 'not found nest data']); } // get categories list if ($result->nest['json']['useCategory'] && $this->searchKeyInArray($print, 'category')) { $result->category = core\Spawn::items(['table' => core\Spawn::getTableName('Category'), 'where' => 'nest_srl=' . (int) $result->nest['srl'], 'field' => 'srl,name', 'order' => 'turn', 'sort' => 'asc']); $cnt_all = core\Spawn::count(['table' => core\Spawn::getTableName('Article'), 'where' => 'app_srl=' . $options['app_srl'] . ' and nest_srl=' . (int) $result->nest['srl']]); if (count($result->category)) { $check_active = false; $index = [['srl' => 0, 'name' => 'All', 'count' => $cnt_all, 'active' => false]]; foreach ($result->category as $k => $v) { $cnt = $cnt_all > 0 ? core\Spawn::count(['table' => core\Spawn::getTableName('Article'), 'where' => 'category_srl=' . (int) $v['srl']]) : 0; if ($options['category_srl'] == (int) $v['srl']) { $check_active = true; } $index[] = ['srl' => (int) $v['srl'], 'name' => $v['name'], 'count' => $cnt, 'active' => $options['category_srl'] == (int) $v['srl']]; } if (!$check_active) { $index[0]['active'] = true; } $result->category = $index; } } } // get articles // init paginate $options['page'] = $options['page'] > 1 ? $options['page'] : 1; $count = $options['count']; $scale = $options['pageScale']; $params = ['keyword' => $_GET['keyword'] ? $_GET['keyword'] : '']; $nest_srl = $options['nest_id'] ? isset($result->nest['srl']) ? $result->nest['srl'] : -1 : null; $where = 'app_srl=' . $options['app_srl']; $where .= $nest_srl ? ' and nest_srl=' . $nest_srl : ''; $where .= $options['category_srl'] ? ' and category_srl=' . (int) $options['category_srl'] : ''; $where .= $_GET['keyword'] ? ' and (title LIKE "%' . $_GET['keyword'] . '%" or content LIKE "%' . $_GET['keyword'] . '%")' : ''; // get total article $total = core\Spawn::count(['table' => core\Spawn::getTableName('Article'), 'where' => $where]); // set paginate instance $paginate = new core\Paginate($total, $_GET['page'], $params, $count, $scale); // set limit $limit = $paginate->offset . ',' . $paginate->size; // get articles $result->articles = core\Spawn::items(['table' => core\Spawn::getTableName('Article'), 'field' => 'srl,nest_srl,category_srl,hit,json,regdate,title', 'where' => $where, 'limit' => $limit, 'sort' => 'desc', 'order' => 'srl', 'jsonField' => ['json']]); // adjustment articles if ($this->searchKeyInArray($print, 'article')) { foreach ($result->articles as $k => $v) { if (isset($v['regdate'])) { $result->articles[$k]['regdate'] = core\Util::convertDate($v['regdate']); } if (isset($v['modate'])) { $result->articles[$k]['modate'] = core\Util::convertDate($v['modate']); } if (isset($v['category_srl'])) { $category = core\Spawn::item(['table' => core\Spawn::getTableName('Category'), 'where' => 'srl=' . (int) $v['category_srl']]); $result->articles[$k]['category_name'] = isset($category['name']) ? $category['name'] : ''; } } } // set paginate if ($this->searchKeyInArray($print, 'nav_paginate')) { $result->pageNavigation = $paginate->createNavigationToObject(); } // set nextpage if ($this->searchKeyInArray($print, 'nav_more')) { $nextPaginate = new core\Paginate($total, $options['page'] + 1, $params, $count, $scale); $limit = $nextPaginate->offset . ',' . $nextPaginate->size; $nextArticles = core\Spawn::items(['table' => core\Spawn::getTableName('Article'), 'field' => 'srl', 'where' => $where, 'limit' => $limit, 'sort' => 'desc', 'order' => 'srl']); $result->nextpage = count($nextArticles) ? $options['page'] + 1 : null; } $result->nest = $this->searchKeyInArray($print, 'nest') ? $result->nest : null; $result->articles = $this->searchKeyInArray($print, 'article') ? $result->articles : null; $result->state = 'success'; return $result; }
$file = core\Spawn::item(['table' => core\Spawn::getTableName('File'), 'field' => 'loc', 'where' => 'article_srl=' . $v['srl']]); $output['result'][] = ['srl' => $v['srl'], 'img' => __GOOSE_URL__ . '/' . $file['loc'], 'title' => $v['title']]; } break; // article - update hit count // article - update hit count case 'updateHit': hitUpdate($_GET['srl']); $output = ["state" => "success"]; break; // article - index // article - index default: $_GET['page'] = (int) $_GET['page'] > 1 ? (int) $_GET['page'] : 1; // get count $count = core\Spawn::count(['table' => core\Spawn::getTableName('Article'), 'where' => 'nest_srl=' . $nest_srl]); if ($count) { // set paginate $paginate = new core\Paginate($count, $_GET['page'], [], $defaultItemCount, 10); // get article data $repo['articles'] = core\Spawn::items(['table' => core\Spawn::getTableName('Article'), 'where' => 'nest_srl=' . $nest_srl, 'limit' => [$paginate->offset, $paginate->size], 'order' => 'srl', 'sort' => 'asc', 'jsonField' => ['json']]); // set output data foreach ($repo['articles'] as $k => $v) { $item = ['srl' => $v['srl'], 'img' => __GOOSE_URL__ . '/' . $v['json']['thumbnail']['url'], 'title' => $v['title']]; $output['result'][] = $item; } $next_page = $_GET['page'] + 1; $next_paginate = new core\Paginate($count, $next_page, [], $defaultItemCount, 10); $repo['next_article_count'] = core\Spawn::items(['table' => core\Spawn::getTableName('Article'), 'field' => 'srl', 'where' => 'nest_srl=' . $nest_srl, 'limit' => [$next_paginate->offset, $next_paginate->size]]); $output['next'] = 0 < count($repo['next_article_count']) ? true : false; }