Skip to content

Commit

Permalink
Merge pull request #4547 from Vitaliy-1/i10506_usergroup_eloquent
Browse files Browse the repository at this point in the history
pkp/pkp-lib#10506 User Group refactoring to Eloquent
  • Loading branch information
Vitaliy-1 authored Dec 16, 2024
2 parents cbcf124 + dcf8b50 commit e3069d7
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 56 deletions.
12 changes: 3 additions & 9 deletions api/v1/_dois/BackendDoiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\LazyCollection;
use PKP\db\DAORegistry;
use PKP\userGroup\UserGroup;

class BackendDoiController extends \PKP\API\v1\_dois\PKPBackendDoiController
{
Expand Down Expand Up @@ -129,27 +129,21 @@ public function editIssue(Request $illuminateRequest): JsonResponse

Repo::issue()->edit($issue, ['doiId' => $doi->getId()]);
$issue = Repo::issue()->get($issue->getId());
$userGroups = UserGroup::withContextIds([$context->getId()])->get();

return response()->json(
Repo::issue()
->getSchemaMap()
->map(
$issue,
$context,
$this->getUserGroups($context->getId()),
$userGroups,
$this->getGenres($context->getId())
),
Response::HTTP_OK
);
}

protected function getUserGroups(int $contextId): LazyCollection
{
return Repo::userGroup()->getCollector()
->filterByContextIds([$contextId])
->getMany();
}

protected function getGenres(int $contextId): array
{
/** @var \PKP\submission\GenreDAO $genreDao */
Expand Down
9 changes: 4 additions & 5 deletions api/v1/issues/IssueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\LazyCollection;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\db\DAORegistry;
Expand All @@ -36,6 +36,7 @@
use PKP\security\authorization\UserRolesRequiredPolicy;
use PKP\security\Role;
use PKP\submission\GenreDAO;
use PKP\userGroup\UserGroup;

class IssueController extends PKPBaseController
{
Expand Down Expand Up @@ -266,11 +267,9 @@ public function get(Request $illuminateRequest): JsonResponse
return response()->json($data, Response::HTTP_OK);
}

protected function getUserGroups(int $contextId): LazyCollection
protected function getUserGroups(int $contextId): Collection
{
return Repo::userGroup()->getCollector()
->filterByContextIds([$contextId])
->getMany();
return UserGroup::withContextIds([$contextId])->get();
}

protected function getGenres(int $contextId): array
Expand Down
13 changes: 6 additions & 7 deletions classes/issue/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use APP\issue\IssueGalleyDAO;
use APP\journal\Journal;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use PKP\db\DAORegistry;
use PKP\services\PKPSchemaService;
use PKP\submission\Genre;
Expand All @@ -33,8 +32,8 @@ class Schema extends \PKP\core\maps\Schema
/** @copydoc \PKP\core\maps\Schema::$schema */
public string $schema = PKPSchemaService::SCHEMA_ISSUE;

/** @var LazyCollection<int,UserGroup> The user groups for this context. */
public LazyCollection $userGroups;
/** @var Enumerable<int,UserGroup> The user groups for this context. */
public Enumerable $userGroups;

/** @var Genre[] The genres for this context. */
public array $genres;
Expand All @@ -44,10 +43,10 @@ class Schema extends \PKP\core\maps\Schema
*
* Includes all properties in the Issue schema
*
* @param LazyCollection<int,UserGroup> $userGroups The user groups of this content
* @param Enumerable<int,UserGroup> $userGroups The user groups of this content
* @param Genre[] $genres The genres of this context
*/
public function map(Issue $item, Journal $context, LazyCollection $userGroups, array $genres): array
public function map(Issue $item, Journal $context, Enumerable $userGroups, array $genres): array
{
$this->userGroups = $userGroups;
$this->genres = $genres;
Expand All @@ -71,10 +70,10 @@ public function summarize(Issue $item, Journal $context): array
*
* @see self::map
*
* @param LazyCollection<int,UserGroup> $userGroups The user groups of this content
* @param Enumerable<int,UserGroup> $userGroups The user groups of this content
* @param Genre[] $genres The genres of this context
*/
public function mapMany(Enumerable $collection, Journal $context, LazyCollection $userGroups, array $genres): Enumerable
public function mapMany(Enumerable $collection, Journal $context, Enumerable $userGroups, array $genres): Enumerable
{
$this->collection = $collection;
return $collection->map(function ($item) use ($context, $userGroups, $genres) {
Expand Down
3 changes: 2 additions & 1 deletion classes/search/ArticleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PKP\plugins\Hook;
use PKP\search\SubmissionSearch;
use PKP\submission\PKPSubmission;
use PKP\userGroup\UserGroup;

class ArticleSearch extends SubmissionSearch
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getSparseArray($unorderedResults, $orderBy, $orderDir, $exclude)
}

$i = 0; // Used to prevent ties from clobbering each other
$authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->getMany();
$authorUserGroups = UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->get();
foreach ($unorderedResults as $submissionId => $data) {
// Exclude unwanted IDs.
if (in_array($submissionId, $exclude)) {
Expand Down
7 changes: 5 additions & 2 deletions classes/submission/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

use APP\facades\Repo;
use APP\publication\Publication;
use PKP\facades\Locale;
use PKP\submission\PKPSubmission;
use PKP\userGroup\UserGroup;

class Submission extends PKPSubmission
{
Expand Down Expand Up @@ -69,7 +69,10 @@ public function _getContextLicenseFieldValue($locale, $field, $publication = nul
$publication = $this->getCurrentPublication();
}

$authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$context->getId()])->getMany();
$authorUserGroups = UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$context->getId()])
->get();

$fieldValue = [$context->getPrimaryLocale() => $publication->getAuthorString($authorUserGroups)];
break;
case 'context':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PKP\controllers\grid\users\userSelect\UserSelectGridCellProvider;
use PKP\security\authorization\ContextAccessPolicy;
use PKP\security\Role;
use PKP\userGroup\UserGroup;

class SubscriberSelectGridHandler extends GridHandler
{
Expand Down Expand Up @@ -64,14 +65,16 @@ public function initialize($request, $args = null)
{
parent::initialize($request, $args);

$builder = UserGroup::withContextIds([$request->getContext()->getId()]);
$stageId = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_WORKFLOW_STAGE);
$userGroups = Repo::userGroup()->getUserGroupsByStage(
$request->getContext()->getId(),
$stageId
);
if ($stageId) {
$builder->withStageIds([$stageId]);
}
$userGroups = $builder->get();

$this->_userGroupOptions = [];
foreach ($userGroups as $userGroup) {
$this->_userGroupOptions[$userGroup->getId()] = $userGroup->getLocalizedName();
$this->_userGroupOptions[$userGroup->id] = $userGroup->getLocalizedData('name');
}

$this->setTitle('editor.submission.findAndSelectUser');
Expand Down Expand Up @@ -146,14 +149,12 @@ protected function loadData($request, $filter)
*/
public function renderFilter($request, $filterData = [])
{
$context = $request->getContext();
$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$contextId = $request->getContext()->getId();
$userGroups = UserGroup::withContextIds([$contextId])->get();

$userGroupOptions = ['' => __('grid.user.allRoles')];
foreach ($userGroups as $userGroup) {
$userGroupOptions[$userGroup->getId()] = $userGroup->getLocalizedName();
$userGroupOptions[$userGroup->id] = $userGroup->getLocalizedData('name');
}

return parent::renderFilter(
Expand Down
4 changes: 2 additions & 2 deletions cypress/tests/data/60-content/AmwandengaSubmission.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('Data suite: Amwandenga', function() {
it('Allow author to edit publication details', function() {
cy.login('dbarnes');
cy.visit('/index.php/publicknowledge/workflow/access/' + submission.id);
cy.get('button[aria-label="Alan Mwandenga More Actions"]').click();
cy.get('button[aria-label="Alan Mwandenga More Actions"]').scrollIntoView().should('be.visible').click();
cy.get('button:contains("Edit")').click();
cy.get('[name="canChangeMetadata"]').check();
cy.get('[id^="submitFormButton"]').contains('OK').click();
Expand Down Expand Up @@ -464,7 +464,7 @@ describe('Data suite: Amwandenga', function() {
cy.get('button').contains('Create New Version').click();
cy.contains('Are you sure you want to create a new version?');
cy.get('div[role=dialog]:contains("Create New Version")').get('button').contains('Yes').click();

// Once there is 'in progress' overlay when creating new version this can be improved
cy.wait(10000);

Expand Down
2 changes: 1 addition & 1 deletion lib/pkp
Submodule pkp updated 110 files
5 changes: 4 additions & 1 deletion pages/article/ArticleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PKP\submission\GenreDAO;
use PKP\submission\PKPSubmission;
use PKP\submissionFile\SubmissionFile;
use PKP\userGroup\UserGroup;
use stdClass;

class ArticleHandler extends Handler
Expand Down Expand Up @@ -286,7 +287,9 @@ public function view($args, $request)
$templateMgr->assign([
'primaryGalleys' => $primaryGalleys,
'supplementaryGalleys' => $supplementaryGalleys,
'userGroupsById' => Repo::userGroup()->getCollector()->filterByPublicationIds([$this->publication->getId()])->getMany()->toArray()
'userGroupsById' => UserGroup::withPublicationIds([$this->publication->getId()])
->get()
->all()
]);

// Citations
Expand Down
6 changes: 5 additions & 1 deletion pages/issue/IssueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use PKP\security\Validation;
use PKP\submission\GenreDAO;
use PKP\submission\PKPSubmission;
use PKP\userGroup\UserGroup;

class IssueHandler extends Handler
{
Expand Down Expand Up @@ -390,7 +391,10 @@ public static function _setupIssueTemplate(Request $request, Issue $issue, ?Jour
$issueSubmissionsInSection[$sectionId]['articles'][] = $submission;
}

$authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$journal->getId()])->getMany()->remember();
$authorUserGroups = UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$journal->getId()])
->get();

$templateMgr->assign([
'issue' => $issue,
'issueGalleys' => $issueGalleyDao->getByIssueId($issue->getId()),
Expand Down
8 changes: 4 additions & 4 deletions pages/search/SearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use APP\search\ArticleSearch;
use APP\security\authorization\OjsJournalMustPublishPolicy;
use APP\template\TemplateManager;
use PKP\userGroup\UserGroup;

class SearchHandler extends Handler
{
Expand Down Expand Up @@ -166,10 +167,9 @@ public function search($args, $request)
'simDocsEnabled' => true,
'results' => $results,
'error' => $error,
'authorUserGroups' => Repo::userGroup()->getCollector()
->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->filterByContextIds($searchFilters['searchJournal'] ? [$searchFilters['searchJournal']->getId()] : null)
->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds($searchFilters['searchJournal'] ? [$searchFilters['searchJournal']->getId()] : null)
->get(),
'searchResultOrderOptions' => $articleSearch->getResultSetOrderingOptions($request),
'searchResultOrderDirOptions' => $articleSearch->getResultSetOrderingDirectionOptions(),
]);
Expand Down
2 changes: 1 addition & 1 deletion plugins/generic/citationStyleLanguage
2 changes: 1 addition & 1 deletion plugins/generic/webFeed
2 changes: 1 addition & 1 deletion plugins/oaiMetadataFormats/oaiJats
12 changes: 6 additions & 6 deletions plugins/reports/articles/ArticleReportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PKP\submission\SubmissionDisciplineDAO;
use PKP\submission\SubmissionKeywordDAO;
use PKP\submission\SubmissionSubjectDAO;
use PKP\userGroup\UserGroup;

class ArticleReportPlugin extends ReportPlugin
{
Expand Down Expand Up @@ -90,15 +91,14 @@ public function display($args, $request)
$submissionDisciplineDao = DAORegistry::getDAO('SubmissionDisciplineDAO'); /** @var SubmissionDisciplineDAO $submissionDisciplineDao */
$submissionAgencyDao = DAORegistry::getDAO('SubmissionAgencyDAO'); /** @var SubmissionAgencyDAO $submissionAgencyDao */

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany()
->toArray();
$userGroups = UserGroup::withContextIds([$context->getId()])
->get()
->all();

$editorUserGroupIds = array_map(function ($userGroup) {
return $userGroup->getId();
return $userGroup->id;
}, array_filter($userGroups, function ($userGroup) {
return in_array($userGroup->getRoleId(), [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR]);
return in_array($userGroup->roleId, [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR]);
}));

// Load the data from the database and store it in an array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
{fbvFormSection list=true title="manager.sections.form.assignEditors"}
<div>{translate key="manager.sections.form.assignEditors.description"}</div>
{foreach from=$assignableUserGroups item="assignableUserGroup"}
{assign var="role" value=$assignableUserGroup.userGroup->getLocalizedName()}
{assign var="userGroupId" value=$assignableUserGroup.userGroup->getId()}
{assign var="role" value=$assignableUserGroup.userGroup->getLocalizedData('name')}
{assign var="userGroupId" value=$assignableUserGroup.userGroup->id}
{foreach from=$assignableUserGroup.users item=$username key="id"}
{fbvElement
type="checkbox"
Expand Down
4 changes: 2 additions & 2 deletions templates/frontend/objects/article_details.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
</span>
{/if}
{assign var=authorUserGroup value=$userGroupsById[$author->getData('userGroupId')]}
{if $authorUserGroup->getShowTitle()}
{if $authorUserGroup->showTitle}
<span class="userGroup">
{$authorUserGroup->getLocalizedName()|escape}
{$authorUserGroup->getLocalizedData('name')|escape}
</span>
{/if}
{if $author->getData('orcid')}
Expand Down

0 comments on commit e3069d7

Please sign in to comment.