Skip to content

Commit

Permalink
pkp#33: Move reusable methods from Handler to Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ctgraham committed Oct 17, 2019
1 parent 8a0b0bb commit 75f7ed7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 77 deletions.
84 changes: 7 additions & 77 deletions TextureHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -387,79 +365,31 @@ 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
* @return SubmissionFile
*/
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);
}

/**
Expand Down
107 changes: 107 additions & 0 deletions TexturePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 75f7ed7

Please sign in to comment.