1 <?php
2
3 class YandexMapsHomepageWidget extends HomepageWidget {
4
5 static $db = array(
6 'MapType' => 'Int',
7 'MapAddress' => 'Varchar(255)',
8 'MapBlock' => 'HTMLText',
9 'Link' => 'Varchar(255)',
10 'OpenInNewWindow' => 'Boolean',
11 'Longitude' => 'Decimal(12,8)',
12 'Latitude' => 'Decimal(12,8)',
13 );
14
15 function getCMSFields() {
16 $fields = parent::getCMSFields();
17
18 $fields->addFieldToTab('Root.Main', new HeaderField('YandexMapHeader', _t('YandexMapsHomepageWidget.MapHeader', 'Карта яндекса')), 'Longitude');
19
20 $coordsGroup = new FieldGroup();
21
22 $coordsGroup->push($fields->dataFieldByName('Longitude'));
23 $coordsGroup->push($fields->dataFieldByName('Latitude'));
24
25 $coordsGroup->push(new LiteralField('YandexMap', sprintf('<a href="#" class="popuplink" id="map_object_ymap_link">%s</a>', _t('YandexMapsHomepageWidget.MapLink', 'Указать объект на Яндекс.Карте'))));
26 $coordsGroup->push(new LiteralField('MapPageType', '<div state="not_loaded" id="map_object_ymap"></div>'));
27
28 $mapTypesTitle = array();
29 $mapTypesTitle[0] = _t('YandexMapsHomepageWidget.MapByAddress', 'By Address');
30 $mapTypesTitle[1] = _t('YandexMapsHomepageWidget.MapByCoordinates', 'By Coordinates');
31 $mapTypesTitle[2] = _t('YandexMapsHomepageWidget.MapByConstructor', 'By Constructor');
32
33 $fields->removeByName('MapType');
34 $fields->removeByName('Longitude');
35 $fields->removeByName('Latitude');
36 $fields->removeByName('MapAddress');
37 $fields->removeByName('MapBlock');
38
39 $types = array(
40 "0//{$mapTypesTitle[0]}" => new TextField('MapAddress', $this->fieldLabel('MapAddress')),
41 "1//{$mapTypesTitle[1]}" => $coordsGroup,
42 "2//{$mapTypesTitle[2]}" => new TextAreaField('MapBlock', $this->fieldLabel('MapBlock')),
43 );
44
45 if (!SiteConfig::current_site_config()->isNeedMapApiKey()) {
46 unset($types["0//{$mapTypesTitle[0]}"]);
47 unset($types["1//{$mapTypesTitle[1]}"]);
48 }
49 $fields->addFieldToTab("Root.Main",
50 new SelectionGroup("MapType", $types)
51 );
52
53
54 SiteConfig::current_site_config()->RequireMapApi('https://api-maps.yandex.ru/2.0.48/?load=package.full&lang=ru-RU&apikey=');
55 Requirements::css('widgets/css/map_objects_map.css');
56 Requirements::javascript('widgets/javascript/map_objects.js');
57
58 return $fields;
59 }
60
61 function hasContent() {
62 switch($this->MapType) {
63 case 0:
64 return ($this->MapAddress ? true : false);
65 case 1:
66 return (($this->Longitude && $this->Latitude) ? true : false);
67 case 2:
68 return ($this->MapBlock ? true : false);
69 }
70 return false;
71 }
72
73 function ByAddress() {
74 return ($this->MapType == 0);
75 }
76
77 function ByCoordinates() {
78 return ($this->MapType == 1);
79 }
80
81 function ByConstructor() {
82 return ($this->MapType == 2);
83 }
84 }
85
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-