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 flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface; |
12
|
|
|
use flipbox\craft\sortable\associations\records\SortableAssociationInterface; |
13
|
|
|
use flipbox\craft\sortable\associations\services\SortableAssociations; |
14
|
|
|
use flipbox\ember\services\traits\records\Accessor; |
15
|
|
|
use flipbox\organizations\db\UserTypeAssociationQuery; |
16
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
17
|
|
|
use flipbox\organizations\records\UserTypeAssociation; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
* |
23
|
|
|
* @method UserTypeAssociationQuery parentGetQuery($config = []) |
24
|
|
|
* @method UserTypeAssociation create(array $attributes = []) |
25
|
|
|
* @method UserTypeAssociation find($identifier) |
26
|
|
|
* @method UserTypeAssociation get($identifier) |
27
|
|
|
* @method UserTypeAssociation findByCondition($condition = []) |
28
|
|
|
* @method UserTypeAssociation getByCondition($condition = []) |
29
|
|
|
* @method UserTypeAssociation findByCriteria($criteria = []) |
30
|
|
|
* @method UserTypeAssociation getByCriteria($criteria = []) |
31
|
|
|
* @method UserTypeAssociation[] findAllByCondition($condition = []) |
32
|
|
|
* @method UserTypeAssociation[] getAllByCondition($condition = []) |
33
|
|
|
* @method UserTypeAssociation[] findAllByCriteria($criteria = []) |
34
|
|
|
* @method UserTypeAssociation[] getAllByCriteria($criteria = []) |
35
|
|
|
*/ |
36
|
|
|
class UserTypeAssociations extends SortableAssociations |
37
|
|
|
{ |
38
|
|
|
use Accessor { |
39
|
|
|
getQuery as parentGetQuery; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
$settings = OrganizationPlugin::getInstance()->getSettings(); |
48
|
|
|
$this->cacheDuration = $settings->userTypeAssociationsCacheDuration; |
49
|
|
|
$this->cacheDependency = $settings->userTypeAssociationsCacheDependency; |
50
|
|
|
|
51
|
|
|
parent::init(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
const SOURCE_ATTRIBUTE = UserTypeAssociation::SOURCE_ATTRIBUTE; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
const TARGET_ATTRIBUTE = UserTypeAssociation::TARGET_ATTRIBUTE; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
protected static function tableAlias(): string |
68
|
|
|
{ |
69
|
|
|
return UserTypeAssociation::tableAlias(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $config |
74
|
|
|
* @return SortableAssociationQueryInterface|UserTypeAssociationQuery |
75
|
|
|
*/ |
76
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
77
|
|
|
{ |
78
|
|
|
return $this->parentGetQuery($config); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public static function recordClass(): string |
85
|
|
|
{ |
86
|
|
|
return UserTypeAssociation::class; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param SortableAssociationInterface|UserTypeAssociation $record |
91
|
|
|
* @return SortableAssociationQueryInterface|UserTypeAssociationQuery |
92
|
|
|
*/ |
93
|
|
|
protected function associationQuery( |
94
|
|
|
SortableAssociationInterface $record |
95
|
|
|
): SortableAssociationQueryInterface { |
96
|
|
|
return $this->query( |
97
|
|
|
$record->{static::SOURCE_ATTRIBUTE} |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param SortableAssociationQueryInterface|UserTypeAssociationQuery $query |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected function existingAssociations( |
106
|
|
|
SortableAssociationQueryInterface $query |
107
|
|
|
): array { |
108
|
|
|
if (null === ($associationId = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE))) { |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->associations((int)$associationId); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $userAssociationId |
117
|
|
|
* @return UserTypeAssociationQuery |
118
|
|
|
*/ |
119
|
|
|
private function query( |
120
|
|
|
int $userAssociationId |
121
|
|
|
): UserTypeAssociationQuery { |
122
|
|
|
return $this->getQuery() |
123
|
|
|
->where([ |
124
|
|
|
static::SOURCE_ATTRIBUTE => $userAssociationId |
125
|
|
|
]) |
126
|
|
|
->orderBy(['sortOrder' => SORT_ASC]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param int $userAssociationId |
131
|
|
|
* @return UserTypeAssociation[] |
132
|
|
|
*/ |
133
|
|
|
private function associations( |
134
|
|
|
int $userAssociationId |
135
|
|
|
): array { |
136
|
|
|
return $this->query($userAssociationId) |
137
|
|
|
->indexBy('typeId') |
138
|
|
|
->all(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|