diff --git a/CitationStyleLanguagePlugin.inc.php b/CitationStyleLanguagePlugin.php similarity index 88% rename from CitationStyleLanguagePlugin.inc.php rename to CitationStyleLanguagePlugin.php index 59b0c57..13bb53d 100644 --- a/CitationStyleLanguagePlugin.inc.php +++ b/CitationStyleLanguagePlugin.php @@ -1,7 +1,7 @@ getName(); - $this->applicationOmp = stripos($applicationName, 'omp') !== false; + $this->application = Application::get()->getName(); } /** @@ -77,9 +96,12 @@ public function register($category, $path, $mainContextId = null) return $success; } if ($success && $this->getEnabled($mainContextId)) { - HookRegistry::register('CatalogBookHandler::book', [$this, 'getTemplateData']); - HookRegistry::register('ArticleHandler::view', [$this, 'getTemplateData']); - HookRegistry::register('LoadHandler', [$this, 'setPageHandler']); + Hook::add('CatalogBookHandler::book', [$this, 'getTemplateData']); + Hook::add('ArticleHandler::view', [$this, 'getTemplateData']); + Hook::add('Templates::Article::Details', [$this, 'addCitationMarkup']); + Hook::add('Templates::Catalog::Book::Details', [$this, 'addCitationMarkup']); + Hook::add('Templates::Catalog::Chapter::Details', [$this, 'addCitationMarkup']); + Hook::add('LoadHandler', [$this, 'setPageHandler']); } return $success; } @@ -151,7 +173,7 @@ public function getCitationStyles() // If hooking in to add a custom .csl file, add a `useCsl` key to your // style definition with the path to the file. - HookRegistry::call('CitationStyleLanguage::citationStyleDefaults', [&$defaults, $this]); + Hook::call('CitationStyleLanguage::citationStyleDefaults', [&$defaults, $this]); $this->_citationStyles = $defaults; return $this->_citationStyles; @@ -234,7 +256,7 @@ public function getCitationDownloads() // If hooking in to add a custom .csl file, add a `useCsl` key to your // style definition with the path to the file. - HookRegistry::call('CitationStyleLanguage::citationDownloadDefaults', [&$defaults, $this]); + Hook::call('CitationStyleLanguage::citationDownloadDefaults', [&$defaults, $this]); $this->_citationDownloads = $defaults; return $this->_citationDownloads; @@ -296,41 +318,34 @@ public function getCitationStyleConfig($styleId) * Retrieve citation information for the article details template. This * method is hooked in before a template displays. * - * @see ArticleHandler::view() - * * @param $args array * * @return false + * @throws Exception + * @see ArticleHandler::view() */ public function getTemplateData($hookName, $args) { - $templateMgr = TemplateManager::getManager(); $request = $args[0]; - if ($this->isApplicationOmp()) { - $submission =& $args[1]; - $publication = $request->_router->_handler->publication; - if ( $request->_router->_handler->isChapterRequest ) { - $chapter = $request->_router->_handler->chapter; - } else { - $chapter = null; - } + $templateMgr = TemplateManager::getManager($request); - $issue = null; - $templateMgr->addStyleSheet( - 'cslPluginStyles', - $request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/citationStyleLanguagePlugin.css', - [ - 'priority' => PKPTemplateManager::STYLE_SEQUENCE_LAST, - 'contexts' => ['frontend'], - 'inline' => false, - ] - ); - } else { - $issue = $args[1]; - $submission = $args[2]; - $publication = $args[3]; - $chapter = null; + switch ($this->application) { + case 'ojs2': + $issue = $args[1]; + $submission = $args[2]; + $publication = $args[3]; + $chapter = null; + break; + case 'omp': + /** @var Submission $submission */ + $submission =& $args[1]; + $publication =& $args[2]; + $chapter =& $args[3]; + $issue = null; + break; + default: throw new Exception('Unknown application!'); } + $context = $request->getContext(); $contextId = $context ? $context->getId() : 0; @@ -352,6 +367,16 @@ public function getTemplateData($hookName, $args) 'citationDownloads' => $this->getEnabledCitationDownloads($contextId), ]); + $templateMgr->addStyleSheet( + 'cslPluginStyles', + $request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/citationStyleLanguagePlugin.css', + [ + 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, + 'contexts' => ['frontend'], + 'inline' => false, + ] + ); + $templateMgr->addJavaScript( 'citationStyleLanguage', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/articleCitation.js' @@ -360,20 +385,9 @@ public function getTemplateData($hookName, $args) return false; } - /** - * Add citation style language to book view page - * Hooked to `Templates::Catalog::Book::Main` - * - * @param $hookName string - * @param $params array array [ - * @option Smarty - * @option string HTML output to return - * ] - * @return false - */ - public function displayCitationMonograph($hookName, $params) { - $smarty =& $params[1]; - $output =& $params[2]; + public function addCitationMarkup($hookName, $args) { + $smarty =& $args[1]; + $output =& $args[2]; $output .= $smarty->fetch($this->getTemplateResource('citationblock.tpl')); return false; @@ -389,21 +403,22 @@ public function displayCitationMonograph($hookName, $params) { * @see Zotero's mappings https://aurimasv.github.io/z2csl/typeMap.xml#map-journalArticle * @see Mendeley's mappings http://support.mendeley.com/customer/portal/articles/364144-csl-type-mapping * - * @param $request PkPRequest - * @param $submission Submission + * @param $request PkPRequest + * @param $submission Submission * @param $citationStyle string Name of the citation style to use. - * @param $issue Issue Optional. Will fetch from db if not passed. - * @param $publication Publication Optional. A particular version - * @param $chapter Chapter Optional. OMP chapter pages only. + * @param $issue Issue Optional. Will fetch from db if not passed. + * @param $publication Publication Optional. A particular version + * @param $chapter Chapter Optional. OMP chapter pages only. * * @return string + * @throws Exception */ public function getCitation($request, $submission, $citationStyle = 'apa', $issue = null, $publication = null, $chapter = null) { $publication = $publication ?? $submission->getCurrentPublication(); $context = $request->getContext(); if (!$chapter) { - $chapter = $this->isApplicationOmp() ? $this->getChapter($request, $publication) : null; + $chapter = $this->application === 'omp' ? $this->getChapter($request, $publication) : null; } $this->setDocumentType($chapter); /* @var $submissionKeywordDao SubmissionKeywordDAO */ @@ -510,12 +525,12 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu $citationData->{'original-date'}->raw = htmlspecialchars($originalPublication->getData('datePublished')); } } - } elseif (!$this->isApplicationOmp() && $issue && $issue->getPublished()) { + } elseif ($this->application === 'ojs2' && $issue && $issue->getPublished()) { $citationData->issued = new stdClass(); $citationData->issued->raw = htmlspecialchars($issue->getDatePublished()); } - HookRegistry::call('CitationStyleLanguage::citation', [&$citationData, &$citationStyle, $submission, $issue, $context, $publication]); + Hook::call('CitationStyleLanguage::citation', [&$citationData, &$citationStyle, $submission, $issue, $context, $publication]); $citation = ''; @@ -530,18 +545,23 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu 'citationStyle' => $citationStyle, 'publication' => $publication, ]); - if ($this->isApplicationOmp()) { - $templateMgr->assign([ - 'book' => $submission, - 'press' => $context, - ]); - } else { - $templateMgr->assign([ - 'article' => $submission, - 'issue' => $issue, - 'journal' => $context, - ]); + switch ($this->application) { + case 'ojs2': + $templateMgr->assign([ + 'article' => $submission, + 'issue' => $issue, + 'journal' => $context, + ]); + break; + case 'omp': + $templateMgr->assign([ + 'book' => $submission, + 'press' => $context, + ]); + break; + default: throw new Exception('Unknown application!'); } + $citation = $templateMgr->fetch($styleConfig['useTemplate']); } else { $style = $this->loadStyle($styleConfig); @@ -616,10 +636,8 @@ public function loadStyle($styleConfig) */ public function downloadCitation($request, $submission, $citationStyle = 'ris', $issue = null, $publication = null, $chapter = null) { - if (!$this->isApplicationOmp()) { - if (empty($issue)) { - $issue = Repo::issue()->get($submission->getCurrentPublication()->getData('issueId')); - } + if (($this->application === 'ojs2') && empty($issue)) { + $issue = Repo::issue()->get($submission->getCurrentPublication()->getData('issueId')); } $styleConfig = $this->getCitationStyleConfig($citationStyle); @@ -691,7 +709,6 @@ public function manage($args, $request) { switch ($request->getUserVar('verb')) { case 'settings': - $this->import('CitationStyleLanguageSettingsForm'); $form = new CitationStyleLanguageSettingsForm($this); if ($request->getUserVar('save')) { @@ -718,20 +735,15 @@ public function manage($args, $request) */ public function setPageHandler($hookName, $params) { - $page = $params[0]; + $page =& $params[0]; + $handler =& $params[3]; if ($this->getEnabled() && $page === 'citationstylelanguage') { - $this->import('pages/CitationStyleLanguageHandler'); - define('HANDLER_CLASS', 'CitationStyleLanguageHandler'); + $handler = new CitationStyleLanguageHandler($this); return true; } return false; } - public function isApplicationOmp(): bool - { - return $this->applicationOmp; - } - public function getEditorGroups(int $contextId = 0): array { return $this->getSetting($contextId, 'groupEditor') ?? []; @@ -828,17 +840,23 @@ protected function getChapter(PKPRequest $request, Publication $publication): ?C return null; } + /** + * @throws Exception + */ protected function setDocumentType($chapter): void { - if ($this->isApplicationOmp()) { - if ($chapter) { - $this->chapter = true; - } else { - $this->book = true; - } - - } else { - $this->article = true; + switch ($this->application) { + case 'ojs2': + $this->article = true; + break; + case 'omp': + if ($chapter) { + $this->chapter = true; + } else { + $this->book = true; + } + break; + default: throw new Exception('Unknown application!'); } } @@ -863,7 +881,7 @@ protected function setArticleAuthors($citationData, $publication, $context): std $authorsGroups = $this->getAuthorGroups($context->getId()); $translatorsGroups = $this->getTranslatorGroups($context->getId()); if (count($authors)) { - /** @var \APP\author\Author $author */ + /** @var Author $author */ foreach ($authors as $author) { $currentAuthor = new stdClass(); if (empty($author->getLocalizedFamilyName())) { @@ -906,7 +924,7 @@ protected function setBookAuthors($citationData, $publication, $context): stdCla $editorsGroups = $this->getEditorGroups($context->getId()); $translatorsGroups = $this->getTranslatorGroups($context->getId()); if (count($authors)) { - /** @var \APP\author\Author $author */ + /** @var Author $author */ foreach ($authors as $author) { $currentAuthor = new stdClass(); if (empty($author->getLocalizedFamilyName())) { @@ -953,7 +971,7 @@ protected function setBookChapterAuthors($citationData, $publication, $context, $chapterAuthorGroups = $this->getChapterAuthorGroups($context->getId()); $chapterAuthors = $chapter->getAuthors(); - /** @var \APP\author\Author $chapterAuthor */ + /** @var Author $chapterAuthor */ foreach ($chapterAuthors as $chapterAuthor) { $currentAuthor = new stdClass(); if (empty($chapterAuthor->getLocalizedFamilyName())) { @@ -974,7 +992,7 @@ protected function setBookChapterAuthors($citationData, $publication, $context, $editorsGroups = $this->getEditorGroups($context->getId()); $translatorsGroups = $this->getTranslatorGroups($context->getId()); if (count($bookAuthors)) { - /** @var \APP\author\Author $bookAuthor */ + /** @var Author $bookAuthor */ foreach ($bookAuthors as $bookAuthor) { $currentAuthor = new stdClass(); if (empty($bookAuthor->getLocalizedFamilyName())) { diff --git a/CitationStyleLanguageSettingsForm.inc.php b/CitationStyleLanguageSettingsForm.php similarity index 85% rename from CitationStyleLanguageSettingsForm.inc.php rename to CitationStyleLanguageSettingsForm.php index ae7c29e..605bc36 100644 --- a/CitationStyleLanguageSettingsForm.inc.php +++ b/CitationStyleLanguageSettingsForm.php @@ -1,6 +1,6 @@ getTemplateResource('settings.tpl')); $this->plugin = $plugin; - $this->addCheck(new \PKP\form\validation\FormValidatorPost($this)); - $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); + $this->addCheck(new FormValidatorPost($this)); + $this->addCheck(new FormValidatorCSRF($this)); } /** @@ -50,7 +56,7 @@ public function initData(): void $this->setData('publisherLocation', $this->plugin->getSetting($contextId, 'publisherLocation')); $this->setData('groupAuthor', $this->plugin->getAuthorGroups($contextId)); $this->setData('groupTranslator', $this->plugin->getTranslatorGroups($contextId)); - if ($this->plugin->isApplicationOmp()) { + if ($this->plugin->application === 'omp') { $this->setData('groupEditor', $this->plugin->getEditorGroups($contextId)); $this->setData('groupChapterAuthor', $this->plugin->getChapterAuthorGroups($contextId)); } @@ -69,7 +75,7 @@ public function readInputData(): void 'groupAuthor', 'groupTranslator' ]); - if ($this->plugin->isApplicationOmp()) { + if ($this->plugin->application === 'omp') { $this->readUserVars(['groupEditor']); $this->readUserVars(['groupChapterAuthor']); } @@ -96,9 +102,9 @@ public function fetch($request, $template = null, $display = false): ?string } $allUserGroups = []; - $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */ - $userGroups = $userGroupDao->getByRoleId( $contextId, Role::ROLE_ID_AUTHOR ); - while ($userGroup = $userGroups->next()) { + $userGroups = Repo::userGroup()->getByRoleIds( [Role::ROLE_ID_AUTHOR], $contextId ); + $userGroups = $userGroups->toArray(); + foreach ($userGroups as $userGroup) { $allUserGroups[(int) $userGroup->getId()] = $userGroup->getLocalizedName(); } asort($allUserGroups); @@ -111,13 +117,13 @@ public function fetch($request, $template = null, $display = false): ?string 'primaryCitationStyle' => $this->getData('primaryCitationStyle'), 'enabledStyles' => $this->plugin->mapCitationIds($this->plugin->getEnabledCitationStyles($contextId)), 'enabledDownloads' => $this->plugin->mapCitationIds($this->plugin->getEnabledCitationDownloads($contextId)), - 'isApplicationOmp' => $this->plugin->isApplicationOmp(), + 'application' => $this->plugin->application, 'groupAuthor' => $this->getData('groupAuthor'), 'groupTranslator' => $this->getData('groupTranslator'), 'allUserGroups' => $allUserGroups, ]); - if ($this->plugin->isApplicationOmp()) { + if ($this->plugin->application === 'omp') { $templateMgr->assign([ 'groupEditor' => $this->getData('groupEditor'), 'groupChapterAuthor' => $this->getData('groupChapterAuthor'), @@ -143,7 +149,7 @@ public function execute(...$functionArgs) $this->plugin->updateSetting($contextId, 'publisherLocation', $this->getData('publisherLocation')); $this->plugin->updateSetting($contextId, 'groupAuthor', $this->getData('groupAuthor')); $this->plugin->updateSetting($contextId, 'groupTranslator', $this->getData('groupTranslator')); - if( $this->plugin->isApplicationOmp()) { + if( $this->plugin->application === 'omp') { $this->plugin->updateSetting($contextId, 'groupEditor', $this->getData('groupEditor')); $this->plugin->updateSetting($contextId, 'groupChapterAuthor', $this->getData('groupChapterAuthor')); } diff --git a/index.php b/index.php index e0db84c..b273e63 100644 --- a/index.php +++ b/index.php @@ -13,5 +13,5 @@ * @brief Wrapper for Citation Style Language plugin. * */ -require_once('CitationStyleLanguagePlugin.inc.php'); -return new CitationStyleLanguagePlugin(); +require_once('CitationStyleLanguagePlugin.php'); +return new APP\plugins\generic\citationStyleLanguage\CitationStyleLanguagePlugin(); diff --git a/pages/CitationStyleLanguageHandler.inc.php b/pages/CitationStyleLanguageHandler.php similarity index 91% rename from pages/CitationStyleLanguageHandler.inc.php rename to pages/CitationStyleLanguageHandler.php index 3984088..eafa9c6 100644 --- a/pages/CitationStyleLanguageHandler.inc.php +++ b/pages/CitationStyleLanguageHandler.php @@ -1,25 +1,30 @@ plugin = PluginRegistry::getPlugin('generic', 'citationstylelanguageplugin'); + $this->plugin = $plugin; } /** @@ -142,7 +147,7 @@ public function _setupRequest($args, $request) $this->chapter = $chapterDao->getChapter((int) $userVars['chapterId'], $this->publication->getId()); } - if ($this->submission && !$this->plugin->isApplicationOmp()) { + if ($this->submission && $this->plugin->application === 'ojs2') { // Support OJS 3.1.x and 3.2 $issueId = method_exists($this->submission, 'getCurrentPublication') ? $this->submission->getCurrentPublication()->getData('issueId') : $this->submission->getIssueId(); $issue = Repo::issue()->get($issueId); @@ -154,8 +159,8 @@ public function _setupRequest($args, $request) // journal manager or an assigned subeditor or assistant. This ensures the // article preview will work for those who can see it. if ($this->submission->getStatus() !== PKPSubmission::STATUS_PUBLISHED - || (!$this->plugin->isApplicationOmp() && !$this->issue ) - || (!$this->plugin->isApplicationOmp() && !$this->issue->getPublished())) { + || ($this->plugin->application === 'ojs2' + && (!$this->issue || !$this->issue->getPublished()))) { $userCanAccess = false; if ($user && $user->hasRole([Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_ASSISTANT], $context->getId())) { diff --git a/templates/settings.tpl b/templates/settings.tpl index ddcdf15..401d88d 100644 --- a/templates/settings.tpl +++ b/templates/settings.tpl @@ -21,7 +21,7 @@ {fbvFormArea id="citationStyleLanguagePluginSettings"} {fbvFormSection list=true title="plugins.generic.citationStyleLanguage.settings.citationFormatsPrimary"}
- {if $isApplicationOmp} + {if $application === 'omp'} {translate key="plugins.generic.citationStyleLanguage.settings.citationFormatsPrimaryDescription.omp"} {else} {translate key="plugins.generic.citationStyleLanguage.settings.citationFormatsPrimaryDescription"} @@ -51,7 +51,7 @@ {fbvElement type="checkbox" id="groupAuthor[]" value=$id checked=in_array($id, $groupAuthor) label=$group translate=false} {/foreach} {/fbvFormSection} - {if $isApplicationOmp} + {if $application === 'omp'} {fbvFormSection list=true label="plugins.generic.citationStyleLanguage.settings.citationChooseChapterAuthor"}
{translate key='plugins.generic.citationStyleLanguage.settings.citationOptionChooseChapterAuthor'}
{foreach from=$allUserGroups item="group" key="id"}