|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/domains/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/domains/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\domains\actions; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use craft\base\FieldInterface; |
|
14
|
|
|
use flipbox\domains\records\Domain; |
|
15
|
|
|
use flipbox\ember\actions\model\traits\Manage; |
|
16
|
|
|
use yii\web\HttpException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class Action extends \yii\base\Action |
|
23
|
|
|
{ |
|
24
|
|
|
use Manage; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $field |
|
28
|
|
|
* @param string $source |
|
29
|
|
|
* @param string $target |
|
30
|
|
|
* @param int|null $siteId |
|
31
|
|
|
* @param int|null $sortOrder |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
* @throws HttpException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function run( |
|
36
|
|
|
string $field, |
|
37
|
|
|
string $source, |
|
38
|
|
|
string $target, |
|
39
|
|
|
int $siteId = null, |
|
40
|
|
|
int $sortOrder = null |
|
41
|
|
|
) { |
|
42
|
|
|
// Resolve Field |
|
43
|
|
|
if (null === ($domainField = Craft::$app->getFields()->getFieldById($field))) { |
|
44
|
|
|
return $this->handleInvalidFieldResponse($field); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Resolve Source |
|
48
|
|
|
if (null === ($sourceElement = Craft::$app->getElements()->getElementById($source))) { |
|
49
|
|
|
return $this->handleInvalidElementResponse($source); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Resolve Target |
|
53
|
|
|
if (null === ($targetElement = Craft::$app->getElements()->getElementById($target))) { |
|
54
|
|
|
return $this->handleInvalidElementResponse($target); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Resolve Site Id |
|
58
|
|
|
if (null === $siteId) { |
|
59
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$model = new Domain([ |
|
63
|
|
|
'fieldId' => $domainField->id, |
|
64
|
|
|
'sourceId' => $sourceElement->getId(), |
|
65
|
|
|
'targetId' => $targetElement->getId(), |
|
66
|
|
|
'siteId' => $siteId, |
|
67
|
|
|
'sortOrder' => $sortOrder |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
return $this->runInternal($model); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param int $fieldId |
|
75
|
|
|
* @throws HttpException |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function handleInvalidFieldResponse(int $fieldId) |
|
78
|
|
|
{ |
|
79
|
|
|
throw new HttpException(sprintf( |
|
80
|
|
|
"The provided field '%s' must be an instance of '%s'", |
|
81
|
|
|
(string)$fieldId, |
|
82
|
|
|
(string)FieldInterface::class |
|
83
|
|
|
)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param int $elementId |
|
88
|
|
|
* @throws HttpException |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function handleInvalidElementResponse(int $elementId) |
|
91
|
|
|
{ |
|
92
|
|
|
throw new HttpException(sprintf( |
|
93
|
|
|
"The provided source '%s' must be an instance of '%s'", |
|
94
|
|
|
(string)$elementId, |
|
95
|
|
|
(string)ElementInterface::class |
|
96
|
|
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|