Skip to content

Commit

Permalink
pkp#229 OrcidProfilePlugin-705
Browse files Browse the repository at this point in the history
  • Loading branch information
withanage committed Feb 8, 2023
1 parent 36199ca commit fafbdac
Showing 1 changed file with 108 additions and 97 deletions.
205 changes: 108 additions & 97 deletions OrcidProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,32 @@
use APP\core\Application;
use APP\decision\Decision;
use APP\facades\Repo;
use APP\plugins\generic\orcidProfile\classes\form\OrcidProfileSettingsForm;
use APP\plugins\generic\orcidProfile\classes\form\OrcidProfileStatusForm;
use APP\plugins\generic\orcidProfile\classes\OrcidValidator;
use APP\plugins\generic\orcidProfile\mailables\OrcidCollectAuthorId;
use APP\plugins\generic\orcidProfile\mailables\OrcidRequestAuthorAuthorization;
use APP\plugins\generic\orcidProfile\OrcidProfileHanlder;
use APP\template\TemplateManager;
use Illuminate\Support\Facades\Mail;
use PKP\components\forms\FieldOptions;
use PKP\components\forms\publication\ContributorForm;
use PKP\config\Config;
use PKP\core\Core;
use PKP\core\JSONMessage;
use PKP\core\PKPApplication;
use PKP\linkAction\LinkAction;
use PKP\plugins\Hook;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\submission\PKPSubmission;
use APP\plugins\generic\orcidProfile\OrcidProfileHanlder;
use APP\plugins\generic\orcidProfile\mailables\OrcidCollectAuthorId;
use APP\plugins\generic\orcidProfile\classes\form\OrcidProfileSettingsForm;
use APP\plugins\generic\orcidProfile\classes\form\OrcidProfileStatusForm;
use APP\plugins\generic\orcidProfile\classes\OrcidValidator;



class OrcidProfilePlugin extends GenericPlugin
{
public const PUBID_TO_ORCID_EXT_ID = ['doi' => 'doi', 'other::urn' => 'urn'];
public const USER_GROUP_TO_ORCID_ROLE = ['Author' => 'AUTHOR', 'Translator' => 'CHAIR_OR_TRANSLATOR','Journal manager' => 'AUTHOR'];
public const USER_GROUP_TO_ORCID_ROLE = ['Author' => 'AUTHOR', 'Translator' => 'CHAIR_OR_TRANSLATOR', 'Journal manager' => 'AUTHOR'];

private $submissionIdToBePublished;
private $currentContextId;
Expand Down Expand Up @@ -193,9 +194,11 @@ public function register($category, $path, $mainContextId = null)

Hook::add('Mailer::Mailables', [$this, 'addMailable']);

Hook::add('Form::config::before', [$this, 'addOrcidFormFields']);
#Hook::add('Form::config::before', [$this, 'addOrcidFormFields']);
Hook::add('Author::edit', [$this, 'handleAuthorFormExecute']);

Hook::add('Form::config::before', [$this, 'addOrcidFormFields']);

}

return $success;
Expand All @@ -207,11 +210,11 @@ public function register($category, $path, $mainContextId = null)
* @param $form
* @return bool
*/
public function addOrcidFormFields($hookName, $form)
function addOrcidFormFields($hookName, $form): bool

{

if (!$form instanceof PKP\components\forms\publication\ContributorForm) return Hook::CONTINUE;
if (!$form instanceof ContributorForm) return Hook::CONTINUE;
$form->addField(new FieldOptions('requestOrcidAuthorization', [
'label' => __('plugins.generic.orcidProfile.verify.title'),
'options' => [
Expand All @@ -237,7 +240,8 @@ public function addOrcidFormFields($hookName, $form)
* @param $hookName
* @param $args
*/
function handleThankReviewer($hookName, $args) {
function handleThankReviewer($hookName, $args)
{
$request = PKPApplication::get()->getRequest();
$context = $request->getContext();
$newPublication =& $args[0];
Expand Down Expand Up @@ -291,23 +295,24 @@ public function setupCallbackHandler($hookName, $params)
{
$page = $params[0];
if ($this->getEnabled() && $page == 'orcidapi') {
define('HANDLER_CLASS', 'APP\plugins\generic\orcidProfile\OrcidProfileHandler');
return true;
define('HANDLER_CLASS', 'APP\plugins\generic\orcidProfile\OrcidProfileHandler');
return true;
}
return false;
}

/**
* Check if there exist a valid orcid configuration section in the global config.inc.php of OJS.
* @return boolean True, if the config file has api_url, client_id and client_secret set in an [orcid] section
*/
function isGloballyConfigured() {
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}
/**
* Check if there exist a valid orcid configuration section in the global config.inc.php of OJS.
* @return boolean True, if the config file has api_url, client_id and client_secret set in an [orcid] section
*/
function isGloballyConfigured()
{
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}


/**
Expand All @@ -329,7 +334,7 @@ public function handleFormDisplay($hookName, $args)
$templateMgr = TemplateManager::getManager($request);
switch ($hookName) {
case 'authorform::display':
$authorForm = & $args[0];
$authorForm = &$args[0];
$author = $authorForm->getAuthor();
if ($author) {
$authenticated = !empty($author->getData('orcidAccessToken'));
Expand Down Expand Up @@ -364,8 +369,8 @@ public function handleFormDisplay($hookName, $args)
public function handleTemplateDisplay($hookName, $args)
{
//TODO orcid
$templateMgr = & $args[0];
$template = & $args[1];
$templateMgr = &$args[0];
$template = &$args[1];
$request = Application::get()->getRequest();

// Assign our private stylesheet, for front and back ends.
Expand All @@ -385,14 +390,15 @@ public function handleTemplateDisplay($hookName, $args)
return false;
}

/**
* Return the location of the plugin's CSS file
*
* @return string
*/
function getStyleSheet() {
return $this->getPluginPath() . '/css/orcidProfile.css';
}
/**
* Return the location of the plugin's CSS file
*
* @return string
*/
function getStyleSheet()
{
return $this->getPluginPath() . '/css/orcidProfile.css';
}


/**
Expand Down Expand Up @@ -442,27 +448,30 @@ public function buildOAuthUrl($handlerMethod, $redirectParams)
);

return $this->getOauthPath() . 'authorize?' . http_build_query(
[
'client_id' => $this->getSetting($contextId, 'orcidClientId'),
'response_type' => 'code',
'scope' => $scope,
'redirect_uri' => $redirectUrl]
);
[
'client_id' => $this->getSetting($contextId, 'orcidClientId'),
'response_type' => 'code',
'scope' => $scope,
'redirect_uri' => $redirectUrl]
);
}

/**
* Return a string of the ORCiD SVG icon
*
* @return string
*/
function getIcon() {
function getIcon()
{
$path = Core::getBaseDir() . '/' . $this->getPluginPath() . '/templates/images/orcid.svg';
return file_exists($path) ? file_get_contents($path) : '';
}

/**
* @return bool True if the ORCID Member API has been selected in this context.
*/
public function isMemberApiEnabled($contextId) {
public function isMemberApiEnabled($contextId)
{
$apiUrl = $this->getSetting($contextId, 'orcidProfileAPIPath');
if ($apiUrl === ORCID_API_URL_MEMBER || $apiUrl === ORCID_API_URL_MEMBER_SANDBOX) {
return true;
Expand All @@ -481,7 +490,8 @@ public function getOauthPath()
return $this->getOrcidUrl() . 'oauth/';
}

public function isSandbox() {
public function isSandbox()
{

$apiUrl = $this->getSetting($this->getCurrentContextId(), 'orcidProfileAPIPath');
return ($apiUrl == ORCID_API_URL_MEMBER_SANDBOX);
Expand Down Expand Up @@ -534,8 +544,8 @@ public function registrationFilter($output, $templateMgr)
*/
public function handleUserPublicProfileDisplay($hookName, $params)
{
$templateMgr = & $params[1];
$output = & $params[2];
$templateMgr = &$params[1];
$output = &$params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();
$user = $request->getUser();
Expand Down Expand Up @@ -568,7 +578,7 @@ public function handleUserPublicProfileDisplay($hookName, $params)
*/
public function handleAuthorFormExecute($hookname, $args)
{
if (count($args)==3) {
if (count($args) == 3) {
$author = &$args[0];
$values = $args[2];

Expand Down Expand Up @@ -629,6 +639,7 @@ public function sendAuthorMail($author, $updateAuthor = false)
}
}
}

/**
* Remove all data fields, which belong to an ORCID access token from the
* given Author object. Also updates fields in the db.
Expand Down Expand Up @@ -711,7 +722,7 @@ public function handleSubmissionSubmitStep3FormExecute($hookName, $params)
*/
public function handleAdditionalFieldNames($hookName, $params)
{
$fields = & $params[1];
$fields = &$params[1];
$fields[] = 'orcidSandbox';
$fields[] = 'orcidAccessToken';
$fields[] = 'orcidAccessScope';
Expand Down Expand Up @@ -794,12 +805,12 @@ public function getActions($request, $actionArgs)
__('manager.plugins.settings'),
null
),
new LinkAction(
'status',
new AjaxModal($router->url($request, null, null, 'manage', null, array('verb' => 'status', 'plugin' => $this->getName(), 'category' => 'generic')), $this->getDisplayName()),
__('common.status'),
null
)
new LinkAction(
'status',
new AjaxModal($router->url($request, null, null, 'manage', null, array('verb' => 'status', 'plugin' => $this->getName(), 'category' => 'generic')), $this->getDisplayName()),
__('common.status'),
null
)
] : [],
parent::getActions($request, $actionArgs)
);
Expand All @@ -808,35 +819,36 @@ public function getActions($request, $actionArgs)
/**
* @see Plugin::manage()
*/
function getDisplayName() {
return __('plugins.generic.orcidProfile.displayName');
}

function getDisplayName()
{
return __('plugins.generic.orcidProfile.displayName');
}

function setEnabled($enabled) {
$contextId = $this->getCurrentContextId();
$request = Application::get()->getRequest();
$validator = new OrcidValidator($this);

if ($this->isSitePlugin()) {
$contextId = 0;
}
if ($request->getUserVar('save') == 1) {
$clientId = $request->getUserVar('orcidClientId');
$clientSecret = $request->getUserVar('orcidClientSecret');
} else {
$clientId = $this->getSetting($contextId, 'orcidClientId');
$clientSecret = $this->getSetting($contextId, 'orcidClientSecret');
function setEnabled($enabled)
{
$contextId = $this->getCurrentContextId();
$request = Application::get()->getRequest();
$validator = new OrcidValidator($this);

}
if ($this->isSitePlugin()) {
$contextId = 0;
}
if ($request->getUserVar('save') == 1) {
$clientId = $request->getUserVar('orcidClientId');
$clientSecret = $request->getUserVar('orcidClientSecret');
} else {
$clientId = $this->getSetting($contextId, 'orcidClientId');
$clientSecret = $this->getSetting($contextId, 'orcidClientSecret');

if (!$validator->validateClientSecret($clientSecret) or !$validator->validateClientId($clientId)) {
$enabled = false;
}

}
$this->updateSetting($contextId, 'enabled', $enabled, 'bool');
}
if (!$validator->validateClientSecret($clientSecret) or !$validator->validateClientId($clientId)) {
$enabled = false;

}
$this->updateSetting($contextId, 'enabled', $enabled, 'bool');
}


public function manage($args, $request)
Expand All @@ -857,19 +869,19 @@ public function manage($args, $request)
ORCID_API_URL_MEMBER_SANDBOX => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.memberSandbox'
]);

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$countries = array();
foreach ($isoCodes->getCountries() as $country) {
$countries[$country->getAlpha2()] = $country->getLocalName();
}
asort($countries);
$templateMgr->assign('countries', $countries);
$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$countries = array();
foreach ($isoCodes->getCountries() as $country) {
$countries[$country->getAlpha2()] = $country->getLocalName();
}
asort($countries);
$templateMgr->assign('countries', $countries);
$templateMgr->assign('logLevelOptions', [
'ERROR' => 'plugins.generic.orcidProfile.manager.settings.logLevel.error',
'ALL' => 'plugins.generic.orcidProfile.manager.settings.logLevel.all'
]);

$form = new OrcidProfileSettingsForm($this, $contextId);
$form = new OrcidProfileSettingsForm($this, $contextId);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
Expand All @@ -879,10 +891,10 @@ public function manage($args, $request)
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
case 'status':
$form = new OrcidProfileStatusForm($this, $contextId);
$form->initData();
return new JSONMessage(true, $form->fetch($request));
case 'status':
$form = new OrcidProfileStatusForm($this, $contextId);
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
Expand All @@ -901,11 +913,11 @@ public function manage($args, $request)
*/
public function handlePublicationStatusChange($hookName, $args)
{
$newPublication = & $args[0];
$newPublication = &$args[0];
/** @var Publication $newPublication */
$publication = & $args[1];
$publication = &$args[1];
/** @var Publication $publication */
$submission = & $args[2];
$submission = &$args[2];
/** @var Submission $submission */

$request = Application::get()->getRequest();
Expand Down Expand Up @@ -1166,11 +1178,10 @@ public function buildOrcidWork($publication, $context, $authors, $request, $issu
'citation-type' => 'bibtex',
'citation-value' => $bibtexCitation,
];
$orcidWork['type'] = 'journal-article';
$orcidWork['type'] = 'journal-article';
} elseif ($applicationName == 'ops') {
$orcidWork['type'] = 'preprint';
}
elseif ($applicationName == 'ops') {
$orcidWork['type'] = 'preprint';
}

$translatedTitleAvailable = false;
foreach ($supportedSubmissionLocales as $defaultLanguage) {
Expand Down

0 comments on commit fafbdac

Please sign in to comment.