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\organizations\elements\actions; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementAction; |
13
|
|
|
use craft\elements\db\ElementQuery; |
14
|
|
|
use craft\elements\db\ElementQueryInterface; |
15
|
|
|
use craft\elements\db\UserQuery; |
16
|
|
|
use craft\helpers\ArrayHelper; |
17
|
|
|
use flipbox\organizations\objects\OrganizationAttributeTrait; |
18
|
|
|
use yii\base\InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class DissociateUsersFromOrganizationAction extends ElementAction |
25
|
|
|
{ |
26
|
|
|
use OrganizationAttributeTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function settingsAttributes(): array |
32
|
|
|
{ |
33
|
|
|
return array_merge( |
34
|
|
|
parent::settingsAttributes(), |
35
|
|
|
[ |
36
|
|
|
'organization' |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public static function isDestructive(): bool |
45
|
|
|
{ |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function getTriggerLabel(): string |
53
|
|
|
{ |
54
|
|
|
return Craft::t('organizations', 'Disassociate'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
* @param ElementQuery $query |
60
|
|
|
* @throws \Throwable |
61
|
|
|
*/ |
62
|
|
|
public function performAction(ElementQueryInterface $query): bool |
63
|
|
|
{ |
64
|
|
|
if (null === ($organization = $this->getOrganization())) { |
65
|
|
|
throw new InvalidArgumentException("Organization could not be found"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$query instanceof UserQuery) { |
69
|
|
|
throw new InvalidArgumentException(sprintf( |
70
|
|
|
"Query must be an instance of %s, %s given.", |
71
|
|
|
UserQuery::class, |
72
|
|
|
get_class($query) |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Get the count because it's cleared when dissociated |
77
|
|
|
$userCount = $query->count(); |
78
|
|
|
|
79
|
|
|
if (false === $organization->getUsers()->remove($query)->save()) { |
80
|
|
|
$this->setMessage( |
81
|
|
|
Craft::t( |
82
|
|
|
'organizations', |
83
|
|
|
$this->assembleFailMessage($query) |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->setMessage($this->assembleSuccessMessage($userCount)); |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ElementQueryInterface|UserQuery $query |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function assembleFailMessage(ElementQueryInterface $query): string |
99
|
|
|
{ |
100
|
|
|
$message = 'Failed to remove user: '; |
101
|
|
|
|
102
|
|
|
$users = $query->all(); |
103
|
|
|
$badEmails = ArrayHelper::index($users, 'email'); |
104
|
|
|
|
105
|
|
|
$message .= implode(", ", $badEmails); |
106
|
|
|
|
107
|
|
|
return Craft::t('organizations', $message); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $count |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function assembleSuccessMessage(int $count): string |
115
|
|
|
{ |
116
|
|
|
$message = 'User'; |
117
|
|
|
|
118
|
|
|
if ($count != 1) { |
119
|
|
|
$message = '{count} ' . $message . 's'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$message .= ' dissociated.'; |
123
|
|
|
|
124
|
|
|
return Craft::t('organizations', $message, ['count' => $count]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|