Skip to content

Commit

Permalink
Adapt to support OMP
Browse files Browse the repository at this point in the history
  • Loading branch information
nongenti committed Jan 26, 2023
1 parent b84ec2c commit 22ff435
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 225 deletions.
144 changes: 39 additions & 105 deletions CitationStyleLanguagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class CitationStyleLanguagePlugin extends GenericPlugin
/** @var string Name of the application */
public string $application;

private bool $book = false;
private bool $chapter = false;
private bool $article = false;
protected bool $isBook = false;
protected bool $isChapter = false;
protected bool $isArticle = false;

/**
* Constructor
Expand Down Expand Up @@ -108,10 +108,8 @@ public function register($category, $path, $mainContextId = null)

/**
* Get list of citation styles available
*
* @return array
*/
public function getCitationStyles()
public function getCitationStyles(): array
{
if (!empty($this->_citationStyles)) {
return $this->_citationStyles;
Expand Down Expand Up @@ -181,12 +179,8 @@ public function getCitationStyles()

/**
* Get the primary style name or default to the first available style
*
* @param $contextId integer Journal ID
*
* @return string
*/
public function getPrimaryStyleName($contextId = 0)
public function getPrimaryStyleName(int $contextId): string
{
$primaryStyleName = $this->getSetting($contextId, 'primaryCitationStyle');
if ($primaryStyleName) {
Expand All @@ -205,12 +199,8 @@ public function getPrimaryStyleName($contextId = 0)

/**
* Get enabled citation styles
*
* @param $contextId integer Journal ID
*
* @return array
*/
public function getEnabledCitationStyles($contextId = 0)
public function getEnabledCitationStyles(int $contextId): array
{
$styles = $this->getCitationStyles();
$enabled = $this->getSetting($contextId, 'enabledCitationStyles');
Expand All @@ -227,10 +217,8 @@ public function getEnabledCitationStyles($contextId = 0)

/**
* Get list of citation download formats available
*
* @return array
*/
public function getCitationDownloads()
public function getCitationDownloads(): array
{
if (!empty($this->_citationDownloads)) {
return $this->_citationDownloads;
Expand Down Expand Up @@ -264,12 +252,8 @@ public function getCitationDownloads()

/**
* Get enabled citation styles
*
* @param $contextId integer Journal ID
*
* @return array
*/
public function getEnabledCitationDownloads($contextId = 0)
public function getEnabledCitationDownloads(int $contextId): array
{
$downloads = $this->getCitationDownloads();
$enabled = $this->getSetting($contextId, 'enabledCitationDownloads');
Expand All @@ -286,26 +270,18 @@ public function getEnabledCitationDownloads($contextId = 0)

/**
* Pluck citation IDs from array of citations
*
* @param $citations array See getCitationStyles()
*
* @return array
*/
public function mapCitationIds($citations)
public function mapCitationIds(array $citations): array
{
return array_values(array_map(function ($citation) {
return $citation['id'];
}, $citations));
}

/**
* Get citation config for a citation ID
*
* @param $styleId string Example: 'apa'
*
* @return array
* Get citation config for a citation ID (example: 'apa')
*/
public function getCitationStyleConfig($styleId)
public function getCitationStyleConfig(string $styleId): array
{
$styleConfigs = array_merge($this->getCitationStyles(), $this->getCitationDownloads());
$styleConfig = array_filter($styleConfigs, function ($styleConfig) use ($styleId) {
Expand All @@ -318,13 +294,9 @@ public function getCitationStyleConfig($styleId)
* Retrieve citation information for the article details template. This
* method is hooked in before a template displays.
*
* @param $args array
*
* @return false
* @throws Exception
* @see ArticleHandler::view()
*/
public function getTemplateData($hookName, $args)
public function getTemplateData(string $hookName, array $args): bool
{
$request = $args[0];
$templateMgr = TemplateManager::getManager($request);
Expand All @@ -347,7 +319,7 @@ public function getTemplateData($hookName, $args)
}

$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$contextId = $context->getId();

$citationArgs = [
'submissionId' => $submission->getId(),
Expand Down Expand Up @@ -385,7 +357,8 @@ public function getTemplateData($hookName, $args)
return false;
}

public function addCitationMarkup($hookName, $args) {
public function addCitationMarkup(string $hookName, array $args): bool
{
$smarty =& $args[1];
$output =& $args[2];
$output .= $smarty->fetch($this->getTemplateResource('citationblock.tpl'));
Expand Down Expand Up @@ -426,7 +399,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu
$keywords = $submissionKeywordDao->getKeywords($publication->getId(), [Locale::getSupportedLocales()]);

$citationData = new stdClass();
if ($this->isArticle()) {
if ($this->isArticle) {
$citationData->type = 'article-journal';
$citationData->risType = 'JOUR';
$citationData->id = $submission->getId();
Expand Down Expand Up @@ -456,7 +429,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu
if ($publication->getDoi()) {
$citationData->DOI = $publication->getDoi();
}
} elseif ($this->isBook()) {
} elseif ($this->isBook) {
$citationData->type = 'book';
$citationData->risType = 'BOOK';
$citationData->id = $submission->getId();
Expand All @@ -474,7 +447,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu
if ($publication->getDoi()) {
$citationData->DOI = $publication->getDoi();
}
} elseif ($this->isBookChapter()) {
} elseif ($this->isChapter) {
$citationData->type = 'chapter';
$citationData->risType = 'CHAP';
$citationData->id = $chapter->getSourceChapterId();
Expand All @@ -493,6 +466,8 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu
if ($chapter->getDoi()) {
$citationData->DOI = $chapter->getDoi();
}
} else {
throw new Exception('Unknown submission content type!');
}

/* @var $submissionLanguageDao SubmissionLanguageDAO */
Expand Down Expand Up @@ -583,14 +558,14 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu
//Clickable URL and DOI including affixes
$additionalMarkup = [
'DOI' => [
'function' => function ($cslItem, $renderedText) {
return '<a href="https://doi.org/' . $cslItem->DOI . '">' . $renderedText . '</a>';
'function' => function ($item, $renderedValue) {
return '<a href="https://doi.org/' . $item->DOI . '">' . $renderedValue . '</a>';
},
'affixes' => true
],
'URL' => [
'function' => function ($cslItem, $renderedText) {
return '<a href="' . $cslItem->URL . '">' . $renderedText . '</a>';
'function' => function ($item, $renderedValue) {
return '<a href="' . $item->URL . '">' . $renderedValue . '</a>';
},
'affixes' => true
],
Expand All @@ -607,10 +582,8 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu

/**
* Load a CSL style and return the contents as a string
*
* @param $styleConfig array CSL configuration to load
*/
public function loadStyle($styleConfig)
public function loadStyle(array $styleConfig): false|string
{
if (!empty($styleConfig['useCsl'])) {
return file_get_contents($styleConfig['useCsl']);
Expand Down Expand Up @@ -645,10 +618,6 @@ public function downloadCitation($request, $submission, $citationStyle = 'ris',
return false;
}

if (!$publication) {
$publication = $submission->getCurrentPublication();
}

$citation = trim(strip_tags($this->getCitation($request, $submission, $citationStyle, $issue, $publication, $chapter)));
// TODO this is likely going to cause an error in a citation some day,
// but is necessary to get the .ris downloadable format working. The
Expand Down Expand Up @@ -727,13 +696,8 @@ public function manage($args, $request)

/**
* Route requests for the citation styles to custom page handler
*
* @see PKPPageRouter::route()
*
* @param $hookName string
* @param $params array
*/
public function setPageHandler($hookName, $params)
public function setPageHandler(string $hookName, array $params): bool
{
$page =& $params[0];
$handler =& $params[3];
Expand All @@ -744,22 +708,22 @@ public function setPageHandler($hookName, $params)
return false;
}

public function getEditorGroups(int $contextId = 0): array
public function getEditorGroups(int $contextId): array
{
return $this->getSetting($contextId, 'groupEditor') ?? [];
}

public function getTranslatorGroups(int $contextId = 0): array
public function getTranslatorGroups(int $contextId): array
{
return $this->getSetting($contextId, 'groupTranslator') ?? [];
}

public function getAuthorGroups(int $contextId = 0): array
public function getAuthorGroups(int $contextId): array
{
return $this->getSetting($contextId, 'groupAuthor') ?? [];
}

public function getChapterAuthorGroups(int $contextId = 0): array
public function getChapterAuthorGroups(int $contextId): array
{
return $this->getSetting($contextId, 'groupChapterAuthor') ?? [];
}
Expand Down Expand Up @@ -787,8 +751,7 @@ protected function addSeriesInformation(stdClass $citationData, Publication $pub
if ($seriesId) {
/** @var SeriesDAO $seriesDao */
$seriesDao = DAORegistry::getDAO('SeriesDAO');
if (null !== $seriesDao) {
$series = $seriesDao->getById($seriesId);
$series = $seriesDao->getById($seriesId);
if (null !== $series) {
$citationData->{'collection-title'} = htmlspecialchars(trim($series->getLocalizedFullTitle()));
$citationData->volume = htmlspecialchars($publication->getData('seriesPosition'));
Expand All @@ -802,7 +765,6 @@ protected function addSeriesInformation(stdClass $citationData, Publication $pub
$citationData->serialNumber[] = htmlspecialchars($printISSN);
}
}
}
}
return $citationData;
}
Expand All @@ -816,26 +778,13 @@ protected function getChapter(PKPRequest $request, Publication $publication): ?C
} elseif (isset($args[1], $args[3], $args[4]) && $args[1] === 'version' && $args[3] === 'chapter') {
$key = 4;
} else {
$key = 0;
return null;
}

if ($key !== 0) {
$chapterId = (int) $args[$key];
if ($chapterId > 0) {
$chapterDao = DAORegistry::getDAO('ChapterDAO');
$chapters = $chapterDao->getBySourceChapterId($chapterId);
$chapters = $chapters->toAssociativeArray();
$chaptersCount = count($chapters);

if ($chaptersCount > 0) {
/** @var Chapter $chapter */
foreach ($chapters as $chapter) {
if ((int) $chapter->getData('publicationId') === $publication->getId()) {
return $chapter;
}
}
}
}
$chapterId = (int) $args[$key];
if ($chapterId > 0) {
$chapterDao = DAORegistry::getDAO('ChapterDAO');
return $chapterDao->getBySourceChapterId($chapterId, true, $publication->getId());
}
return null;
}
Expand All @@ -847,34 +796,19 @@ protected function setDocumentType($chapter): void
{
switch ($this->application) {
case 'ojs2':
$this->article = true;
$this->isArticle = true;
break;
case 'omp':
if ($chapter) {
$this->chapter = true;
$this->isChapter = true;
} else {
$this->book = true;
$this->isBook = true;
}
break;
default: throw new Exception('Unknown application!');
}
}

protected function isArticle(): bool
{
return $this->article;
}

protected function isBook(): bool
{
return $this->book;
}

protected function isBookChapter(): bool
{
return $this->chapter;
}

protected function setArticleAuthors($citationData, $publication, $context): stdClass
{
$authors = $publication->getData('authors');
Expand Down
6 changes: 3 additions & 3 deletions CitationStyleLanguageSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function initData(): void
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$contextId = $context->getId();
$this->setData('primaryCitationStyle', $this->plugin->getSetting($contextId, 'primaryCitationStyle'));
$this->setData('enabledCitationStyles', array_keys($this->plugin->getEnabledCitationStyles($contextId)));
$this->setData('enabledCitationDownloads', $this->plugin->getEnabledCitationDownloads($contextId));
Expand Down Expand Up @@ -89,7 +89,7 @@ public function readInputData(): void
public function fetch($request, $template = null, $display = false): ?string
{
$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$contextId = $context->getId();

$allStyles = [];
foreach ($this->plugin->getCitationStyles() as $style) {
Expand Down Expand Up @@ -140,7 +140,7 @@ public function execute(...$functionArgs)
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$contextId = $context->getId();
$this->plugin->updateSetting($contextId, 'primaryCitationStyle', $this->getData('primaryCitationStyle'));
$enabledCitationStyles = $this->getData('enabledCitationStyles') ?: [];
$this->plugin->updateSetting($contextId, 'enabledCitationStyles', $enabledCitationStyles);
Expand Down
Loading

0 comments on commit 22ff435

Please sign in to comment.