Problem uploading file to plugin settings - is it possible?

Hi,
I am currently creating a plugin designed to tabulate some JSON (or CSV) data that is uploaded to the plugin settings page in the backend interface.

I am facing a couple of issues, but I shall post individually for those as I don’t believe they’re that closely related. The first issue is related to file uploads.

The settings page is appearing, but nothing seems to be saved when I add a file:

Capture

The template used to upload the files is as follows:-

<script>
	$(function() {ldelim}
		$('#rvskStrengthOfEvidenceSettings').pkpHandler('$.pkp.controllers.form.AjaxFormHandler');
	{rdelim});
</script>

<form
  class="pkp_form"
  id="rvskStrengthOfEvidenceSettings"
  method="POST"
  action="{url router=$smarty.const.ROUTE_COMPONENT op="manage" category="generic" plugin=$pluginName verb="settings" save=true}"
>
  <!-- Always add the csrf token to secure your form -->
	{csrf}
    Please upload the .CSV files holding strength of evidence data for Strong, Moderate and Weak.
  {fbvFormArea}
		{fbvFormSection}
			{fbvElement
        type="file"
        id="strongEvidence"
        value=$strongEvidence
        label="Upload Strong Evidence"
      }
      {/fbvFormSection}
      {fbvFormSection}
      {fbvElement
        type="file"
        id="moderateEvidence"
        value=$moderateEvidence
        label="Upload Moderate Evidence"
      }
      {/fbvFormSection}
      {fbvFormSection}
      {fbvElement
        type="file"
        id="weakEvidence"
        value=$weakEvidence
        label="Upload Weak Evidence"
      }
		{/fbvFormSection}
  {/fbvFormArea}
	{fbvFormButtons submitText="common.save"}
</form>

The class that handles the form is:-

<?php
import('lib.pkp.classes.form.Form');
class RCVSKStrengthOfEvidenceSettingsForm extends Form {

  /** @var RCVSKStrengthOfEvidencePlugin  */
  public $plugin;

	public function __construct($plugin) {

    // Define the settings template and store a copy of the plugin object
		parent::__construct($plugin->getTemplateResource('settings.tpl'));
    $this->plugin = $plugin;

    // Always add POST and CSRF validation to secure your form.
		$this->addCheck(new FormValidatorPost($this));
		$this->addCheck(new FormValidatorCSRF($this));
	}

  /**
   * Load settings already saved in the database
   *
   * Settings are stored by context, so that each journal or press
   * can have different settings.
   */
	public function initData() {
    $contextId = Application::get()->getRequest()->getContext()->getId();
    $this->setData('strongEvidence', $this->plugin->getSetting($contextId, 'strongEvidence'));
    $this->setData('moderateEvidence', $this->plugin->getSetting($contextId, 'moderateEvidence'));
    $this->setData('weakEvidence', $this->plugin->getSetting($contextId, 'weakEvidence'));

    parent::initData();
	}

  /**
   * Load data that was submitted with the form
   */
	public function readInputData() {
		$this->readUserVars(['strongEvidence','moderateEvidence','weakEvidence']);
    parent::readInputData();
  }

  /**
   * 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.
   */
	public function fetch($request, $template = null, $display = false) {

    // Pass the plugin name to the template so that it can be
    // used in the URL that the form is submitted to
    $templateMgr = TemplateManager::getManager($request);
    $templateMgr->assign('pluginName', $this->plugin->getName());

    return parent::fetch($request, $template, $display);
  }

	/**
	 * Save the settings
	 */
	public function execute() {
    $contextId = Application::get()->getRequest()->getContext()->getId();
		$this->plugin->updateSetting($contextId, 'strongEvidence', $this->getData('strongEvidence'));
    $this->plugin->updateSetting($contextId, 'moderateEvidence', $this->getData('moderateEvidence'));
    $this->plugin->updateSetting($contextId, 'weakEvidence', $this->getData('weakEvidence'));

    // Tell the user that the save was successful.
		import('classes.notification.NotificationManager');
		$notificationMgr = new NotificationManager();
		$notificationMgr->createTrivialNotification(
      Application::get()->getRequest()->getUser()->getId(),
      NOTIFICATION_TYPE_SUCCESS,
      ['contents' => __('common.changesSaved')]
    );

		return parent::execute();
	}
}

I based the code on one of the examples in the Plugin Guide

I have been looking at the guide constantly and am unsure why the files are not being uploaded/allowed. It raises the question - does the system allow for file uploads in this manner? Am I doing something wrong or am I just barking up the wrong tree? :slight_smile:

I am using OJS Verion 3.3.0.10

If I can get the files to upload, be stored (somewhere) and then be available to the theme/templates then that will be progress!

Kind regards,

Anthony

1 Like