Skip to content

Commit

Permalink
Improve json
Browse files Browse the repository at this point in the history
  • Loading branch information
voltan committed Oct 22, 2014
1 parent eb9feb7 commit b9039af
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 14 deletions.
42 changes: 42 additions & 0 deletions src/Api/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
*/

Expand Down Expand Up @@ -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')) {
Expand Down
41 changes: 37 additions & 4 deletions src/Controller/Front/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -167,7 +200,7 @@ public function jsonList($topic, $start, $limit)
}
// return story
return $story;
}
} */

public function setLinkOrder($sort = 'publishDESC')
{
Expand All @@ -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;
Expand Down
94 changes: 93 additions & 1 deletion src/Controller/Front/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class JsonController extends IndexController
{
public function indexAction()
/* public function indexAction()
{
// Set view
$this->view()->setTemplate(false)->setLayout('layout-content');
Expand Down Expand Up @@ -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);
}
}
13 changes: 4 additions & 9 deletions src/Route/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit b9039af

Please sign in to comment.