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
30 changes: 30 additions & 0 deletions AllowedUploadsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @file plugins/generic/allowedUploads/AllowedUploadsForm.php
*
*
* @brief Form component for AllowedUploads plugin settings
*/

namespace APP\plugins\generic\allowedUploads;

use PKP\components\forms\FieldText;
use PKP\components\forms\FormComponent;

class AllowedUploadsForm extends FormComponent
{
public $id = 'allowedUploadsSettings';
public $method = 'PUT';

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

$this->addField(new FieldText('allowedExtensions', [
'label' => __('plugins.generic.allowedUploads.manager.settings.allowedExtensions'),
'description' => __('plugins.generic.allowedUploads.manager.settings.description'),
'value' => '',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this changes anything based on the updates here, but how does the plugin handle an empty string for this value? Is an empty string distinct from the idea of a null value? (Not necessarily in the form field, but for the setting value in general?)

]));
}
}
87 changes: 42 additions & 45 deletions AllowedUploadsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
namespace APP\plugins\generic\allowedUploads;

use APP\core\Application;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\core\APIRouter;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\linkAction\request\VueModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

class AllowedUploadsPlugin extends GenericPlugin
{
private AllowedUploadsSettingsController $controller;

/**
* @copydoc Plugin::register()
*
Expand All @@ -38,10 +39,17 @@ public function register($category, $path, $mainContextId = null)
return true;
}
if ($success && $this->getEnabled($mainContextId)) {

Hook::add('SubmissionFile::validate', $this->checkUploadWizard(...));
Hook::add('SubmissionFile::validate', $this->checkUploadWizard(...));
Hook::add('submissionfilesuploadform::validate', $this->checkUpload(...));

$this->controller = new AllowedUploadsSettingsController($this);

Hook::add('APIHandler::endpoints::plugin', function (string $hookName, APIRouter $apiRouter): bool {
$apiRouter->registerPluginApiControllers([
$this->controller,
]);
return Hook::CONTINUE;
});
}
return $success;
}
Expand All @@ -67,48 +75,37 @@ public function getDescription()
*/
public function getActions($request, $verb)
{
$router = $request->getRouter();
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
$actions = parent::getActions($request, $verb);

/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', $this->smartyPluginUrl(...));

$form = new AllowedUploadsSettingsForm($this, $context->getId());

if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
if (!$this->getEnabled()) {
return $actions;
}
return parent::manage($args, $request);

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

$form = new AllowedUploadsForm($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
45 changes: 45 additions & 0 deletions AllowedUploadsSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @file plugins/generic/allowedUploads/AllowedUploadsSettingsController.php
*
* @class AllowedUploadsSettingsController
*
* @brief API controller for AllowedUploads plugin settings
*/

namespace APP\plugins\generic\allowedUploads;

use APP\plugins\generic\allowedUploads\formRequests\EditAllowedUploadsSettings;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PKP\plugins\PluginSettingsController;

class AllowedUploadsSettingsController extends PluginSettingsController
{

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

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

public function edit(EditAllowedUploadsSettings $illuminateRequest): JsonResponse
{
$contextId = $this->getRequest()->getContext()->getId();
$allowedExtensions = $illuminateRequest->validated()['allowedExtensions'];

$this->plugin->updateSetting($contextId, 'allowedExtensions', $allowedExtensions);

return response()->json(
['allowedExtensions' => $allowedExtensions],
Response::HTTP_OK
);
}

}
92 changes: 0 additions & 92 deletions AllowedUploadsSettingsForm.php

This file was deleted.

40 changes: 40 additions & 0 deletions formRequests/EditAllowedUploadsSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @file plugins/generic/allowedUploads/formRequests/EditAllowedUploadsSettings.php
*
* Copyright (c) 2014-2026 Simon Fraser University
* Copyright (c) 2003-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class EditAllowedUploadsSettings
*
* @ingroup plugins_generic_allowedUploads
*
* @brief Handle validation for updating AllowedUploads plugin settings
*/

namespace APP\plugins\generic\allowedUploads\formRequests;

use Illuminate\Foundation\Http\FormRequest;

class EditAllowedUploadsSettings extends FormRequest
{
public function rules(): array
{
return [
'allowedExtensions' => [
'required',
'string',
'regex:/^\s*[a-zA-Z0-9]+(\s*;\s*[a-zA-Z0-9]+)*\s*$/',
],
];
}

public function messages(): array
{
return [
'allowedExtensions.regex' => __('plugins.generic.allowedUploads.manager.settings.validationError'),
];
}
}
3 changes: 3 additions & 0 deletions locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ msgstr "Add a semicolon separated list of allowed file extensions. For example d

msgid "plugins.generic.allowedUploads.manager.settings.allowedExtensions"
msgstr "Allowed file extensions"

msgid "plugins.generic.allowedUploads.manager.settings.validationError"
msgstr "Use semicolons to separate extensions (e.g. doc;pdf;docx)."
31 changes: 0 additions & 31 deletions templates/settingsForm.tpl

This file was deleted.