|
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\settings\view; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\UrlHelper; |
|
13
|
|
|
use flipbox\craft\hubspot\connections\SavableConnectionInterface; |
|
14
|
|
|
use flipbox\craft\hubspot\HubSpot; |
|
15
|
|
|
use flipbox\craft\hubspot\records\Connection; |
|
16
|
|
|
use yii\di\Instance; |
|
17
|
|
|
use yii\web\Response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Flipbox Factory <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConnectionsController extends AbstractController |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The template base path |
|
27
|
|
|
*/ |
|
28
|
|
|
const TEMPLATE_BASE = parent::TEMPLATE_BASE . '/connections'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The index view template path |
|
32
|
|
|
*/ |
|
33
|
|
|
const TEMPLATE_INDEX = self::TEMPLATE_BASE . '/index'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The index view template path |
|
37
|
|
|
*/ |
|
38
|
|
|
const TEMPLATE_UPSERT = self::TEMPLATE_BASE . '/upsert'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return Response |
|
42
|
|
|
* @throws \yii\base\InvalidConfigException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function actionIndex(): Response |
|
45
|
|
|
{ |
|
46
|
|
|
$variables = []; |
|
47
|
|
|
$this->baseVariables($variables); |
|
48
|
|
|
|
|
49
|
|
|
$variables['connections'] = Connection::find()->all(); |
|
50
|
|
|
$variables['types'] = $this->getAvailableConnections(); |
|
51
|
|
|
|
|
52
|
|
|
return $this->renderTemplate( |
|
53
|
|
|
static::TEMPLATE_INDEX, |
|
54
|
|
|
$variables |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param null $identifier |
|
60
|
|
|
* @param Connection|null $connection |
|
61
|
|
|
* @return Response |
|
62
|
|
|
* @throws \craft\errors\MissingComponentException |
|
63
|
|
|
* @throws \yii\base\InvalidConfigException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function actionUpsert($identifier = null, Connection $connection = null): Response |
|
66
|
|
|
{ |
|
67
|
|
|
if (null === $connection) { |
|
68
|
|
|
if (null !== $identifier) { |
|
69
|
|
|
$connection = Connection::findOne($identifier); |
|
70
|
|
|
} else { |
|
71
|
|
|
$connection = new Connection; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Bad connection |
|
76
|
|
|
if (null === $connection) { |
|
77
|
|
|
$message = HubSpot::t( |
|
78
|
|
|
"Connection '{$identifier}' is not configurable", |
|
79
|
|
|
[ |
|
80
|
|
|
'identifier' => $identifier |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
Craft::$app->getSession()->setError($message); |
|
84
|
|
|
$this->redirect($this->getBaseCpPath()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$variables = []; |
|
88
|
|
|
if ($connection === null || $connection->getIsNewRecord()) { |
|
|
|
|
|
|
89
|
|
|
$this->insertVariables($variables); |
|
90
|
|
|
} else { |
|
91
|
|
|
$this->updateVariables($variables, $connection); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$variables['types'] = $this->getAvailableConnections(); |
|
95
|
|
|
$variables['connection'] = $connection; |
|
96
|
|
|
$variables['fullPageForm'] = true; |
|
97
|
|
|
|
|
98
|
|
|
return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return array |
|
103
|
|
|
* @throws \yii\base\InvalidConfigException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getAvailableConnections() |
|
106
|
|
|
{ |
|
107
|
|
|
$classes = HubSpot::getInstance()->getCp()->getAvailableConnections(); |
|
108
|
|
|
|
|
109
|
|
|
$connections = []; |
|
110
|
|
|
foreach ($classes as $connection) { |
|
111
|
|
|
if (!$connection instanceof SavableConnectionInterface) { |
|
112
|
|
|
$connection = Instance::ensure($connection, SavableConnectionInterface::class); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$connections[get_class($connection)] = $connection; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $connections; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/******************************************* |
|
122
|
|
|
* BASE PATHS |
|
123
|
|
|
*******************************************/ |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getBaseCpPath(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return parent::getBaseCpPath() . '/connections'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function getBaseActionPath(): string |
|
137
|
|
|
{ |
|
138
|
|
|
return parent::getBaseActionPath() . '/connections'; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/******************************************* |
|
142
|
|
|
* UPDATE VARIABLES |
|
143
|
|
|
*******************************************/ |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param array $variables |
|
147
|
|
|
* @param Connection $connection |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function updateVariables(array &$variables, Connection $connection) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->baseVariables($variables); |
|
152
|
|
|
$variables['title'] .= ' - ' . HubSpot::t('Edit') . ' ' . $connection->handle; |
|
153
|
|
|
$variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $connection->getId()); |
|
154
|
|
|
$variables['crumbs'][] = [ |
|
155
|
|
|
'label' => $connection->handle, |
|
156
|
|
|
'url' => UrlHelper::url($variables['continueEditingUrl']) |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: