From c1eb8fedfd82964a1ee04dd044a6e38c742cc912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Chauveau?= <jerome.chauveau@unicaen.fr> Date: Wed, 17 Jan 2024 11:36:44 +0100 Subject: [PATCH] form + settings save --- XML2HTMLPlugin.php | 39 +++++++----- XML2HTMLSettingsForm.inc.php | 111 +++++++++++++++++++++++------------ templates/settingsForm.tpl | 13 +++- 3 files changed, 109 insertions(+), 54 deletions(-) diff --git a/XML2HTMLPlugin.php b/XML2HTMLPlugin.php index 7032452..dc7b9b2 100644 --- a/XML2HTMLPlugin.php +++ b/XML2HTMLPlugin.php @@ -144,29 +144,40 @@ class XML2HTMLPlugin extends GenericPlugin } - public function manage($args, $request) { + /** + * Load a form when the `settings` button is clicked and + * save the form when the user saves it. + * + * @param array $args + * @param Request $request + * @return JSONMessage + */ + public function manage($args, $request) + { switch ($request->getUserVar('verb')) { case 'settings': - AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER); $this->import('XML2HTMLSettingsForm'); - $form = new XML2HTMLSettingsForm($this, $request->getContext()->getId()); - - if ($request->getUserVar('save')) { - $form->readInputData(); - if ($form->validate()) { - $form->execute(); - $notificationManager = new NotificationManager(); - $notificationManager->createTrivialNotification($request->getUser()->getId()); - return new JSONMessage(true); - } - } else { + // Load the custom form + $form = new XML2HTMLSettingsForm($this); + + // Fetch the form the first time it loads, before + // the user has tried to save it + if (!$request->getUserVar('save')) { $form->initData(); + return new JSONMessage(true, $form->fetch($request)); + } + + // Validate and save the form data + $form->readInputData(); + if ($form->validate()) { + $form->execute(); + return new JSONMessage(true); } - return new JSONMessage(true, $form->fetch($request)); } return parent::manage($args, $request); } + function getPluginAssetsPath($request) { return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/resources/'; } diff --git a/XML2HTMLSettingsForm.inc.php b/XML2HTMLSettingsForm.inc.php index de1f510..cd70433 100644 --- a/XML2HTMLSettingsForm.inc.php +++ b/XML2HTMLSettingsForm.inc.php @@ -1,73 +1,106 @@ <?php - import('lib.pkp.classes.form.Form'); +import('classes.notification.NotificationManager'); + class XML2HTMLSettingsForm extends Form { - private static $FORMAT_SETTING = "format"; - private $_contextId; + private static $FORMAT = "format"; + /*private $_contextId;*/ - private $_plugin; + private $plugin; /** - * Constructor - * @param $plugin xml2html plugin - * @param $contextId int Context ID + * */ - function __construct($plugin, $contextId) { - $this->_contextId = $contextId; - $this->_plugin = $plugin; + function __construct(XML2HTMLPlugin $plugin) { + /*$this->_contextId = $contextId;*/ + $this->plugin = $plugin; parent::__construct($plugin->getTemplateResource('settingsForm.tpl')); $this->addCheck(new FormValidatorPost($this)); $this->addCheck(new FormValidatorCSRF($this)); } + /** - * Initialize form data. + * Load settings already saved in the database + * + * Settings are stored by context, so that each journal, press, + * or preprint server can have different settings. */ - function initData() { - $contextId = $this->_contextId; - $plugin = $this->_plugin; - $this->setData('format', $plugin->getSetting($contextId, XML2HTMLSettingsForm::$FORMAT)); - + public function initData() + { + $context = Application::get() + ->getRequest() + ->getContext(); + + $this->setData( + 'format', + $this->plugin->getSetting( + $context->getId(), + 'format' + ) + ); + + parent::initData(); } + /** - * Assign form data to user-submitted data. + * Load data that was submitted with the form */ - function readInputData() { - $this->readUserVars(array(XML2HTMLSettingsForm::$FORMAT)); - -// -// // if recent items is selected, check that we have a value -// if ($this->getData('displayItems') == 'recent') { -// $this->addCheck(new FormValidator($this, 'recentItems', 'required', 'plugins.generic.webfeed.settings.recentItemsRequired')); -// } + public function readInputData() + { + $this->readUserVars(['format']); + parent::readInputData(); } + /** - * Fetch the form. - * @copydoc Form::fetch() + * Fetch any additional data needed for your form. + * + * Data assigned to the form using $this->setData() during the + * initData() or readInputData() methods will be passed to the + * template. + * + * In the example below, the plugin name is passed to the + * template so that it can be used in the URL that the form is + * submitted to. */ - function fetch($request, $template = null, $display = false) { + public function fetch($request, $template = null, $display = false) + { $templateMgr = TemplateManager::getManager($request); - $templateMgr->assign('pluginName', $this->_plugin->getName()); - return parent::fetch($request); + $templateMgr->assign('pluginName', $this->plugin->getName()); + + return parent::fetch($request, $template, $display); } /** - * @copydoc Form::execute() + * Save the plugin settings and notify the user + * that the save was successful */ - function execute(...$functionArgs) { - $plugin = $this->_plugin; - $contextId = $this->_contextId; - - $plugin->updateSetting($contextId, XML2HTMLSettingsForm::$FORMAT, $this->getData(XML2HTMLSettingsForm::$FORMAT)); - - - parent::execute(...$functionArgs); + public function execute(...$functionArgs) + { + $context = Application::get() + ->getRequest() + ->getContext(); + + $this->plugin->updateSetting( + $context->getId(), + 'format', + $this->getData('format') + ); + + $notificationMgr = new NotificationManager(); + $notificationMgr->createTrivialNotification( + Application::get()->getRequest()->getUser()->getId(), + NOTIFICATION_TYPE_SUCCESS, + ['contents' => __('common.changesSaved')] + ); + + return parent::execute(); } } diff --git a/templates/settingsForm.tpl b/templates/settingsForm.tpl index 875ae0d..7770e53 100644 --- a/templates/settingsForm.tpl +++ b/templates/settingsForm.tpl @@ -8,6 +8,17 @@ <form class="pkp_form" id="xml2htmlSettingsForm" method="post" action="{url router=$smarty.const.ROUTE_COMPONENT op="manage" category="generic" plugin=$pluginName verb="settings" save=true}"> <div id="xml2htmlSettings"> - TEST + {csrf} + {fbvFormSection label="plugins.generic.xml2html.format"} + <select name="format"> + <option value="JATS" {if $format=='JATS'} + selected + {/if}>JATS</option> + <option value="TEI" {if $format=='TEI'} + selected + {/if}>TEI</option> + </select> + {/fbvFormSection} + {fbvFormButtons submitText="common.save"} </div> </form> -- GitLab