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

handler, js et template spécifique à OMP

parent 5143eb73
Loading
Loading
Loading
Loading
+81
Changes for OMPViewHtmlHandler.inc.php: 81 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

import('classes.handler.Handler');
import('lib.pkp.classes.file.PrivateFileManager');

class OMPViewHtmlHandler extends Handler
{
    function __construct()
    {
        parent::__construct();
    }

    function authorize($request, &$args, $roleAssignments)
    {
        import('classes.security.authorization.OmpPublishedSubmissionAccessPolicy');
        $this->addPolicy(new OmpPublishedSubmissionAccessPolicy($request, $args, $roleAssignments));
        return parent::authorize($request, $args, $roleAssignments);
    }

    public function viewhtml($args, $request)
    {

        $plugin = PluginRegistry::getPlugin('generic', 'xml2htmlplugin');
        $requiredFileId = $args[2];
        $templateMgr = TemplateManager::getManager($request);
        $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
        $this->setupTemplate($request);
        $publication = $submission->getCurrentPublication();
        if (!$publication || $publication->getData('status') !== STATUS_PUBLISHED) {
            $request->getDispatcher()->handle404();
        }

        $pubFormatFiles = Services::get('submissionFile')->getMany([
            'submissionIds' => [$submission->getId()],
            'assocTypes' => [ASSOC_TYPE_PUBLICATION_FORMAT]
        ]);

        // Provide the publication formats to the template
        $availablePublicationFormats = [];
        $availableRemotePublicationFormats = [];
        foreach ($publication->getData('publicationFormats') as $format) {
            if ($format->getIsAvailable()) {
                $availablePublicationFormats[] = $format;
                if ($format->getRemoteURL()) {
                    $availableRemotePublicationFormats[] = $format;
                }
            }
        }
        $availableFiles = [];
        foreach ($pubFormatFiles as $pubFormatFile) {
            if ($pubFormatFile->getDirectSalesPrice() !== null) {
                $availableFiles[] = $pubFormatFile;
            }
        }

        $targetFile = NULL;
        foreach ($availableFiles as $submissionFile) {
            foreach ($availablePublicationFormats as $format) {
                if ($submissionFile->getData('assocId') == $format->getId() &&
                    (int)$requiredFileId === $submissionFile->getId()) {
                    $targetFile = $submissionFile;
                    break;
                }
            }
        }

        $filePath = $targetFile->getData('path');
        $fileMgr = new PrivateFileManager();
        $path = $fileMgr->getBasePath();
        $filesDir = $path . DIRECTORY_SEPARATOR;
        $xmlFilePath = $filesDir . $filePath;
        $html = XML2HTMLPlugin::transform($xmlFilePath, $targetFile, $request);
        $baseUrl = $request->getBaseUrl() . '/' . $plugin->getPluginPath();
        $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');
        //plugin's js
        $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml2html.js');
        $templateMgr->assign('text', $html);
        return $templateMgr->display($plugin->getTemplateResource('BookPublicationView.tpl'));

    }
}
+50 −11
Changes for XML2HTMLPlugin.php: 50 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
 */

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

class XML2HTMLPlugin extends GenericPlugin
{
    public function register($category, $path, $mainContextId = NULL)
@@ -18,13 +17,25 @@ class XML2HTMLPlugin extends GenericPlugin

        if ($success && $this->getEnabled()) {
            HookRegistry::register('Templates::Article::Main', array($this, 'xml2html'));
            HookRegistry::register('Templates::Catalog::Book::Main', array($this, 'xml2html'));
            HookRegistry::register('TemplateManager::display', array($this, 'xml2htmlImports'));
            HookRegistry::register('LoadHandler', array($this, 'loadImageHandler'));
            HookRegistry::register('LoadHandler', array($this, 'setPageHandler'));
        }

        return $success;
    }

    public function setPageHandler($hookName, $params) {
        $page = $params[0];
        $op = $params[1];
        if ($page === 'catalog' && $op === 'viewhtml') {
            define('HANDLER_CLASS', 'OMPViewHtmlHandler');
            import('plugins.generic.xml2html.OMPViewHtmlHandler');
            return true;
        }
        return false;
    }

    /**
     * Provide a name for this plugin
@@ -59,8 +70,8 @@ class XML2HTMLPlugin extends GenericPlugin
    {
        $templateMgr = $args[0];
        $template = $args[1];

        if ($template !== "frontend/pages/article.tpl") return false;
        if ($template !== "frontend/pages/article.tpl" &&
            $template !== "frontend/pages/book.tpl") return false;

        $request = $this->getRequest();
        $baseUrl = $request->getBaseUrl() . '/' . $this->getPluginPath();
@@ -99,6 +110,7 @@ class XML2HTMLPlugin extends GenericPlugin
        $imgFiles = [];
        $submissionFile = null;
        while ($sf = $resultFactory->next()) {

            $fileId = $sf->getData("fileId");
            $file = Services::get('file')->get($fileId);
            $filePath = $file->path;
@@ -115,6 +127,31 @@ class XML2HTMLPlugin extends GenericPlugin
            return false;
        }

//        ////// xslt transfo
//        $xsltPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . '2html.xsl';
//        // Load the XML source
//        $xml = new DOMDocument;
//        $xml->load($xmlFilePath);
//
//        $xsl = new DOMDocument;
//        $xsl->load($xsltPath);
//
//        // Configure the transformer
//        $proc = new XSLTProcessor;
//        $proc->importStyleSheet($xsl); // attach the xsl rules
//
//        $html = $this->resolveImagePaths($proc->transformToXML($xml), $submissionFile);
        $output .= XML2HTMLPlugin::transform($xmlFilePath, $submissionFile, $this->getRequest());

        return false;

    }

    static function transform(
        string $xmlFilePath,
        SubmissionFile $submissionFile,
        Request $request
    ){
        ////// xslt transfo
        $xsltPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . '2html.xsl';
        // Load the XML source
@@ -128,11 +165,10 @@ class XML2HTMLPlugin extends GenericPlugin
        $proc = new XSLTProcessor;
        $proc->importStyleSheet($xsl); // attach the xsl rules

        $html = $this->resolveImagePaths($proc->transformToXML($xml), $submissionFile);
        $output .= $html;

        return false;

        return  XML2HTMLPlugin::resolveImagePaths(
            $proc->transformToXML($xml),
            $submissionFile,
            $request);
    }

    /**
@@ -140,7 +176,8 @@ class XML2HTMLPlugin extends GenericPlugin
     * @param $args array
     * @brief Handle associated files of the full-text, only images are supported
     */
    function loadImageHandler($hookName, array $args) {
    function loadImageHandler($hookName, array $args)
    {
        $page = $args[0];
        $op = $args[1];

@@ -157,7 +194,9 @@ class XML2HTMLPlugin extends GenericPlugin
     * @return string
     * @brief Substitute path to attached images in html source
     */
    function resolveImagePaths(string $htmlString, SubmissionFile $submissionFile) {
    static function resolveImagePaths(
        string $htmlString, SubmissionFile $submissionFile, Request $request)
    {

        $dependentFilesIterator = Services::get('submissionFile')->getMany([
            'assocTypes' => [ASSOC_TYPE_SUBMISSION_FILE],
@@ -167,7 +206,7 @@ class XML2HTMLPlugin extends GenericPlugin
            'includeDependentFiles' => true,
        ]);

            $request = $this->getRequest();
        $request = $request;//$this->getRequest();
        foreach ($dependentFilesIterator as $dependentFile) {
            $filePath = $request->url(
                null, 'article',
+5
Changes for resources/javascript/xml2html.js: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -29,6 +29,11 @@ window.addEventListener('DOMContentLoaded', (event) => {
        }
    });

    //omp specific, tests
    document.querySelectorAll(".page_book .files .cmp_download_link").forEach((link) => {
        link.setAttribute('href',link.getAttribute('href').replace('view','viewhtml'))
    })

});


+5
Changes for templates/BookPublicationView.tpl: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
{include file="frontend/components/header.tpl"}
<div>
	{$text}
</div>
{include file="frontend/components/footer.tpl"}
−3

File deleted.