Skip to content
Snippets Groups Projects
Commit c1eb8fed authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

form + settings save

parent ae7c0fc2
No related branches found
No related tags found
No related merge requests found
...@@ -144,29 +144,40 @@ class XML2HTMLPlugin extends GenericPlugin ...@@ -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')) { switch ($request->getUserVar('verb')) {
case 'settings': case 'settings':
AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER);
$this->import('XML2HTMLSettingsForm'); $this->import('XML2HTMLSettingsForm');
$form = new XML2HTMLSettingsForm($this, $request->getContext()->getId()); // Load the custom form
$form = new XML2HTMLSettingsForm($this);
if ($request->getUserVar('save')) { // 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(); $form->readInputData();
if ($form->validate()) { if ($form->validate()) {
$form->execute(); $form->execute();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($request->getUser()->getId());
return new JSONMessage(true); return new JSONMessage(true);
} }
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
} }
return parent::manage($args, $request); return parent::manage($args, $request);
} }
function getPluginAssetsPath($request) { function getPluginAssetsPath($request) {
return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/resources/'; return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/resources/';
} }
......
<?php <?php
import('lib.pkp.classes.form.Form'); import('lib.pkp.classes.form.Form');
import('classes.notification.NotificationManager');
class XML2HTMLSettingsForm extends Form { class XML2HTMLSettingsForm extends Form {
private static $FORMAT_SETTING = "format"; private static $FORMAT = "format";
private $_contextId; /*private $_contextId;*/
private $_plugin; private $plugin;
/** /**
* Constructor *
* @param $plugin xml2html plugin
* @param $contextId int Context ID
*/ */
function __construct($plugin, $contextId) { function __construct(XML2HTMLPlugin $plugin) {
$this->_contextId = $contextId; /*$this->_contextId = $contextId;*/
$this->_plugin = $plugin; $this->plugin = $plugin;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl')); parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidatorPost($this)); $this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($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() { public function initData()
$contextId = $this->_contextId; {
$plugin = $this->_plugin; $context = Application::get()
$this->setData('format', $plugin->getSetting($contextId, XML2HTMLSettingsForm::$FORMAT)); ->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() { public function readInputData()
$this->readUserVars(array(XML2HTMLSettingsForm::$FORMAT)); {
$this->readUserVars(['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'));
// }
parent::readInputData();
} }
/** /**
* Fetch the form. * Fetch any additional data needed for your form.
* @copydoc Form::fetch() *
* 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 = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->_plugin->getName()); $templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request);
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) { public function execute(...$functionArgs)
$plugin = $this->_plugin; {
$contextId = $this->_contextId; $context = Application::get()
->getRequest()
$plugin->updateSetting($contextId, XML2HTMLSettingsForm::$FORMAT, $this->getData(XML2HTMLSettingsForm::$FORMAT)); ->getContext();
$this->plugin->updateSetting(
parent::execute(...$functionArgs); $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();
} }
} }
...@@ -8,6 +8,17 @@ ...@@ -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}"> <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"> <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> </div>
</form> </form>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment