diff --git a/TextureHandler.inc.php b/TextureHandler.inc.php index 05d3676..f429daf 100644 --- a/TextureHandler.inc.php +++ b/TextureHandler.inc.php @@ -174,29 +174,7 @@ public function json($args, $request) { $media = (array)json_decode($postData)->media; if (!empty($media)) { - import('classes.file.PublicFileManager'); - $publicFileManager = new PublicFileManager(); - - - $journal = $request->getJournal(); - $genreDao = DAORegistry::getDAO('GenreDAO'); - $genres = $genreDao->getByDependenceAndContextId(true, $journal->getId()); - $genreId = null; - $extension = $publicFileManager->getImageExtension($media["fileType"]); - while ($candidateGenre = $genres->next()) { - if ($extension) { - if ($candidateGenre->getKey() == 'IMAGE') { - $genreId = $candidateGenre->getId(); - break; - } - } else { - if ($candidateGenre->getKey() == 'MULTIMEDIA') { - $genreId = $candidateGenre->getId(); - break; - - } - } - } + $genreId = $this->_plugin->suggestDepenedentGenre($media["fileType"]); if (!$genreId) { // Could not identify the genre -- it's an error condition return new JSONMessage(false); @@ -387,44 +365,23 @@ public function media($args, $request) { /** * creates dependent file - * @param $genreId intr - * @param $mediaData string + * @param $genreId int + * @param $mediaData string Base64 encoded media file with JSON prefix * @param $submission Article - * @param $submissionFile SubmissionFie + * @param $submissionFile SubmissionFile * @param $user User * @return SubmissionArtworkFile */ protected function _createDependentFile($genreId, $mediaData, $submission, $submissionFile, $user) { $mediaBlob = base64_decode(preg_replace('#^data:\w+/\w+;base64,#i', '', $mediaData["data"])); - $tmpfname = tempnam(sys_get_temp_dir(), 'texture'); - file_put_contents($tmpfname, $mediaBlob); - - $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); - $newMediaFile = $submissionFileDao->newDataObjectByGenreId($genreId); - $newMediaFile->setSubmissionId($submission->getId()); - $newMediaFile->setSubmissionLocale($submission->getLocale()); - $newMediaFile->setGenreId($genreId); - $newMediaFile->setFileStage(SUBMISSION_FILE_DEPENDENT); - $newMediaFile->setDateUploaded(Core::getCurrentDate()); - $newMediaFile->setDateModified(Core::getCurrentDate()); - $newMediaFile->setUploaderUserId($user->getId()); - $newMediaFile->setFileSize(filesize($tmpfname)); - $newMediaFile->setFileType($mediaData["fileType"]); - $newMediaFile->setAssocId($submissionFile->getFileId()); - $newMediaFile->setAssocType(ASSOC_TYPE_SUBMISSION_FILE); - $newMediaFile->setOriginalFileName($mediaData["fileName"]); - $insertedMediaFile = $submissionFileDao->insertObject($newMediaFile, $tmpfname); - - unlink($tmpfname); - - return $insertedMediaFile; + return $this->_plugin->createDependentFile($genreId, $mediaBlob, $submission, $submissionFile, $user); } /** * Update manuscript XML file * @param $fileStage int * @param $genreId int - * @param $resources array + * @param $resources array With index "manuscript.xml" pointing to XML content * @param $submission Article * @param $submissionFile SubmissionFile * @param $user User @@ -432,34 +389,7 @@ protected function _createDependentFile($genreId, $mediaData, $submission, $subm */ protected function _updateManuscriptFile($fileStage, $genreId, $resources, $submission, $submissionFile, $user) { $manuscriptXml = $resources['manuscript.xml']->data; - $tmpfname = tempnam(sys_get_temp_dir(), 'texture'); - file_put_contents($tmpfname, $manuscriptXml); - - - $fileSize = filesize($tmpfname); - - $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); - $newSubmissionFile = $submissionFileDao->newDataObjectByGenreId($genreId); - - $newSubmissionFile->setSubmissionId($submission->getId()); - $newSubmissionFile->setSubmissionLocale($submission->getLocale()); - $newSubmissionFile->setGenreId($genreId); - $newSubmissionFile->setFileStage($fileStage); - $newSubmissionFile->setDateUploaded(Core::getCurrentDate()); - $newSubmissionFile->setDateModified(Core::getCurrentDate()); - $newSubmissionFile->setOriginalFileName($submissionFile->getOriginalFileName()); - $newSubmissionFile->setUploaderUserId($user->getId()); - $newSubmissionFile->setFileSize($fileSize); - $newSubmissionFile->setFileType($submissionFile->getFileType()); - $newSubmissionFile->setSourceFileId($submissionFile->getFileId()); - $newSubmissionFile->setSourceRevision($submissionFile->getRevision()); - $newSubmissionFile->setFileId($submissionFile->getFileId()); - $newSubmissionFile->setRevision($submissionFile->getRevision() + 1); - $insertedSubmissionFile = $submissionFileDao->insertObject($newSubmissionFile, $tmpfname); - - unlink($tmpfname); - - return $insertedSubmissionFile; + return $this->_plugin->updateManuscriptFile($fileStage, $genreId, $manuscriptXml, $submission, $submissionFile, $user); } /** diff --git a/TexturePlugin.inc.php b/TexturePlugin.inc.php index 9ecc71a..bc76eb5 100644 --- a/TexturePlugin.inc.php +++ b/TexturePlugin.inc.php @@ -41,6 +41,7 @@ function register($category, $path, $mainContextId = null) { // Register callbacks. HookRegistry::register('LoadHandler', array($this, 'callbackLoadHandler')); HookRegistry::register('TemplateManager::fetch', array($this, 'templateFetchCallback')); + HookRegistry::register('submissionfilesuploadform::execute', array($this, 'processUpload')); $this->_registerTemplateResource(); } @@ -133,4 +134,110 @@ public function templateFetchCallback($hookName, $params) { } } } + + /** + * creates dependent file + * @param $genreId int Genre of the new dependent file + * @param $mediaData string Dependent media file contents + * @param $submission Submission Submission to which to attach the dependent file + * @param $submissionFile SubmissionFile Submission file to which to attach the dependent file + * @param $user User Submitting user + * @return SubmissionArtworkFile + */ + public function createDependentFile($genreId, $mediaData, $submission, $submissionFile, $user) { + $tmpfname = tempnam(sys_get_temp_dir(), 'texture'); + file_put_contents($tmpfname, $mediaData); + + $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); + $newMediaFile = $submissionFileDao->newDataObjectByGenreId($genreId); + $newMediaFile->setSubmissionId($submission->getId()); + $newMediaFile->setSubmissionLocale($submission->getLocale()); + $newMediaFile->setGenreId($genreId); + $newMediaFile->setFileStage(SUBMISSION_FILE_DEPENDENT); + $newMediaFile->setDateUploaded(Core::getCurrentDate()); + $newMediaFile->setDateModified(Core::getCurrentDate()); + $newMediaFile->setUploaderUserId($user->getId()); + $newMediaFile->setFileSize(filesize($tmpfname)); + $newMediaFile->setFileType($mediaData["fileType"]); + $newMediaFile->setAssocId($submissionFile->getFileId()); + $newMediaFile->setAssocType(ASSOC_TYPE_SUBMISSION_FILE); + $newMediaFile->setOriginalFileName($mediaData["fileName"]); + $insertedMediaFile = $submissionFileDao->insertObject($newMediaFile, $tmpfname); + + unlink($tmpfname); + + return $insertedMediaFile; + } + + /** + * Update manuscript XML file + * @param $fileStage int File stage of the new submission file + * @param $genreId int Genre of the new submission file + * @param $manuscriptXml string Manuscript XML content + * @param $submission Submission Submission to which to attach the new SubmissionFile + * @param $submissionFile SubmissionFile Original submission file to update + * @param $user User Submitting user + * @return SubmissionFile + */ + public function updateManuscriptFile($fileStage, $genreId, $manuscriptXml, $submission, $submissionFile, $user) { + $tmpfname = tempnam(sys_get_temp_dir(), 'texture'); + file_put_contents($tmpfname, $manuscriptXml); + + + $fileSize = filesize($tmpfname); + + $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); + $newSubmissionFile = $submissionFileDao->newDataObjectByGenreId($genreId); + + $newSubmissionFile->setSubmissionId($submission->getId()); + $newSubmissionFile->setSubmissionLocale($submission->getLocale()); + $newSubmissionFile->setGenreId($genreId); + $newSubmissionFile->setFileStage($fileStage); + $newSubmissionFile->setDateUploaded(Core::getCurrentDate()); + $newSubmissionFile->setDateModified(Core::getCurrentDate()); + $newSubmissionFile->setOriginalFileName($submissionFile->getOriginalFileName()); + $newSubmissionFile->setUploaderUserId($user->getId()); + $newSubmissionFile->setFileSize($fileSize); + $newSubmissionFile->setFileType($submissionFile->getFileType()); + $newSubmissionFile->setSourceFileId($submissionFile->getFileId()); + $newSubmissionFile->setSourceRevision($submissionFile->getRevision()); + $newSubmissionFile->setFileId($submissionFile->getFileId()); + $newSubmissionFile->setRevision($submissionFile->getRevision() + 1); + $insertedSubmissionFile = $submissionFileDao->insertObject($newSubmissionFile, $tmpfname); + + unlink($tmpfname); + + return $insertedSubmissionFile; + } + + /** + * Suggest a dependent Genre by file type + * @param $filetype string mime file type + * @return null|int Genre Id if available + */ + function suggestDependentGenreId($filetype) { + $request = $this->getRequest(); + $journal = $request->getJournal(); + $genreDao = DAORegistry::getDAO('GenreDAO'); + $genres = $genreDao->getByDependenceAndContextId(true, $journal->getId()); + $genreId = null; + import('classes.file.PublicFileManager'); + $publicFileManager = new PublicFileManager(); + $extension = $publicFileManager->getImageExtension($filetype); + while ($candidateGenre = $genres->next()) { + if ($extension) { + if ($candidateGenre->getKey() == 'IMAGE') { + $genreId = $candidateGenre->getId(); + break; + } + } else { + if ($candidateGenre->getKey() == 'MULTIMEDIA') { + $genreId = $candidateGenre->getId(); + break; + + } + } + } + return $genreId; + } }