1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/domains/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/domains/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\domains\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\domains\db\DomainsQuery; |
15
|
|
|
use flipbox\domains\records\Domain; |
16
|
|
|
use yii\db\ActiveQuery; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Associations extends SortableAssociations |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
const TABLE_ALIAS = Domain::TABLE_ALIAS; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
const SOURCE_ATTRIBUTE = Domain::SOURCE_ATTRIBUTE; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
const TARGET_ATTRIBUTE = Domain::TARGET_ATTRIBUTE; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
* @return DomainsQuery |
42
|
|
|
*/ |
43
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
44
|
|
|
{ |
45
|
|
|
return new DomainsQuery(Domain::class, $config); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SortableAssociationInterface|Domain $record |
50
|
|
|
* @return SortableAssociationQueryInterface|DomainsQuery |
51
|
|
|
*/ |
52
|
|
|
protected function associationQuery( |
53
|
|
|
SortableAssociationInterface $record |
54
|
|
|
): SortableAssociationQueryInterface { |
55
|
|
|
return $this->query( |
56
|
|
|
$record->{static::SOURCE_ATTRIBUTE}, |
57
|
|
|
$record->fieldId, |
58
|
|
|
$record->siteId |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param SortableAssociationQueryInterface|DomainsQuery $query |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function existingAssociations( |
67
|
|
|
SortableAssociationQueryInterface $query |
68
|
|
|
): array { |
69
|
|
|
$source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE); |
70
|
|
|
$field = $this->resolveStringAttribute($query, 'fieldId'); |
71
|
|
|
$site = $this->resolveStringAttribute($query, 'siteId'); |
72
|
|
|
|
73
|
|
|
if ($source === null || $field === null || $site === null) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->associations($source, $field, $site); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $source |
82
|
|
|
* @param int $fieldId |
83
|
|
|
* @param int $siteId |
84
|
|
|
* @return SortableAssociationQueryInterface|ActiveQuery |
85
|
|
|
*/ |
86
|
|
|
private function query( |
87
|
|
|
$source, |
88
|
|
|
int $fieldId, |
89
|
|
|
int $siteId |
90
|
|
|
): SortableAssociationQueryInterface { |
91
|
|
|
return $this->getQuery() |
92
|
|
|
->where([ |
93
|
|
|
static::SOURCE_ATTRIBUTE => $source, |
94
|
|
|
'fieldId' => $fieldId, |
95
|
|
|
'siteId' => $siteId |
96
|
|
|
]) |
97
|
|
|
->orderBy(['sortOrder' => SORT_ASC]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $source |
102
|
|
|
* @param int $fieldId |
103
|
|
|
* @param int $siteId |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
private function associations( |
107
|
|
|
$source, |
108
|
|
|
int $fieldId, |
109
|
|
|
int $siteId |
110
|
|
|
): array { |
111
|
|
|
return $this->query($source, $fieldId, $siteId) |
112
|
|
|
->indexBy(static::TARGET_ATTRIBUTE) |
113
|
|
|
->all(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|