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