-
Notifications
You must be signed in to change notification settings - Fork 611
/
Delete.php
245 lines (214 loc) · 7.07 KB
/
Delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements\actions;
use Craft;
use craft\base\ElementAction;
use craft\base\ElementInterface;
use craft\base\NestedElementInterface;
use craft\db\Table;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\Db;
use craft\helpers\Html;
use craft\services\Elements;
/**
* Delete represents a Delete element action.
*
* Element types that make this action available should implement [[ElementInterface::canDelete()]] to explicitly state whether they can be
* deleted by the current user.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Delete extends ElementAction implements DeleteActionInterface
{
/**
* @var bool Whether to delete the element’s descendants as well.
* @since 3.5.0
*/
public bool $withDescendants = false;
/**
* @var bool Whether to permanently delete the elements.
* @since 3.5.0
*/
public bool $hard = false;
/**
* @var string|null The confirmation message that should be shown before the elements get deleted
*/
public ?string $confirmationMessage = null;
/**
* @var string|null The message that should be shown after the elements get deleted
*/
public ?string $successMessage = null;
/**
* @inheritdoc
*/
public function canHardDelete(): bool
{
return !$this->withDescendants;
}
/**
* @inheritdoc
*/
public function setHardDelete(): void
{
$this->hard = true;
}
/**
* @inheritdoc
* @since 3.5.0
*/
public function getTriggerHtml(): ?string
{
// Only enable for deletable elements, per canDelete()
Craft::$app->getView()->registerJsWithVars(fn($type) => <<<JS
(() => {
new Craft.ElementActionTrigger({
type: $type,
validateSelection: (selectedItems, elementIndex) => {
for (let i = 0; i < selectedItems.length; i++) {
if (!Garnish.hasAttr(selectedItems.eq(i).find('.element'), 'data-deletable')) {
return false;
}
}
return elementIndex.settings.canDeleteElements(selectedItems);
},
beforeActivate: async (selectedItems, elementIndex) => {
await elementIndex.settings.onBeforeDeleteElements(selectedItems);
},
afterActivate: async (selectedItems, elementIndex) => {
await elementIndex.settings.onDeleteElements(selectedItems);
},
});
})();
JS, [static::class]);
if ($this->hard) {
return Html::tag('div', $this->getTriggerLabel(), [
'class' => ['btn', 'formsubmit'],
]);
}
return null;
}
/**
* @inheritdoc
*/
public function getTriggerLabel(): string
{
if ($this->hard) {
return Craft::t('app', 'Delete permanently');
}
if ($this->withDescendants) {
return Craft::t('app', 'Delete (with descendants)');
}
return Craft::t('app', 'Delete');
}
/**
* @inheritdoc
*/
public static function isDestructive(): bool
{
return true;
}
/**
* @inheritdoc
*/
public function getConfirmationMessage(): ?string
{
if (isset($this->confirmationMessage)) {
return $this->confirmationMessage;
}
/** @var ElementInterface|string $elementType */
$elementType = $this->elementType;
if ($this->hard) {
return Craft::t('app', 'Are you sure you want to permanently delete the selected {type}?', [
'type' => $elementType::pluralLowerDisplayName(),
]);
}
if ($this->withDescendants) {
return Craft::t('app', 'Are you sure you want to delete the selected {type} along with their descendants?', [
'type' => $elementType::pluralLowerDisplayName(),
]);
}
return Craft::t('app', 'Are you sure you want to delete the selected {type}?', [
'type' => $elementType::pluralLowerDisplayName(),
]);
}
/**
* @inheritdoc
*/
public function performAction(ElementQueryInterface $query): bool
{
$withDescendants = $this->withDescendants && !$this->hard;
$elementsService = Craft::$app->getElements();
if ($withDescendants) {
$query
->with([
[
'descendants',
[
'orderBy' => ['structureelements.lft' => SORT_DESC],
],
],
])
->orderBy(['structureelements.lft' => SORT_DESC]);
}
$deletedElementIds = [];
$user = Craft::$app->getUser()->getIdentity();
$deleteOwnership = [];
foreach ($query->all() as $element) {
if (!$elementsService->canView($element, $user) || !$elementsService->canDelete($element, $user)) {
continue;
}
if (!isset($deletedElementIds[$element->id])) {
if ($withDescendants) {
foreach ($element->getDescendants()->all() as $descendant) {
if (
!isset($deletedElementIds[$descendant->id]) &&
$elementsService->canView($descendant, $user) &&
$elementsService->canDelete($descendant, $user)
) {
$this->deleteElement($element, $elementsService, $deleteOwnership);
$deletedElementIds[$descendant->id] = true;
}
}
}
$this->deleteElement($element, $elementsService, $deleteOwnership);
$deletedElementIds[$element->id] = true;
}
}
foreach ($deleteOwnership as $ownerId => $elementIds) {
Db::delete(Table::ELEMENTS_OWNERS, [
'elementId' => $elementIds,
'ownerId' => $ownerId,
]);
}
if (isset($this->successMessage)) {
$this->setMessage($this->successMessage);
} else {
/** @var ElementInterface|string $elementType */
$elementType = $this->elementType;
$this->setMessage(Craft::t('app', '{type} deleted.', [
'type' => $elementType::pluralDisplayName(),
]));
}
return true;
}
private function deleteElement(
ElementInterface $element,
Elements $elementsService,
array &$deleteOwnership,
): void {
// If the element primarily belongs to a different element, just delete the ownership
if ($element instanceof NestedElementInterface) {
$ownerId = $element->getOwnerId();
if ($ownerId && $element->getPrimaryOwnerId() !== $ownerId) {
$deleteOwnership[$ownerId][] = $element->id;
return;
}
}
$elementsService->deleteElement($element, $this->hard);
}
}