Commit aa4a3c52 authored by Jérôme Chauveau's avatar Jérôme Chauveau
Browse files

form + settings save

parent 75c69aaa
Loading
Loading
Loading
Loading
+21 −10
Changes for XML2HTMLPlugin.php: 21 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -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());
                // 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();
                if ($form->validate()) {
                    $form->execute();
                        $notificationManager = new NotificationManager();
                        $notificationManager->createTrivialNotification($request->getUser()->getId());
                    return new JSONMessage(true);
                }
                } else {
                    $form->initData();
                }
                return new JSONMessage(true, $form->fetch($request));
        }
        return parent::manage($args, $request);
    }


    function getPluginAssetsPath($request) {
        return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/resources/';
    }
+72 −39
Changes for XML2HTMLSettingsForm.inc.php: 72 added lines, 39 removed lines.
Original line number Diff line number Diff line
<?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();
    }
}
+12 −1
Changes for templates/settingsForm.tpl: 12 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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>