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; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\db\Query; |
13
|
|
|
use craft\elements\db\UserQuery; |
14
|
|
|
use craft\elements\User; |
15
|
|
|
use craft\helpers\ArrayHelper; |
16
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
17
|
|
|
use flipbox\organizations\managers\RelationshipManagerInterface; |
18
|
|
|
use flipbox\organizations\managers\UserRelationshipManager; |
19
|
|
|
use flipbox\organizations\records\UserAssociation; |
20
|
|
|
use Tightenco\Collect\Support\Collection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Flipbox Factory <[email protected]> |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* |
26
|
|
|
* @mixin Organization |
27
|
|
|
*/ |
28
|
|
|
trait UsersAttributeTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var RelationshipManagerInterface |
32
|
|
|
*/ |
33
|
|
|
private $userManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return RelationshipManagerInterface |
37
|
|
|
*/ |
38
|
|
|
public function getUserManager(): RelationshipManagerInterface |
39
|
|
|
{ |
40
|
|
|
if (null === $this->userManager) { |
41
|
|
|
$this->userManager = new UserRelationshipManager($this); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->userManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $sourceElements |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
private static function eagerLoadingUsersMap(array $sourceElements) |
52
|
|
|
{ |
53
|
|
|
// Get the source element IDs |
54
|
|
|
$sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id'); |
55
|
|
|
|
56
|
|
|
$map = (new Query()) |
57
|
|
|
->select(['organizationId as source', 'userId as target']) |
58
|
|
|
->from(UserAssociation::tableName()) |
59
|
|
|
->where(['organizationId' => $sourceElementIds]) |
60
|
|
|
->all(); |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
'elementType' => User::class, |
64
|
|
|
'map' => $map |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/************************************************************ |
69
|
|
|
* REQUEST |
70
|
|
|
************************************************************/ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* AssociateUserToOrganization an array of users from request input |
74
|
|
|
* |
75
|
|
|
* @param string $identifier |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setUsersFromRequest(string $identifier = 'users') |
79
|
|
|
{ |
80
|
|
|
if (null !== ($users = Craft::$app->getRequest()->getBodyParam($identifier))) { |
81
|
|
|
$this->getUserManager()->setMany((array)$users); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/************************************************************ |
89
|
|
|
* USERS QUERY |
90
|
|
|
************************************************************/ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $criteria |
94
|
|
|
* @return UserQuery |
95
|
|
|
*/ |
96
|
|
|
public function userQuery($criteria = []): UserQuery |
97
|
|
|
{ |
98
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
99
|
|
|
$query = User::find() |
100
|
|
|
->organization($this) |
101
|
|
|
->orderBy([ |
102
|
|
|
'userOrder' => SORT_ASC, |
103
|
|
|
'username' => SORT_ASC, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
if (!empty($criteria)) { |
107
|
|
|
QueryHelper::configure( |
108
|
|
|
$query, |
109
|
|
|
$criteria |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $query; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get an array of users associated to an organization |
118
|
|
|
* |
119
|
|
|
* @param array $criteria |
120
|
|
|
* @return User[]|Collection |
121
|
|
|
*/ |
122
|
|
|
public function getUsers(array $criteria = []): Collection |
123
|
|
|
{ |
124
|
|
|
return Collection::make(QueryHelper::configure( |
125
|
|
|
$this->userQuery($criteria) |
126
|
|
|
->id( |
127
|
|
|
$this->getUserManager()->findAll() |
|
|
|
|
128
|
|
|
->pluck('userId') |
129
|
|
|
) |
130
|
|
|
->fixedOrder(true) |
131
|
|
|
->limit(null), |
132
|
|
|
$criteria |
133
|
|
|
)->all()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set an array or query of users to an organization |
138
|
|
|
* |
139
|
|
|
* @param $users |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setUsers($users) |
143
|
|
|
{ |
144
|
|
|
$this->getUserManager()->setMany($users); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: