From 10ba5625da3ca21ce05ca43c462d787c5bc18791 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Sun, 12 May 2019 12:32:12 +0200 Subject: [PATCH] Get categories via the API --- bl-kernel/category.class.php | 18 ++++++++- bl-kernel/functions.php | 4 +- bl-plugins/api/plugin.php | 76 ++++++++++++++++++++++++++++++++++-- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/bl-kernel/category.class.php b/bl-kernel/category.class.php index d7c58531..f48a5970 100644 --- a/bl-kernel/category.class.php +++ b/bl-kernel/category.class.php @@ -59,4 +59,20 @@ class Category { { return $this->getValue('list'); } -} + + // Returns an array in json format with all the data of the tag + public function json($returnsArray=false) + { + $tmp['key'] = $this->key(); + $tmp['name'] = $this->name(); + $tmp['description'] = $this->description(); + $tmp['permalink'] = $this->permalink(); + $tmp['pages'] = $this->pages(); + + if ($returnsArray) { + return $tmp; + } + + return json_encode($tmp); + } +} \ No newline at end of file diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index 3de2d6bc..bc645b94 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -571,7 +571,9 @@ function editSettings($args) { $args['uriBlog'] = ''; } - $args['extremeFriendly'] = (($args['extremeFriendly']=='true')?true:false); + if (isset($args['extremeFriendly'])) { + $args['extremeFriendly'] = (($args['extremeFriendly']=='true')?true:false); + } if ($site->set($args)) { // Check current order-by if changed it reorder the content diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 1106f091..dead0763 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -164,6 +164,15 @@ class pluginAPI extends Plugin { $tagKey = $parameters[1]; $data = $this->getTag($tagKey); } + // (GET) /api/categories + elseif ( ($method==='GET') && ($parameters[0]==='categories') && empty($parameters[1]) ) { + $data = $this->getCategories(); + } + // (GET) /api/categories/ + elseif ( ($method==='GET') && ($parameters[0]==='categories') && !empty($parameters[1]) ) { + $categoryKey = $parameters[1]; + $data = $this->getCategory($categoryKey); + } else { $this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.')); } @@ -284,7 +293,7 @@ class pluginAPI extends Plugin { } catch (Exception $e) { return array( 'status'=>'1', - 'message'=>'Tag not found by the tag key: '.$key + 'message'=>'Tag not found by the key: '.$key ); } @@ -301,7 +310,7 @@ class pluginAPI extends Plugin { return array( 'status'=>'0', - 'message'=>'Tag data and pages related to the tag.', + 'message'=>'Information about the tag and pages related.', 'data'=>$data ); } @@ -491,9 +500,9 @@ class pluginAPI extends Plugin { | Edit the settings | You can edit any field defined in the class site.class.php variable $dbFields | - | @args array + | @args array | - | @return array + | @return array */ private function editSettings($args) { @@ -509,4 +518,63 @@ class pluginAPI extends Plugin { ); } + /* + | Returns the categories in the system + | Included the category name, key, description and the list of pages + | The list of pages are the page's key + | + | @return array + */ + private function getCategories() + { + global $categories; + $tmp = array( + 'status'=>'0', + 'message'=>'List of categories.', + 'data'=>array() + ); + foreach ($categories->keys() as $key) { + $category = $categories->getMap($key); + array_push($tmp['data'], $category); + } + return $tmp; + } + + /* + | Returns information about the category and pages related + | The pages are expanded which mean the title, content and more fields are returned in the query + | This can degrade the performance + | + | @key string Category key + | + | @return array + */ + private function getCategory($key) + { + try { + $category = new Category($key); + } catch (Exception $e) { + return array( + 'status'=>'1', + 'message'=>'Category not found by the key: '.$key + ); + } + + $list = array(); + foreach ($category->pages() as $pageKey) { + try { + $page = new Page($pageKey); + array_push($list, $page->json($returnsArray=true)); + } catch (Exception $e){} + } + + $data = $category->json($returnsArray=true); + $data['pages'] = $list; + + return array( + 'status'=>'0', + 'message'=>'Information about the category and pages related.', + 'data'=>$data + ); + } } \ No newline at end of file