-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.php
129 lines (108 loc) · 4.95 KB
/
init.php
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
<?php
namespace Initbiz\LeafletPro;
use Event;
use System\Classes\PluginManager;
use RainLab\Location\Models\Country;
use Initbiz\LeafletPro\Models\Marker;
if (PluginManager::instance()->exists('Initbiz.CumulusCore')) {
Event::listen('backend.form.extendFieldsBefore', function ($formWidget) {
if (!$formWidget->model instanceof Marker) {
return;
}
$clusterField = [];
$clusterField['cluster_id'] = [
'label' => 'initbiz.leafletpro::lang.marker.cluster',
'commentAbove' => 'initbiz.leafletpro::lang.marker.cluster_comment',
'type' => 'dropdown',
'span' => 'full',
'emptyOption' => 'initbiz.leafletpro::lang.marker.cluster_empty_option',
'tab' => 'initbiz.leafletpro::lang.marker.address_tab',
];
$fields = $formWidget->tabs['fields'];
$fields['street']['dependsOn'] = 'cluster_id';
$fields['postal_code']['dependsOn'] = 'cluster_id';
$fields['city']['dependsOn'] = 'cluster_id';
$fields['country_id']['dependsOn'] = 'cluster_id';
$formWidget->tabs['fields'] = $clusterField + $fields;
});
Marker::extend(function ($model) {
$model->belongsTo['cluster'] = ['Initbiz\CumulusCore\Models\Cluster'];
$model->addDynamicMethod('getClusterIdOptions', function () {
return \Initbiz\CumulusCore\Models\Cluster::all()->pluck('name', 'id')->toArray();
});
});
\Initbiz\CumulusCore\Models\Cluster::extend(function ($model) {
$model->hasMany['markers'] = ['Initbiz\LeafletPro\Models\Marker'];
$model->bindEvent('model.afterSave', function () use ($model) {
if ($model->markers()->count() === 0) {
// If cluster has no marker, create one
if (!empty($model->city) && !empty($model->thoroughfare) && !empty($model->name)) {
$marker = new Marker();
$marker->name = $model->name;
$marker->cluster_id = $model->id;
$marker->street = $model->thoroughfare;
$marker->city = $model->city;
if (!empty($marker->country_id)) {
$country = Country::find($marker->country_id);
$marker->country()->associate($country);
}
if (!empty($marker->postal_code)) {
$marker->postal_code = $model->postal_code;
}
try {
$marker->refreshLatLon();
$marker->save();
} catch (\Exception $e) {
$message = [
'message' => 'Tried to refresh the marker',
'marker' => $marker->toArray(),
'exception' => $e->getMessage(),
];
trace_log($message);
}
}
} else {
// else get first and if address updated than update marker
if (!empty($model->city) && !empty($model->thoroughfare) && !empty($model->name)) {
$marker = $model->markers()->first();
$markerChanged = false;
if ($marker->city !== $model->city) {
$marker->city = $model->city;
$markerChanged = true;
}
if ($marker->street !== $model->thoroughfare) {
$marker->street = $model->thoroughfare;
$markerChanged = true;
}
if ($marker->country_id !== $model->country_id) {
$marker->country_id = $model->country_id;
$markerChanged = true;
}
if ($marker->postal_code !== $model->postal_code) {
$marker->postal_code = $model->postal_code;
$markerChanged = true;
}
if ($markerChanged) {
try {
$marker->refreshLatLon();
$marker->save();
} catch (\Exception $e) {
$message = [
'message' => 'Tried to refresh the marker',
'marker' => $marker->toArray(),
'exception' => $e->getMessage(),
];
trace_log($message);
}
}
}
}
}, 50);
});
\Initbiz\CumulusCore\Controllers\Clusters::extendFormFields(function ($form, $model, $context) {
if (!$model instanceof \Initbiz\CumulusCore\Models\Cluster) {
return;
}
//TODO: add relation controller for managing map markers
});
}