MappableData::getRenderableMap()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.7
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 4
1
<?php
2
3
/*
4
 * Provides a GoogleMap() function to ViewableData objects.
5
 *
6
 * @author Uncle Cheese
7
 * @package mappable
8
 */
9
class MappableData extends Extension
10
{
11
    /**
12
     * Optional template values for the map info windows.
13
     */
14
    private $MarkerTemplateValues = null;
15
16
    /**
17
     * URL of static maps api.
18
     *
19
     * @var string
20
     */
21
    private static $staticmap_api_url = '//maps.googleapis.com/maps/api/staticmap';
22
23
    /**
24
     * Default zoom for static map.
25
     *
26
     * @var int
27
     */
28
    private static $staticmap_default_zoom = 13;
29
30
    /**
31
     * Pass through values to the markers so that when rendering the map info windows, these
32
     * parameters are available to the template.  This is of course optional.
33
     *
34
     * @param array $values hash array of template key to template value
35
     */
36 1
    public function setMarkerTemplateValues($values)
37
    {
38 1
        $this->MarkerTemplateValues = $values;
39 1
    }
40
41 32
    public function getRenderableMap($width = null, $height = null, $zoom = 9)
42
    {
43 32
        $gmap = MapUtil::get_map(new ArrayList(array($this->owner)), $this->MarkerTemplateValues);
44 32
        $w = $width ? $width : MapUtil::$map_width;
45 32
        $h = $height ? $height : MapUtil::$map_height;
46 32
        $gmap->setSize($w, $h);
47 32
        $gmap->setZoom($zoom);
48 32
        $gmap->setEnableAutomaticCenterZoom(false);
49 32
        if ($this->owner->MapPinEdited) {
0 ignored issues
show
Bug introduced by
The property MapPinEdited does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50 4
            $gmap->setLatLongCenter(array(
51 4
                'lat' => $this->owner->getMappableLatitude(),
52 4
                'lng' => $this->owner->getMappableLongitude(),
53 4
            ));
54 4
        }
55
56 32
        return $gmap;
57
    }
58
59
    /**
60
     * returns an <img> with a src set to a static map picture.
61
     *
62
     * You can use MappableData.staticmap_api_url config var to set the domain of the static map.
63
     * You can use MappableData.staticmap_default_zoom config var to set the default zoom for the static map.
64
     *
65
     * @uses Mappable::getMappableMapPin() to draw a special marker, be sure this image is publicly available
66
     *
67
     * @param int $width
68
     * @param int $height
69
     *
70
     * @return string
71
     */
72 5
    public function StaticMap($width, $height, $zoom = null, $mapType = 'roadmap')
0 ignored issues
show
Coding Style introduced by
Method name "MappableData::StaticMap" is not in camel caps format
Loading history...
73
    {
74 5
        $lat = $this->owner->getMappableLatitude();
75 5
        $lng = $this->owner->getMappableLongitude();
76 5
        $pin = $this->owner->getMappableMapPin();
77
78
        // use provided zoom or set a default
79 5
        if ($zoom == null) {
80 3
            $zoom = Config::inst()->get('MappableData', 'staticmap_default_zoom');
81 3
        }
82
83
        //https://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&key=YOUR_API_KEY
84
        //maps.googleapis.com/maps/api/staticmap';
85
86 5
        $apiurl = Config::inst()->get('MappableData', 'staticmap_api_url');
87
88
        $urlparts = array(
89 5
            'center' => "$lat,$lng",
90 5
            'markers' => "$lat,$lng",
91 5
            'zoom' => $zoom,
92 5
            'size' => "{$width}x{$height}",
93 5
            'sensor' => 'false', //@todo: make sensor param configurable
94 5
            'maptype' => $mapType,
95 5
        );
96 5
        if ($pin) {
97 1
            $urlparts['markers'] = "icon:$pin|$lat,$lng";
98 1
        }
99
100 5
        $src = htmlentities($apiurl.'?'.http_build_query($urlparts));
101
102 5
        return '<img src="'.$src.'" width="'.$width.'" height="'.$height.'" alt="'.$this->owner->Title.'" />';
0 ignored issues
show
Bug introduced by
The property Title does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
103
    }
104
}
105