1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\hubspot\cp\controllers\view; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use craft\helpers\StringHelper; |
14
|
|
|
use flipbox\craft\hubspot\fields\Objects; |
15
|
|
|
use flipbox\craft\hubspot\HubSpot; |
16
|
|
|
use flipbox\craft\hubspot\helpers\TransformerHelper; |
17
|
|
|
use flipbox\craft\hubspot\transformers\CreateUpsertPayloadFromElement; |
18
|
|
|
use yii\web\HttpException; |
19
|
|
|
use yii\web\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class ObjectPayloadsController extends AbstractController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The template base path |
29
|
|
|
*/ |
30
|
|
|
const TEMPLATE_BASE = parent::TEMPLATE_BASE . '/objects'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The index view template path |
34
|
|
|
*/ |
35
|
|
|
const TEMPLATE_INDEX = self::TEMPLATE_BASE . '/payload'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $field |
39
|
|
|
* @param int $element |
40
|
|
|
* @return Response |
41
|
|
|
* @throws HttpException |
42
|
|
|
*/ |
43
|
|
|
public function actionIndex(int $field, int $element): Response |
44
|
|
|
{ |
45
|
|
|
$variables = []; |
46
|
|
|
$this->baseVariables($variables); |
47
|
|
|
|
48
|
|
|
/** @var Objects $field */ |
49
|
|
|
$field = Craft::$app->getFields()->getFieldById($field); |
50
|
|
|
if (!$field instanceof Objects) { |
51
|
|
|
throw new HttpException(401, "Invalid field."); |
52
|
|
|
} |
53
|
|
|
$variables['field'] = $field; |
54
|
|
|
|
55
|
|
|
$element = Craft::$app->getElements()->getElementById($element); |
56
|
|
|
if (!$element instanceof ElementInterface) { |
57
|
|
|
throw new HttpException(401, "Invalid element."); |
58
|
|
|
} |
59
|
|
|
$variables['element'] = $element; |
60
|
|
|
|
61
|
|
|
$variables['tabs'] = $this->getTabs(); |
62
|
|
|
|
63
|
|
|
$payloads = []; |
64
|
|
|
|
65
|
|
|
foreach ($this->getPayloadTypes() as $type) { |
66
|
|
|
$payloads[$type] = $this->getPayload( |
67
|
|
|
$field, |
68
|
|
|
$element, |
69
|
|
|
$type |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$variables['payloads'] = $payloads; |
74
|
|
|
|
75
|
|
|
return $this->renderTemplate( |
76
|
|
|
static::TEMPLATE_INDEX, |
77
|
|
|
$variables |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function getPayloadTypes(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
TransformerHelper::PAYLOAD_ACTION_SYNC, |
88
|
|
|
TransformerHelper::PAYLOAD_ACTION_SAVE |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Objects $field |
94
|
|
|
* @param ElementInterface $element |
95
|
|
|
* @param string $action |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function getPayload( |
99
|
|
|
Objects $field, |
100
|
|
|
ElementInterface $element, |
101
|
|
|
string $action |
102
|
|
|
): array { |
103
|
|
|
|
104
|
|
|
$transformer = [ |
105
|
|
|
'class' => CreateUpsertPayloadFromElement::class, |
106
|
|
|
'action' => $action |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
// Get callable used to create payload |
110
|
|
|
if (null === ($transformer = TransformerHelper::resolveTransformer($transformer))) { |
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Create payload |
115
|
|
|
$payload = call_user_func_array( |
116
|
|
|
$transformer, |
117
|
|
|
[ |
118
|
|
|
$element, |
119
|
|
|
$field |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $payload; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
private function getTabs(): array |
131
|
|
|
{ |
132
|
|
|
$return = []; |
133
|
|
|
|
134
|
|
|
foreach ($this->getPayloadTypes() as $type) { |
135
|
|
|
$return[$type] = [ |
136
|
|
|
'label' => HubSpot::t(StringHelper::toTitleCase($type)), |
137
|
|
|
'url' => '#' . $type |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/******************************************* |
145
|
|
|
* BASE PATHS |
146
|
|
|
*******************************************/ |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function getBaseCpPath(): string |
152
|
|
|
{ |
153
|
|
|
return parent::getBaseCpPath() . '/object-fields'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
protected function getBaseActionPath(): string |
160
|
|
|
{ |
161
|
|
|
return parent::getBaseActionPath() . '/object-fields'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/******************************************* |
166
|
|
|
* VARIABLES |
167
|
|
|
*******************************************/ |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
|
|
protected function baseVariables(array &$variables = []) |
173
|
|
|
{ |
174
|
|
|
parent::baseVariables($variables); |
175
|
|
|
|
176
|
|
|
$title = HubSpot::t("Object Payload"); |
177
|
|
|
$variables['title'] .= ' ' . $title; |
178
|
|
|
|
179
|
|
|
// Breadcrumbs |
180
|
|
|
$variables['crumbs'][] = [ |
181
|
|
|
'label' => $title, |
182
|
|
|
'url' => '' |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|