1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organization\elements\actions; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementAction; |
13
|
|
|
use craft\elements\db\ElementQueryInterface; |
14
|
|
|
use craft\helpers\Json; |
15
|
|
|
use flipbox\organization\elements\Organization; |
16
|
|
|
use flipbox\organization\Organization as OrganizationPlugin; |
17
|
|
|
use yii\base\Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class DeleteOrganization extends ElementAction |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var int|null The organization ID that the deleted organization’s users should be transferred to |
27
|
|
|
*/ |
28
|
|
|
public $transferUsersTo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function getTriggerLabel(): string |
34
|
|
|
{ |
35
|
|
|
return Craft::t('app', 'Delete'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public static function isDestructive(): bool |
42
|
|
|
{ |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function getTriggerHtml() |
50
|
|
|
{ |
51
|
|
|
$type = Json::encode(static::class); |
52
|
|
|
$elementType = Json::encode(Organization::class); |
53
|
|
|
$redirect = Json::encode(Craft::$app->getSecurity()->hashData('organization')); |
54
|
|
|
|
55
|
|
|
$js = <<<EOD |
56
|
|
|
(function() |
57
|
|
|
{ |
58
|
|
|
var trigger = new Craft.ElementActionTrigger({ |
59
|
|
|
type: {$type}, |
60
|
|
|
batch: true, |
61
|
|
|
activate: function(\$selectedItems) |
62
|
|
|
{ |
63
|
|
|
var modal = new Craft.DeleteOrganizationModal(Craft.elementIndex.getSelectedElementIds(), { |
64
|
|
|
onSubmit: function() |
65
|
|
|
{ |
66
|
|
|
Craft.elementIndex.submitAction({$type}, Garnish.getPostData(modal.\$container)); |
67
|
|
|
modal.hide(); |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
}, |
71
|
|
|
redirect: {$redirect}, |
72
|
|
|
elementType: {$elementType} |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
})(); |
77
|
|
|
EOD; |
78
|
|
|
|
79
|
|
|
Craft::$app->getView()->registerJs($js); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Performs the action on any elements that match the given criteria. |
84
|
|
|
* |
85
|
|
|
* @param ElementQueryInterface $query The element query defining which elements the action should affect. |
86
|
|
|
* |
87
|
|
|
* @return bool Whether the action was performed successfully. |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
|
public function performAction(ElementQueryInterface $query): bool |
91
|
|
|
{ |
92
|
|
|
/** @var Organization[] $organizations */ |
93
|
|
|
$organizations = $query->all(); |
94
|
|
|
|
95
|
|
|
// Are we transferring the user's content to a different user? |
96
|
|
|
if (is_array($this->transferUsersTo) && isset($this->transferUsersTo[0])) { |
97
|
|
|
$this->transferUsersTo = $this->transferUsersTo[0]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!empty($this->transferUsersTo)) { |
101
|
|
|
$transferUsersTo = OrganizationPlugin::getInstance()->getOrganization()->getById($this->transferUsersTo); |
102
|
|
|
} else { |
103
|
|
|
$transferUsersTo = null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Transfer the users |
107
|
|
|
foreach ($organizations as $organization) { |
108
|
|
|
if ($transferUsersTo) { |
109
|
|
|
$users = $organization->getMembers(['status' => null]); |
110
|
|
|
foreach ($users->all() as $user) { |
111
|
|
|
// Remove current association |
112
|
|
|
OrganizationPlugin::getInstance()->getUser()->dissociate( |
113
|
|
|
$user, |
114
|
|
|
$organization |
115
|
|
|
); |
116
|
|
|
// Apply new association |
117
|
|
|
OrganizationPlugin::getInstance()->getUser()->associate( |
118
|
|
|
$user, |
119
|
|
|
$transferUsersTo |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
Craft::$app->getElements()->deleteElement($organization); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->setMessage(Craft::t('organization', 'Organizations were deleted.')); |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|