Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 57 additions & 38 deletions OrcidProfilePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
define('ORCID_EMAIL_URL', 'email');
define('ORCID_WORK_URL', 'work');

class OrcidProfilePlugin extends GenericPlugin {
class OrcidProfilePlugin extends GenericPlugin {
const PUBID_TO_ORCID_EXT_ID = [ "doi" => "doi", "other::urn" => "urn"];
const USERGROUP_TO_ORCID_ROLE = [ "Author" => "AUTHOR", "Translator" => "CHAIR_OR_TRANSLATOR"];

Expand All @@ -48,7 +48,7 @@ function register($category, $path, $mainContextId = null) {
if ($success && $this->getEnabled($mainContextId)) {
// Register callback for Smarty filters; add CSS
HookRegistry::register('TemplateManager::display', array($this, 'handleTemplateDisplay'));
// Add "Connect ORCID" button to PublicProfileForm
// Add "Connect ORCID" button to PublicProfileForm
HookRegistry::register('User::PublicProfile::AdditionalItems', array($this, 'handleUserPublicProfileDisplay'));
// Display additional ORCID access information and checkbox to send e-mail to authors in the AuthorForm
HookRegistry::register('authorform::display', array($this, 'handleFormDisplay'));
Expand Down Expand Up @@ -202,12 +202,12 @@ function handleTemplateDisplay($hookName, $args) {
break;
case 'frontend/pages/article.tpl':
//$templateMgr->assign('orcidIcon', $this->getIcon());
$script = 'var orcidIconSvg = $('. json_encode($this->getIcon()) .');';
$script = 'var orcidIconSvg = '. json_encode($this->getIcon()) .';';
$article = $templateMgr->getTemplateVars('article');
$authors = $article->getAuthors();
foreach ($authors as $author) {
if(!empty($author->getOrcid()) && !empty($author->getData('orcidAccessToken'))) {
$script .= '$("a[href=\"'.$author->getOrcid().'\"]").prepend(orcidIconSvg);';
$script .= '$("a[href=\"'.$author->getOrcid().'\"]").html(orcidIconSvg + "' . $author->getOrcid() . '");';
}
}
$templateMgr->addJavaScript('orcidIconDisplay', $script, ['inline' => true]);
Expand All @@ -231,19 +231,32 @@ function getOauthPath() {
* @return string
*/
function getOrcidUrl() {
if ($this->usingSandbox()) {
return ORCID_URL_SANDBOX;
} else {
return ORCID_URL;
}
}

/**
* Check if the plugin is configured to use the ORCID Sandbox API
*
* @return boolean true if using the ORCID Sandbox API, false otherwise
*/
function usingSandbox() {
$context = Request::getContext();
$contextId = ($context == null) ? 0 : $context->getId();

$apiPath = $this->getSetting($contextId, 'orcidProfileAPIPath');
if ($apiPath == ORCID_API_URL_PUBLIC || $apiPath == ORCID_API_URL_MEMBER) {
return ORCID_URL;
if ($apiPath == ORCID_API_URL_PUBLIC_SANDBOX || $apiPath == ORCID_API_URL_MEMBER_SANDBOX) {
return true;
} else {
return ORCID_URL_SANDBOX;
return false;
}
}

/**
* Return an ORCID OAuth authorization link with
* Return an ORCID OAuth authorization link with
*
* @param $handlerMethod string containting a valid method of the OrcidHandler
* @param $redirectParams Array associative array with additional request parameters for the redirect URL
Expand All @@ -262,7 +275,7 @@ function buildOAuthUrl($handlerMethod, $redirectParams) {
$scope = ORCID_API_SCOPE_PUBLIC;
}
// We need to construct a page url, but the request is using the component router.
// Use the Dispatcher to construct the url and set the page router.
// Use the Dispatcher to construct the url and set the page router.
$redirectUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'orcidapi',
$handlerMethod, null, $redirectParams);
return $this->getOauthPath() . 'authorize?' . http_build_query(array(
Expand Down Expand Up @@ -304,7 +317,7 @@ function registrationFilter($output, $templateMgr) {

/**
* Renders additional content for the PublicProfileForm.
*
*
* Called by @see lib/pkp/templates/user/publicProfileForm.tpl
*
* @param $output string
Expand All @@ -315,9 +328,12 @@ function handleUserPublicProfileDisplay($hookName, $params) {
$templateMgr =& $params[1];
$output =& $params[2];
$context = Request::getContext();
$user = Request::getUser();
$user = Request::getUser();
$contextId = ($context == null) ? 0 : $context->getId();
$targetOp = 'profile';
if ($user->getOrcid() && $user->getData('orcidSandbox')) {
$templateMgr->assign('orcid', rtrim(ORCID_URL_SANDBOX, '/') . parse_url($user->getOrcid(), PHP_URL_PATH));
}
$templateMgr->assign(array(
'targetOp' => $targetOp,
'orcidUrl' => $this->getOrcidUrl(),
Expand All @@ -326,7 +342,7 @@ function handleUserPublicProfileDisplay($hookName, $params) {
'orcidIcon' => $this->getIcon(),
'orcidAuthenticated' => !empty($user->getData('orcidAccessToken')),
));

$output = $templateMgr->fetch($this->getTemplateResource('orcidProfile.tpl'));
return true;
}
Expand Down Expand Up @@ -365,7 +381,7 @@ function handleAuthorFormExecute($hookname, $args) {
$form->readUserVars(array('requestOrcidAuthorization', 'deleteOrcid'));

$requestAuthorization = $form->getData('requestOrcidAuthorization');
$deleteOrcid = $form->getData('deleteOrcid');
$deleteOrcid = $form->getData('deleteOrcid');
$author = $form->getAuthor();
if ($author && $requestAuthorization) {
$this->sendAuthorMail($author);
Expand Down Expand Up @@ -441,15 +457,16 @@ function handleSubmissionSubmitStep3FormExecute($hookName, $params) {
* @return bool
*/
function handleAdditionalFieldNames($hookName, $params) {
$fields =& $params[1];
$fields =& $params[1];
// the ORCID id was retrieved from the ORCID sandbox API
$fields[] = 'orcidSandbox';
$fields[] = 'orcidAccessToken';
$fields[] = 'orcidAccessScope';
$fields[] = 'orcidRefreshToken';
$fields[] = 'orcidAccessExpiresOn';
$fields[] = 'orcidAccessDenied';
if ($hookName === 'authordao::getAdditionalFieldNames') {
// holds a one time hash string generated before sending the ORCID authorization e-mail
// holds a one time hash string generated before sending the ORCID authorization e-mail
$fields[] = 'orcidEmailToken';
// holds the id of the added work entry in the corresponding ORCID profile for updates
$fields[] = 'orcidWorkPutCode';
Expand Down Expand Up @@ -623,17 +640,17 @@ public function sendAuthorMail($author, $updateAuthor = false)
assert($context != null);
$contextId = $context->getId();
if ( $this->isMemberApiEnabled($contextId) ) {
$mailTemplate = 'ORCID_REQUEST_AUTHOR_AUTHORIZATION';
$mailTemplate = 'ORCID_REQUEST_AUTHOR_AUTHORIZATION';
}
else {
$mailTemplate = 'ORCID_COLLECT_AUTHOR_ID';
$mailTemplate = 'ORCID_COLLECT_AUTHOR_ID';
}
$mail = $this->getMailTemplate($mailTemplate, $context);
$emailToken = md5(microtime().$author->getEmail());
$author->setData('orcidEmailToken', $emailToken);
$articleDao = DAORegistry::getDAO('ArticleDAO');
$article = $articleDao->getById($author->getSubmissionId());
$oauthUrl = $this->buildOAuthUrl('orcidVerify', array('token' => $emailToken, 'articleId' => $author->getSubmissionId()));
$oauthUrl = $this->buildOAuthUrl('orcidVerify', array('token' => $emailToken, 'articleId' => $author->getSubmissionId()));
$aboutUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'orcidapi', 'about', null);
// Set From to primary journal contact
$mail->setFrom($context->getSetting('contactEmail'), $context->getSetting('contactName'));
Expand Down Expand Up @@ -947,35 +964,37 @@ function($curl, $header) use (&$responseHeaders)
*/
public function buildOrcidWork($article, $journal, $authors, $issue, $request) {
$articleLocale = $article->getLocale();
$titles = $article->getTitle($articleLocale);
$titles = $article->getTitle($articleLocale);
$citationPlugin = PluginRegistry::getPlugin('generic', 'citationstylelanguageplugin');
$bibtexCitation = trim(strip_tags($citationPlugin->getCitation($request, $article, 'bibtex')));
$articleUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'article', 'view', $article->getBestArticleId());
$orcidWork = [
'title' => [
'title' => [
'value' => $article->getLocalizedTitle($articleLocale)
],
'subtitle' => [
'value' => $article->getSubtitle($articleLocale)
]
],
'journal-title' => [
'value' => $journal->getName('en_US')
'value' => $journal->getLocalizedName($articleLocale)
],
'short-description' => trim(strip_tags($article->getAbstract('en_US'))),
'short-description' => trim(strip_tags($article->getLocalizedData('abstract', $articleLocale))),
'type' => 'JOURNAL_ARTICLE',
'external-ids' => [ 'external-id' => $this->buildOrcidExternalIds($article, $journal, $issue, $articleUrl)],
'publication-date' => $this->buildOrcidPublicationDate($issue),
'url' => $articleUrl,
'citation' => [
'citation-type' => 'BIBTEX',
'citation-value' => $bibtexCitation
],
'language-code' => substr($articleLocale, 0, 2),
'contributors' => [ 'contributor' => $this->buildOrcidContributors($authors, $journal->getId()) ]
];
if ($articleLocale !== 'en_US') {
if ($citationPlugin) {
$bibtexCitation = trim(strip_tags($citationPlugin->getCitation($request, $article, 'bibtex')));
$orcidWork['citation'] = [
'citation-type' => 'BIBTEX',
'citation-value' => $bibtexCitation
];
}
if ($article->getSubtitle($articleLocale)) {
$orcidWork['title']['subtitle'] = [ 'value' => $article->getSubtitle($articleLocale)];
}
if ($articleLocale !== 'en_US' && $article->getTitle('en_US')) {
$orcidWork['title']['translated-title'] = [
'value' => $article->getTitle('en_US'),
'language-code' => 'en'
Expand Down Expand Up @@ -1046,7 +1065,7 @@ private function buildOrcidExternalIds($article, $journal, $issue, $articleUrl)
}
else {
error_log("OrcidProfilePlugin::buildOrcidExternalIds: No pubId plugins could be loaded");
}
}
if ( !$articleHasStoredPubId ) {
// No pubidplugins available or article does not have any stored pubid
// Use URL as an external-id
Expand All @@ -1072,7 +1091,7 @@ private function buildOrcidExternalIds($article, $journal, $issue, $articleUrl)
/**
* Build associative array fitting for ORCID contributor mentions in an
* ORCID work from the supplied Authors array.
*
*
* @param $authors Author[] Array of Author objects
* @param $contextId int Id of the context the Author objects belong to
* @return array[] Array of associative arrays,
Expand Down Expand Up @@ -1116,9 +1135,9 @@ private function buildOrcidContributors($authors, $contextId) {
}

/**
* Remove all data fields, which belong to an ORCID access token from the
* Remove all data fields, which belong to an ORCID access token from the
* given Author object. Also updates fields in the db.
*
*
* @param $author Author object with ORCID access token
* @return void
*/
Expand All @@ -1134,7 +1153,7 @@ public function removeOrcidAccessToken($author, $saveAuthor=true) {
}
}

/**
/**
* @return string Path to a custom ORCID log file.
*/
public static function logFilePath() {
Expand All @@ -1143,7 +1162,7 @@ public static function logFilePath() {

/**
* Write error message to log.
*
*
* @param $message string Message to write
* @return void
*/
Expand All @@ -1153,7 +1172,7 @@ public function logError($message) {

/**
* Write info message to log.
*
*
* @param $message string Message to write
* @return void
*/
Expand All @@ -1168,7 +1187,7 @@ public function logInfo($message) {

/**
* Write a message with specified level to log
*
*
* @param $message string Message to write
* @param $level string Error level to add to message
* @return void
Expand Down
2 changes: 1 addition & 1 deletion OrcidProfileSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OrcidProfileSettingsForm extends Form {
const CONFIG_VARS = array(
'orcidProfileAPIPath' => 'string',
'orcidClientId' => 'string',
'orcidClientSecret' => 'string',
'orcidClientSecret' => 'string',
'sendMailToAuthorsOnPublication' => 'bool',
'logLevel' => 'string');
/** @var $contextId int */
Expand Down
Loading