first commit
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/author/Author.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\author\Author
|
||||
*
|
||||
* @ingroup author
|
||||
*
|
||||
* @see DAO
|
||||
*
|
||||
* @brief Author metadata class.
|
||||
*/
|
||||
|
||||
namespace PKP\author;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\identity\Identity;
|
||||
|
||||
class Author extends Identity
|
||||
{
|
||||
/**
|
||||
* Get the default/fall back locale the values should exist for
|
||||
*/
|
||||
public function getDefaultLocale(): ?string
|
||||
{
|
||||
return $this->getSubmissionLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Identity::getLocalizedGivenName()
|
||||
*/
|
||||
public function getLocalizedGivenName()
|
||||
{
|
||||
return $this->getLocalizedData(self::IDENTITY_SETTING_GIVENNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Identity::getLocalizedFamilyName()
|
||||
*/
|
||||
public function getLocalizedFamilyName()
|
||||
{
|
||||
// Prioritize the current locale, then the default locale.
|
||||
$locale = Locale::getLocale();
|
||||
$givenName = $this->getGivenName($locale);
|
||||
// Only use the family name if a given name exists (to avoid mixing locale data)
|
||||
if (!empty($givenName)) {
|
||||
return $this->getFamilyName($locale);
|
||||
}
|
||||
// Fall back on the submission locale.
|
||||
return $this->getFamilyName($this->getSubmissionLocale());
|
||||
}
|
||||
|
||||
//
|
||||
// Get/set methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Get ID of submission.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSubmissionId()
|
||||
{
|
||||
return $this->getData('submissionId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ID of submission.
|
||||
*
|
||||
* @param int $submissionId
|
||||
*/
|
||||
public function setSubmissionId($submissionId)
|
||||
{
|
||||
$this->setData('submissionId', $submissionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get submission locale.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSubmissionLocale()
|
||||
{
|
||||
return $this->getData('locale');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set submission locale.
|
||||
*
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setSubmissionLocale($locale)
|
||||
{
|
||||
return $this->setData('locale', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user group id
|
||||
*
|
||||
* @param int $userGroupId
|
||||
*/
|
||||
public function setUserGroupId($userGroupId)
|
||||
{
|
||||
$this->setData('userGroupId', $userGroupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user group id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getUserGroupId()
|
||||
{
|
||||
return $this->getData('userGroupId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not to include in browse lists.
|
||||
*
|
||||
* @param bool $include
|
||||
*/
|
||||
public function setIncludeInBrowse($include)
|
||||
{
|
||||
$this->setData('includeInBrowse', $include);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether or not to include in browse lists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getIncludeInBrowse()
|
||||
{
|
||||
return $this->getData('includeInBrowse');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "show title" flag (whether or not the title of the role
|
||||
* should be included in the list of submission contributor names).
|
||||
* This is fetched from the user group for performance reasons.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getShowTitle()
|
||||
{
|
||||
return $this->getData('showTitle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "show title" flag. This attribute belongs to the user group,
|
||||
* NOT the author; fetched for performance reasons only.
|
||||
*
|
||||
* @param bool $showTitle
|
||||
*/
|
||||
public function _setShowTitle($showTitle)
|
||||
{
|
||||
$this->setData('showTitle', $showTitle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get primary contact.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getPrimaryContact()
|
||||
{
|
||||
return $this->getData('primaryContact');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set primary contact.
|
||||
*
|
||||
* @param bool $primaryContact
|
||||
*/
|
||||
public function setPrimaryContact($primaryContact)
|
||||
{
|
||||
$this->setData('primaryContact', $primaryContact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sequence of author in submissions' author list.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getSequence()
|
||||
{
|
||||
return $this->getData('seq');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set sequence of author in submissions' author list.
|
||||
*
|
||||
* @param float $sequence
|
||||
*/
|
||||
public function setSequence($sequence)
|
||||
{
|
||||
$this->setData('seq', $sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user group for this contributor.
|
||||
*
|
||||
* @return \PKP\userGroup\UserGroup
|
||||
*/
|
||||
public function getUserGroup()
|
||||
{
|
||||
//FIXME: should this be queried when fetching Author from DB? - see #5231.
|
||||
static $userGroup; // Frequently we'll fetch the same one repeatedly
|
||||
if (!$userGroup || $this->getUserGroupId() != $userGroup->getId()) {
|
||||
$userGroup = Repo::userGroup()->get($this->getUserGroupId());
|
||||
}
|
||||
return $userGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a localized version of the User Group
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedUserGroupName()
|
||||
{
|
||||
$userGroup = $this->getUserGroup();
|
||||
return $userGroup->getLocalizedName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get competing interests.
|
||||
* @return string|array|null
|
||||
*/
|
||||
function getCompetingInterests(?string $locale)
|
||||
{
|
||||
return $this->getData('competingInterests', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set competing interests.
|
||||
* @param $competingInterests string|array|null
|
||||
*/
|
||||
function setCompetingInterests($competingInterests, ?string $locale)
|
||||
{
|
||||
$this->setData('competingInterests', $competingInterests, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a localized version competing interest statement
|
||||
*/
|
||||
function getLocalizedCompetingInterests(): ?string
|
||||
{
|
||||
return $this->getLocalizedData('competingInterests');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/author/Collector.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Collector
|
||||
*
|
||||
* @brief A helper class to configure a Query Builder to get a collection of announcements
|
||||
*/
|
||||
|
||||
namespace PKP\author;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\interfaces\CollectorInterface;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
/**
|
||||
* @template T of Author
|
||||
*/
|
||||
class Collector implements CollectorInterface
|
||||
{
|
||||
public const ORDERBY_SEQUENCE = 'sequence';
|
||||
public const ORDERBY_ID = 'id';
|
||||
|
||||
/** @var string The default orderBy value for authors collector */
|
||||
public $orderBy = self::ORDERBY_SEQUENCE;
|
||||
|
||||
/** @var DAO */
|
||||
public $dao;
|
||||
|
||||
/** @var int[]|null */
|
||||
public $contextIds = null;
|
||||
|
||||
/** @var int[]|null */
|
||||
public $publicationIds = null;
|
||||
|
||||
/** Get authors with a family name */
|
||||
protected ?string $familyName = null;
|
||||
|
||||
/** Get authors with a given name */
|
||||
protected ?string $givenName = null;
|
||||
|
||||
/** Get authors with a specified country code */
|
||||
protected ?string $country = null;
|
||||
|
||||
/** Get authors with a specified affiliation */
|
||||
protected ?string $affiliation = null;
|
||||
|
||||
public ?int $count = null;
|
||||
|
||||
public ?int $offset = null;
|
||||
|
||||
public ?bool $includeInBrowse = null;
|
||||
|
||||
public function __construct(DAO $dao)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
}
|
||||
|
||||
public function getCount(): int
|
||||
{
|
||||
return $this->dao->getCount($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(): Collection
|
||||
{
|
||||
return $this->dao->getIds($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc DAO::getMany()
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(): LazyCollection
|
||||
{
|
||||
return $this->dao->getMany($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by contexts
|
||||
*/
|
||||
public function filterByContextIds(?array $contextIds): self
|
||||
{
|
||||
$this->contextIds = $contextIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by publications
|
||||
*/
|
||||
public function filterByPublicationIds(?array $publicationIds): self
|
||||
{
|
||||
$this->publicationIds = $publicationIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by include in browse
|
||||
*/
|
||||
public function filterByIncludeInBrowse(?bool $includeInBrowse): self
|
||||
{
|
||||
$this->includeInBrowse = $includeInBrowse;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Include orderBy columns to the collector query
|
||||
*/
|
||||
public function orderBy(?string $orderBy): self
|
||||
{
|
||||
$this->orderBy = $orderBy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by the given and family name
|
||||
*
|
||||
*
|
||||
*/
|
||||
public function filterByName(?string $givenName, ?string $familyName): self
|
||||
{
|
||||
$this->givenName = $givenName;
|
||||
$this->familyName = $familyName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by the specified country code
|
||||
*
|
||||
* @param string $country Country code (2-letter)
|
||||
*
|
||||
* */
|
||||
public function filterByCountry(?string $country): self
|
||||
{
|
||||
$this->country = $country;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by the specified affiliation code
|
||||
*
|
||||
* */
|
||||
public function filterByAffiliation(?string $affiliation): self
|
||||
{
|
||||
$this->affiliation = $affiliation;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Limit the number of objects retrieved
|
||||
*/
|
||||
public function limit(?int $count): self
|
||||
{
|
||||
$this->count = $count;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset the number of objects retrieved, for example to
|
||||
* retrieve the second page of contents
|
||||
*/
|
||||
public function offset(?int $offset): self
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc CollectorInterface::getQueryBuilder()
|
||||
*/
|
||||
public function getQueryBuilder(): Builder
|
||||
{
|
||||
$q = DB::table('authors as a')
|
||||
->select(['a.*', 's.locale AS submission_locale'])
|
||||
->join('publications as p', 'a.publication_id', '=', 'p.publication_id')
|
||||
->join('submissions as s', 'p.submission_id', '=', 's.submission_id');
|
||||
|
||||
if (isset($this->contextIds)) {
|
||||
$q->whereIn('s.context_id', $this->contextIds);
|
||||
}
|
||||
|
||||
$q->when($this->familyName !== null, function (Builder $q) {
|
||||
$q->whereIn('a.author_id', function (Builder $q) {
|
||||
$q->select('author_id')
|
||||
->from($this->dao->settingsTable)
|
||||
->where('setting_name', '=', 'familyName')
|
||||
->where('setting_value', $this->familyName);
|
||||
});
|
||||
});
|
||||
|
||||
$q->when($this->givenName !== null, function (Builder $q) {
|
||||
$q->whereIn('a.author_id', function (Builder $q) {
|
||||
$q->select('author_id')
|
||||
->from($this->dao->settingsTable)
|
||||
->where('setting_name', '=', 'givenName')
|
||||
->where('setting_value', $this->givenName);
|
||||
});
|
||||
});
|
||||
|
||||
if (isset($this->publicationIds)) {
|
||||
$q->whereIn('a.publication_id', $this->publicationIds);
|
||||
}
|
||||
|
||||
$q->when($this->country !== null, function (Builder $q) {
|
||||
$q->whereIn('a.author_id', function (Builder $q) {
|
||||
$q->select('author_id')
|
||||
->from($this->dao->settingsTable)
|
||||
->where('setting_name', '=', 'country')
|
||||
->where('setting_value', $this->country);
|
||||
});
|
||||
});
|
||||
|
||||
$q->when($this->affiliation !== null, function (Builder $q) {
|
||||
$q->whereIn('a.author_id', function (Builder $q) {
|
||||
$q->select('author_id')
|
||||
->from($this->dao->settingsTable)
|
||||
->where('setting_name', '=', 'affiliation')
|
||||
->where('setting_value', $this->affiliation);
|
||||
});
|
||||
});
|
||||
|
||||
if ($this->includeInBrowse) {
|
||||
$q->where('a.include_in_browse', $this->includeInBrowse);
|
||||
}
|
||||
|
||||
if (isset($this->count)) {
|
||||
$q->limit($this->count);
|
||||
}
|
||||
|
||||
if (isset($this->offset)) {
|
||||
$q->offset($this->offset);
|
||||
}
|
||||
|
||||
switch ($this->orderBy) {
|
||||
case self::ORDERBY_SEQUENCE:
|
||||
$q->orderBy('a.seq', 'asc');
|
||||
break;
|
||||
case self::ORDERBY_ID:
|
||||
default:
|
||||
$q->orderBy('a.author_id', 'asc');
|
||||
break;
|
||||
}
|
||||
|
||||
// Add app-specific query statements
|
||||
Hook::call('Author::Collector', [&$q, $this]);
|
||||
|
||||
return $q;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/author/DAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class DAO
|
||||
*
|
||||
* @ingroup author
|
||||
*
|
||||
* @see \PKP\author\Author
|
||||
*
|
||||
* @brief Operations for retrieving and modifying Author objects.
|
||||
*/
|
||||
|
||||
namespace PKP\author;
|
||||
|
||||
use APP\author\Author;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\EntityDAO;
|
||||
use PKP\facades\Repo;
|
||||
use PKP\services\PKPSchemaService;
|
||||
|
||||
/**
|
||||
* @template T of Author
|
||||
* @extends EntityDAO<T>
|
||||
*/
|
||||
class DAO extends EntityDAO
|
||||
{
|
||||
/** @copydoc EntityDAO::$schema */
|
||||
public $schema = PKPSchemaService::SCHEMA_AUTHOR;
|
||||
|
||||
/** @copydoc EntityDAO::$table */
|
||||
public $table = 'authors';
|
||||
|
||||
/** @copydoc EntityDAO::$settingsTable */
|
||||
public $settingsTable = 'author_settings';
|
||||
|
||||
/** @copydoc EntityDAO::$primaryKeyColumn */
|
||||
public $primaryKeyColumn = 'author_id';
|
||||
|
||||
/** @copydoc EntityDAO::$primaryTableColumns */
|
||||
public $primaryTableColumns = [
|
||||
'id' => 'author_id',
|
||||
'email' => 'email',
|
||||
'includeInBrowse' => 'include_in_browse',
|
||||
'publicationId' => 'publication_id',
|
||||
'seq' => 'seq',
|
||||
'userGroupId' => 'user_group_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the parent object ID column name
|
||||
*/
|
||||
public function getParentColumn(): string
|
||||
{
|
||||
return 'publication_id';
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate a new DataObject
|
||||
*/
|
||||
public function newDataObject(): Author
|
||||
{
|
||||
return app(Author::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an author.
|
||||
*
|
||||
* Optionally, pass the publication ID to only get an author
|
||||
* if it exists and is assigned to that publication.
|
||||
*/
|
||||
public function get(int $id, int $publicationId = null): ?Author
|
||||
{
|
||||
// This is overridden due to the need to include submission_locale
|
||||
// to the fromRow function
|
||||
$row = DB::table('authors as a')
|
||||
->join('publications as p', 'a.publication_id', '=', 'p.publication_id')
|
||||
->join('submissions as s', 'p.submission_id', '=', 's.submission_id')
|
||||
->where('a.author_id', '=', $id)
|
||||
->when($publicationId !== null, fn (Builder $query) => $query->where('a.publication_id', '=', $publicationId))
|
||||
->select(['a.*', 's.locale AS submission_locale'])
|
||||
->first();
|
||||
return $row ? $this->fromRow($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an author exists.
|
||||
*
|
||||
* Optionally, pass the publication ID to check if the author
|
||||
* exists and is assigned to that publication.
|
||||
*/
|
||||
public function exists(int $id, int $publicationId = null): bool
|
||||
{
|
||||
return DB::table($this->table)
|
||||
->where($this->primaryKeyColumn, '=', $id)
|
||||
->when($publicationId !== null, fn (Builder $query) => $query->where($this->getParentColumn(), $publicationId))
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total count of rows matching the configured query
|
||||
*/
|
||||
public function getCount(Collector $query): int
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of ids matching the configured query
|
||||
*
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(Collector $query): Collection
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->select('a.' . $this->primaryKeyColumn)
|
||||
->pluck('a.' . $this->primaryKeyColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of publications matching the configured query
|
||||
*
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(Collector $query): LazyCollection
|
||||
{
|
||||
$rows = $query
|
||||
->getQueryBuilder()
|
||||
->get();
|
||||
|
||||
return LazyCollection::make(function () use ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
yield $row->author_id => $this->fromRow($row);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::fromRow()
|
||||
*/
|
||||
public function fromRow(object $row): Author
|
||||
{
|
||||
$author = parent::fromRow($row);
|
||||
|
||||
// Set the primary locale from the submission
|
||||
$author->setData('locale', $row->submission_locale);
|
||||
|
||||
return $author;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::insert()
|
||||
*/
|
||||
public function insert(Author $author): int
|
||||
{
|
||||
return parent::_insert($author);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::update()
|
||||
*/
|
||||
public function update(Author $author)
|
||||
{
|
||||
parent::_update($author);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::delete()
|
||||
*/
|
||||
public function delete(Author $author)
|
||||
{
|
||||
DB::table('publications')
|
||||
->where('primary_contact_id', $author->getId())
|
||||
->update(['primary_contact_id' => null]);
|
||||
|
||||
parent::_delete($author);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next sequence that should be used when adding a contributor to a publication
|
||||
*/
|
||||
public function getNextSeq(int $publicationId): int
|
||||
{
|
||||
$nextSeq = 0;
|
||||
$seq = DB::table('authors as a')
|
||||
->join('publications as p', 'a.publication_id', '=', 'p.publication_id')
|
||||
->where('p.publication_id', '=', $publicationId)
|
||||
->max('a.seq');
|
||||
|
||||
if ($seq) {
|
||||
$nextSeq = $seq + 1;
|
||||
}
|
||||
|
||||
return $nextSeq;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the order of contributors in a publication
|
||||
*
|
||||
* This method resets the seq property for each contributor in a publication
|
||||
* so that they are numbered sequentially without any gaps.
|
||||
*
|
||||
* eg - 1, 3, 4, 6 will become 1, 2, 3, 4
|
||||
*/
|
||||
public function resetContributorsOrder(int $publicationId)
|
||||
{
|
||||
$authorIds = Repo::author()
|
||||
->getCollector()
|
||||
->filterByPublicationIds([$publicationId])
|
||||
->orderBy(Repo::author()->getCollector()::ORDERBY_SEQUENCE)
|
||||
->getIds();
|
||||
|
||||
foreach ($authorIds as $seq => $authorId) {
|
||||
DB::table('authors')->where('author_id', '=', $authorId)->update(['seq' => $seq]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/author/Repository.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2000-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Repository
|
||||
*
|
||||
* @brief A repository to find and manage authors.
|
||||
*/
|
||||
|
||||
namespace PKP\author;
|
||||
|
||||
use APP\author\Author;
|
||||
use APP\author\DAO;
|
||||
use APP\core\Request;
|
||||
use APP\core\Services;
|
||||
use APP\facades\Repo;
|
||||
use APP\submission\Submission;
|
||||
use PKP\context\Context;
|
||||
use PKP\plugins\Hook;
|
||||
use PKP\services\PKPSchemaService;
|
||||
use PKP\submission\PKPSubmission;
|
||||
use PKP\user\User;
|
||||
use PKP\validation\ValidatorFactory;
|
||||
|
||||
class Repository
|
||||
{
|
||||
/** @var DAO */
|
||||
public $dao;
|
||||
|
||||
/** @var string $schemaMap The name of the class to map this entity to its schema */
|
||||
public $schemaMap = maps\Schema::class;
|
||||
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
/** @var PKPSchemaService<Author> */
|
||||
protected $schemaService;
|
||||
|
||||
public function __construct(DAO $dao, Request $request, PKPSchemaService $schemaService)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
$this->request = $request;
|
||||
$this->schemaService = $schemaService;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::newDataObject() */
|
||||
public function newDataObject(array $params = []): Author
|
||||
{
|
||||
$object = $this->dao->newDataObject();
|
||||
if (!empty($params)) {
|
||||
$object->setAllData($params);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::get() */
|
||||
public function get(int $id, int $publicationId = null): ?Author
|
||||
{
|
||||
return $this->dao->get($id, $publicationId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::exists() */
|
||||
public function exists(int $id, int $publicationId = null): bool
|
||||
{
|
||||
return $this->dao->exists($id, $publicationId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::getCollector() */
|
||||
public function getCollector(): Collector
|
||||
{
|
||||
return app(Collector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the map class for mapping
|
||||
* authors to their schema
|
||||
*/
|
||||
public function getSchemaMap(): maps\Schema
|
||||
{
|
||||
return app('maps')->withExtensions($this->schemaMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate properties for an author
|
||||
*
|
||||
* Perform validation checks on data used to add or edit an author.
|
||||
*
|
||||
* @param Author|null $author The author being edited. Pass `null` if creating a new author
|
||||
* @param array $props A key/value array with the new data to validate
|
||||
*
|
||||
* @return array A key/value array with validation errors. Empty if no errors
|
||||
*/
|
||||
public function validate($author, $props, Submission $submission, Context $context)
|
||||
{
|
||||
$schemaService = Services::get('schema');
|
||||
$allowedLocales = $context->getSupportedSubmissionLocales();
|
||||
$primaryLocale = $submission->getData('locale');
|
||||
|
||||
$validator = ValidatorFactory::make(
|
||||
$props,
|
||||
$schemaService->getValidationRules(PKPSchemaService::SCHEMA_AUTHOR, $allowedLocales),
|
||||
[
|
||||
'country.regex' => __('validator.country.regex'),
|
||||
]
|
||||
);
|
||||
|
||||
// Check required fields
|
||||
ValidatorFactory::required(
|
||||
$validator,
|
||||
$author,
|
||||
$schemaService->getRequiredProps(PKPSchemaService::SCHEMA_AUTHOR),
|
||||
$schemaService->getMultilingualProps(PKPSchemaService::SCHEMA_AUTHOR),
|
||||
$allowedLocales,
|
||||
$primaryLocale
|
||||
);
|
||||
|
||||
// Check for input from disallowed locales
|
||||
ValidatorFactory::allowedLocales($validator, $schemaService->getMultilingualProps(PKPSchemaService::SCHEMA_AUTHOR), $allowedLocales);
|
||||
|
||||
// The publicationId must match an existing publication that is not yet published
|
||||
$validator->after(function ($validator) use ($props) {
|
||||
if (isset($props['publicationId']) && !$validator->errors()->get('publicationId')) {
|
||||
$publication = Repo::publication()->get($props['publicationId']);
|
||||
if (!$publication) {
|
||||
$validator->errors()->add('publicationId', __('author.publicationNotFound'));
|
||||
} elseif ($publication->getData('status') === PKPSubmission::STATUS_PUBLISHED) {
|
||||
$validator->errors()->add('publicationId', __('author.editPublishedDisabled'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$errors = [];
|
||||
if ($validator->fails()) {
|
||||
$errors = $schemaService->formatValidationErrors($validator->errors());
|
||||
}
|
||||
|
||||
Hook::call('Author::validate', [$errors, $author, $props, $allowedLocales, $primaryLocale]);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc \PKP\services\entityProperties\EntityWriteInterface::add()
|
||||
*/
|
||||
public function add(Author $author): int
|
||||
{
|
||||
$existingSeq = $author->getData('seq');
|
||||
|
||||
if (!isset($existingSeq)) {
|
||||
$nextSeq = $this->dao->getNextSeq($author->getData('publicationId'));
|
||||
$author->setData('seq', $nextSeq);
|
||||
}
|
||||
|
||||
$authorId = $this->dao->insert($author);
|
||||
$author = Repo::author()->get($authorId);
|
||||
|
||||
Hook::call('Author::add', [$author]);
|
||||
|
||||
return $author->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc \PKP\services\entityProperties\EntityWriteInterface::edit()
|
||||
*/
|
||||
public function edit(Author $author, array $params)
|
||||
{
|
||||
$newAuthor = Repo::author()->newDataObject(array_merge($author->_data, $params));
|
||||
|
||||
Hook::call('Author::edit', [$newAuthor, $author, $params]);
|
||||
|
||||
$this->dao->update($newAuthor);
|
||||
|
||||
Repo::author()->get($newAuthor->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc \PKP\services\entityProperties\EntityWriteInterface::delete()
|
||||
*/
|
||||
public function delete(Author $author)
|
||||
{
|
||||
Hook::call('Author::delete::before', [$author]);
|
||||
$this->dao->delete($author);
|
||||
|
||||
$this->dao->resetContributorsOrder($author->getData('publicationId'));
|
||||
|
||||
Hook::call('Author::delete', [$author]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an Author object from a User object
|
||||
*
|
||||
* This does not save the author in the database.
|
||||
*/
|
||||
public function newAuthorFromUser(User $user): Author
|
||||
{
|
||||
$author = Repo::author()->newDataObject();
|
||||
$author->setGivenName($user->getGivenName(null), null);
|
||||
$author->setFamilyName($user->getFamilyName(null), null);
|
||||
$author->setAffiliation($user->getAffiliation(null), null);
|
||||
$author->setCountry($user->getCountry());
|
||||
$author->setEmail($user->getEmail());
|
||||
$author->setUrl($user->getUrl());
|
||||
$author->setBiography($user->getBiography(null), null);
|
||||
$author->setIncludeInBrowse(1);
|
||||
$author->setOrcid($user->getOrcid());
|
||||
|
||||
Hook::call('Author::newAuthorFromUser', [$author, $user]);
|
||||
|
||||
return $author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update author names when publication locale changes.
|
||||
*
|
||||
* @param int $publicationId
|
||||
* @param string $oldLocale
|
||||
* @param string $newLocale
|
||||
*/
|
||||
public function changePublicationLocale($publicationId, $oldLocale, $newLocale)
|
||||
{
|
||||
$authors = $this->getCollector()
|
||||
->filterByPublicationIds([$publicationId])
|
||||
->getMany();
|
||||
|
||||
foreach ($authors as $author) {
|
||||
if (empty($author->getGivenName($newLocale))) {
|
||||
if (empty($author->getFamilyName($newLocale)) && empty($author->getPreferredPublicName($newLocale))) {
|
||||
// if no name exists for the new locale
|
||||
// copy all names with the old locale to the new locale
|
||||
$author->setGivenName($author->getGivenName($oldLocale), $newLocale);
|
||||
$author->setFamilyName($author->getFamilyName($oldLocale), $newLocale);
|
||||
$author->setPreferredPublicName($author->getPreferredPublicName($oldLocale), $newLocale);
|
||||
} else {
|
||||
// if the given name does not exist, but one of the other names do exist
|
||||
// copy only the given name with the old locale to the new locale, because the given name is required
|
||||
$author->setGivenName($author->getGivenName($oldLocale), $newLocale);
|
||||
}
|
||||
|
||||
$this->dao->update($author);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorders the authors of a publication according to the given order of the authors in the provided author array
|
||||
*/
|
||||
public function setAuthorsOrder(int $publicationId, array $authors)
|
||||
{
|
||||
$seq = 0;
|
||||
foreach ($authors as $author) {
|
||||
$author->setData('seq', $seq);
|
||||
|
||||
$this->dao->update($author);
|
||||
|
||||
$seq++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/author/maps/Schema.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2000-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Schema
|
||||
*
|
||||
* @brief Map authors to the properties defined in the announcement schema
|
||||
*/
|
||||
|
||||
namespace PKP\author\maps;
|
||||
|
||||
use APP\author\Author;
|
||||
use APP\facades\Repo;
|
||||
use Illuminate\Support\Enumerable;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\security\Role;
|
||||
use PKP\services\PKPSchemaService;
|
||||
use PKP\userGroup\UserGroup;
|
||||
use stdClass;
|
||||
|
||||
class Schema extends \PKP\core\maps\Schema
|
||||
{
|
||||
public Enumerable $collection;
|
||||
|
||||
public string $schema = PKPSchemaService::SCHEMA_AUTHOR;
|
||||
|
||||
protected LazyCollection $authorUserGroups;
|
||||
|
||||
public function __construct(PKPRequest $request, \PKP\context\Context $context, PKPSchemaService $schemaService)
|
||||
{
|
||||
parent::__construct($request, $context, $schemaService);
|
||||
|
||||
$this->authorUserGroups = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_AUTHOR], $this->context->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Map an author
|
||||
*
|
||||
* Includes all properties in the announcement schema.
|
||||
*/
|
||||
public function map(Author $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize an author
|
||||
*
|
||||
* Includes properties with the apiSummary flag in the author schema.
|
||||
*/
|
||||
public function summarize(Author $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getSummaryProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a collection of Authors
|
||||
*
|
||||
* @see self::map
|
||||
*/
|
||||
public function mapMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->map($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize a collection of Authors
|
||||
*
|
||||
* @see self::summarize
|
||||
*/
|
||||
public function summarizeMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->summarize($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Map schema properties of an Author to an assoc array
|
||||
*/
|
||||
protected function mapByProperties(array $props, Author $item): array
|
||||
{
|
||||
$output = [];
|
||||
foreach ($props as $prop) {
|
||||
switch ($prop) {
|
||||
case 'userGroupName':
|
||||
/** @var UserGroup $userGroup */
|
||||
$userGroup = $this->authorUserGroups->first(fn (UserGroup $userGroup) => $userGroup->getId() === $item->getData('userGroupId'));
|
||||
$output[$prop] = $userGroup ? $userGroup->getName(null) : new stdClass();
|
||||
break;
|
||||
case 'fullName':
|
||||
$output[$prop] = $item->getFullName();
|
||||
break;
|
||||
default:
|
||||
$output[$prop] = $item->getData($prop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedSubmissionLocales());
|
||||
|
||||
ksort($output);
|
||||
|
||||
return $this->withExtensions($output, $item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user