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\Organizations as OrganizationPlugin; |
19
|
|
|
use flipbox\organizations\records\UserAssociation; |
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
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function init() |
47
|
|
|
{ |
48
|
|
|
$settings = OrganizationPlugin::getInstance()->getSettings(); |
49
|
|
|
$this->cacheDuration = $settings->userOrganizationAssociationsCacheDuration; |
50
|
|
|
$this->cacheDependency = $settings->userOrganizationAssociationsCacheDependency; |
51
|
|
|
|
52
|
|
|
parent::init(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
const SOURCE_ATTRIBUTE = UserAssociation::SOURCE_ATTRIBUTE; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
const TARGET_ATTRIBUTE = UserAssociation::TARGET_ATTRIBUTE; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The sort order attribute name |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
const SORT_ORDER_ATTRIBUTE = 'userOrder'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
protected static function tableAlias(): string |
75
|
|
|
{ |
76
|
|
|
return UserAssociation::tableAlias(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
* @return UserOrganizationAssociationQuery |
82
|
|
|
*/ |
83
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
84
|
|
|
{ |
85
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
86
|
|
|
$query = Craft::createObject( |
87
|
|
|
UserOrganizationAssociationQuery::class, |
88
|
|
|
[UserAssociation::class] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
QueryHelper::configure( |
92
|
|
|
$query, |
93
|
|
|
$this->prepareQueryConfig($config) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return $query; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public static function recordClass() |
103
|
|
|
{ |
104
|
|
|
return UserAssociation::class; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param SortableAssociationInterface|UserAssociation $record |
109
|
|
|
* @return SortableAssociationQueryInterface|UserOrganizationAssociationQuery |
110
|
|
|
*/ |
111
|
|
|
protected function associationQuery( |
112
|
|
|
SortableAssociationInterface $record |
113
|
|
|
): SortableAssociationQueryInterface { |
114
|
|
|
return $this->query( |
115
|
|
|
$record->{static::SOURCE_ATTRIBUTE} |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param SortableAssociationQueryInterface|UserOrganizationAssociationQuery $query |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function existingAssociations( |
124
|
|
|
SortableAssociationQueryInterface $query |
125
|
|
|
): array { |
126
|
|
|
$source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE); |
127
|
|
|
|
128
|
|
|
if ($source === null) { |
129
|
|
|
return []; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->associations($source); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $source |
137
|
|
|
* @return SortableAssociationQueryInterface|UserOrganizationAssociationQuery |
138
|
|
|
*/ |
139
|
|
|
private function query( |
140
|
|
|
$source |
141
|
|
|
): SortableAssociationQueryInterface { |
142
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
143
|
|
|
$query = $this->getQuery(); |
144
|
|
|
return $query->where([ |
145
|
|
|
static::TARGET_ATTRIBUTE => $source |
146
|
|
|
]) |
147
|
|
|
->orderBy([static::SORT_ORDER_ATTRIBUTE => SORT_ASC]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $source |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
private function associations( |
155
|
|
|
$source |
156
|
|
|
): array { |
157
|
|
|
/** @var UserOrganizationAssociationQuery $query */ |
158
|
|
|
$query = $this->query($source); |
159
|
|
|
return $query->indexBy(static::SOURCE_ATTRIBUTE) |
160
|
|
|
->all(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|