|
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; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\Plugin; |
|
13
|
|
|
use craft\events\RegisterComponentTypesEvent; |
|
14
|
|
|
use craft\events\RegisterTemplateRootsEvent; |
|
15
|
|
|
use craft\events\RegisterUrlRulesEvent; |
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
17
|
|
|
use craft\services\Fields; |
|
18
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
19
|
|
|
use craft\web\UrlManager; |
|
20
|
|
|
use craft\web\View; |
|
21
|
|
|
use flipbox\craft\ember\modules\LoggerTrait; |
|
22
|
|
|
use flipbox\craft\hubspot\fields\Companies; |
|
23
|
|
|
use flipbox\craft\hubspot\fields\ContactLists; |
|
24
|
|
|
use flipbox\craft\hubspot\fields\Contacts; |
|
25
|
|
|
use flipbox\craft\hubspot\models\Settings as SettingsModel; |
|
26
|
|
|
use flipbox\craft\hubspot\records\ObjectAssociation; |
|
27
|
|
|
use flipbox\craft\hubspot\services\Visitor; |
|
28
|
|
|
use flipbox\craft\hubspot\web\twig\variables\HubSpot as HubSpotVariable; |
|
29
|
|
|
use flipbox\craft\psr3\Logger; |
|
30
|
|
|
use yii\base\Event; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @author Flipbox Factory <[email protected]> |
|
34
|
|
|
* @since 1.0.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @method SettingsModel getSettings() |
|
37
|
|
|
* |
|
38
|
|
|
* @property services\Cache $cache |
|
39
|
|
|
* @property services\Connections $connections |
|
40
|
|
|
* @property Logger $psr3Logger |
|
41
|
|
|
*/ |
|
42
|
|
|
class HubSpot extends Plugin |
|
43
|
|
|
{ |
|
44
|
|
|
use LoggerTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static $category = 'hubspot'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
9 |
|
public function init() |
|
55
|
|
|
{ |
|
56
|
9 |
|
parent::init(); |
|
57
|
|
|
|
|
58
|
|
|
// Components |
|
59
|
9 |
|
$this->setComponents([ |
|
60
|
9 |
|
'cache' => services\Cache::class, |
|
61
|
|
|
'connections' => services\Connections::class, |
|
62
|
3 |
|
'psr3Logger' => function () { |
|
63
|
9 |
|
return Craft::createObject([ |
|
64
|
9 |
|
'class' => Logger::class, |
|
65
|
9 |
|
'logger' => static::getLogger(), |
|
|
|
|
|
|
66
|
9 |
|
'category' => static::$category |
|
67
|
|
|
]); |
|
68
|
9 |
|
}, |
|
69
|
|
|
'visitor' => services\Visitor::class |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
// Modules |
|
73
|
9 |
|
$this->setModules([ |
|
74
|
9 |
|
'cp' => cp\Cp::class |
|
75
|
|
|
|
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
9 |
|
\Flipbox\HubSpot\HubSpot::setLogger( |
|
79
|
9 |
|
$this->getPsrLogger() |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
// Template variables |
|
83
|
9 |
|
Event::on( |
|
84
|
9 |
|
CraftVariable::class, |
|
85
|
9 |
|
CraftVariable::EVENT_INIT, |
|
86
|
3 |
|
function (Event $event) { |
|
87
|
|
|
/** @var CraftVariable $variable */ |
|
88
|
|
|
$variable = $event->sender; |
|
89
|
|
|
$variable->set('hubspot', HubSpotVariable::class); |
|
90
|
9 |
|
} |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
// Integration template directory |
|
94
|
9 |
|
Event::on( |
|
95
|
9 |
|
View::class, |
|
96
|
9 |
|
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
|
97
|
3 |
|
function (RegisterTemplateRootsEvent $e) { |
|
98
|
|
|
$e->roots['flipbox/integration'] = Craft::$app->getPath()->getVendorPath() . |
|
99
|
|
|
'/flipboxfactory/craft-integration/src/templates'; |
|
100
|
9 |
|
} |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
// Register our field types |
|
104
|
9 |
|
Event::on( |
|
105
|
9 |
|
Fields::class, |
|
106
|
9 |
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
|
107
|
3 |
|
function (RegisterComponentTypesEvent $event) { |
|
108
|
|
|
$event->types[] = Companies::class; |
|
109
|
|
|
$event->types[] = ContactLists::class; |
|
110
|
|
|
$event->types[] = Contacts::class; |
|
111
|
9 |
|
} |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
// CP routes |
|
115
|
9 |
|
Event::on( |
|
116
|
9 |
|
UrlManager::class, |
|
117
|
9 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
118
|
9 |
|
[self::class, 'onRegisterCpUrlRules'] |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
// Make sure we have an objects table |
|
122
|
9 |
|
if ($this->isInstalled) { |
|
123
|
|
|
ObjectAssociation::ensureEnvironmentTableExists(); |
|
124
|
|
|
} |
|
125
|
9 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getCpNavItem() |
|
131
|
|
|
{ |
|
132
|
|
|
return array_merge( |
|
133
|
|
|
parent::getCpNavItem(), |
|
134
|
|
|
[ |
|
135
|
|
|
'subnav' => [ |
|
136
|
|
|
'hubspot.visitors' => [ |
|
137
|
|
|
'label' => static::t('Visitors'), |
|
138
|
|
|
'url' => 'hubspot/visitors', |
|
139
|
|
|
], |
|
140
|
|
|
'hubspot.limits' => [ |
|
141
|
|
|
'label' => static::t('Limits'), |
|
142
|
|
|
'url' => 'hubspot/limits', |
|
143
|
|
|
], |
|
144
|
|
|
'hubspot.settings' => [ |
|
145
|
|
|
'label' => static::t('Settings'), |
|
146
|
|
|
'url' => 'hubspot/settings', |
|
147
|
|
|
] |
|
148
|
|
|
] |
|
149
|
|
|
] |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritdoc |
|
155
|
|
|
* @return SettingsModel |
|
156
|
|
|
*/ |
|
157
|
|
|
public function createSettingsModel() |
|
158
|
|
|
{ |
|
159
|
|
|
return new SettingsModel(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @inheritdoc |
|
164
|
|
|
*/ |
|
165
|
|
|
public function settingsHtml() |
|
166
|
|
|
{ |
|
167
|
|
|
Craft::$app->getResponse()->redirect( |
|
168
|
|
|
UrlHelper::cpUrl('hubspot/settings') |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
Craft::$app->end(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/******************************************* |
|
175
|
|
|
* SERVICES |
|
176
|
|
|
*******************************************/ |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
180
|
|
|
* @return services\Cache |
|
181
|
|
|
*/ |
|
182
|
3 |
|
public function getCache(): services\Cache |
|
183
|
|
|
{ |
|
184
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
185
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
186
|
3 |
|
return $this->get('cache'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
191
|
|
|
* @return services\Connections |
|
192
|
|
|
*/ |
|
193
|
3 |
|
public function getConnections(): services\Connections |
|
194
|
|
|
{ |
|
195
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
196
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
197
|
3 |
|
return $this->get('connections'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
202
|
|
|
* @return Logger |
|
203
|
|
|
*/ |
|
204
|
9 |
|
public function getPsrLogger(): Logger |
|
205
|
|
|
{ |
|
206
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
207
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
208
|
9 |
|
return $this->get('psr3Logger'); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
213
|
|
|
* @return Visitor |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getVisitor(): Visitor |
|
216
|
|
|
{ |
|
217
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
218
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
219
|
|
|
return $this->get('visitor'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/******************************************* |
|
223
|
|
|
* TRANSLATE |
|
224
|
|
|
*******************************************/ |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Translates a message to the specified language. |
|
228
|
|
|
* |
|
229
|
|
|
* This is a shortcut method of [[\Craft::t()]]. |
|
230
|
|
|
* * |
|
231
|
|
|
* @param string $message the message to be translated. |
|
232
|
|
|
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
|
233
|
|
|
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current |
|
234
|
|
|
* [[\yii\base\Application::language|application language]] will be used. |
|
235
|
|
|
* @return string the translated message. |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function t($message, $params = [], $language = null) |
|
238
|
|
|
{ |
|
239
|
|
|
return Craft::t('hubspot', $message, $params, $language); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/******************************************* |
|
244
|
|
|
* MODULES |
|
245
|
|
|
*******************************************/ |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
249
|
|
|
* @return cp\Cp |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getCp(): cp\Cp |
|
252
|
|
|
{ |
|
253
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
254
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
255
|
|
|
return $this->getModule('cp'); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/******************************************* |
|
260
|
|
|
* EVENTS |
|
261
|
|
|
*******************************************/ |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param RegisterUrlRulesEvent $event |
|
265
|
|
|
*/ |
|
266
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
|
267
|
|
|
{ |
|
268
|
|
|
$event->rules = array_merge( |
|
269
|
|
|
$event->rules, |
|
270
|
|
|
[ |
|
271
|
|
|
// ?? |
|
272
|
|
|
'hubspot' => 'hubspot/cp/settings/view/general/index', |
|
273
|
|
|
|
|
274
|
|
|
// LIMITS |
|
275
|
|
|
'hubspot/limits' => 'hubspot/cp/view/limits/index', |
|
276
|
|
|
|
|
277
|
|
|
// VISITORS |
|
278
|
|
|
'hubspot/visitors' => 'hubspot/cp/view/visitors/index', |
|
279
|
|
|
'hubspot/visitors/<identifier:\d+>' => 'hubspot/cp/view/visitors/detail', |
|
280
|
|
|
|
|
281
|
|
|
// OBJECTS: PAYLOAD |
|
282
|
|
|
'hubspot/objects/payloads/<field:\d+>/element/<element:\d+>' => 'hubspot/cp/view/object-payloads/index', |
|
283
|
|
|
|
|
284
|
|
|
// SETTINGS |
|
285
|
|
|
'hubspot/settings' => 'hubspot/cp/settings/view/general/index', |
|
286
|
|
|
|
|
287
|
|
|
// SETTINGS: CONNECTIONS |
|
288
|
|
|
'hubspot/settings/connections' => 'hubspot/cp/settings/view/connections/index', |
|
289
|
|
|
'hubspot/settings/connections/new' => 'hubspot/cp/settings/view/connections/upsert', |
|
290
|
|
|
'hubspot/settings/connections/<identifier:\d+>' => 'hubspot/cp/settings/view/connections/upsert', |
|
291
|
|
|
|
|
292
|
|
|
] |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
This method has been deprecated.