1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\ember\records\traits; |
10
|
|
|
|
11
|
|
|
use craft\models\Site as SiteModel; |
12
|
|
|
use craft\records\Site as SiteRecord; |
13
|
|
|
use flipbox\ember\helpers\SiteHelper; |
14
|
|
|
use flipbox\ember\traits\SiteMutator; |
15
|
|
|
use flipbox\ember\traits\SiteRules; |
16
|
|
|
use yii\db\ActiveQueryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
* |
22
|
|
|
* @method SiteModel parentResolveSite() |
23
|
|
|
*/ |
24
|
|
|
trait SiteAttribute |
25
|
|
|
{ |
26
|
|
|
use ActiveRecord, |
27
|
|
|
SiteRules, |
28
|
|
|
SiteMutator { |
29
|
|
|
resolveSite as parentResolveSite; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get associated siteId |
34
|
|
|
* |
35
|
|
|
* @return int|null |
36
|
|
|
*/ |
37
|
|
|
public function getSiteId() |
38
|
|
|
{ |
39
|
|
|
$siteId = $this->getAttribute('siteId'); |
40
|
|
|
if (null === $siteId && null !== $this->site) { |
41
|
|
|
$siteId = $this->siteId = $this->site->id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $siteId; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return SiteModel|null |
49
|
|
|
* @throws \yii\base\InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
protected function resolveSite() |
52
|
|
|
{ |
53
|
|
|
if ($site = $this->resolveSiteFromRelation()) { |
54
|
|
|
return $site; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->parentResolveSite(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return SiteModel|null |
62
|
|
|
* @throws \yii\base\InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
private function resolveSiteFromRelation() |
65
|
|
|
{ |
66
|
|
|
if (false === $this->isRelationPopulated('siteRecord')) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return SiteHelper::resolve($this->getRelation('siteRecord')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the associated Site |
75
|
|
|
* |
76
|
|
|
* @return ActiveQueryInterface |
77
|
|
|
*/ |
78
|
|
|
public function getSiteRecord() |
79
|
|
|
{ |
80
|
|
|
return $this->hasOne( |
81
|
|
|
SiteRecord::class, |
82
|
|
|
['siteId' => 'id'] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|