From b9039af3bf24216a0a767387c6096f0ab9ec5f83 Mon Sep 17 00:00:00 2001 From: voltan Date: Wed, 22 Oct 2014 11:13:53 +0330 Subject: [PATCH] Improve json --- src/Api/Story.php | 42 +++++++++++ src/Controller/Front/IndexController.php | 41 ++++++++++- src/Controller/Front/JsonController.php | 94 +++++++++++++++++++++++- src/Route/News.php | 13 +--- 4 files changed, 176 insertions(+), 14 deletions(-) diff --git a/src/Api/Story.php b/src/Api/Story.php index 233ef90..e7a4f0d 100644 --- a/src/Api/Story.php +++ b/src/Api/Story.php @@ -27,6 +27,7 @@ * Pi::api('story', 'news')->getListFromIdLight($id); * Pi::api('story', 'news')->canonizeStory($story, $topicList, $authorList); * Pi::api('story', 'news')->canonizeStoryLight($story); + * Pi::api('story', 'news')->canonizeStoryJson($story); * Pi::api('story', 'news')->sitemap(); */ @@ -349,6 +350,47 @@ public function canonizeStoryLight($story) return $story; } + public function canonizeStoryJson($story) + { + // Check + if (empty($story)) { + return ''; + } + // Get config + $config = Pi::service('registry')->config->read($this->getModule()); + // boject to array + $story = $story->toArray(); + // Set story url + $story['storyUrl'] = Pi::url(Pi::service('url')->assemble('news', array( + 'module' => $this->getModule(), + 'controller' => 'story', + 'slug' => $story['slug'], + ))); + // Set image url + if ($story['image']) { + // Set image thumb url + $story['thumbUrl'] = Pi::url( + sprintf('upload/%s/thumb/%s/%s', + $config['image_path'], + $story['path'], + $story['image'] + )); + } else { + $story['thumbUrl'] = ''; + } + // Set return array + $storyJson = array( + 'id' => $story['id'], + 'title' => $story['title'], + 'time_publish' => $story['time_publish'], + 'thumbUrl' => $story['thumbUrl'], + 'storyUrl' => $story['storyUrl'], + 'topic' => Json::decode($story['topic']), + ); + // return item + return $storyJson; + } + public function sitemap() { if (Pi::service('module')->isActive('sitemap')) { diff --git a/src/Controller/Front/IndexController.php b/src/Controller/Front/IndexController.php index e34b15e..31cf085 100644 --- a/src/Controller/Front/IndexController.php +++ b/src/Controller/Front/IndexController.php @@ -93,6 +93,39 @@ public function storyList($where, $limit, $orderLink = 'publishDESC') return $story; } + public function storyJsonList($where) + { + // Set info + $story = array(); + $limit = 10; + $page = $this->params('page', 1); + $module = $this->params('module'); + $offset = (int)($page - 1) * $limit; + $order = array('time_publish DESC', 'id DESC'); + // Set info + $columns = array('story' => new Expression('DISTINCT story')); + // Get info from link table + $select = $this->getModel('link')->select()->where($where)->columns($columns)->order($order)->offset($offset)->limit($limit); + $rowset = $this->getModel('link')->selectWith($select)->toArray(); + // Make list + foreach ($rowset as $id) { + $storyId[] = $id['story']; + } + if (empty($storyId)) { + return $story; + } + // Set info + $where = array('status' => 1, 'id' => $storyId); + // Get list of story + $select = $this->getModel('story')->select()->where($where)->order($order); + $rowset = $this->getModel('story')->selectWith($select); + foreach ($rowset as $row) { + $story[$row->id] = Pi::api('story', 'news')->canonizeStoryJson($row); + } + // return story + return $story; + } + public function storyPaginator($template, $where, $limit) { $page = $this->params('page', 1); @@ -123,7 +156,7 @@ public function storyPaginator($template, $where, $limit) return $paginator; } - public function jsonList($topic, $start, $limit) + /* public function jsonList($topic, $start, $limit) { // Set story info if ($start) { @@ -167,7 +200,7 @@ public function jsonList($topic, $start, $limit) } // return story return $story; - } + } */ public function setLinkOrder($sort = 'publishDESC') { @@ -178,12 +211,12 @@ public function setLinkOrder($sort = 'publishDESC') break; case 'publishASC': - $order = array('time_publish ASC', 'id ASC');; + $order = array('time_publish ASC', 'id ASC'); break; case 'publishDESC': default: - $order = array('time_publish DESC', 'id DESC');; + $order = array('time_publish DESC', 'id DESC'); break; } return $order; diff --git a/src/Controller/Front/JsonController.php b/src/Controller/Front/JsonController.php index 4407202..3b28159 100644 --- a/src/Controller/Front/JsonController.php +++ b/src/Controller/Front/JsonController.php @@ -18,7 +18,7 @@ class JsonController extends IndexController { - public function indexAction() + /* public function indexAction() { // Set view $this->view()->setTemplate(false)->setLayout('layout-content'); @@ -67,5 +67,97 @@ public function indexAction() echo Json::encode($storyList); exit; } + } */ + + public function indexAction() + { + // Set return + $return = array( + 'website' => Pi::url(), + 'module' => $this->params('module'), + ); + // Set view + $this->view()->setTemplate(false)->setLayout('layout-content'); + return Json::encode($return); + } + + public function storyAllAction() + { + // Get info from url + $module = $this->params('module'); + // Get config + $config = Pi::service('registry')->config->read($module); + // Get topic or homepage setting + $topic = Pi::api('topic', 'news')->canonizeTopic(); + // Set story info + $where = array('status' => 1); + // Get story List + $storyList = $this->storyJsonList($where); + // Set view + $this->view()->setTemplate(false)->setLayout('layout-content'); + return Json::encode($storyList); + } + + public function storyTopicAction() + { + // Get info from url + $id = $this->params('id'); + $module = $this->params('module'); + // Get config + $config = Pi::service('registry')->config->read($module); + // Get topic information from model + $topic = $this->getModel('topic')->find($id); + $topic = Pi::api('topic', 'news')->canonizeTopic($topic); + // Check category + if (!$topic || $topic['status'] != 1) { + $storyList = array(); + } else { + // Set story info + $where = array('status' => 1, 'topic' => $topic['ids']); + // Get story List + $storyList = $this->storyJsonList($where); + } + // Set view + $this->view()->setTemplate(false)->setLayout('layout-content'); + return Json::encode($storyList); + } + + public function storySingleAction() + { + // Get info from url + $id = $this->params('id'); + $module = $this->params('module'); + // Get config + $config = Pi::service('registry')->config->read($module); + // Get topic list + $topicList = Pi::registry('topicList', 'news')->read(); + // Get author list + $authorList = Pi::registry('authorList', 'news')->read(); + // Find story + $story = $this->getModel('story')->find($id); + $story = Pi::api('story', 'news')->canonizeStory($story, $topicList, $authorList); + // Check item + if (!$story || $story['status'] != 1) { + $storySingle = array(); + } else { + $storySingle = $story; + // Attached + if ($config['show_attach'] && $story['attach']) { + $attach = Pi::api('story', 'news')->AttachList($story['id']); + $storySingle['attachList'] = $attach; + } else { + $storySingle['attachList'] = array(); + } + // Extra + if ($config['show_extra'] && $story['extra']) { + $extra = Pi::api('extra', 'news')->Story($story['id']); + $storySingle['extraList'] = $extra; + } else { + $storySingle['extraList'] = array(); + } + } + // Set view + $this->view()->setTemplate(false)->setLayout('layout-content'); + return Json::encode($storySingle); } } \ No newline at end of file diff --git a/src/Route/News.php b/src/Route/News.php index e40b564..b06dbb9 100644 --- a/src/Route/News.php +++ b/src/Route/News.php @@ -102,16 +102,11 @@ protected function parse($path) break; case 'json': - $matches['topic'] = $this->decode($parts[1]); - if (isset($parts[2]) && $parts[2] == 'start' && isset($parts[4]) && $parts[4] == 'limit') { - $matches['start'] = $parts[3]; - $matches['limit'] = $parts[5]; - } elseif (isset($parts[2]) &&$parts[2] == 'start') { - $matches['start'] = $parts[3]; - } elseif (isset($parts[2]) &&$parts[2] == 'limit') { - $matches['limit'] = $parts[3]; + $matches['action'] = $this->decode($parts[1]); + if (isset($parts[2]) && $parts[2] == 'id') { + $matches['id'] = intval($parts[3]); } - break; + break; case 'media': $matches['action'] = $this->decode($parts[1]);