Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions PluginTemplatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* @file PluginTemplatePlugin.php
*
* Copyright (c) 2017-2023 Simon Fraser University
* Copyright (c) 2017-2023 John Willinsky
* Copyright (c) 2017-2026 Simon Fraser University
* Copyright (c) 2017-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PluginTemplatePlugin
Expand All @@ -12,25 +12,41 @@

namespace APP\plugins\generic\pluginTemplate;

use APP\core\Request;
use APP\core\Application;
use APP\plugins\generic\pluginTemplate\classes\FrontEnd\ArticleDetails;
use APP\plugins\generic\pluginTemplate\classes\Settings\Actions;
use APP\plugins\generic\pluginTemplate\classes\Settings\Manage;
use PKP\core\JSONMessage;
use PKP\core\APIRouter;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\VueModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

class PluginTemplatePlugin extends GenericPlugin
{
private PluginTemplateSettingsController $controller;

/** @copydoc GenericPlugin::register() */
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path);
$success = parent::register($category, $path, $mainContextId);

if (Application::isUnderMaintenance()) {
return $success;
}

if ($success && $this->getEnabled()) {
if ($success && $this->getEnabled($mainContextId)) {
// Display the publication statement on the article details page
$articleDetails = new ArticleDetails($this);
Hook::add('Templates::Article::Main', $articleDetails->addPublicationStatement(...));

// Register the settings API controller
$this->controller = new PluginTemplateSettingsController($this);

Hook::add('APIHandler::endpoints::plugin', function (string $hookName, APIRouter $apiRouter): bool {
$apiRouter->registerPluginApiControllers([
$this->controller,
]);
return Hook::CONTINUE;
});
}

return $success;
Expand Down Expand Up @@ -61,26 +77,42 @@ public function getDescription(): string
/**
* Add a settings action to the plugin's entry in the plugins list.
*
* @param Request $request
* @param \APP\core\Request $request
* @param array $actionArgs
*/
public function getActions($request, $actionArgs): array
{
$actions = new Actions($this);
return $actions->execute($request, $actionArgs, parent::getActions($request, $actionArgs));
}
$actions = parent::getActions($request, $actionArgs);

/**
* Load a form when the `settings` button is clicked and
* save the form when the user saves it.
*
* @param array $args
* @param Request $request
*/
public function manage($args, $request): JSONMessage
{
$manage = new Manage($this);
return $manage->execute($args, $request);
if (!$this->getEnabled()) {
return $actions;
}

$context = $request->getContext();
$apiUrl = $request->getDispatcher()->url(
$request,
Application::ROUTE_API,
$context->getPath(),
$this->controller->getHandlerPath()
);

$form = new PluginTemplateSettingsForm($apiUrl);

array_unshift($actions, new LinkAction(
'settings',
new VueModal(
'PkpFormModal',
[
'title' => $this->getDisplayName(),
'formConfig' => $form->getConfig(),
'getApiUrl' => $apiUrl,
]
),
__('manager.plugins.settings'),
null
));

return $actions;
}
}

Expand Down
48 changes: 48 additions & 0 deletions PluginTemplateSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @file plugins/generic/pluginTemplate/PluginTemplateSettingsController.php
*
* Copyright (c) 2017-2026 Simon Fraser University
* Copyright (c) 2017-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PluginTemplateSettingsController
*
* @brief API controller for PluginTemplate plugin settings
*/

namespace APP\plugins\generic\pluginTemplate;

use APP\plugins\generic\pluginTemplate\classes\Constants;
use APP\plugins\generic\pluginTemplate\formRequests\EditPluginTemplateSettings;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PKP\plugins\PluginSettingsController;

class PluginTemplateSettingsController extends PluginSettingsController
{
public function get(Request $illuminateRequest): JsonResponse
{
$contextId = $this->getRequest()->getContext()->getId();

return response()->json(
[Constants::PUBLICATION_STATEMENT => $this->plugin->getSetting($contextId, Constants::PUBLICATION_STATEMENT) ?? ''],
Response::HTTP_OK
);
}

public function edit(EditPluginTemplateSettings $illuminateRequest): JsonResponse
{
$contextId = $this->getRequest()->getContext()->getId();
$publicationStatement = $illuminateRequest->validated()[Constants::PUBLICATION_STATEMENT];

$this->plugin->updateSetting($contextId, Constants::PUBLICATION_STATEMENT, $publicationStatement);

return response()->json(
[Constants::PUBLICATION_STATEMENT => $publicationStatement],
Response::HTTP_OK
);
}
}
36 changes: 36 additions & 0 deletions PluginTemplateSettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @file plugins/generic/pluginTemplate/PluginTemplateSettingsForm.php
*
* Copyright (c) 2017-2026 Simon Fraser University
* Copyright (c) 2017-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PluginTemplateSettingsForm
*
* @brief Form component for PluginTemplate plugin settings
*/

namespace APP\plugins\generic\pluginTemplate;

use APP\plugins\generic\pluginTemplate\classes\Constants;
use PKP\components\forms\FieldText;
use PKP\components\forms\FormComponent;

class PluginTemplateSettingsForm extends FormComponent
{
public $id = 'pluginTemplateSettings';
public $method = 'PUT';

public function __construct(string $action)
{
$this->action = $action;

$this->addField(new FieldText(Constants::PUBLICATION_STATEMENT, [
'label' => __('plugins.generic.pluginTemplate.publicationStatement'),
'description' => __('plugins.generic.pluginTemplate.publicationStatement.description'),
'value' => '',
]));
}
}
5 changes: 0 additions & 5 deletions classes/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

class Constants
{
/**
* The file name of the settings template
*/
public const SETTINGS_TEMPLATE = 'settings.tpl';

/**
* The name of the publication statement,
* used to save to the database and to show on the front end.
Expand Down
75 changes: 0 additions & 75 deletions classes/Settings/Actions.php

This file was deleted.

62 changes: 0 additions & 62 deletions classes/Settings/Manage.php

This file was deleted.

Loading