Skip to content

Commit 5fa5488

Browse files
committed
Add “Publisher Notifications User Group” to optionally specify a user group just for publisher notifications
1 parent 574c2cd commit 5fa5488

File tree

4 files changed

+34
-68
lines changed

4 files changed

+34
-68
lines changed

src/controllers/BaseController.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,19 @@ public function actionSettings(): Response
2222
{
2323
$settings = Workflow::$plugin->getSettings();
2424

25-
$publishers = [];
25+
$userGroups = [
26+
['label' => Craft::t('workflow', 'All Publishers'), 'value' => ''],
27+
];
2628

27-
if ($settings->publisherUserGroup) {
28-
$publisherUserGroup = $settings->publisherUserGroup;
29-
30-
// Backward-compatibility support for config files, which won't be migrated
31-
if (!is_array($publisherUserGroup)) {
32-
Craft::$app->getDeprecator()->log('publisherUserGroup', 'The `publisherUserGroup` setting has been updated, and will cause a fatal error in Craft 4. Please review our [docs](https://verbb.io/craft-plugins/workflow/docs/get-started/configuration).');
33-
34-
$publisherUserGroupUid = $publisherUserGroup;
35-
$publisherUserGroup = [];
36-
37-
foreach (Craft::$app->getSites()->getAllSites() as $site) {
38-
$publisherUserGroup[$site->uid] = $publisherUserGroupUid;
39-
}
40-
}
41-
42-
foreach ($publisherUserGroup as $siteUid => $publisherUserGroupUid) {
43-
$publisherUserGroupId = Db::idByUid(Table::USERGROUPS, $publisherUserGroupUid);
44-
45-
foreach (User::find()->groupId($publisherUserGroupId)->all() as $user) {
46-
$publishers[] = ['value' => $user->id, 'label' => (string)$user];
47-
}
48-
}
29+
foreach (Craft::$app->getUserGroups()->getAllGroups() as $userGroup) {
30+
$userGroups[] = ['label' => $userGroup->name, 'value' => $userGroup->uid];
4931
}
5032

51-
$publishers = array_unique($publishers, SORT_REGULAR);
33+
$userGroups = array_unique($userGroups, SORT_REGULAR);
5234

5335
return $this->renderTemplate('workflow/settings', [
5436
'settings' => $settings,
55-
'publishers' => $publishers,
37+
'userGroups' => $userGroups,
5638
]);
5739
}
5840

src/models/Settings.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Craft;
55
use craft\base\Model;
6+
use craft\elements\User;
67
use craft\helpers\ArrayHelper;
78

89
class Settings extends Model
@@ -25,11 +26,14 @@ class Settings extends Model
2526
public bool $reviewerApprovalNotifications = false;
2627
public bool $publisherNotifications = true;
2728
public bool $publishedAuthorNotifications = false;
28-
public mixed $selectedPublishers = '*';
29+
public ?string $publisherNotificationsUserGroup = null;
2930

3031
// Permissions
3132
public mixed $enabledSections = '*';
3233

34+
// Deprecated
35+
public mixed $selectedPublishers = '*';
36+
3337

3438
// Public Methods
3539
// =========================================================================
@@ -140,6 +144,18 @@ public function getPublisherNotesRequired($site)
140144
return $this->publisherNotesRequired[$site->uid] ?? false;
141145
}
142146

147+
public function getPublishersForNotificationEmail($site): array
148+
{
149+
// Get the user group for email notifications
150+
$publisherGroup = $this->publisherNotificationsUserGroup ?? $this->getPublisherUserGroup($site);
151+
152+
if (!$publisherGroup) {
153+
return [];
154+
}
155+
156+
return User::find()->groupId($publisherGroup->id)->all();
157+
}
158+
143159

144160
// Private Methods
145161
// =========================================================================

src/services/Emails.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,7 @@ public function sendPublisherNotificationEmail(Submission $submission, Review $r
117117

118118
$settings = Workflow::$plugin->getSettings();
119119

120-
$publisherGroup = $settings->getPublisherUserGroup($entry->site);
121-
122-
if (!$publisherGroup) {
123-
Workflow::log('No publisher group found to send notifications to.');
124-
}
125-
126-
$query = User::find()->groupId($publisherGroup->id);
127-
128-
// Check settings to see if we should email all publishers or not
129-
if (isset($settings->selectedPublishers) && $settings->selectedPublishers != '*') {
130-
$query->id($settings->selectedPublishers);
131-
}
132-
133-
$publishers = $query->all();
120+
$publishers = $settings->getPublishersForNotificationEmail($entry->site);
134121

135122
// Fire a 'preparePublisherEmail' event
136123
$event = new PrepareEmailEvent([

src/templates/settings/_panes/notifications.html

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,15 @@
5656
warning: macros.configWarning('publisherNotifications', 'workflow'),
5757
}) }}
5858

59-
{% if settings.publisherUserGroup %}
60-
{{ forms.checkboxSelectField({
61-
label: 'Publishers To Receive Notifications' | t('workflow'),
62-
instructions: 'Select all, or specific publishers to receive email notifications. By default, all will be notified.' | t('workflow'),
63-
id: 'selectedPublishers',
64-
name: 'selectedPublishers',
65-
values: settings.selectedPublishers,
66-
options: publishers,
67-
showAllOption: true,
68-
warning: macros.configWarning('selectedPublishers', 'workflow'),
69-
}) }}
70-
{% else %}
71-
<div class="field">
72-
<div class="heading">
73-
<label>
74-
{{ "Publishers To Receive Notifications" | t('workflow') }}
75-
</label>
76-
77-
<div class="instructions">
78-
<p>{{ "Select all, or specific publishers to receive email notifications. By default, all will be notified." | t('workflow') }}</p>
79-
</div>
80-
</div>
81-
82-
<p class="small warning">
83-
{{ "Select a Publisher User Group first." | t('workflow') }}
84-
</p>
85-
</div>
86-
{% endif %}
59+
{{ forms.selectField({
60+
label: 'Publisher Notifications User Group' | t('workflow'),
61+
instructions: 'By default, all users in the "Publisher User Group" will receive email notifications. Set a specific group to receive email notifications.' | t('workflow'),
62+
id: 'publisherNotificationsUserGroup',
63+
name: 'publisherNotificationsUserGroup',
64+
value: settings.publisherNotificationsUserGroup,
65+
options: userGroups,
66+
warning: macros.configWarning('publisherNotificationsUserGroup', 'workflow'),
67+
}) }}
8768

8869
{{ forms.lightswitchField({
8970
label: 'Published Author Notifications' | t('workflow'),

0 commit comments

Comments
 (0)