ub-unibe-ch / iiifViewer

GNU General Public License v3.0
3 stars 0 forks source link

OMP 3.4 compatibility #5

Closed stutzmann72 closed 6 months ago

stutzmann72 commented 6 months ago

Description:

Acceptance:

stutzmann72 commented 6 months ago

compatible in.php file:

<?php

/**
 * @file plugins/generic/pdfJsViewer/IiifViewerPlugin.inc.php
 *
 * Copyright (c) 2014-2021 Simon Fraser University
 * Copyright (c) 2003-2021 John Willinsky
 * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
 *
 * @class IiifViewerPlugin
 * @ingroup plugins_generic_iiifViewer
 *
 * @brief Class for IiifViewer plugin
 */

import('lib.pkp.classes.plugins.GenericPlugin');

class IiifViewerPlugin extends GenericPlugin {
    /**
     * @copydoc Plugin::register()
     */
    function register($category, $path, $mainContextId = null) {
        if (parent::register($category, $path, $mainContextId)) {
            if ($this->getEnabled($mainContextId)) {
                $request = Application::get()->getRequest();
                $url = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/styles/iiifviewer.css';
                $templateMgr = TemplateManager::getManager($request);
                $templateMgr->addStyleSheet('iiifViewerStyles', $url);

                HookRegistry::register('CatalogBookHandler::view', array($this, 'viewCallback'), HOOK_SEQUENCE_NORMAL);
            }
            return true;
        }
        return false;
    }

    /**
     * Install default settings on press creation.
     * @return string
     */
    function getContextSpecificPluginSettingsFile() {
        return $this->getPluginPath() . '/settings.xml';
    }

    /**
     * Get the display name of this plugin.
     * @return String
     */
    function getDisplayName() {
        return __('plugins.generic.iiifViewer.displayName');
    }

    /**
     * Get a description of the plugin.
     */
    function getDescription() {
        return __('plugins.generic.iiifViewer.description');
    }

    /**
     * Callback to view the Image content rather than downloading.
     * @param $hookName string
     * @param $args array
     * @return boolean
     */
    function viewCallback($hookName, $args) {
        $submission =& $args[1];
        $publicationFormat =& $args[2];
        $submissionFile =& $args[3];

        $mime_type = $submissionFile->getData('mimetype');
        $format = $publicationFormat->getBestId();

        $contextid = $this->getCurrentContextId();

        if ($format == 'iiif_manifest' && (($mime_type == 'application/json') || ($mime_type == 'text/plain'))) {
            #$this->viewManifestFile($publicationFormat, $mime_type, $submission, $submissionFile, "display_manifest.tpl" );
            $this->viewImageFile($publicationFormat, $mime_type, $submission, $submissionFile, "display_manifest.tpl" );
            return true;

        } elseif ($mime_type == 'image/jpeg') {
            $this->viewImageFile($publicationFormat, $mime_type, $submission, $submissionFile, "display.tpl" );
            return true;
        }

        return false;
    }

    /**
     * function to prepare IIIFViewer data for an image file.
     * @param $publicationFormat PublicationFormat
     * @param $mime_type string
     * @param $submission Submission
     * @param $submissionFile SubmissionFile
     * @param $submission Submission
     * @param $theTemplate string
     * @return boolean
     */
    function viewImageFile($publicationFormat, $mime_type, $submission, $submissionFile, $theTemplate) {

        foreach ($submission->getData('publications') as $publication) {
            if ($publication->getId() === $publicationFormat->getData('publicationId')) {
                $filePublication = $publication;
                break;
            }
        }
        $fileService = Services::get('file');
        $imgfile = $fileService->get($submissionFile->getData('fileId'));
        $imgpath = $imgfile->path;

        $fileId = $submissionFile->getId();
        $fileStage = $submissionFile->getFileStage();
        $subStageId = $submission->getStageId();
        $submissionId = $submission->getId();

error_log("iiifviewer::viewImageFile called submissionid[".$submissionId."] fileid[".$fileId."] sub stage [".$subStageId."] filestage[".$fileStage."] mimetype [".$submissionFile->getData('mimetype')."] path[".$this->getPluginPath()."]\n");

        $request = Application::get()->getRequest();
        $router = $request->getRouter();
        $contextPath = $router->getRequestedContextPath($request, 1);

        $apiUrl = $request->getIndexUrl()."/".$contextPath.'/$$$call$$$/api/file/file-api/download-file?submissionFileId='.$fileId."&submissionId=".$submissionId."&stageId=".$subStageId;

        $templateMgr = TemplateManager::getManager($request);
        $templateMgr->assign(array(
            'apiUrl' => $apiUrl,
            'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
            'isLatestPublication' => $submission->getData('currentPublicationId') === $publicationFormat->getData('publicationId'),
            'filePublication' => $filePublication,
            'subId' => $submissionId,
            'subStageId' => $subStageId,
            'fileId' => $fileId,
        ));

        $templateMgr->display($this->getTemplateResource($theTemplate));

        return true;
    }

    /**
     * Callback for download function
     * @param $hookName string
     * @param $params array
     * @return boolean
     */
    function downloadCallback($hookName, $params) {
        $submission =& $params[1];
        $publicationFormat =& $params[2];
        $submissionFile =& $params[3];
        $inline =& $params[4];

        $request = Application::get()->getRequest();
        $mimetype = $submissionFile->getData('mimetype');
        if ($mimetype == 'application/pdf' && $request->getUserVar('inline')) {
            // Turn on the inline flag to ensure that the content
            // disposition header doesn't foil the PDF embedding
            // plugin.
            $inline = true;
        }

        // Return to regular handling
        return false;
    }

    /**
     * Get the plugin base URL.
     * @param $request PKPRequest
     * @return string
     */
    private function _getPluginUrl($request) {
        return $request->getBaseUrl() . '/' . $this->getPluginPath();
    }
}
stutzmann72 commented 6 months ago

comment Peter (Mail 11.4.):

I have looked at the changes for version 3.4 and I have updated the call that was failing so that it uses a different approach to get the same data. This should now work with version 3.4.

I have also checked the rest of the code and I note two things:

Firstly I use "Services::get('file')". This might become deprecated in the future as a lot of the "Services" calls have now been replaced. I could not find a reference to the "file" service being deprecated yet.

Secondly, I use the "$$$call$$$/api" syntax to access the manifest/jpg file. There may well be a better way to do this using the 3.4 updates to the rest api but I have not found a better way yet.

I have attached a new version of the IiifViewerPlugin.inc.php file. (The new version is committed to the Basel GitLab SWITCH repository).

stutzmann72 commented 6 months ago

See: https://books-test.unibe.ch/index.php/BB/catalog/book/13/chapter/192

stutzmann72 commented 6 months ago

3.4. compatibility is given now.