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\services; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface; |
13
|
|
|
use flipbox\craft\sortable\associations\records\SortableAssociationInterface; |
14
|
|
|
use flipbox\craft\sortable\associations\services\SortableAssociations; |
15
|
|
|
use flipbox\ember\helpers\QueryHelper; |
16
|
|
|
use flipbox\ember\services\traits\records\Accessor; |
17
|
|
|
use flipbox\organizations\db\OrganizationUserAssociationQuery; |
18
|
|
|
use flipbox\organizations\db\UserOrganizationAssociationQuery; |
19
|
|
|
use flipbox\organizations\records\UserAssociation; |
20
|
|
|
use yii\db\ActiveQuery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Manage the User associations for an Organization. An Organization may have multiple user associations. |
24
|
|
|
* |
25
|
|
|
* @author Flipbox Factory <[email protected]> |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
* |
28
|
|
|
* @method UserAssociation create(array $attributes = []) |
29
|
|
|
* @method UserAssociation find($identifier) |
30
|
|
|
* @method UserAssociation get($identifier) |
31
|
|
|
* @method UserAssociation findByCondition($condition = []) |
32
|
|
|
* @method UserAssociation getByCondition($condition = []) |
33
|
|
|
* @method UserAssociation findByCriteria($criteria = []) |
34
|
|
|
* @method UserAssociation getByCriteria($criteria = []) |
35
|
|
|
* @method UserAssociation[] findAllByCondition($condition = []) |
36
|
|
|
* @method UserAssociation[] getAllByCondition($condition = []) |
37
|
|
|
* @method UserAssociation[] findAllByCriteria($criteria = []) |
38
|
|
|
* @method UserAssociation[] getAllByCriteria($criteria = []) |
39
|
|
|
*/ |
40
|
|
|
class OrganizationUserAssociations extends SortableAssociations |
41
|
|
|
{ |
42
|
|
|
use Accessor; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
const SOURCE_ATTRIBUTE = UserAssociation::SOURCE_ATTRIBUTE; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
const TARGET_ATTRIBUTE = UserAssociation::TARGET_ATTRIBUTE; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
const SORT_ORDER_ATTRIBUTE = 'organizationOrder'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
protected static function tableAlias(): string |
63
|
|
|
{ |
64
|
|
|
return UserAssociation::tableAlias(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
* @return OrganizationUserAssociationQuery |
70
|
|
|
*/ |
71
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
72
|
|
|
{ |
73
|
|
|
/** @var OrganizationUserAssociationQuery $query */ |
74
|
|
|
$query = Craft::createObject( |
75
|
|
|
OrganizationUserAssociationQuery::class, |
76
|
|
|
[UserAssociation::class] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
QueryHelper::configure( |
80
|
|
|
$query, |
81
|
|
|
$this->prepareQueryConfig($config) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $query; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public static function recordClass() |
91
|
|
|
{ |
92
|
|
|
return UserAssociation::class; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param SortableAssociationInterface|UserAssociation $record |
97
|
|
|
* @return SortableAssociationQueryInterface|UserOrganizationAssociationQuery |
98
|
|
|
*/ |
99
|
|
|
protected function associationQuery( |
100
|
|
|
SortableAssociationInterface $record |
101
|
|
|
): SortableAssociationQueryInterface { |
102
|
|
|
return $this->query( |
103
|
|
|
$record->{static::SOURCE_ATTRIBUTE} |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param SortableAssociationQueryInterface|UserOrganizationAssociationQuery $query |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function existingAssociations( |
112
|
|
|
SortableAssociationQueryInterface $query |
113
|
|
|
): array { |
114
|
|
|
$source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE); |
115
|
|
|
|
116
|
|
|
if ($source === null) { |
117
|
|
|
return []; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->associations($source); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $source |
125
|
|
|
* @return SortableAssociationQueryInterface|UserOrganizationAssociationQuery |
126
|
|
|
*/ |
127
|
|
|
private function query( |
128
|
|
|
$source |
129
|
|
|
): SortableAssociationQueryInterface { |
130
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
131
|
|
|
$query = $this->getQuery(); |
132
|
|
|
return $query->where([ |
133
|
|
|
static::SOURCE_ATTRIBUTE => $source |
134
|
|
|
]) |
135
|
|
|
->orderBy([static::SORT_ORDER_ATTRIBUTE => SORT_ASC]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $source |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
private function associations( |
143
|
|
|
$source |
144
|
|
|
): array { |
145
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
146
|
|
|
$query = $this->query($source); |
147
|
|
|
return $query->indexBy(static::TARGET_ATTRIBUTE) |
148
|
|
|
->all(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|