Skip to content

Commit b97e2f5

Browse files
committed
Merge branch 'craft-4' of https://github.com/verbb/workflow into craft-5
# Conflicts: # src/controllers/BaseController.php # src/models/Settings.php # src/services/Emails.php
2 parents 0ac7f49 + 2dde661 commit b97e2f5

File tree

9 files changed

+83
-61
lines changed

9 files changed

+83
-61
lines changed

Diff for: docs/get-started/configuration.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return [
2323
'reviewerApprovalNotifications' => false,
2424
'publisherNotifications' => true,
2525
'publishedAuthorNotifications' => false,
26-
'selectedPublishers' => '*',
26+
'publisherNotificationsUserGroup' => null,
2727

2828
// Permissions
2929
'enabledSections' => '*',
@@ -48,7 +48,7 @@ Notifications
4848
- `reviewerApprovalNotifications` - Whether email notifications should be delivered to editors when each reviewer approves an entry after review.
4949
- `publisherNotifications` - Whether email notifications should be delivered to publishers when editors submit an entry for review.
5050
- `publishedAuthorNotifications` - Whether email notifications should be delivered to the entry author when approved and published by a Publisher.
51-
- `selectedPublishers` - An array of user IDs of publishers to receive email notifications. Use '\*' for all.
51+
- `publisherNotificationsUserGroup` - The user group UID to have email notifications for publishers sent to. By default, all users in the `publisherUserGroup` will receive email notifications.
5252

5353
Permissions
5454
- `enabledSections` - An array of section UIDs to enable submissions on. Use '\*' for all.

Diff for: src/Workflow.php

+5
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ private function _registerEventHandlers(): void
213213
// Use `Entry::EVENT_BEFORE_SAVE` in order to add validation errors. `Elements::EVENT_BEFORE_SAVE_ELEMENT` doesn't work
214214
Event::on(Entry::class, Entry::EVENT_BEFORE_SAVE, [$this->getService(), 'onBeforeSaveEntry']);
215215

216+
// When a publisher applies a draft instead of using the Workflow action, approve any pending submissions
217+
// Note the use of "beforeApply" due to when applying a draft of an existing entry, the draft will already be
218+
// deleted by the time we reach "afterApply" and the FK constraint updated on the review, so a submission can't be found.
219+
Event::on(Drafts::class, Drafts::EVENT_BEFORE_APPLY_DRAFT, [$this->getService(), 'onBeforeApplyDraft']);
220+
216221
// Use `Elements::EVENT_AFTER_SAVE_ELEMENT` so that the element is fully finished with propagating, etc.
217222
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, [$this->getService(), 'onAfterSaveElement']);
218223

Diff for: src/controllers/BaseController.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -22,25 +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-
foreach ($publisherUserGroup as $siteUid => $publisherUserGroupUid) {
31-
$publisherUserGroupId = Db::idByUid(Table::USERGROUPS, $publisherUserGroupUid);
32-
33-
foreach (User::find()->groupId($publisherUserGroupId)->all() as $user) {
34-
$publishers[] = ['value' => $user->id, 'label' => (string)$user];
35-
}
36-
}
29+
foreach (Craft::$app->getUserGroups()->getAllGroups() as $userGroup) {
30+
$userGroups[] = ['label' => $userGroup->name, 'value' => $userGroup->uid];
3731
}
3832

39-
$publishers = array_unique($publishers, SORT_REGULAR);
33+
$userGroups = array_unique($userGroups, SORT_REGULAR);
4034

4135
return $this->renderTemplate('workflow/settings', [
4236
'settings' => $settings,
43-
'publishers' => $publishers,
37+
'userGroups' => $userGroups,
4438
]);
4539
}
4640

Diff for: src/elements/Submission.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ protected static function defineDefaultTableAttributes(string $source): array
109109
protected static function defineSortOptions(): array
110110
{
111111
return [
112+
'id' => Craft::t('workflow', 'Submission'),
112113
'ownerId' => Craft::t('workflow', 'Entry'),
113-
'editorId' => Craft::t('workflow', 'Editor'),
114-
'publisherId' => Craft::t('workflow', 'Publisher'),
115114
'lastReviewDate' => Craft::t('workflow', 'Last Reviewed'),
116115
'dateCreated' => Craft::t('workflow', 'Date Submitted'),
117116
];
@@ -399,6 +398,13 @@ public function canUserReview(User $user, $site): bool
399398
return $canReview;
400399
}
401400

401+
public function canUserPublish(User $user, $site): bool
402+
{
403+
$settings = Workflow::$plugin->getSettings();
404+
405+
return $user->isInGroup($settings->getPublisherUserGroup($site));
406+
}
407+
402408
public function afterSave(bool $isNew): void
403409
{
404410
if (!$isNew) {

Diff for: src/models/Settings.php

+17-1
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
@@ -26,11 +27,14 @@ class Settings extends Model
2627
public bool $reviewerApprovalNotifications = false;
2728
public bool $publisherNotifications = true;
2829
public bool $publishedAuthorNotifications = false;
29-
public mixed $selectedPublishers = '*';
30+
public ?string $publisherNotificationsUserGroup = null;
3031

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

35+
// Deprecated
36+
public mixed $selectedPublishers = '*';
37+
3438

3539
// Public Methods
3640
// =========================================================================
@@ -115,4 +119,16 @@ public function getPublisherNotesRequired($site)
115119
{
116120
return $this->publisherNotesRequired[$site->uid] ?? false;
117121
}
122+
123+
public function getPublishersForNotificationEmail($site): array
124+
{
125+
// Get the user group for email notifications
126+
$publisherGroup = $this->publisherNotificationsUserGroup ?? $this->getPublisherUserGroup($site);
127+
128+
if (!$publisherGroup) {
129+
return [];
130+
}
131+
132+
return User::find()->groupId($publisherGroup->id)->all();
133+
}
118134
}

Diff for: src/services/Emails.php

+1-14
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::info('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([

Diff for: src/services/Service.php

+33
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,39 @@ public function onAfterSaveElement(ElementEvent $event): void
211211
}
212212
}
213213

214+
public function onBeforeApplyDraft(DraftEvent $event): void
215+
{
216+
if (!($event->draft instanceof Entry)) {
217+
return;
218+
}
219+
220+
// Because we're using "beforeApply" for complicated reasons, we should at least check if things validate first
221+
$event->draft->setScenario(Element::SCENARIO_LIVE);
222+
223+
if (!$event->draft->validate()) {
224+
return;
225+
}
226+
227+
// Check if a publisher has applied the draft by mistake, and there's a pending submission. Just mark as done.
228+
$submission = Submission::find()
229+
->ownerId($event->draft->getCanonicalId())
230+
->ownerSiteId($event->draft->siteId)
231+
->ownerDraftId($event->draft->draftId)
232+
->limit(1)
233+
->isComplete(false)
234+
->isPending(true)
235+
->one();
236+
237+
if ($submission) {
238+
$currentUser = Craft::$app->getUser()->getIdentity();
239+
240+
// Ensure current user is allowed to publish the submission
241+
if ($submission->canUserPublish($currentUser, $event->draft->site)) {
242+
Workflow::$plugin->getSubmissions()->approveSubmission($event->draft);
243+
}
244+
}
245+
}
246+
214247
public function renderEntrySidebar(DefineHtmlEvent $event): void
215248
{
216249
$entry = $event->sender;

Diff for: src/services/Submissions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ private function _setReviewFromPost(Submission $submission, ElementInterface $en
436436

437437
$review = new Review();
438438
$review->submissionId = $submission->id;
439-
$review->elementId = $entry->id;
439+
$review->elementId = $entry->getCanonicalId();
440440
$review->elementSiteId = $entry->siteId;
441441
$review->draftId = $entry->draftId;
442442
$review->userId = $currentUser->id;

Diff for: src/templates/settings/_panes/notifications.html

+9-28
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)