Completed
Push — master ( 8e8284...16c896 )
by Nicolaas
02:59
created

googleMapBasicCenterForLink()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 0
1
<?php
2
3
class GoogleMapBasic_Controller extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $js_location = '';
0 ignored issues
show
Unused Code introduced by
The property $js_location is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
7
    private static $id_of_map_div = 'GoogleMapBasic';
0 ignored issues
show
Unused Code introduced by
The property $id_of_map_div is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
8
9
    private static $api_key = '';
0 ignored issues
show
Unused Code introduced by
The property $api_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    public function HasGoogleMap()
12
    {
13
        return $this->owner->ShowMap && $this->owner->Address;
14
    }
15
16
    public function GoogleMapBasic()
17
    {
18
        if ($this->owner->HasGoogleMap()) {
19
            if ($this->owner->StaticMap) {
20
                return true;
21
            } else {
22
                $fileLocation = Config::inst()->get("GoogleMapBasic_Controller", "js_location");
23
                $idOfMapDiv = Config::inst()->get("GoogleMapBasic_Controller", "id_of_map_div");
24
                $apiKey = Config::inst()->get("GoogleMapBasic_Controller", "api_key");
25
                if (! $fileLocation) {
26
                    $fileLocation = 'googlemapbasic/javascript/GoogleMapBasic.js';
27
                }
28
                Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
29
                Requirements::javascript(Director::protocol() . 'maps.googleapis.com/maps/api/js?key='.$apiKey .'');
30
                Requirements::javascript($fileLocation);
31
                $infoWindow = '<div class="infoWindowContent typography">'.$this->owner->InfoWindowContent.$this->GoogleMapBasicExternalLinkHTML().'</div>';
32
                Requirements::customScript(
33
                    "
34
                    if(typeof GoogleMapBasicOptions === 'undefined') {
35
                        var GoogleMapBasicOptions = new Array();
36
                    }
37
                    GoogleMapBasicOptions.push(
38
                        {
39
                            idOfMapDiv: \"".$this->cleanJS($idOfMapDiv)."\",
40
                            infoWindowContent: \"".$this->cleanJS($infoWindow)."\",
41
                            title: \"".$this->cleanJS($this->owner->Title)."\",
42
                            address: \"".$this->cleanJS($this->owner->Address)."\",
43
                            lat: ".floatval($this->owner->Lat).",
44
                            lng: ".floatval($this->owner->Lng).",
45
                            zoomLevel: ".intval($this->owner->ZoomLevel)."
46
                        }
47
                    );
48
                    ",
49
                    'GoogleMapBasicData'
50
                );
51
                Requirements::themedCSS('GoogleMapBasic', "googlemapbasic");
52
                return _t("GoolgeMapBasic.MAPLOADING", "map loading...");
53
            }
54
        }
55
        return false;
56
    }
57
58
    public function GoogleMapBasicStaticMapSource($width = 512, $height = 512)
59
    {
60
        $center = $this->googleMapBasicCenterForLink();
61
        $src = Director::protocol() . 'maps.googleapis.com/maps/api/staticmap?';
62
        $src .= 'center='.$center;
63
        $src .= '&zoom='.$this->owner->ZoomLevel;
64
        $src .= '&size='.$width.'x'.$height.'';
65
        $src .= '&maptype=roadmap';
66
        $src .= '&markers=color:red%7C'.$center;
67
68
        return $src;
69
    }
70
71
72
    public function GoogleMapBasicExternalLink()
73
    {
74
        if ($this->owner->HasGoogleMap()) {
75
            $center = $this->googleMapBasicCenterForLink();
76
            return Director::protocol() . 'maps.google.com/maps?q='.$center.'&z='.$this->owner->ZoomLevel;
77
        }
78
    }
79
80
    public function GoogleMapBasicExternalLinkHTML()
81
    {
82
        if ($this->owner->HasGoogleMap()) {
83
            return '<p id="GoogleMapBasicExternalLink"><a href="'.$this->GoogleMapBasicExternalLink().'" target="_map">'._t("GoogleMapBasic.OPENINGOOGLEMAPS", "open in Google Maps").'</a></p>';
84
        }
85
    }
86
87
    protected function googleMapBasicCenterForLink()
88
    {
89
        if ($this->owner->Lat && $this->owner->Lng) {
90
            $center = $this->owner->Lat.','.$this->owner->Lng;
91
        } elseif($this->owner->Address) {
92
            $center = urlencode($this->owner->Address);
93
        } else {
94
            $center = '';
95
        }
96
97
        return $center;
98
    }
99
100
    protected function cleanJS($s)
101
    {
102
        $s = Convert::raw2js($s);
103
        $s = str_replace("\r\n", " ", $s);
104
        $s = str_replace("\n", " ", $s);
105
        $s = str_replace('/', '\/', $s);
106
        return $s;
107
    }
108
109
110
}
111