addMapUsingRawLink()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * Adds functions to a Page_Controller
5
 * to action a map.
6
 *
7
 *
8
 */
9
10
class GoogleMapLocationsDOD_Controller extends Extension
11
{
12
13
14
15
16
17
    #####################
18
    # INITS
19
    #####################
20
21
    /**
22
     * @inherited
23
     */
24
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
25
        "AddressFinderForm",
26
        "doAddressFinderForm",
27
        "SearchByAddressForm",
28
        "loadmap"
29
    );
30
31
    /**
32
     * @var GoogleMap
33
     */
34
    protected $googleMap = null;
35
36
    /**
37
     * @var String
38
     */
39
    protected $googleMapAddress = "";
40
41
    /**
42
     * look for address
43
     *
44
     */
45
    public function onAfterInit()
46
    {
47
        if (!$this->googleMapAddress && isset($_REQUEST["address"])) {
48
            $this->googleMapAddress = urldecode($_REQUEST["address"]);
49
        }
50
        if ($this->googleMapAddress) {
51
            $this->MyGoogleMap()->setAddress($this->googleMapAddress);
52
        }
53
    }
54
55
56
    /**
57
     * initialise GoogleMap
58
     * @return GoogleMap
59
     */
60
    public function MyGoogleMap()
61
    {
62
        if (!$this->googleMap) {
63
            $this->googleMap = Injector::inst()->get("GoogleMap");
64
        }
65
        return $this->googleMap;
66
    }
67
68
69
70
71
72
73
74
    #####################
75
    # ACTIONS
76
    #####################
77
78
79
    /**
80
     * provides a link to any map you like.
81
     * e.g. mysite.com/mypage/mysub-page/loadmap/optionsHereURLEncoded/
82
     * optionsHereURLEncoded are basically the link to the map.
83
     * you can use this to link through to a page and provide a specific map
84
     *
85
     * @param HTTPRequest
86
     */
87
    public function loadmap($request)
88
    {
89
        $link = urldecode($request->param("ID"));
90
        $options = explode("/", $link);
91
        $title = $options[3];
92
        $this->owner->addMapUsingRawLink($title, $link);
93
        return array();
94
    }
95
96
    /**
97
     * returns encoded link for the loadmap function
98
     *
99
     * @param SiteTree $page
0 ignored issues
show
Documentation introduced by
Should the type for parameter $page not be SiteTree|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
100
     * @param String $action
101
     * @param String $title
102
     * @param Int $lng
103
     * @param Int $lat
104
     * @param String $filterCode
105
     *
106
     * @return String
107
     */
108
    public function LoadmapLink($page = null, $action = "", $title = "", $lng = 0, $lat = 0, $filterCode = "")
109
    {
110
        if (!$page) {
111
            $page = $this->owner->dataRecord;
112
        }
113
114
        //todo: why not Convert::raw2js
115
        return urlencode($this->getLinkForData($page->ID, $action, $title, $lng, $lat, $filterCode));
116
    }
117
118
119
120
121
122
123
124
    #####################
125
    # TEMPLATE METHODS
126
    #####################
127
128
129
    /**
130
     * @return GoogleMap
131
     */
132
    public function GoogleMapController()
133
    {
134
        $obj =  $this->MyGoogleMap()->loadGoogleMap();
135
        return $obj;
136
    }
137
138
    /**
139
     * @return Boolean
140
     */
141
    public function HasGoogleMap()
142
    {
143
        if ($this->MyGoogleMap() && $this->owner->classHasGoogleMap()) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $this->MyGoogleMa...r->classHasGoogleMap();.
Loading history...
144
            return true;
145
        } else {
146
            return false;
147
        }
148
    }
149
150
151
    /**
152
     * @param array (optional)  $classNamesSearchedFor e.g. StockistPage
153
     *
154
     * @return Form
155
     */
156
    public function AddressFinderForm($classNamesSearchedFor = array())
157
    {
158
        return $this->SearchByAddressForm($classNamesSearchedFor);
159
    }
160
161
    public function doAddressFinderForm($data, $form)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
162
    {
163
        return $this->redirect($this->owner->link('addresssearch').'?searchterm='.$data['searchterm']);
0 ignored issues
show
Bug introduced by
The method redirect() does not seem to exist on object<GoogleMapLocationsDOD_Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
    }
165
166
167
    /**
168
     * @param array (optional)  $classNamesSearchedFor e.g. StockistPage
169
     *
170
     * @return Form
171
     */
172
    public function SearchByAddressForm($classNamesSearchedFor = array())
173
    {
174
        return SearchByAddressForm::create(
175
            $this->owner,
176
            "SearchByAddressForm",
177
            $this->googleMapAddress,
178
            $classNamesSearchedFor
179
        );
180
    }
181
182
    /**
183
     * @param array (optional)  $classNamesSearchedFor e.g. StockistPage
184
     *
185
     * @return Form
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
186
     */
187
    public function AddAddressFinder()
188
    {
189
        return $this->MyGoogleMap()->AddAddressFinder();
190
    }
191
192
193
194
195
    #####################
196
    # CREATE MAPS
197
    #####################
198
199
200
    /**
201
     * add a layer to a Google Map
202
     *
203
     * @param String $action - see GoogleMapDataResponse::allowed_actions to get a list of actions
204
     * @param String $title
205
     * @param float $lng - default LATITUDE
0 ignored issues
show
Documentation introduced by
Should the type for parameter $lng not be integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
206
     * @param float $lat - default LONGITUDE
0 ignored issues
show
Documentation introduced by
Should the type for parameter $lat not be integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
207
     * @param String $filterCode - can be a SiteTree class name, e.g. "ProductPage"
208
     *                             filter depends on the type of action
209
     *
210
     */
211
    public function addMap($action = "", $title = "", $lng = 0, $lat = 0, $filterCode = "")
212
    {
213
        if (!$title) {
214
            $title = $this->owner->Title;
215
        }
216
        $allowedActions = Config::inst()->get("GoogleMapDataResponse", "allowed_actions");
217
        if (isset($allowedActions[$action]) || in_array($action, $allowedActions)) {
218
            $title = str_replace('/', '', $title);
219
            $linkForData = $this->getLinkForData($this->owner->ID, $action, $title, $lng, $lat, $filterCode);
220
            //where the magic happens...
221
            $this->MyGoogleMap()->addLayer($linkForData, $title);
222
        } else {
223
            user_error("Could not find $action action in GoogleMapDataResponse", E_USER_NOTICE);
224
        }
225
    }
226
227
    /**
228
     * add a layer to a Google Map
229
     *
230
     * @param String $action - see GoogleMapDataResponse::allowed_actions to get a list of actions
0 ignored issues
show
Bug introduced by
There is no parameter named $action. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
231
     * @param String $title
232
     * @param String $filterCode - can be a SiteTree class name, e.g. "ProductPage"
233
     *                         filter depends on the type of action
234
     *
235
     */
236
    public function addMapUsingRawLink($link = "", $title = "", $filterCode = "")
0 ignored issues
show
Unused Code introduced by
The parameter $filterCode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
237
    {
238
        if (!$title) {
239
            $title = $this->owner->Title;
240
        }
241
        $this->MyGoogleMap()->addLayer($link, $title);
242
    }
243
244
    /**
245
     * add an additional layer to an existing map
246
     *
247
     * @param String $action
248
     * @param String $title
249
     * @param Int $lng
250
     * @param Int $lat
251
     * @param String $filter
252
     */
253
    public function addExtraLayer($action = "", $title = "", $lng = 0, $lat = 0, $filter = "")
254
    {
255
        $linkForData = $this->getLinkForData($this->owner->ID, $action, $title, $lng, $lat, $filter);
256
        $this->owner->addExtraLayersUsingRawLink($title, $linkForData);
257
    }
258
259
    /**
260
     * Make up your own link and add this as a layer
261
     *
262
     * @param String $title
263
     * @param String $link
264
     */
265
    public function addExtraLayerUsingRawLink($title, $link)
266
    {
267
        $this->MyGoogleMap()->addExtraLayer($title, $link);
268
    }
269
270
271
    /**
272
     * add an address to the map
273
     *
274
     * @param String $address
275
     * @param Boolean $addShowAroundAdress
276
     * @param String $filter - usually a SiteTree ClassName (e.g. ProductPage)
277
     * @param array $params - params for the Google Server
278
     */
279
    public function addAddress($address, $addShowAroundAdress = false, $filter = "", $params = [])
280
    {
281
        if ($addShowAroundAdress) {
282
            $pointArray = GetLatLngFromGoogleUsingAddress::get_placemark_as_array($address, false, $params);
283
            if ($pointArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pointArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
284
                $title = $pointArray["FullAddress"];
285
                $lng = $pointArray["Longitude"];
286
                $lat = $pointArray["Latitude"];
287
                $this->owner->addMap("showaroundmexml", $title, $lng, $lat, $filter);
288
            }
289
        } else {
290
            $this->owner->MyGoogleMap();
291
            $this->googleMapAddress = $address;
292
        }
293
    }
294
295
296
297
    /**
298
     * @param DataList $pagesOrGoogleMapLocationsObjects
299
     * @param Boolean $retainOldSessionData
300
     * @param string $title
301
     * @param string $filterCode
302
     *
303
     * @param String $title
304
     */
305
    public function addCustomMap($pagesOrGoogleMapLocationsObjects, $retainOldSessionData = false, $title = '', $filterCode = "")
306
    {
307
        $isGoogleMapLocationsObject = $pagesOrGoogleMapLocationsObjects->DataClass() == "GoogleMapLocationsObject" ? true : false;
308
        if (!$filterCode) {
309
            $filterCode = ""
310
                .$this->owner->ID."_"
311
                .($this->owner->request->param("Action") ? $this->owner->request->param("Action") : "index")
312
                .($this->owner->request->param("ID") ? $this->owner->request->param("ID") : 0);
313
        }
314
        if ($pagesOrGoogleMapLocationsObjects) {
315
            if (!$retainOldSessionData) {
316
                $addCustomGoogleMapArray = array();
317
                $this->owner->clearCustomMaps($filterCode);
318
            } else {
319
                $addCustomGoogleMapArray = GoogleMapDataResponse::get_custom_google_map_session_data($filterCode);
320
            }
321
            foreach ($pagesOrGoogleMapLocationsObjects as $obj) {
322
                if (!$obj->ID) {
323
                    user_error("Page provided to addCustomMap that does not have an ID", E_USER_ERROR);
324
                }
325
                $addCustomGoogleMapArray[$obj->ID] = $obj->ID;
326
            }
327
        }
328
        GoogleMapDataResponse::set_custom_google_map_session_data($addCustomGoogleMapArray, $filterCode);
0 ignored issues
show
Bug introduced by
The variable $addCustomGoogleMapArray does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
329
        Session::save();
330
        if ($isGoogleMapLocationsObject) {
331
            $fn = "showcustomdosmapxml";
332
        } else {
333
            $fn = "showcustompagesmapxml";
334
        }
335
        $this->owner->addMap($fn, $title, $lng = 0, $lat = 0, $filterCode);
336
    }
337
338
339
340
341
342
343
344
345
346
    #####################
347
    # MAP SETTINGS
348
    #####################
349
350
351
    /**
352
     * @param String $updateServerUrlAddPoint
353
     */
354
    public function addUpdateServerUrlAddressSearchPoint($updateServerUrlAddPoint = "/googlemap/showaroundmexml/")
355
    {
356
        $link = Controller::join_links($updateServerUrlAddPoint, $this->owner->ID);
357
        $this->MyGoogleMap()->setUpdateServerUrlAddressSearchPoint($link);
358
    }
359
360
    /**
361
     * @param String $updateServerUrlDragend
362
     */
363
    public function addUpdateServerUrlDragend($updateServerUrlDragend = "googlemap/updatemexml/")
0 ignored issues
show
Unused Code introduced by
The parameter $updateServerUrlDragend is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
364
    {
365
        $link = Controller::join_links($UpdateServerUrlDragend, $this->owner->ID);
0 ignored issues
show
Bug introduced by
The variable $UpdateServerUrlDragend does not exist. Did you mean $updateServerUrlDragend?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
366
        $this->MyGoogleMap()->setUpdateServerUrlDragend($link);
367
    }
368
369
    /**
370
     * make the map editable
371
     */
372
    public function addAllowAddingAndDeletingPoints()
373
    {
374
        $this->MyGoogleMap()->AllowAddPointsToMap();
375
    }
376
377
    /**
378
     * removes user settings for map
379
     * a custom map is a bunch of points that are customised via a session
380
     *
381
     * @param string $filterCode
382
     */
383
    public function clearCustomMaps($filterCode = "")
384
    {
385
        GoogleMapDataResponse::clear_custom_google_map_session_data($filterCode);
386
    }
387
388
389
    /**
390
     * @param String $action
391
     * @param String $title
392
     * @param Int $lng
393
     * @param Int $lat
394
     * @param String $filter
0 ignored issues
show
Documentation introduced by
There is no parameter named $filter. Did you maybe mean $filterCode?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
395
     *
396
     * @return String
397
     */
398
    protected function getLinkForData($pageID = 0, $action = "", $title = "", $lng = 0, $lat = 0, $filterCode = "")
399
    {
400
        if (!$pageID) {
401
            $pageID = $this->owner->ID;
402
        }
403
        $linkForData = "googlemap/".$action."/".$pageID."/".urlencode($title)."/";
404
        if (($lng && $lat) || $filterCode) {
405
            $linkForData .= $lng."/".$lat."/";
406
        }
407
        if ($filterCode) {
408
            $linkForData .= '?filtercode='.$filterCode;
409
        }
410
411
        return $linkForData;
412
    }
413
}
414