|
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\UserOrganizationAssociationQuery; |
|
18
|
|
|
use flipbox\organizations\records\UserAssociation; |
|
19
|
|
|
use yii\db\ActiveQuery; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manage the Organization associations for a user. A user may have multiple organization associations. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Flipbox Factory <[email protected]> |
|
25
|
|
|
* @since 1.0.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @method UserAssociation create(array $attributes = []) |
|
28
|
|
|
* @method UserAssociation find($identifier) |
|
29
|
|
|
* @method UserAssociation get($identifier) |
|
30
|
|
|
* @method UserAssociation findByCondition($condition = []) |
|
31
|
|
|
* @method UserAssociation getByCondition($condition = []) |
|
32
|
|
|
* @method UserAssociation findByCriteria($criteria = []) |
|
33
|
|
|
* @method UserAssociation getByCriteria($criteria = []) |
|
34
|
|
|
* @method UserAssociation[] findAllByCondition($condition = []) |
|
35
|
|
|
* @method UserAssociation[] getAllByCondition($condition = []) |
|
36
|
|
|
* @method UserAssociation[] findAllByCriteria($criteria = []) |
|
37
|
|
|
* @method UserAssociation[] getAllByCriteria($criteria = []) |
|
38
|
|
|
*/ |
|
39
|
|
|
class UserOrganizationAssociations extends SortableAssociations |
|
40
|
|
|
{ |
|
41
|
|
|
use Accessor; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
const SOURCE_ATTRIBUTE = UserAssociation::SOURCE_ATTRIBUTE; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
const TARGET_ATTRIBUTE = UserAssociation::TARGET_ATTRIBUTE; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The sort order attribute name |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
const SORT_ORDER_ATTRIBUTE = 'userOrder'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
protected static function tableAlias(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return UserAssociation::tableAlias(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
* @return UserOrganizationAssociationQuery |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
|
74
|
|
|
$query = Craft::createObject( |
|
75
|
|
|
UserOrganizationAssociationQuery::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::TARGET_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::SOURCE_ATTRIBUTE) |
|
148
|
|
|
->all(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|