Skip to content

Commit

Permalink
Merge pull request #1787 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 Model
  • Loading branch information
Vitaliy-1 authored Dec 16, 2024
2 parents 1187580 + eb13ac6 commit 74b1370
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 38 deletions.
6 changes: 4 additions & 2 deletions api/v1/submissions/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use APP\components\forms\publication\PublicationLicenseForm;
use APP\components\forms\submission\AudienceForm;
use APP\components\forms\submission\PublicationDatesForm;
use APP\facades\Repo;
use APP\file\PublicFileManager;
use APP\publication\Publication;
use APP\submission\Submission;
Expand All @@ -33,6 +32,7 @@
use PKP\context\Context;
use PKP\core\PKPApplication;
use PKP\security\Role;
use PKP\userGroup\UserGroup;

class SubmissionController extends PKPSubmissionController
{
Expand Down Expand Up @@ -150,7 +150,9 @@ protected function getPublicationLicenseForm(Request $illuminateRequest): JsonRe
$publicationApiUrl = $data['publicationApiUrl']; /** @var String $publicationApiUrl */

$locales = $this->getPublicationFormLocales($context, $submission);
$authorUserGroups = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_AUTHOR], $submission->getData('contextId'));
$authorUserGroups = UserGroup::withRoleIds([Role::ROLE_ID_AUTHOR])
->withContextIds([$submission->getData('contextId')])
->get();

$publicationLicenseForm = new PublicationLicenseForm($publicationApiUrl, $locales, $publication, $context, $authorUserGroups);
$submissionLocale = $submission->getData('locale');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/components/form/publication/PublicationLicenseForm.php
*
Expand All @@ -20,7 +21,7 @@
use APP\press\Press;
use APP\publication\Publication;
use APP\submission\Submission;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Enumerable;
use PKP\components\forms\FieldText;
use PKP\components\forms\publication\PKPPublicationLicenseForm;
use PKP\userGroup\UserGroup;
Expand All @@ -34,7 +35,7 @@ class PublicationLicenseForm extends PKPPublicationLicenseForm
* @param array $locales Supported locales
* @param Publication $publication The publication to change settings for
* @param Press $context The publication's context
* @param LazyCollection<UserGroup> $userGroups User groups in this context
* @param Enumerable<UserGroup> $userGroups User groups in this context
*/
public function __construct($action, $locales, $publication, $context, $userGroups)
{
Expand Down
1 change: 0 additions & 1 deletion classes/monograph/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ public function setSourceChapterId(?int $sourceChapterId): void
public function isPageEnabled(): ?bool
{
return $this->getData('isPageEnabled') || !empty($this->getDoi());
;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion classes/search/MonographSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PKP\db\DAORegistry;
use PKP\plugins\Hook;
use PKP\search\SubmissionSearch;
use PKP\userGroup\UserGroup;

class MonographSearch extends SubmissionSearch
{
Expand Down Expand Up @@ -75,7 +76,8 @@ 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
8 changes: 6 additions & 2 deletions classes/submission/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

namespace APP\submission;

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

class Submission extends PKPSubmission
{
Expand Down Expand Up @@ -67,7 +67,11 @@ public function _getContextLicenseFieldValue($locale, $field, $publication = nul
if (!$publication) {
$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
2 changes: 1 addition & 1 deletion lib/pkp
Submodule pkp updated 120 files
5 changes: 2 additions & 3 deletions pages/catalog/CatalogBookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use PKP\submission\GenreDAO;
use PKP\submission\PKPSubmission;
use PKP\submissionFile\SubmissionFile;
use PKP\userGroup\UserGroup;

class CatalogBookHandler extends Handler
{
Expand Down Expand Up @@ -175,9 +176,7 @@ public function book($args, $request)
return empty($a) || strtotime((string) $b->getData('datePublished')) < strtotime((string) $a->getData('datePublished')) ? $b : $a;
}, 0);

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::withContextIds([$submission->getData('contextId')])->get();

$templateMgr->assign([
'isChapterRequest' => $this->isChapterRequest,
Expand Down
13 changes: 10 additions & 3 deletions pages/catalog/CatalogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PKP\db\DAORegistry;
use PKP\file\ContextFileManager;
use PKP\pages\catalog\PKPCatalogHandler;
use PKP\userGroup\UserGroup;

class CatalogHandler extends PKPCatalogHandler
{
Expand Down Expand Up @@ -104,7 +105,9 @@ public function page($args, $request, $isFirstPage = false)

$templateMgr->assign([
'publishedSubmissions' => $submissions->toArray(),
'authorUserGroups' => $authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$context->getId()])->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$context->getId()])
->get(),
'featuredMonographIds' => $featuredMonographIds,
'contextSeries' => $seriesIterator->toArray(),
]);
Expand All @@ -131,7 +134,9 @@ public function newReleases($args, $request)
$newReleases = $newReleaseDao->getMonographsByAssoc(Application::ASSOC_TYPE_PRESS, $press->getId());
$templateMgr->assign([
'publishedSubmissions' => $newReleases,
'authorUserGroups' => $authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$press->getId()])->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$press->getId()])
->get(),
]);

// Display
Expand Down Expand Up @@ -199,7 +204,9 @@ public function series($args, $request)
'publishedSubmissions' => $submissions->toArray(),
'featuredMonographIds' => $featuredMonographIds,
'newReleasesMonographs' => $newReleases,
'authorUserGroups' => $authorUserGroups = Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$context->getId()])->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$context->getId()])
->get(),
]);

$templateMgr->display('frontend/pages/catalogSeries.tpl');
Expand Down
5 changes: 4 additions & 1 deletion pages/index/IndexHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PKP\pages\index\PKPIndexHandler;
use PKP\security\Validation;
use PKP\site\Site;
use PKP\userGroup\UserGroup;

class IndexHandler extends PKPIndexHandler
{
Expand Down Expand Up @@ -129,7 +130,9 @@ public function _displayPressIndexPage($press, $request)
'homepageImage' => $press->getLocalizedSetting('homepageImage'),
'pageTitleTranslated' => $press->getLocalizedSetting('name'),
'displayCreativeCommons' => $press->getSetting('includeCreativeCommons'),
'authorUserGroups' => Repo::userGroup()->getCollector()->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])->filterByContextIds([$press->getId()])->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds([$press->getId()])
->get(),
]);

$this->_setupAnnouncements($press, $templateMgr);
Expand Down
5 changes: 2 additions & 3 deletions pages/manageCatalog/ManageCatalogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PKP\security\authorization\PKPSiteAccessPolicy;
use PKP\security\Role;
use PKP\submission\GenreDAO;
use PKP\userGroup\UserGroup;

class ManageCatalogHandler extends Handler
{
Expand Down Expand Up @@ -124,9 +125,7 @@ public function index($args, $request)
$total = $collector->getCount();
$submissions = $collector->limit($catalogList->count)->getMany();

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down
9 changes: 4 additions & 5 deletions pages/search/SearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
namespace APP\pages\search;

use APP\core\Request;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\search\MonographSearch;
use APP\template\TemplateManager;
use PKP\userGroup\UserGroup;

class SearchHandler extends Handler
{
Expand Down Expand Up @@ -57,10 +57,9 @@ public function search($args, $request)
$templateMgr->assign([
'results' => $monographSearch->retrieveResults($request, $press, [null => $query], $error, null, null, $rangeInfo),
'searchQuery' => $query,
'authorUserGroups' => Repo::userGroup()->getCollector()
->filterByRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->filterByContextIds($press ? [$press->getId()] : null)
->getMany()->remember(),
'authorUserGroups' => UserGroup::withRoleIds([\PKP\security\Role::ROLE_ID_AUTHOR])
->withContextIds($press ? [$press->getId()] : null)
->get(),
]);

// Display
Expand Down
2 changes: 1 addition & 1 deletion plugins/generic/citationStyleLanguage
2 changes: 1 addition & 1 deletion plugins/generic/webFeed
13 changes: 7 additions & 6 deletions plugins/importexport/csv/CSVImportExportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PKP\submission\GenreDAO;
use PKP\submission\PKPSubmission;
use PKP\submissionFile\SubmissionFile;
use PKP\userGroup\UserGroup;

class CSVImportExportPlugin extends ImportExportPlugin
{
Expand Down Expand Up @@ -162,11 +163,11 @@ public function executeCLI($scriptName, &$args)
continue;
}

$authorGroup = Repo::userGroup()->getCollector()
->filterByContextIds([$press->getId()])
->filterByRoleIds([Role::ROLE_ID_AUTHOR])
->filterByIsDefault(true)
->getMany()
$authorGroup = UserGroup::withContextIds([$press->getId()])
->withContextIds([$press->getId()])
->withRoleIds([Role::ROLE_ID_AUTHOR])
->isDefault(true)
->get()
->first();
if (!$authorGroup) {
echo __('plugins.importexport.csv.noAuthorGroup', ['press' => $pressPath]) . "\n";
Expand Down Expand Up @@ -217,7 +218,7 @@ public function executeCLI($scriptName, &$args)
$author = Repo::author()->newDataObject();
$author->setData('publicationId', $publicationId);
$author->setSubmissionId($submissionId);
$author->setUserGroupId($authorGroup->getId());
$author->setUserGroupId($authorGroup->id);
$author->setGivenName($givenName, $locale);
$author->setFamilyName($familyName, $locale);
$author->setEmail($emailAddress);
Expand Down
8 changes: 5 additions & 3 deletions plugins/reports/monographReport/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ private function getDecision(int $editorIndex, int $decisionIndex): ?Decision
*/
private function getEditorUserGroups(): Collection
{
return $this->editorUserGroups ??= collect(Repo::userGroup()->getCollector()->filterByContextIds([$this->press->getId()])->getMany())
->filter(fn (UserGroup $userGroup) => in_array($userGroup->getRoleId(), [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR]))
->mapWithKeys(fn (UserGroup $userGroup) => [$userGroup->getId() => true]);
$userGroups = UserGroup::withContextIds([$this->press->getId()])->get();

return $this->editorUserGroups ??= $userGroups
->filter(fn (UserGroup $userGroup) => in_array($userGroup->roleId, [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR]))
->mapWithKeys(fn (UserGroup $userGroup) => [$userGroup->id => true]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ul>
{foreach from=$userGroups->toArray() item=userGroup}
<li>
{$userGroup->getLocalizedName()|escape}
{$userGroup->getLocalizedData('name')|escape}
</li>
{/foreach}
</ul>
Expand Down

0 comments on commit 74b1370

Please sign in to comment.