diff --git a/site/plugins/admin/app/Controllers/ApiDeliveryImagesController.php b/site/plugins/admin/app/Controllers/ApiDeliveryImagesController.php new file mode 100644 index 00000000..44313e4e --- /dev/null +++ b/site/plugins/admin/app/Controllers/ApiDeliveryImagesController.php @@ -0,0 +1,262 @@ + 0) { + foreach ($tokens_list as $token) { + if ($token['type'] == 'dir' && Filesystem::has(PATH['tokens'] . '/delivery/images/' . $token['dirname'] . '/token.yaml')) { + $tokens[] = $token; + } + } + } + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/images/index.html', + [ + 'menu_item' => 'api', + 'tokens' => $tokens, + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api'), + ], + 'api_delivery' => [ + 'link' => $this->router->pathFor('admin.api_delivery.index'), + 'title' => __('admin_delivery') + ], + 'api_delivery_images' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.index'), + 'title' => __('admin_images'), + 'active' => true + ], + ], + 'buttons' => [ + 'api_delivery_images_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.add'), + 'title' => __('admin_create_new_delivery_images_token') + ], + ], + ] + ); + } + + /** + * Add token page + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function add(Request $request, Response $response) : Response + { + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/images/add.html', + [ + 'menu_item' => 'api', + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api'), + ], + 'api_delivery' => [ + 'link' => $this->router->pathFor('admin.api_delivery.index'), + 'title' => __('admin_delivery') + ], + 'api_delivery_images' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.index'), + 'title' => __('admin_images') + ], + 'api_delivery_images_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.add'), + 'title' => __('admin_create_new_delivery_images_token'), + 'active' => true + ], + ], + ] + ); + } + + /** + * Add new token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function addProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + // Generate API token + $api_token = bin2hex(random_bytes(16)); + + $api_token_dir_path = PATH['tokens'] . '/delivery/images/' . $api_token; + $api_token_file_path = $api_token_dir_path . '/token.yaml'; + + if (! Filesystem::has($api_token_file_path)) { + + Filesystem::createDir($api_token_dir_path); + + // Generate UUID + $uuid = Uuid::uuid4()->toString(); + + // Get time + $time = date($this->registry->get('flextype.date_format'), time()); + + // Create API Token account + if (Filesystem::write( + $api_token_file_path, + $this->parser->encode([ + 'title' => $post_data['title'], + 'icon' => $post_data['icon'], + 'limit_calls' => (int) $post_data['limit_calls'], + 'calls' => (int) 0, + 'state' => $post_data['state'], + 'uuid' => $uuid, + 'created_by' => Session::get('uuid'), + 'created_at' => $time, + 'updated_by' => Session::get('uuid'), + 'updated_at' => $time, + ], 'yaml') + )) { + $this->flash->addMessage('success', __('admin_message_delvery_images_api_token_created')); + } else { + $this->flash->addMessage('error', __('admin_message_delvery_images_api_token_was_not_created1')); + } + } else { + $this->flash->addMessage('error', __('admin_message_delvery_images_api_token_was_not_created2')); + } + + if (isset($post_data['create-and-edit'])) { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_images.edit') . '?token=' . $api_token); + } else { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_images.index')); + } + } + + /** + * Edit token page + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function edit(Request $request, Response $response) : Response + { + $token = $request->getQueryParams()['token']; + $token_data = $this->parser->decode(Filesystem::read(PATH['tokens'] . '/delivery/images/' . $token . '/token.yaml'), 'yaml'); + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/images/edit.html', + [ + 'menu_item' => 'api', + 'token' => $token, + 'token_data' => $token_data, + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api') + ], + 'api_tokens' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.index'), + 'title' => __('admin_delivery') + ], + 'api_tokens_edit' => [ + 'link' => $this->router->pathFor('admin.api_delivery_images.edit'), + 'title' => __('admin_edit_delivery_token'), + 'active' => true + ], + ] + ] + ); + } + + /** + * Edit token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function editProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + $api_token_dir_path = PATH['tokens'] . '/' . $post_data['api'] . '/' . $post_data['api_token']; + $api_token_file_path = $api_token_dir_path . '/' . 'token.yaml'; + + // Update API Token File + if (Filesystem::has($api_token_file_path)) { + if (Filesystem::write( + $api_token_file_path, + $this->parser->encode([ + 'title' => $post_data['title'], + 'icon' => $post_data['icon'], + 'limit_calls' => (int) $post_data['limit_calls'], + 'calls' => (int) $post_data['calls'], + 'state' => $post_data['state'], + 'uuid' => $post_data['uuid'], + 'created_by' => $post_data['created_by'], + 'created_at' => $post_data['created_at'], + 'updated_by' => Session::get('uuid'), + 'updated_at' => date($this->registry->get('flextype.date_format'), time()), + ], 'yaml') + )) { + $this->flash->addMessage('success', __('admin_message_' . $post_data['api'] . '_api_token_updated')); + } + } else { + $this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_updated')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $post_data['api']); + } + + /** + * Delete token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function deleteProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + $api_token_dir_path = PATH['tokens'] . '/delivery/images/' . $post_data['token']; + + if (Filesystem::deleteDir($api_token_dir_path)) { + $this->flash->addMessage('success', __('admin_message_delivery_images_api_token_deleted')); + } else { + $this->flash->addMessage('error', __('admin_message_delivery_images_api_token_was_not_deleted')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_delivery_images.index')); + } +} diff --git a/site/plugins/admin/routes/web.php b/site/plugins/admin/routes/web.php index b5721381..1f147e44 100644 --- a/site/plugins/admin/routes/web.php +++ b/site/plugins/admin/routes/web.php @@ -101,15 +101,21 @@ $app->group('/' . $admin_route, function () use ($app) : void { // ApiController $app->get('/api', 'ApiController:index')->setName('admin.api.index'); - $app->get('/api/delivery', 'ApiController:deliveryIndex')->setName('admin.api_delivery.index'); - $app->get('/api/delivery/entries', 'ApiController:deliveryEntriesIndex')->setName('admin.api_delivery_entries.index'); - $app->get('/api/delivery/entries/add', 'ApiController:deliveryEntriesAdd')->setName('admin.api_delivery_entries.add'); - $app->post('/api/delivery/entries/add', 'ApiController:deliveryEntriesAddProcess')->setName('admin.api_delivery_entries.addProcess'); - $app->get('/api/delivery/entries/edit', 'ApiController:deliveryEntriesEdit')->setName('admin.api_delivery_entries.edit'); - $app->post('/api/delivery/entries/edit', 'ApiController:deliveryEntriesEditProcess')->setName('admin.api_delivery_entries.editProcess'); - $app->post('/api/delivery/entries/delete', 'ApiController:deliveryEntriesDeleteProcess')->setName('admin.api_delivery_entries.deleteProcess'); + $app->get('/api/delivery', 'ApiDeliveryController:index')->setName('admin.api_delivery.index'); + $app->get('/api/delivery/entries', 'ApiDeliveryEntriesController:index')->setName('admin.api_delivery_entries.index'); + $app->get('/api/delivery/entries/add', 'ApiDeliveryEntriesController:add')->setName('admin.api_delivery_entries.add'); + $app->post('/api/delivery/entries/add', 'ApiDeliveryEntriesController:addProcess')->setName('admin.api_delivery_entries.addProcess'); + $app->get('/api/delivery/entries/edit', 'ApiDeliveryEntriesController:edit')->setName('admin.api_delivery_entries.edit'); + $app->post('/api/delivery/entries/edit', 'ApiDeliveryEntriesController:editProcess')->setName('admin.api_delivery_entries.editProcess'); + $app->post('/api/delivery/entries/delete', 'ApiDeliveryEntriesController:deleteProcess')->setName('admin.api_delivery_entries.deleteProcess'); + + $app->get('/api/delivery/images', 'ApiDeliveryImagesController:index')->setName('admin.api_delivery_images.index'); + $app->get('/api/delivery/images/add', 'ApiDeliveryImagesController:add')->setName('admin.api_delivery_images.add'); + $app->post('/api/delivery/images/add', 'ApiDeliveryImagesController:addProcess')->setName('admin.api_delivery_images.addProcess'); + $app->get('/api/delivery/images/edit', 'ApiDeliveryImagesController:edit')->setName('admin.api_delivery_images.edit'); + $app->post('/api/delivery/images/edit', 'ApiDeliveryImagesController:editProcess')->setName('admin.api_delivery_images.editProcess'); + $app->post('/api/delivery/images/delete', 'ApiDeliveryImagesController:deleteProcess')->setName('admin.api_delivery_images.deleteProcess'); - $app->get('/api/delivery/images', 'ApiController:deliveryImagesIndex')->setName('admin.api_delivery_images.index'); })->add(new AuthMiddleware($flextype)); diff --git a/site/plugins/admin/templates/system/api/delivery/images/add.html b/site/plugins/admin/templates/system/api/delivery/images/add.html new file mode 100644 index 00000000..8bebc23a --- /dev/null +++ b/site/plugins/admin/templates/system/api/delivery/images/add.html @@ -0,0 +1,55 @@ +{% extends "plugins/admin/templates/partials/base.html" %} + +{% block content %} +
+ {{ csrf() }} + +
+
+
+ + + {{ tr('admin_help_text_for_tokens_label') }} +
+
+ + + {{ tr('admin_help_text_for_api_calls_limit_label') }} +
+
+ + + {{ tr('admin_help_text_for_api_token_icon', {':link': 'fontawesome'})|raw }} +
+
+ + +
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/site/plugins/admin/templates/system/api/delivery/images/edit.html b/site/plugins/admin/templates/system/api/delivery/images/edit.html new file mode 100644 index 00000000..67e71734 --- /dev/null +++ b/site/plugins/admin/templates/system/api/delivery/images/edit.html @@ -0,0 +1,52 @@ +{% extends "plugins/admin/templates/partials/base.html" %} + +{% block content %} +
+ {{ csrf() }} + + + + + + +
+
+
+ + + {{ tr('admin_help_text_for_tokens_label') }} +
+
+ + + {{ tr('admin_help_text_for_api_calls_limit_label') }} +
+
+ + + {{ tr('admin_help_text_for_api_token_icon', {':link': 'fontawesome'})|raw }} +
+
+ + +
+
+ +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/site/plugins/admin/templates/system/api/delivery/images/index.html b/site/plugins/admin/templates/system/api/delivery/images/index.html new file mode 100644 index 00000000..2b3b8e80 --- /dev/null +++ b/site/plugins/admin/templates/system/api/delivery/images/index.html @@ -0,0 +1,89 @@ +{% extends "plugins/admin/templates/partials/base.html" %} + +{% block content %} + {% if (tokens | length > 0) %} + + + + + + + + + + + {% for key, token in tokens %} + {% set token_file_path = PATH_TOKENS ~ '/delivery/images/' ~ token.dirname ~ '/' ~ 'token.yaml' %} + {% if filesystem_has(token_file_path) %} + {% set token_data = parser_decode(filesystem_read(token_file_path), 'yaml') %} + {% endif %} + + + + + + + + {% endfor %} + +
+ {{ tr('admin_title') }} + + {{ tr('admin_token') }} + + {{ tr('admin_calls') }} +
+ {{ icon(token_data.icon) }} + + {{ token_data.title }} + {{ token.dirname }}{{ token_data.calls }} + / + {% if (token_data.limit_calls > 0) %}{{ token_data.limit_calls }} + {% else %}∞{% endif %} + + + +
+ {% else %} +
+
+ {{ icon('fas fa-database') }} + {{ icon('fas angle-right') }} + {{ icon('fas fa-truck') }} +

{{ tr('admin_you_have_not_created_any_delvery_images_api_tokens_yet') }}

+ {{ tr('admin_create_new_delivery_images_token') }} +
+
+ {% endif %} +{% endblock %} + +{% block tail %} + +{% endblock %} \ No newline at end of file