|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Socializer plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://enupal.com/ |
|
6
|
|
|
* @copyright Copyright (c) 2019 Enupal LLC |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace enupal\socializer\elements; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\Element; |
|
13
|
|
|
use craft\behaviors\FieldLayoutBehavior; |
|
14
|
|
|
use craft\elements\db\ElementQueryInterface; |
|
15
|
|
|
|
|
16
|
|
|
use craft\elements\User; |
|
17
|
|
|
use enupal\socializer\records\Token as TokenRecord; |
|
18
|
|
|
use enupal\socializer\elements\db\TokensQuery; |
|
19
|
|
|
use yii\base\Model; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Token represents a token element. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class Token extends Element |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
public $userId; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
public $providerId; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
public $accessToken; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function canView(User $user): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function behaviors(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return array_merge(parent::behaviors(), [ |
|
56
|
|
|
'fieldLayout' => [ |
|
57
|
|
|
'class' => FieldLayoutBehavior::class, |
|
58
|
|
|
'elementType' => self::class |
|
59
|
|
|
], |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function init(): void |
|
64
|
|
|
{ |
|
65
|
|
|
parent::init(); |
|
66
|
|
|
|
|
67
|
|
|
$this->setScenario(Model::SCENARIO_DEFAULT); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->accessToken && is_string($this->accessToken)) { |
|
70
|
|
|
$this->accessToken = json_decode($this->accessToken, true); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the element type name. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function displayName(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return Craft::t('enupal-socializer','Socializer'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function refHandle(): ?string |
|
88
|
|
|
{ |
|
89
|
|
|
return 'socializer-providers'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function hasContent(): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function hasTitles(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function isLocalized(): bool |
|
112
|
|
|
{ |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritdoc |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function hasStatuses(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getFieldLayout(): ?\craft\models\FieldLayout |
|
128
|
|
|
{ |
|
129
|
|
|
$behaviors = $this->getBehaviors(); |
|
130
|
|
|
$fieldLayout = $behaviors['fieldLayout']; |
|
131
|
|
|
|
|
132
|
|
|
return $fieldLayout->getFieldLayout(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritdoc |
|
137
|
|
|
* |
|
138
|
|
|
* @return TokensQuery The newly created [[TokensQuery]] instance. |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function find(): ElementQueryInterface |
|
141
|
|
|
{ |
|
142
|
|
|
return new TokensQuery(get_called_class()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @inheritdoc |
|
147
|
|
|
*/ |
|
148
|
|
|
protected static function defineSearchableAttributes(): array |
|
149
|
|
|
{ |
|
150
|
|
|
return ['userId', 'providerId']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritdoc |
|
155
|
|
|
*/ |
|
156
|
|
|
public function datetimeAttributes(): array |
|
157
|
|
|
{ |
|
158
|
|
|
$attributes = parent::datetimeAttributes(); |
|
159
|
|
|
$attributes[] = 'dateCreated'; |
|
160
|
|
|
return $attributes; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
* @param bool $isNew |
|
166
|
|
|
* @throws \Exception |
|
167
|
|
|
*/ |
|
168
|
|
|
public function afterSave(bool $isNew): void |
|
169
|
|
|
{ |
|
170
|
|
|
$record = new TokenRecord(); |
|
171
|
|
|
|
|
172
|
|
|
if (!$isNew) { |
|
173
|
|
|
$record = TokenRecord::findOne($this->id); |
|
174
|
|
|
|
|
175
|
|
|
if (!$record) { |
|
|
|
|
|
|
176
|
|
|
throw new \Exception('Invalid Token ID: ' . $this->id); |
|
177
|
|
|
} |
|
178
|
|
|
} else { |
|
179
|
|
|
$record->id = $this->id; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$record->userId = $this->userId; |
|
183
|
|
|
$record->providerId = $this->providerId; |
|
184
|
|
|
$record->accessToken = is_array($this->accessToken) ? json_encode($this->accessToken) : $this->accessToken; |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
$record->save(false); |
|
187
|
|
|
|
|
188
|
|
|
parent::afterSave($isNew); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @inheritdoc |
|
193
|
|
|
*/ |
|
194
|
|
|
public function rules(): array |
|
195
|
|
|
{ |
|
196
|
|
|
$rules = []; |
|
197
|
|
|
$rules[] = [['userId', 'providerId'], 'required']; |
|
198
|
|
|
|
|
199
|
|
|
return $rules; |
|
200
|
|
|
} |
|
201
|
|
|
} |