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