20
20
use APP \facades \Repo ;
21
21
use APP \template \TemplateManager ;
22
22
use PKP \form \Form ;
23
+ use PKP \security \Role ;
24
+ use PKP \userGroup \relationships \enums \UserUserGroupMastheadStatus ;
23
25
use PKP \userGroup \relationships \UserUserGroup ;
24
26
use PKP \userGroup \UserGroup ;
25
27
@@ -49,7 +51,7 @@ public function __construct($template, $userId = null)
49
51
*/
50
52
public function initData ()
51
53
{
52
- $ userGroupIds = $ masthead = [];
54
+ $ userGroupIds = $ notOnMastheadUserGroupIds = [];
53
55
54
56
if (!is_null ($ this ->userId )) {
55
57
// fetch user groups where the user is assigned
@@ -62,12 +64,24 @@ public function initData()
62
64
63
65
foreach ($ userGroups as $ userGroup ) {
64
66
$ userGroupIds [] = $ userGroup ->id ;
65
- $ masthead [$ userGroup ->id ] = Repo::userGroup ()->userOnMasthead ($ this ->userId , $ userGroup ->id );
66
67
}
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
+
67
81
}
68
82
69
83
$ this ->setData ('userGroupIds ' , $ userGroupIds );
70
- $ this ->setData ('masthead ' , $ masthead );
84
+ $ this ->setData ('notOnMastheadUserGroupIds ' , $ notOnMastheadUserGroupIds );
71
85
72
86
parent ::initData ();
73
87
}
@@ -77,7 +91,7 @@ public function initData()
77
91
*/
78
92
public function readInputData ()
79
93
{
80
- $ this ->readUserVars (['userGroupIds ' ]);
94
+ $ this ->readUserVars (['userGroupIds ' , ' mastheadUserGroupIds ' ]);
81
95
parent ::readInputData ();
82
96
}
83
97
@@ -92,17 +106,21 @@ public function display($request = null, $template = null)
92
106
$ contextId = $ request ->getContext ()?->getId() ?? \PKP \core \PKPApplication::SITE_CONTEXT_ID ;
93
107
$ templateMgr = TemplateManager::getManager ($ request );
94
108
95
- $ allUserGroups = [];
109
+ $ allUserGroups = $ defaultMastheadUserGroups = [];
96
110
97
111
$ userGroups = UserGroup::withContextIds ([$ contextId ])->get ();
98
112
99
113
foreach ($ userGroups as $ userGroup ) {
100
114
$ 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
+ }
101
118
}
102
119
103
120
$ templateMgr ->assign ([
104
121
'allUserGroups ' => $ allUserGroups ,
105
122
'assignedUserGroups ' => array_map (intval (...), $ this ->getData ('userGroupIds ' )),
123
+ 'defaultMastheadUserGroups ' => $ defaultMastheadUserGroups ,
106
124
]);
107
125
108
126
return $ this ->fetch ($ request );
@@ -127,6 +145,15 @@ public function saveUserGroupAssignments(Request $request): void
127
145
128
146
if ($ this ->getData ('userGroupIds ' )) {
129
147
$ 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 );
130
157
131
158
// get current user group IDs for this context
132
159
$ oldUserGroupIds = UserGroup::query ()
@@ -138,26 +165,37 @@ public function saveUserGroupAssignments(Request $request): void
138
165
->pluck ('user_group_id ' )
139
166
->all ();
140
167
141
- $ userGroupsToEnd = array_diff ($ oldUserGroupIds , $ this -> getData ( ' userGroupIds ' ) );
168
+ $ userGroupsToEnd = array_diff ($ oldUserGroupIds , $ userGroupIds );
142
169
collect ($ userGroupsToEnd )
143
170
->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
+ }
150
174
);
151
175
152
- $ userGroupsToAdd = array_diff ($ this -> getData ( ' userGroupIds ' ) , $ oldUserGroupIds );
176
+ $ userGroupsToAdd = array_diff ($ userGroupIds , $ oldUserGroupIds );
153
177
collect ($ userGroupsToAdd )
154
178
->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
+ }
161
199
);
162
200
}
163
201
}
0 commit comments