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\controllers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\ArrayHelper; |
13
|
|
|
use flipbox\craft\element\lists\transformers\RecordResponseTransformer; |
14
|
|
|
use flipbox\craft\ember\controllers\AbstractController; |
15
|
|
|
use flipbox\craft\ember\filters\CallableFilter; |
16
|
|
|
use flipbox\craft\ember\filters\FlashMessageFilter; |
17
|
|
|
use flipbox\craft\ember\filters\ModelErrorFilter; |
18
|
|
|
use flipbox\craft\ember\filters\RedirectFilter; |
19
|
|
|
use flipbox\craft\element\lists\actions\source\Associate; |
20
|
|
|
use flipbox\craft\element\lists\actions\source\Dissociate; |
21
|
|
|
use flipbox\craft\element\lists\ElementList; |
22
|
|
|
use yii\web\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Flipbox Factory <[email protected]> |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
*/ |
28
|
|
|
class SourceController extends AbstractController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return ArrayHelper::merge( |
36
|
|
|
parent::behaviors(), |
37
|
|
|
[ |
38
|
|
|
'redirect' => [ |
39
|
|
|
'class' => RedirectFilter::class, |
40
|
|
|
'only' => ['associate', 'dissociate'], |
41
|
|
|
'actions' => [ |
42
|
|
|
'associate' => [204], |
43
|
|
|
'dissociate' => [201], |
44
|
|
|
] |
45
|
|
|
], |
46
|
|
|
'transform' => [ |
47
|
|
|
'class' => CallableFilter::class, |
48
|
|
|
'formats' => [ |
49
|
|
|
'*' => Response::FORMAT_JSON |
50
|
|
|
], |
51
|
|
|
'actions' => [ |
52
|
|
|
'associate' => [ |
53
|
|
|
RecordResponseTransformer::class, |
54
|
|
|
'transform' |
55
|
|
|
], |
56
|
|
|
'dissociate' => [ |
57
|
|
|
RecordResponseTransformer::class, |
58
|
|
|
'transform' |
59
|
|
|
], |
60
|
|
|
] |
61
|
|
|
], |
62
|
|
|
'error' => [ |
63
|
|
|
'class' => ModelErrorFilter::class |
64
|
|
|
], |
65
|
|
|
'flash' => [ |
66
|
|
|
'class' => FlashMessageFilter::class, |
67
|
|
|
'actions' => [ |
68
|
|
|
'associate' => [ |
69
|
|
|
204 => ElementList::t("Element successfully associated."), |
70
|
|
|
400 => ElementList::t("Failed to associate element.") |
71
|
|
|
], |
72
|
|
|
'dissociate' => [ |
73
|
|
|
201 => ElementList::t("Element successfully dissociated."), |
74
|
|
|
400 => ElementList::t("Failed to dissociate element.") |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
] |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function verbs(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'associate' => ['post'], |
89
|
|
|
'dissociate' => ['post', 'delete'], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string|int|null $source |
95
|
|
|
* @param string|int|null $target |
96
|
|
|
* @param string|int|null $field |
97
|
|
|
* @param string|int|null $site |
98
|
|
|
* @return mixed |
99
|
|
|
* @throws \yii\base\InvalidConfigException |
100
|
|
|
*/ |
101
|
|
|
public function actionAssociate($source = null, $target = null, $field = null, $site = null) |
102
|
|
|
{ |
103
|
|
|
if (null === $source) { |
104
|
|
|
$source = Craft::$app->getRequest()->getBodyParam('source'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (null === $target) { |
108
|
|
|
$target = Craft::$app->getRequest()->getBodyParam('target'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (null === $field) { |
112
|
|
|
$field = Craft::$app->getRequest()->getBodyParam('field'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (null === $site) { |
116
|
|
|
$site = Craft::$app->getRequest()->getBodyParam('site'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @var Associate $action */ |
120
|
|
|
$action = Craft::createObject([ |
121
|
|
|
'class' => Associate::class, |
122
|
|
|
'checkAccess' => [$this, 'checkAssociateAccess'] |
123
|
|
|
], [ |
124
|
|
|
'associate', |
125
|
|
|
$this |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return $action->runWithParams([ |
129
|
|
|
'source' => $source, |
130
|
|
|
'target' => $target, |
131
|
|
|
'field' => $field, |
132
|
|
|
'siteId' => $site |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string|int|null $source |
138
|
|
|
* @param string|int|null $target |
139
|
|
|
* @param string|int|null $field |
140
|
|
|
* @param string|int|null $site |
141
|
|
|
* @return mixed |
142
|
|
|
* @throws \yii\base\InvalidConfigException |
143
|
|
|
*/ |
144
|
|
|
public function actionDissociate($source = null, $target = null, $field = null, $site = null) |
145
|
|
|
{ |
146
|
|
|
if (null === $source) { |
147
|
|
|
$source = Craft::$app->getRequest()->getBodyParam('source'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (null === $target) { |
151
|
|
|
$target = Craft::$app->getRequest()->getBodyParam('target'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (null === $field) { |
155
|
|
|
$field = Craft::$app->getRequest()->getBodyParam('field'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (null === $site) { |
159
|
|
|
$site = Craft::$app->getRequest()->getBodyParam('site'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** @var Dissociate $action */ |
163
|
|
|
$action = Craft::createObject([ |
164
|
|
|
'class' => Dissociate::class, |
165
|
|
|
'checkAccess' => [$this, 'checkDissociateAccess'] |
166
|
|
|
], [ |
167
|
|
|
'dissociate', |
168
|
|
|
$this |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
return $action->runWithParams([ |
172
|
|
|
'source' => $source, |
173
|
|
|
'target' => $target, |
174
|
|
|
'field' => $field, |
175
|
|
|
'siteId' => $site |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function checkAssociateAccess(): bool |
183
|
|
|
{ |
184
|
|
|
return $this->checkAdminAccess(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function checkDissociateAccess(): bool |
191
|
|
|
{ |
192
|
|
|
return $this->checkAdminAccess(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
protected function checkAdminAccess() |
199
|
|
|
{ |
200
|
|
|
$this->requireLogin(); |
201
|
|
|
return Craft::$app->getUser()->getIsAdmin(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|