-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSimpleMap.php
More file actions
256 lines (216 loc) · 5.65 KB
/
Copy pathSimpleMap.php
File metadata and controls
256 lines (216 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* SimpleMap for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/
namespace ether\simplemap;
use Craft;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterGqlQueriesEvent;
use craft\events\RegisterGqlTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\services\Fields;
use craft\services\Gql;
use craft\web\Application;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use ether\simplemap\fields\MapField as MapField;
use ether\simplemap\integrations\craftql\GetCraftQLSchema;
use ether\simplemap\integrations\feedme\FeedMeMaps;
use ether\simplemap\integrations\graphql\MapPartsType;
use ether\simplemap\integrations\graphql\MapType;
use ether\simplemap\models\Settings;
use ether\simplemap\services\EmbedService;
use ether\simplemap\services\GeoLocationService;
use ether\simplemap\services\MapService;
use ether\simplemap\services\StaticService;
use ether\simplemap\web\Variable;
use yii\base\Event;
/**
* Class SimpleMap
*
* @author Ether Creative
* @package ether\simplemap
* @property MapService $map
* @property StaticService $static
* @property EmbedService $embed
* @property GeoLocationService $geolocation
*/
class SimpleMap extends Plugin
{
const EDITION_LITE = 'lite';
const EDITION_PRO = 'pro';
// Properties
// =========================================================================
public $hasCpSettings = true;
// Static
// =========================================================================
public static function editions (): array
{
return [
self::EDITION_LITE,
self::EDITION_PRO,
];
}
// Craft
// =========================================================================
public function init ()
{
parent::init();
Craft::setAlias(
'simplemap',
__DIR__
);
Craft::setAlias(
'simplemapimages',
__DIR__ . '/web/assets/imgs'
);
$this->setComponents([
'map' => MapService::class,
'static' => StaticService::class,
'embed' => EmbedService::class,
'geolocation' => GeoLocationService::class,
]);
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
[$this, 'onRegisterCPUrlRules']
);
Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
[$this, 'onRegisterFieldTypes']
);
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
[$this, 'onRegisterVariable']
);
if (class_exists(Gql::class))
{
Event::on(
Gql::class,
Gql::EVENT_REGISTER_GQL_TYPES,
[$this, 'onRegisterGqlTypes']
);
}
if (class_exists(\markhuot\CraftQL\CraftQL::class))
{
Event::on(
MapField::class,
'craftQlGetFieldSchema',
[new GetCraftQLSchema, 'handle']
);
}
if (class_exists(\craft\feedme\Plugin::class))
{
Event::on(
\craft\feedme\services\Fields::class,
\craft\feedme\services\Fields::EVENT_REGISTER_FEED_ME_FIELDS,
[$this, 'onRegisterFeedMeFields']
);
}
$request = Craft::$app->getRequest();
if (
!$request->getIsConsoleRequest()
&& $request->getMethod() === 'GET'
&& $request->getIsSiteRequest()
&& !$request->getIsPreview()
&& !$request->getIsActionRequest()
) {
Event::on(
Application::class,
Application::EVENT_INIT,
[$this, 'onApplicationInit']
);
}
}
protected function beforeUninstall (): bool
{
if ($this->getSettings()->geoLocationService === GeoLocationService::MaxMindLite)
GeoLocationService::purgeDb();
return parent::beforeUninstall();
}
// Settings
// =========================================================================
protected function createSettingsModel ()
{
return new Settings();
}
/**
* @return bool|\craft\base\Model|Settings
*/
public function getSettings ()
{
return parent::getSettings();
}
protected function settingsHtml ()
{
// Redirect to our settings page
Craft::$app->controller->redirect(
UrlHelper::cpUrl('maps/settings')
);
}
public function afterSaveSettings ()
{
parent::afterSaveSettings();
$service = $this->getSettings()->geoLocationService;
if ($service !== GeoLocationService::MaxMindLite)
GeoLocationService::purgeDb();
else if (!GeoLocationService::dbExists())
GeoLocationService::dbQueueDownload();
}
// Events
// =========================================================================
public function onRegisterCPUrlRules (RegisterUrlRulesEvent $event)
{
$event->rules['maps/settings'] = 'simplemap/settings';
}
public function onRegisterFieldTypes (RegisterComponentTypesEvent $event)
{
$event->types[] = MapField::class;
}
/**
* @param Event $event
*
* @throws \yii\base\InvalidConfigException
*/
public function onRegisterVariable (Event $event)
{
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('simpleMap', Variable::class);
$variable->set('maps', Variable::class);
}
public function onRegisterFeedMeFields (\craft\feedme\events\RegisterFeedMeFieldsEvent $event)
{
$event->fields[] = FeedMeMaps::class;
}
public function onRegisterGqlTypes (RegisterGqlTypesEvent $event)
{
$event->types[] = MapType::class;
$event->types[] = MapPartsType::class;
}
/**
* @throws \Exception
*/
public function onApplicationInit ()
{
if ($this->getSettings()->geoLocationAutoRedirect)
$this->geolocation->redirect();
}
// Helpers
// =========================================================================
public static function t ($message, $params = [])
{
return Craft::t('simplemap', $message, $params);
}
public static function v ($version, $operator = '=')
{
return SimpleMap::getInstance()->is($version, $operator);
}
}