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

ajustements / factorisation omp/ojs

parents 8bccb61c 2c38a107
Loading
Loading
Loading
Loading
+82
Changes for OMPViewHtmlHandler.inc.php: 82 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);
        $html = XML2HTMLPlugin::resolveImagePaths($html, $targetFile, $request);
        $baseUrl = $request->getBaseUrl() . '/' . $plugin->getPluginPath();
        $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');
        //plugin's js
        $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml_2_html.js');
        $templateMgr->assign('text', $html);
        return $templateMgr->display($plugin->getTemplateResource('BookPublicationView.tpl'));

    }
}
+51 −45
Changes for XML2HTMLPlugin.php: 51 added lines, 45 removed lines.
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@

import('lib.pkp.classes.plugins.GenericPlugin');
import('lib.pkp.classes.file.PrivateFileManager');
import('lib.pkp.classes.file.FileManager');

class XML2HTMLPlugin extends GenericPlugin
{
@@ -23,11 +22,23 @@ class XML2HTMLPlugin extends GenericPlugin
            HookRegistry::register('TemplateManager::display', array($this, 'xml2htmlImports'));
            HookRegistry::register('LoadHandler', array($this, 'loadImageHandler'));
            HookRegistry::register('SubmissionFile::add', array($this, 'uploadHandler'));
            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
@@ -62,17 +73,24 @@ class XML2HTMLPlugin extends GenericPlugin
    {
        $templateMgr = $args[0];
        $template = $args[1];

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

        $request = $this->getRequest();
        $baseUrl = $request->getBaseUrl() . '/' . $this->getPluginPath();

        //article : OJS
        if($template === "frontend/pages/article.tpl"){
            //plugin's css
            $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');

            //plugin's js
        $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml2html.js');
            $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml_2_html.js');
            return false;
        }
        //Book: OMP
        else if ($template === "frontend/pages/book.tpl"){
            $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/omp_link_hook.js');
            return false;
        }
        else
            return false;

        return false;
    }
@@ -103,21 +121,18 @@ class XML2HTMLPlugin extends GenericPlugin
        $fileManager = new FileManager();
        $zipMode = FALSE;//true if submission file in a zip archive
        while ($sf = $resultFactory->next()) {

            $fileId = $sf->getData("fileId");
            $file = Services::get('file')->get($fileId);
            $filePath = $file->path;

            $submissionFileExtension = $fileManager->parseFileExtension($filePath);
            if ($submissionFileExtension == 'zip') {
                $zipMode = TRUE;
                $fileInfo = pathinfo($filePath);
                $xmlFilePath =
                    $filesDir . $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . '.xml';
                //$output .= "<div>".$xmlFilePath."</div>";
                //return false;
                break;
            }
            else {
            } else {
                $mimeType = $file->mimetype;
                if ($mimeType == 'text/xml') {
                    $xmlFilePath = $filesDir . $filePath;
@@ -126,12 +141,23 @@ class XML2HTMLPlugin extends GenericPlugin
            }
        }


        if (is_null($xmlFilePath)) {
            $output .= "<div>error / todo " . $xmlFilePath . "</div>";
            return false;
        }
        $html = $zipMode
            ? $this->resolveZipImagePaths(XML2HTMLPlugin::transform($xmlFilePath), $submissionId, $xmlFilePath)
            : $this->resolveImagePaths(XML2HTMLPlugin::transform($xmlFilePath), $submissionFile, $this->getRequest());
        $output .= $html;

        return false;

    }

    static function transform(
        string $xmlFilePath
    )
    {
        ////// xslt transfo
        $xsltPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . '2html.xsl';
        // Load the XML source
@@ -144,21 +170,13 @@ class XML2HTMLPlugin extends GenericPlugin
        // Configure the transformer
        $proc = new XSLTProcessor;
        $proc->importStyleSheet($xsl); // attach the xsl rules

        $html = $zipMode
            ? $this->resolveZipImagePaths($proc->transformToXML($xml), $submissionId, $xmlFilePath)
            : $this->resolveImagePaths($proc->transformToXML($xml), $submissionFile);
        $output .=  $html;

        return false;
        return $proc->transformToXML($xml);

    }

    function uploadHandler($hookName, $params){
        //PKPSubmissionFileHandler::add
        //$submissionFile = Services::get('submissionFile')->add($submissionFile, $request);
        //$request = $params[1];
        //$submission = $request->getContext(ASSOC_TYPE_SUBMISSION);
    function uploadHandler($hookName, $params)
    {

        /**@var $submissionFile SubmissionFile* */
        $submissionFile = $params[0];
        $path = $submissionFile->getData('path');
@@ -174,8 +192,7 @@ class XML2HTMLPlugin extends GenericPlugin
                $archiveFolderName = NULL;
                if ($zip->numFiles > 0) {//gets zip top folder name
                    $archiveFolderName = $zip->getNameIndex(0);
                }
                else {
                } else {
                    throw new \Exception("Bad zip Submission file.");
                }
                $zip->extractTo($zipPathParts['dirname']);
@@ -194,24 +211,12 @@ class XML2HTMLPlugin extends GenericPlugin
                            $basePath . $zipPathParts['filename'] . '.xml'
                        );
                    }

                    //$submissionDir = Services::get('submissionFile')->getSubmissionDir($request->getContext()->getId(), $submission->getId());
                }



//                $submissionDir = Services::get('submissionFile')->getSubmissionDir($request->getContext()->getId(), $submission->getId());
//                $fileId = Services::get('file')->add(
//                    $_FILES['file']['tmp_name'],
//                    $submissionDir . '/' . uniqid() . '.' . $extension
//                );


            } else {
                echo 'échec';
            }
        }
        else
        } else
            throw new \Exception('Submission file must be a zip one.');

    }
@@ -221,7 +226,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];

@@ -238,7 +244,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],
@@ -248,14 +256,12 @@ class XML2HTMLPlugin extends GenericPlugin
            'includeDependentFiles' => true,
        ]);

            $request = $this->getRequest();
        foreach ($dependentFilesIterator as $dependentFile) {
            $filePath = $request->url(
                null, 'article',
                'image',
                array(
                    $submissionFile->getData('submissionId'),
                        //$dependentFile->getData('assocId'),
                    $dependentFile->getData('fileId')
                )
            );
@@ -281,7 +287,8 @@ class XML2HTMLPlugin extends GenericPlugin

    }

    function resolveZipImagePaths(string $htmlString, int $submissionId, string $xmlFilePath) {
    function resolveZipImagePaths(string $htmlString, int $submissionId, string $xmlFilePath)
    {
        $pathInfo = pathinfo($xmlFilePath);
        $extractedZipPath = $pathInfo['dirname'];
        $files = scandir($extractedZipPath);
@@ -295,7 +302,6 @@ class XML2HTMLPlugin extends GenericPlugin
                    'zimage',
                    array(
                        $submissionId,
                        //$dependentFile->getData('assocId'),
                        $file
                    )
                );
+1
Changes for resources/javascript/xml_2_html.js: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ window.addEventListener('DOMContentLoaded', (event) => {
        }
    });


});


+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.