Skip to content
Snippets Groups Projects
Commit aef8c04c authored by Jerome Chauveau's avatar Jerome 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
No related branches found
No related tags found
No related merge requests found
<?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);
}
/**
* @throws Exception
*/
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;
$zipMode = $targetFile->getData('mimetype') === "application/zip";
if($zipMode){
$fileInfo = pathinfo($filePath);
XML2HTMLPlugin::unzipSubmissionUpload($targetFile);
$xmlFilePath =
$filesDir . $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . '.xml';
}
else
$xmlFilePath = $filesDir . $filePath;
$html = XML2HTMLPlugin::transform($xmlFilePath);
if($zipMode) {
$html = XML2HTMLPlugin::resolveZipImagePaths($html, $submission->getId(), $xmlFilePath, $request, 'catalog');
}
else
$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'));
}
}
......@@ -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');
}
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment