1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smindel\GIS\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Configurable; |
6
|
|
|
use SilverStripe\Forms\GridField\GridField; |
7
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField_DataManipulator; |
9
|
|
|
use SilverStripe\View\Requirements; |
10
|
|
|
use SilverStripe\Core\Injector\Injectable; |
11
|
|
|
use SilverStripe\Core\Config\Config; |
12
|
|
|
use SilverStripe\ORM\SS_List; |
13
|
|
|
use Smindel\GIS\GIS; |
14
|
|
|
use proj4php\Proj4php; |
15
|
|
|
use proj4php\Proj; |
16
|
|
|
use proj4php\Point; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* GridFieldPaginator paginates the {@link GridField} list and adds controls |
20
|
|
|
* to the bottom of the {@link GridField}. |
21
|
|
|
*/ |
22
|
|
|
class GridFieldMap implements GridField_HTMLProvider, GridField_DataManipulator |
23
|
|
|
{ |
24
|
|
|
use Injectable; |
25
|
|
|
|
26
|
|
|
use Configurable; |
27
|
|
|
|
28
|
|
|
protected $attribute; |
29
|
|
|
|
30
|
1 |
|
public function __construct($attribute = null) |
31
|
|
|
{ |
32
|
1 |
|
$this->attribute = $attribute; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @param GridField $gridField |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
1 |
|
public function getHTMLFragments($gridField) |
41
|
|
|
{ |
42
|
1 |
|
$srid = GIS::config()->default_srid; |
43
|
1 |
|
$proj = GIS::config()->projections[$srid]; |
44
|
|
|
|
45
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.js'); |
46
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.markercluster.js'); |
47
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet-search.js'); |
48
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/proj4.js'); |
49
|
1 |
|
Requirements::customScript(sprintf('proj4.defs("EPSG:%s", "%s");', $srid, $proj), 'EPSG:' . $srid); |
50
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/GridFieldMap.js'); |
51
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.css'); |
52
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/MarkerCluster.css'); |
53
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/MarkerCluster.Default.css'); |
54
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet-search.css'); |
55
|
|
|
|
56
|
1 |
|
$defaultLocation = Config::inst()->get(MapField::class, 'default_location'); |
57
|
|
|
|
58
|
|
|
return array( |
59
|
1 |
|
'before' => sprintf( |
60
|
1 |
|
'<div class="grid-field-map" data-map-center="%s" data-list="%s" style="z-index:0;"></div>', |
61
|
1 |
|
GIS::create([$defaultLocation['lon'], $defaultLocation['lat']]), |
62
|
1 |
|
htmlentities( |
63
|
1 |
|
self::get_geojson_from_list( |
64
|
1 |
|
$gridField->getList(), |
65
|
1 |
|
$this->attribute ?: GIS::of($gridField->getList()->dataClass()) |
|
|
|
|
66
|
|
|
), |
67
|
1 |
|
ENT_QUOTES, |
68
|
1 |
|
'UTF-8' |
69
|
|
|
) |
70
|
|
|
), |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public static function get_geojson_from_list($list, $geometryField = null) |
75
|
|
|
{ |
76
|
1 |
|
$modelClass = $list->dataClass(); |
77
|
|
|
|
78
|
1 |
|
$geometryField = $geometryField ?: GIS::of($modelClass); |
79
|
|
|
|
80
|
1 |
|
if (($srid = GIS::config()->default_srid) != 4326) { |
81
|
|
|
$projDef = GIS::config()->projections[$srid]; |
82
|
|
|
$proj4 = new Proj4php(); |
83
|
|
|
$proj4->addDef('EPSG:' . $srid, $projDef); |
84
|
|
|
$proj = new Proj('EPSG:' . $srid, $proj4); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$collection = []; |
88
|
|
|
|
89
|
1 |
|
foreach ($list as $item) { |
90
|
1 |
|
if (!$item->canView()) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$geo = GIS::create($item->$geometryField)->reproject(4326); |
95
|
|
|
|
96
|
1 |
|
$collection[$item->ID] = [ |
97
|
1 |
|
$item->Title, |
98
|
1 |
|
$geo->type, |
99
|
1 |
|
$geo->coordinates, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return json_encode($collection); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Manipulate the {@link DataList} as needed by this grid modifier. |
108
|
|
|
* |
109
|
|
|
* @param GridField $gridField |
110
|
|
|
* @param SS_List $dataList |
111
|
|
|
* @return \SilverStripe\ORM\DataList |
112
|
|
|
*/ |
113
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) |
114
|
|
|
{ |
115
|
|
|
return $dataList; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|