1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-element-lists/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-element-lists/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\element\lists\records; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Element; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use flipbox\craft\ember\records\ActiveRecordTrait; |
15
|
|
|
use yii\base\Model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This trait assists w/ managing a 'sourceId' attribute and a 'source' object, keeping them |
19
|
|
|
* in sync if one of the other is altered. In addition, depending on the operation process, a |
20
|
|
|
* newly created object could be set and saved. Subsequent calls to retrieve the Id would return |
21
|
|
|
* the newly created object Id. |
22
|
|
|
* |
23
|
|
|
* @author Flipbox Factory <[email protected]> |
24
|
|
|
* @since 3.0.0 |
25
|
|
|
*/ |
26
|
|
|
trait SourceAttributeTrait |
27
|
|
|
{ |
28
|
|
|
use ActiveRecordTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Element|ElementInterface|null |
32
|
|
|
*/ |
33
|
|
|
private $source; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $attribute |
37
|
|
|
* @return Element|ElementInterface|null |
38
|
|
|
*/ |
39
|
|
|
abstract protected function resolveElementFromIdAttribute(string $attribute); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $element |
43
|
|
|
* @return Element|ElementInterface|null |
44
|
|
|
*/ |
45
|
|
|
abstract protected function resolveElement($element = null); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function isSourceSet(): bool |
51
|
|
|
{ |
52
|
|
|
return null !== $this->source; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
protected function sourceRules(): array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
[ |
62
|
|
|
[ |
63
|
|
|
'sourceId' |
64
|
|
|
], |
65
|
|
|
'number', |
66
|
|
|
'integerOnly' => true |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
[ |
70
|
|
|
'sourceId', |
71
|
|
|
'source' |
72
|
|
|
], |
73
|
|
|
'safe', |
74
|
|
|
'on' => [ |
75
|
|
|
Model::SCENARIO_DEFAULT |
76
|
|
|
] |
77
|
|
|
] |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function sourceAttributeLabels(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'sourceId' => Craft::t('app', 'Source Id') |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int|null $id |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setSourceId(int $id = null) |
96
|
|
|
{ |
97
|
|
|
$this->setAttribute('sourceId', $id); |
98
|
|
|
|
99
|
|
|
if (null !== $this->source && $id !== $this->source->getId()) { |
100
|
|
|
$this->source = null; |
101
|
|
|
} |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get associated sourceId |
107
|
|
|
* |
108
|
|
|
* @return int|null |
109
|
|
|
*/ |
110
|
|
|
public function getSourceId() |
111
|
|
|
{ |
112
|
|
|
if (null === $this->getAttribute('sourceId') && null !== $this->source) { |
113
|
|
|
$this->setSourceId($this->source->getId()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->getAttribute('sourceId'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed $source |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setSource($source = null) |
124
|
|
|
{ |
125
|
|
|
$this->source = null; |
126
|
|
|
$this->setAttribute('sourceId', null); |
127
|
|
|
|
128
|
|
|
if ($source = $this->resolveElement($source)) { |
129
|
|
|
$this->source = $source; |
130
|
|
|
$this->setAttribute('sourceId', $source->getId()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Element|ElementInterface|null |
138
|
|
|
*/ |
139
|
|
|
public function getSource() |
140
|
|
|
{ |
141
|
|
|
if ($this->source === null) { |
142
|
|
|
$source = $this->resolveElementFromIdAttribute('sourceId'); |
143
|
|
|
$this->setSource($source); |
144
|
|
|
return $source; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$sourceId = $this->getAttribute('sourceId'); |
148
|
|
|
if ($sourceId !== null && $sourceId !== $this->source->getId()) { |
149
|
|
|
$this->source = null; |
150
|
|
|
return $this->getSource(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->source; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|