|
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\craft\ember\records; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\elements\User; |
|
13
|
|
|
use craft\records\User as UserRecord; |
|
14
|
|
|
use flipbox\craft\ember\models\UserRulesTrait; |
|
15
|
|
|
use flipbox\craft\ember\objects\UserMutatorTrait; |
|
16
|
|
|
use yii\db\ActiveQueryInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Intended to be used on an ActiveRecord, this class provides `$this->userId` attribute along with 'getters' |
|
20
|
|
|
* and 'setters' to ensure continuity between the Id and Object. An user object is lazy loaded when called. |
|
21
|
|
|
* In addition, ActiveRecord rules are available. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Flipbox Factory <[email protected]> |
|
24
|
|
|
* @since 2.0.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @property UserRecord $userRecord |
|
27
|
|
|
*/ |
|
28
|
|
|
trait UserAttributeTrait |
|
29
|
|
|
{ |
|
30
|
|
|
use ActiveRecordTrait, |
|
31
|
|
|
UserRulesTrait, |
|
32
|
|
|
UserMutatorTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function userAttributes(): array |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
'userId' |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function userAttributeLabels(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'userId' => Craft::t('app', 'User Id') |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritDoc |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function internalSetUserId(int $id = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->setAttribute('userId', $id); |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function internalGetUserId() |
|
67
|
|
|
{ |
|
68
|
|
|
if (null === ($id = $this->getAttribute('userId'))) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
return (int)$id; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return User|null |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function resolveUser() |
|
78
|
|
|
{ |
|
79
|
|
|
if ($model = $this->resolveUserFromRelation()) { |
|
80
|
|
|
return $model; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->resolveUserFromId(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return User|null |
|
88
|
|
|
*/ |
|
89
|
|
|
private function resolveUserFromRelation() |
|
90
|
|
|
{ |
|
91
|
|
|
if (false === $this->isRelationPopulated('userRecord')) { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (null === ($record = $this->getRelation('userRecord'))) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @var UserRecord $record */ |
|
100
|
|
|
|
|
101
|
|
|
return Craft::$app->getUsers()->getUserById($record->id); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the associated User |
|
106
|
|
|
* |
|
107
|
|
|
* @return ActiveQueryInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getUserRecord(): ActiveQueryInterface |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->hasOne( |
|
112
|
|
|
UserRecord::class, |
|
113
|
|
|
['id' => 'userId'] |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|