Skip to content

Commit aa1b581

Browse files
committed
#11087 consider masthead options in the old UserForm
1 parent e0ac0c9 commit aa1b581

File tree

6 files changed

+119
-38
lines changed

6 files changed

+119
-38
lines changed

classes/userGroup/Repository.php

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @file classes/userGroup/Repository.php
45
*
@@ -13,28 +14,27 @@
1314

1415
namespace PKP\userGroup;
1516

16-
use stdClass;
17-
use DateInterval;
17+
use APP\core\Application;
18+
use APP\facades\Repo;
1819
use Carbon\Carbon;
20+
use DateInterval;
21+
use Illuminate\Support\Collection;
22+
use Illuminate\Support\Facades\Cache;
23+
use Illuminate\Support\LazyCollection;
1924
use PKP\core\Core;
20-
use APP\facades\Repo;
21-
use PKP\plugins\Hook;
22-
use PKP\site\SiteDAO;
23-
use PKP\security\Role;
2425
use PKP\db\DAORegistry;
2526
use PKP\facades\Locale;
26-
use APP\core\Application;
27-
use PKP\xml\PKPXMLParser;
28-
use Illuminate\Support\Collection;
27+
use PKP\plugins\Hook;
28+
use PKP\security\Role;
2929
use PKP\services\PKPSchemaService;
30-
use PKP\validation\ValidatorFactory;
31-
use Illuminate\Support\Facades\Cache;
32-
use Illuminate\Support\LazyCollection;
33-
use Illuminate\Database\Query\JoinClause;
34-
use PKP\userGroup\relationships\UserUserGroup;
35-
use PKP\userGroup\relationships\UserGroupStage;
36-
use PKP\userGroup\relationships\enums\UserUserGroupStatus;
30+
use PKP\site\SiteDAO;
3731
use PKP\userGroup\relationships\enums\UserUserGroupMastheadStatus;
32+
use PKP\userGroup\relationships\enums\UserUserGroupStatus;
33+
use PKP\userGroup\relationships\UserGroupStage;
34+
use PKP\userGroup\relationships\UserUserGroup;
35+
use PKP\validation\ValidatorFactory;
36+
use PKP\xml\PKPXMLParser;
37+
use stdClass;
3838

3939
class Repository
4040
{
@@ -253,6 +253,27 @@ public function userOnMasthead(int $userId, ?int $userGroupId = null): bool
253253
return $query->exists();
254254
}
255255

256+
/**
257+
* Update UserUserGroup masthead status for a UserGroup the user is currently active in
258+
*
259+
*/
260+
public function updateUserUserGroupMastheadStatus(int $userId, int $userGroupId, UserUserGroupMastheadStatus $mastheadStatus): void
261+
{
262+
$masthead = match ($mastheadStatus) {
263+
UserUserGroupMastheadStatus::STATUS_ON => 1,
264+
UserUserGroupMastheadStatus::STATUS_OFF => 0,
265+
default => null
266+
};
267+
UserUserGroup::query()
268+
->withUserId($userId)
269+
->withUserGroupIds([$userGroupId])
270+
->withActive()
271+
->update(['masthead' => $masthead]);
272+
273+
$userGroup = UserGroup::find($userGroupId);
274+
self::forgetEditorialCache($userGroup->contextId);
275+
}
276+
256277
/**
257278
* Get UserUserGroup masthead status for a UserGroup the user is currently active in
258279
*
@@ -582,7 +603,7 @@ public function getMastheadUserIdsByRoleIds(array $mastheadRoles, int $contextId
582603
->filterByUserUserGroupMastheadStatus(UserUserGroupMastheadStatus::STATUS_ON)
583604
->orderBy(
584605
$usersCollector::ORDERBY_FAMILYNAME,
585-
$usersCollector::ORDER_DIR_ASC,
606+
$usersCollector::ORDER_DIR_ASC,
586607
[
587608
Locale::getLocale(),
588609
Application::get()->getRequest()->getSite()->getPrimaryLocale()

classes/userGroup/relationships/UserUserGroup.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
namespace PKP\userGroup\relationships;
1616

17-
use PKP\core\Core;
1817
use APP\facades\Repo;
19-
use PKP\userGroup\UserGroup;
2018
use Eloquence\Behaviours\HasCamelCasing;
2119
use Illuminate\Database\Eloquent\Builder;
2220
use Illuminate\Database\Eloquent\Casts\Attribute;
2321
use Illuminate\Database\Eloquent\Relations\BelongsTo;
22+
use PKP\core\Core;
23+
use PKP\userGroup\UserGroup;
2424

2525
class UserUserGroup extends \Illuminate\Database\Eloquent\Model
2626
{
@@ -100,6 +100,11 @@ public function scopeWithMasthead(Builder $query): Builder
100100
return $query->where('user_user_groups.masthead', 1);
101101
}
102102

103+
public function scopeWithMastheadOff(Builder $query): Builder
104+
{
105+
return $query->where('user_user_groups.masthead', 0);
106+
}
107+
103108
public function scopeSortBy(Builder $query, string $column, ?string $direction = 'asc')
104109
{
105110
return $query->orderBy('user_user_groups.' . $column, $direction);

controllers/grid/settings/user/form/UserForm.php

+57-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use APP\facades\Repo;
2121
use APP\template\TemplateManager;
2222
use PKP\form\Form;
23+
use PKP\security\Role;
24+
use PKP\userGroup\relationships\enums\UserUserGroupMastheadStatus;
2325
use PKP\userGroup\relationships\UserUserGroup;
2426
use PKP\userGroup\UserGroup;
2527

@@ -49,7 +51,7 @@ public function __construct($template, $userId = null)
4951
*/
5052
public function initData()
5153
{
52-
$userGroupIds = $masthead = [];
54+
$userGroupIds = $notOnMastheadUserGroupIds = [];
5355

5456
if (!is_null($this->userId)) {
5557
// fetch user groups where the user is assigned
@@ -62,12 +64,24 @@ public function initData()
6264

6365
foreach ($userGroups as $userGroup) {
6466
$userGroupIds[] = $userGroup->id;
65-
$masthead[$userGroup->id] = Repo::userGroup()->userOnMasthead($this->userId, $userGroup->id);
6667
}
68+
69+
// Get user group IDs for user groups this user should not be displayed on the masthead for
70+
// The others will be selected per default
71+
$notOnMastheadUserGroupIds = UserUserGroup::query()
72+
->withUserId($this->userId)
73+
->withActive()
74+
->withMastheadOff()
75+
->get()
76+
->map(
77+
fn (UserUserGroup $userUserGroup) => $userUserGroup->user_group_id
78+
)
79+
->all();
80+
6781
}
6882

6983
$this->setData('userGroupIds', $userGroupIds);
70-
$this->setData('masthead', $masthead);
84+
$this->setData('notOnMastheadUserGroupIds', $notOnMastheadUserGroupIds);
7185

7286
parent::initData();
7387
}
@@ -77,7 +91,7 @@ public function initData()
7791
*/
7892
public function readInputData()
7993
{
80-
$this->readUserVars(['userGroupIds']);
94+
$this->readUserVars(['userGroupIds', 'mastheadUserGroupIds']);
8195
parent::readInputData();
8296
}
8397

@@ -92,17 +106,21 @@ public function display($request = null, $template = null)
92106
$contextId = $request->getContext()?->getId() ?? \PKP\core\PKPApplication::SITE_CONTEXT_ID;
93107
$templateMgr = TemplateManager::getManager($request);
94108

95-
$allUserGroups = [];
109+
$allUserGroups = $defaultMastheadUserGroups = [];
96110

97111
$userGroups = UserGroup::withContextIds([$contextId])->get();
98112

99113
foreach ($userGroups as $userGroup) {
100114
$allUserGroups[(int) $userGroup->id] = $userGroup->getLocalizedData('name');
115+
if ($userGroup->role_id != Role::ROLE_ID_REVIEWER) {
116+
$defaultMastheadUserGroups[(int) $userGroup->id] = $userGroup->getLocalizedData('name');
117+
}
101118
}
102119

103120
$templateMgr->assign([
104121
'allUserGroups' => $allUserGroups,
105122
'assignedUserGroups' => array_map(intval(...), $this->getData('userGroupIds')),
123+
'defaultMastheadUserGroups' => $defaultMastheadUserGroups,
106124
]);
107125

108126
return $this->fetch($request);
@@ -127,6 +145,15 @@ public function saveUserGroupAssignments(Request $request): void
127145

128146
if ($this->getData('userGroupIds')) {
129147
$contextId = $request->getContext()->getId();
148+
$allUserGroupIds = UserGroup::withContextIds([$contextId])
149+
->get()
150+
->map(
151+
fn (UserGroup $userGroup) => $userGroup->user_group_id
152+
)
153+
->all();
154+
// secure that user-specified user group IDs are from the right context
155+
$userGroupIds = array_intersect($this->getData('userGroupIds'), $allUserGroupIds);
156+
$mastheadUserGroupIds = array_intersect($this->getData('mastheadUserGroupIds'), $allUserGroupIds);
130157

131158
// get current user group IDs for this context
132159
$oldUserGroupIds = UserGroup::query()
@@ -138,26 +165,37 @@ public function saveUserGroupAssignments(Request $request): void
138165
->pluck('user_group_id')
139166
->all();
140167

141-
$userGroupsToEnd = array_diff($oldUserGroupIds, $this->getData('userGroupIds'));
168+
$userGroupsToEnd = array_diff($oldUserGroupIds, $userGroupIds);
142169
collect($userGroupsToEnd)
143170
->each(
144-
fn ($userGroupId) =>
145-
UserUserGroup::query()
146-
->withUserId($this->userId)
147-
->withUserGroupIds([$userGroupId])
148-
->withActive()
149-
->update(['date_end' => now()])
171+
function ($userGroupId) use ($contextId) {
172+
Repo::userGroup()->endAssignments($contextId, $this->userId, $userGroupId);
173+
}
150174
);
151175

152-
$userGroupsToAdd = array_diff($this->getData('userGroupIds'), $oldUserGroupIds);
176+
$userGroupsToAdd = array_diff($userGroupIds, $oldUserGroupIds);
153177
collect($userGroupsToAdd)
154178
->each(
155-
fn ($userGroupId) =>
156-
UserUserGroup::create([
157-
'userId' => $this->userId,
158-
'userGroupId' => $userGroupId,
159-
'dateStart' => now(),
160-
])
179+
fn ($userGroupId) => Repo::userGroup()->assignUserToGroup($this->userId, $userGroupId)
180+
);
181+
182+
// update masthead
183+
// ignore reviewer role
184+
$reviewerUserGroupIds = Repo::userGroup()->getArrayIdByRoleId(Role::ROLE_ID_REVIEWER, $contextId);
185+
collect($userGroupIds)
186+
->filter(
187+
function ($userGroupId) use ($reviewerUserGroupIds) {
188+
return !in_array($userGroupId, $reviewerUserGroupIds);
189+
}
190+
)
191+
->each(
192+
function ($userGroupId) use ($mastheadUserGroupIds) {
193+
$masthead = match (in_array($userGroupId, $mastheadUserGroupIds)) {
194+
true => UserUserGroupMastheadStatus::STATUS_ON,
195+
false => UserUserGroupMastheadStatus::STATUS_OFF
196+
};
197+
Repo::userGroup()->updateUserUserGroupMastheadStatus($this->userId, $userGroupId, $masthead);
198+
}
161199
);
162200
}
163201
}

locale/en/grid.po

+3
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ msgstr "User Details"
580580
msgid "grid.user.userRoles"
581581
msgstr "User Roles"
582582

583+
msgid "grid.user.userRoles.masthead"
584+
msgstr "Appear on the Masthead"
585+
583586
msgid "grid.user.userNoRoles"
584587
msgstr "This user does not have any roles."
585588

templates/controllers/grid/settings/user/form/userDetailsForm.tpl

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@
7171
{/foreach}
7272
{/fbvFormSection}
7373
{/fbvFormSection}
74+
{fbvFormSection}
75+
{fbvFormSection list=true title="grid.user.userRoles.masthead"}
76+
{foreach from=$defaultMastheadUserGroups item="mastheadUserGroup" key="id"}
77+
{fbvElement type="checkbox" id="mastheadUserGroupIds[]" value=$id checked=!in_array($id, $notOnMastheadUserGroupIds) label=$mastheadUserGroup|escape translate=false}
78+
{/foreach}
79+
{/fbvFormSection}
80+
{/fbvFormSection}
7481
{/if}
7582
<p><span class="formRequired">{translate key="common.requiredField"}</span></p>
7683
{fbvFormButtons}

templates/controllers/grid/settings/user/form/userRoleForm.tpl

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
{/foreach}
3030
{/fbvFormSection}
3131
{/fbvFormSection}
32+
{fbvFormSection}
33+
{fbvFormSection list=true title="grid.user.userRoles.masthead"}
34+
{foreach from=$defaultMastheadUserGroups item="mastheadUserGroup" key="id"}
35+
{fbvElement type="checkbox" id="mastheadUserGroupIds[]" value=$id checked=!in_array($id, $notOnMastheadUserGroupIds) label=$mastheadUserGroup|escape translate=false}
36+
{/foreach}
37+
{/fbvFormSection}
38+
{/fbvFormSection}
3239

3340
{fbvFormButtons submitText="common.save"}
3441
</form>

0 commit comments

Comments
 (0)