<?php
/**
* @file OjsWpSsoSubscriptionSettingsForm.php
*
* Copyright (c) 2024-2026 GuIT - Gruppo Utilizzatori Italiani di TeX
* Distributed under the GNU GPL v3.
*
* @class OjsWpSsoSubscriptionSettingsForm
* @brief Settings form for the OJS WP SSO Subscription plugin.
*/
namespace APP\plugins\generic\ojsWpSsoSubscription;
use APP\subscription\SubscriptionType;
use PKP\db\DAORegistry;
use PKP\form\Form;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorCSRF;
use PKP\core\PKPApplication;
class OjsWpSsoSubscriptionSettingsForm extends Form
{
private $plugin;
private $contextId;
private static $settingKeys = [
'claimName',
'subscriptionTypeId',
'subscriptionDuration',
'autoExpire',
'revalidateInterval',
'userinfoUrl',
];
public function __construct($plugin, $contextId)
{
$this->plugin = $plugin;
$this->contextId = $contextId;
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
public function initData()
{
foreach (self::$settingKeys as $key) {
$this->setData($key, $this->plugin->getSetting($this->contextId, $key));
}
}
public function readInputData()
{
$this->readUserVars(self::$settingKeys);
}
public function validate($callHooks = true)
{
if (!parent::validate($callHooks)) {
return false;
}
$contextId = (int) $this->contextId;
if ($contextId <= 0 || $contextId === PKPApplication::CONTEXT_SITE) {
return true;
}
$typeId = (int) $this->getData('subscriptionTypeId');
if ($typeId <= 0) {
return true;
}
/** @var \SubscriptionTypeDAO $typeDao */
$typeDao = DAORegistry::getDAO('SubscriptionTypeDAO');
$type = $typeDao->getById($typeId);
if (!$type || (int) $type->getJournalId() !== $contextId) {
$this->addError(
'subscriptionTypeId',
__('plugins.generic.ojsWpSsoSubscription.error.subscriptionTypeInvalid')
);
return false;
}
$fmt = (int) $type->getFormat();
if (!in_array($fmt, [
SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_ONLINE,
SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE,
], true)) {
$this->addError(
'subscriptionTypeId',
__('plugins.generic.ojsWpSsoSubscription.error.subscriptionTypeNeedsOnline')
);
return false;
}
return true;
}
public function fetch($request, $template = null, $display = false)
{
$templateMgr = \APP\template\TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
public function execute(...$functionArgs)
{
$sanitized = [];
$sanitized['claimName'] = preg_match('/^[a-z_][a-z0-9_]*$/', $this->getData('claimName'))
? $this->getData('claimName')
: 'subscription_active';
$sanitized['subscriptionTypeId'] = max(0, (int) $this->getData('subscriptionTypeId'));
$sanitized['subscriptionDuration'] = max(1, (int) ($this->getData('subscriptionDuration') ?: 12));
$sanitized['autoExpire'] = $this->getData('autoExpire') ? '1' : '';
$sanitized['revalidateInterval'] = max(0, (int) ($this->getData('revalidateInterval') ?: 300));
$sanitized['userinfoUrl'] = filter_var($this->getData('userinfoUrl'), FILTER_VALIDATE_URL) ?: '';
foreach ($sanitized as $key => $value) {
$this->plugin->updateSetting($this->contextId, $key, $value);
}
parent::execute(...$functionArgs);
}
}