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

Le plugin n'intervient qu'à l'affichage des galleys au format zip....

Le plugin n'intervient qu'à l'affichage des galleys au format zip. Simplification du code, factoristation OJS/OMP.
parent d850512c
Loading
Loading
Loading
Loading
−98

File deleted.

+75 −159
Changes for XML2HTMLPlugin.php: 75 added lines, 159 removed lines.
Original line number Diff line number Diff line
@@ -18,28 +18,83 @@ class XML2HTMLPlugin extends GenericPlugin
        $success = parent::register($category, $path);

        if ($success && $this->getEnabled()) {
            HookRegistry::register('Templates::Article::Main', array($this, 'xml2html'));
            HookRegistry::register('TemplateManager::display', array($this, 'xml2htmlImports'));
            HookRegistry::register('ArticleHandler::view::galley', array($this, 'articleHandler'));
            HookRegistry::register('ArticleHandler::download', array($this, 'articleDownloadCallback'), HOOK_SEQUENCE_LATE);
//            HookRegistry::register('Templates::Article::Main', array($this, 'xml2html'));
//            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'));
//            HookRegistry::register('SubmissionFile::add', array($this, 'uploadHandler'));
//            HookRegistry::register('LoadHandler', array($this, 'setPageHandler'));

            //HookRegistry::register('LoadHandler', array($this, 'setPageHandler'));

            HookRegistry::register('CatalogBookHandler::view', array($this, 'catalogHandler'));

        }

        return $success;
    }

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

    }

    /**
     * @throws Exception
     */
    public function articleHandler($hookName, $params)
    {
        $request =& $params[0];
        $galley =& $params[2];
        $submission = &$params[3];
        $templateMgr = TemplateManager::getManager($request);
        //handle only on zip galleys - supposed to be an xml archive (source + images)
        $zipMode = $galley && $galley->getFileType() === 'application/zip';
        if ($zipMode) {
            $this->archiveView($galley->getFile()->getData('path'), $submission->getId(),'article');
//            $filePath = $galley->getFile()->getData('path');
//            $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($filePath);
//            $html = $this->resolveZipImagePaths(XML2HTMLPlugin::transform($xmlFilePath), $submission->getId(), $xmlFilePath, $this->getRequest());
//            $baseUrl = $request->getBaseUrl() . '/' . $this->getPluginPath();
//            $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');
//            $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml_2_html.js');
//            $templateMgr->assign('text', $html);
//            $templateMgr->display($this->getTemplateResource('HTMLGalleyView.tpl'));
            return true;
        } else return false;
    }

    public function catalogHandler($hookName, $params)
    {

        $submission = &$params[1];
        $submissionFile =& $params[3];
        $zipMode = $submissionFile->getData('mimetype') === "application/zip";
        if ($zipMode) {
            $this->archiveView($submissionFile->getData('path'), $submission->getId(),'catalog');
            return true;
        } else
            return false;
    }


    private function archiveView(string $filePath, int $submissionId, string $op){
        $templateMgr = TemplateManager::getManager($this->getRequest());
        $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($filePath);
        $html = $this->resolveZipImagePaths(
            XML2HTMLPlugin::transform($xmlFilePath),
            $submissionId,
            $xmlFilePath, $this->getRequest(),
            $op);
        $baseUrl = $this->getRequest()->getBaseUrl() . '/' . $this->getPluginPath();
        $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');
        $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml_2_html.js');
        $templateMgr->assign('text', $html);
        $templateMgr->display($this->getTemplateResource('HTMLGalleyView.tpl'));
    }


    /**
     * Provide a name for this plugin
     *
@@ -63,96 +118,6 @@ class XML2HTMLPlugin extends GenericPlugin
    }


    /**
     * @param string $hookname
     * @param array $args
     * @return bool
     * @brief theme-specific styles & js for galley and article landing page
     */
    function xml2htmlImports(string $hookname, array $args)
    {
        $templateMgr = $args[0];
        $template = $args[1];
        $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/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;
    }


    /**
     * @param string $hookname
     * @param array $args
     * @return bool
     * @brief Displays transformed JATS source as HTML on article landing page
     * @throws Exception
     */
    function xml2html(string $hookname, array $args)
    {
        $templateMgr =& $args[1];
        $output =& $args[2];
        $publication = $templateMgr->getTemplateVars('publication');
        $submissionId = $publication->getData('submissionId');
        $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');

        //retrieve submission file to get its path
        $result = $submissionFileDao->retrieve('SELECT * FROM submission_files where submission_id = ?', [$submissionId]);
        $resultFactory = new DAOResultFactory($result, $submissionFileDao, '_fromRow');
        $fileMgr = new PrivateFileManager();
        $filesDir = $fileMgr->getBasePath() . DIRECTORY_SEPARATOR;
        $xmlFilePath = NULL;
        $submissionFile = null;
        $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';
                break;
            } else {
                $mimeType = $file->mimetype;
                if ($mimeType == 'text/xml') {
                    $xmlFilePath = $filesDir . $filePath;
                    $submissionFile = $sf;
                }
            }
        }

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

        return false;

    }

    static function transform(
        string $xmlFilePath
@@ -174,71 +139,22 @@ class XML2HTMLPlugin extends GenericPlugin

    }

    /**
     * @throws Exception
     */
    function uploadHandler($hookName, $params)
    {

        /**@var $submissionFile SubmissionFile* */
        $submissionFile = $params[0];
        if($submissionFile->getAssocType() ===  ASSOC_TYPE_SUBMISSION_FILE) {
            XML2HTMLPlugin::unzipSubmissionUpload($submissionFile);
//            $path = $submissionFile->getData('path');
//            $fileManager = new FileManager();
//            $submissionFileExtension = $fileManager->parseFileExtension($path);
//            if ($submissionFileExtension == 'zip') {
//                $fileMgr = new PrivateFileManager();
//                $filesDir = $fileMgr->getBasePath() . DIRECTORY_SEPARATOR;
//                $zip = new ZipArchive;
//                $zipPath = $filesDir . $path;
//                if ($zip->open($zipPath) === TRUE) {
//                    $zipPathParts = pathinfo($zipPath);
//                    $archiveFolderName = NULL;
//                    if ($zip->numFiles > 0) {//gets zip top folder name
//                        $archiveFolderName = $zip->getNameIndex(0);
//                    } else {
//                        throw new \Exception("Bad zip Submission file.");
//                    }
//                    $zip->extractTo($zipPathParts['dirname']);
//                    rename(
//                        $zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $archiveFolderName,
//                        $zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $zipPathParts['filename']);
//                    $zip->close();
//
//                    $files = scandir($zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $zipPathParts['filename']);
//                    foreach ($files as $file) {
//                        $extension = $fileManager->parseFileExtension($file);
//                        if ($extension == 'xml') {
//                            $basePath = $zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $zipPathParts['filename'] . '/';
//                            rename(
//                                $basePath . $file,
//                                $basePath . $zipPathParts['filename'] . '.xml'
//                            );
//                        }
//                    }
//
//                } else {
//                    echo 'échec';
//                }
//            } else
//                throw new \Exception('Submission file must be a zip one.');
        }

    }

    static function unzipSubmissionUpload(SubmissionFile $submissionFile){
        $path = $submissionFile->getData('path');
    static function unzipXMLArchive(string $path)
    {
        $fileManager = new FileManager();
        $submissionFileExtension = $fileManager->parseFileExtension($path);
        $fileInfo = pathinfo($path);
        if ($submissionFileExtension == 'zip') {
            $fileMgr = new PrivateFileManager();
            $filesDir = $fileMgr->getBasePath() . DIRECTORY_SEPARATOR;
            $zip = new ZipArchive;
            $zipPath = $filesDir . $path;
            $zipPathParts = pathinfo($zipPath);
            if(file_exists($zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $zipPathParts['filename']))
                return;
            if (file_exists($zipPathParts['dirname'] . DIRECTORY_SEPARATOR . $zipPathParts['filename'])) {
                return $filesDir . $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . '.xml';
            }
            if ($zip->open($zipPath) === TRUE) {

                $archiveFolderName = NULL;
@@ -262,13 +178,14 @@ class XML2HTMLPlugin extends GenericPlugin
                            $basePath . $file,
                            $basePath . $zipPathParts['filename'] . '.xml'
                        );
                        return $filesDir . $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . '.xml';
                    }
                }

            } else {
                echo 'échec';
                return NULL;
            }
        } else return false;
        } else return NULL;

    }

@@ -285,8 +202,7 @@ class XML2HTMLPlugin extends GenericPlugin
        if ($page == 'article' && ($op == 'image' || $op == 'zimage')) {
            define('HANDLER_CLASS', 'ArticleImageDataHandler');
            import('plugins.generic.xml2html.ArticleImageDataHandler');
        }
        else if (($page == 'catalog') && ($op == 'image' || $op == 'zimage')) {
        } else if (($page == 'catalog') && ($op == 'image' || $op == 'zimage')) {
            define('HANDLER_CLASS', 'BookImageDataHandler');
            import('plugins.generic.xml2html.BookImageDataHandler');
        }