From df9bc523fe57556a94df76774185556669d2ced4 Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Tue, 16 Jul 2024 16:42:52 +0200 Subject: [PATCH] outsource RestfulApi - it will grow for sure --- boot.php | 156 +----------------------------- lib/neues_restful_api.php | 168 +++++++++++++++++++++++++++++++++ lib/rex_cronjob_neues_sync.php | 17 ++-- 3 files changed, 180 insertions(+), 161 deletions(-) create mode 100644 lib/neues_restful_api.php diff --git a/boot.php b/boot.php index c72e0c1..505d949 100644 --- a/boot.php +++ b/boot.php @@ -53,161 +53,7 @@ rex_api_function::register('neues_rss', neues_rss_api::class); if (rex_plugin::get('yform', 'rest')->isAvailable() && !rex::isSafeMode()) { - /* YForm Rest API */ - $rex_neues_entry_route = new rex_yform_rest_route( - [ - 'path' => '/neues/entry/5.0.0/', - 'auth' => '\rex_yform_rest_auth_token::checkToken', - 'type' => Entry::class, - 'query' => Entry::query(), - 'get' => [ - 'fields' => [ - 'rex_neues_entry' => [ - 'id', - 'status', - 'name', - 'teaser', - 'description', - 'domain_ids', - 'lang_id', - 'publishdate', - 'author_id', - 'url', - 'image', - 'images', - 'createdate', - 'createuser', - 'updatedate', - 'updateuser', - ], - 'rex_neues_category' => [ - 'id', - 'name', - 'image', - 'status', - ], - 'rex_neues_author' => [ - 'id', - 'name', - 'nickname', - 'text', - 'image', - 'be_user_id', - ], - ], - ], - 'post' => [ - 'fields' => [ - 'rex_neues_entry' => [ - 'status', - 'name', - 'teaser', - 'description', - 'domain_ids', - 'lang_id', - 'publishdate', - 'author_id', - 'url', - 'image', - 'images', - 'createdate', - 'createuser', - 'updatedate', - 'updateuser', - ], - ], - ], - 'delete' => [ - 'fields' => [ - 'rex_neues_entry' => [ - 'id', - ], - ], - ], - ], - ); - - rex_yform_rest::addRoute($rex_neues_entry_route); - - /* YForm Rest API */ - $rex_neues_category_route = new rex_yform_rest_route( - [ - 'path' => '/neues/category/5.0.0/', - 'auth' => '\rex_yform_rest_auth_token::checkToken', - 'type' => Category::class, - 'query' => Category::query(), - 'get' => [ - 'fields' => [ - 'rex_neues_category' => [ - 'id', - 'name', - 'image', - 'status', - ], - ], - ], - 'post' => [ - 'fields' => [ - 'rex_neues_category' => [ - 'name', - 'image', - 'status', - ], - ], - ], - 'delete' => [ - 'fields' => [ - 'rex_neues_category' => [ - 'id', - ], - ], - ], - ], - ); - - rex_yform_rest::addRoute($rex_neues_category_route); - - /* YForm Rest API */ - $rex_neues_author_route = new rex_yform_rest_route( - [ - 'path' => '/neues/author/5.0.0/', - 'auth' => '\rex_yform_rest_auth_token::checkToken', - 'type' => Author::class, - 'query' => Author::query(), - 'get' => [ - 'fields' => [ - 'rex_neues_author' => [ - 'id', - 'name', - 'nickname', - 'text', - 'image', - 'be_user_id', - ], - ], - ], - 'post' => [ - 'fields' => [ - 'rex_neues_author' => [ - 'name', - 'nickname', - 'text', - 'image', - 'be_user_id', - ], - ], - ], - 'delete' => [ - 'fields' => [ - 'rex_neues_author' => [ - 'id', - ], - ], - ], - ], - ); - - rex_yform_rest::addRoute($rex_neues_author_route); + RestfulApi::init(); } rex_extension::register('YFORM_DATA_LIST', static function ($ep) { diff --git a/lib/neues_restful_api.php b/lib/neues_restful_api.php new file mode 100644 index 0000000..f8a84aa --- /dev/null +++ b/lib/neues_restful_api.php @@ -0,0 +1,168 @@ + '/neues/entry/5.0.0/', + 'auth' => '\rex_yform_rest_auth_token::checkToken', + 'type' => Entry::class, + 'query' => Entry::query(), + 'get' => [ + 'fields' => [ + 'rex_neues_entry' => [ + 'id', + 'status', + 'name', + 'teaser', + 'description', + 'domain_ids', + 'lang_id', + 'publishdate', + 'author_id', + 'url', + 'image', + 'images', + 'createdate', + 'createuser', + 'updatedate', + 'updateuser', + ], + 'rex_neues_category' => [ + 'id', + 'name', + 'image', + 'status', + ], + 'rex_neues_author' => [ + 'id', + 'name', + 'nickname', + 'text', + 'image', + 'be_user_id', + ], + ], + ], + 'post' => [ + 'fields' => [ + 'rex_neues_entry' => [ + 'status', + 'name', + 'teaser', + 'description', + 'domain_ids', + 'lang_id', + 'publishdate', + 'author_id', + 'url', + 'image', + 'images', + 'createdate', + 'createuser', + 'updatedate', + 'updateuser', + ], + ], + ], + 'delete' => [ + 'fields' => [ + 'rex_neues_entry' => [ + 'id', + ], + ], + ], + ], + ); + + rex_yform_rest::addRoute($rex_neues_entry_route); + + /* YForm Rest API */ + $rex_neues_category_route = new rex_yform_rest_route( + [ + 'path' => '/neues/category/5.0.0/', + 'auth' => '\rex_yform_rest_auth_token::checkToken', + 'type' => Category::class, + 'query' => Category::query(), + 'get' => [ + 'fields' => [ + 'rex_neues_category' => [ + 'id', + 'name', + 'image', + 'status', + ], + ], + ], + 'post' => [ + 'fields' => [ + 'rex_neues_category' => [ + 'name', + 'image', + 'status', + ], + ], + ], + 'delete' => [ + 'fields' => [ + 'rex_neues_category' => [ + 'id', + ], + ], + ], + ], + ); + + rex_yform_rest::addRoute($rex_neues_category_route); + + /* YForm Rest API */ + $rex_neues_author_route = new rex_yform_rest_route( + [ + 'path' => '/neues/author/5.0.0/', + 'auth' => '\rex_yform_rest_auth_token::checkToken', + 'type' => Author::class, + 'query' => Author::query(), + 'get' => [ + 'fields' => [ + 'rex_neues_author' => [ + 'id', + 'name', + 'nickname', + 'text', + 'image', + 'be_user_id', + ], + ], + ], + 'post' => [ + 'fields' => [ + 'rex_neues_author' => [ + 'name', + 'nickname', + 'text', + 'image', + 'be_user_id', + ], + ], + ], + 'delete' => [ + 'fields' => [ + 'rex_neues_author' => [ + 'id', + ], + ], + ], + ], + ); + + rex_yform_rest::addRoute($rex_neues_author_route); + + } + +} diff --git a/lib/rex_cronjob_neues_sync.php b/lib/rex_cronjob_neues_sync.php index 29f8a1e..b4b9963 100644 --- a/lib/rex_cronjob_neues_sync.php +++ b/lib/rex_cronjob_neues_sync.php @@ -9,9 +9,9 @@ class rex_cronjob_neues_sync extends rex_cronjob { - private $rest_urls = ['category' => '/rest/neues/category/5.0.0', - 'author' => '/rest/neues/author/5.0.0', - 'entry' => '/rest/neues/entry/5.0.0']; + private $rest_urls = ['category' => '/rest/neues/category/5.0.0/', + 'author' => '/rest/neues/author/5.0.0/', + 'entry' => '/rest/neues/entry/5.0.0/']; public function execute() { @@ -28,7 +28,7 @@ public function execute() $response = $socket->doGet(); if (!$response->isOk()) { - $this->setMessage(sprintf(rex_i18n::msg('neues_entry_sync_error'), $response->statusMessage())); + $this->setMessage(sprintf(rex_i18n::msg('neues_entry_sync_error'), $response->getStatusCode())); return false; } @@ -71,13 +71,18 @@ public function execute() $neues_entry->setValue('teaser', $entry['teaser']); $neues_entry->setValue('description', $entry['description']); $neues_entry->setValue('url', $entry['url']); - $neues_entry->setValue('image', $entry['image']); - // $neues_entry->setValue('images', $entry['images']); + // $neues_entry->setValue('image', $entry['image']); + // $neues_entry->setValue('images', $entry['images']); $neues_entry->setValue('lang_id', $entry['lang_id']); $neues_entry->setValue('category_id', $entry['category_id']); $neues_entry->setValue('author_id', $entry['author_id']); $neues_entry->setValue('status', $status); $neues_entry->setValue('publishdate', $entry['publishdate']); + $neues_entry->setValue('domain_ids', 0); + $neues_entry->setValue('createuser', 'cronjob'); + $neues_entry->setValue('updateuser', 'cronjob'); + $neues_entry->setValue('createdate', $entry['createdate']); + $neues_entry->setValue('updatedate', $entry['updatedate']); $neues_entry->save(); } }