GoogleMapsField   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 2
cbo 5
dl 0
loc 106
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A field() 0 11 1
A getHiddenFields() 0 8 2
A getExtraFields() 0 4 1
A setExtraFields() 0 4 1
A addExtraField() 0 4 1
A getCustomOptions() 0 4 1
A setCustomOptions() 0 4 1
1
<?php
2
3
namespace Firesphere\GoogleMapsField\Forms;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\SiteConfig\SiteConfig;
10
use SilverStripe\View\Requirements;
11
12
/**
13
 * Class GoogleMapsField
14
 * @package Firesphere\GoogleMapsField\Forms
15
 */
16
class GoogleMapsField extends TextField
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $extraFields = [
22
        'GoogleMapsLatField',
23
        'GoogleMapsLngField',
24
        'subpremise',
25
        'street_number',
26
        'route',
27
        'sublocality_level_1',
28
        'locality',
29
        'administrative_area_level_1',
30
        'country',
31
        'postal_code'
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    protected $customOptions = [];
38
39
40
    /**
41
     * GoogleMapsField constructor.
42
     * @param $name
43
     * @param null $title
44
     * @param string $value
45
     * @param null $maxLength
46
     * @param Form|null $form
47
     */
48
    public function __construct($name, $title = null, $value = '', $maxLength = null, Form $form = null)
49
    {
50
        $this->setAttribute('data-mapsfield', 'mapsfield');
51
        parent::__construct($name, $title, $value, $maxLength, $form);
52
    }
53
54
    /**
55
     * @param array $properties
56
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
57
     */
58
    public function field($properties = [])
59
    {
60
        $config = SiteConfig::current_site_config();
61
62
        Requirements::javascript(
63
            'https://maps.googleapis.com/maps/api/js?key=' . $config->MapsBrowserKey . '&libraries=places'
64
        );
65
        Requirements::javascript('firesphere/googlemapsfield:client/dist/main.js');
66
67
        return parent::Field($properties);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getHiddenFields()
74
    {
75
        $output = '';
76
        foreach ($this->extraFields as $field) {
77
            $output .= HiddenField::create($field)->Field();
78
        }
79
        return $output;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getExtraFields()
86
    {
87
        return $this->extraFields;
88
    }
89
90
    /**
91
     * @param array $extraFields
92
     */
93
    public function setExtraFields($extraFields)
94
    {
95
        $this->extraFields = $extraFields;
96
    }
97
98
    /**
99
     * @param string $field
100
     */
101
    public function addExtraField($field)
102
    {
103
        $this->extraFields[] = $field;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getCustomOptions()
110
    {
111
        return Convert::array2json($this->customOptions);
112
    }
113
114
    /**
115
     * @param array $customOptions
116
     */
117
    public function setCustomOptions($customOptions)
118
    {
119
        $this->customOptions = $customOptions;
120
    }
121
}
122