Skip to content

Commit

Permalink
outsource RestfulApi - it will grow for sure
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndr-w committed Jul 16, 2024
1 parent 3bbef07 commit df9bc52
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 161 deletions.
156 changes: 1 addition & 155 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
168 changes: 168 additions & 0 deletions lib/neues_restful_api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace FriendsOfRedaxo\Neues;

use rex_yform_rest_route;
use rex_yform_rest;

class RestfulApi {

public static function init() :void {
$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);

}

}
17 changes: 11 additions & 6 deletions lib/rex_cronjob_neues_sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit df9bc52

Please sign in to comment.