Test Failed
Push — CI ( 0f01dd...c95a04 )
by Adam
55:13
created

jjwg_MapsController::action_map_markers()   F

Complexity

Conditions 74
Paths > 20000

Size

Total Lines 350
Code Lines 215

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 350
rs 2
c 1
b 0
f 0
cc 74
eloc 215
nc 23619
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if (!defined('sugarEntry') || !sugarEntry)
4
    die('Not A Valid Entry Point');
5
6
// modules/jjwg_Maps/controller.php
7
8
require_once('include/utils.php');
9
require_once('include/export_utils.php');
10
require_once("include/Sugar_Smarty.php");
11
require_once('modules/jjwg_Maps/jjwg_Maps.php');
12
13
class jjwg_MapsController extends SugarController {
14
15
    /**
16
     * @var settings array
17
     */
18
    var $settings = array();
19
20
    /**
21
     * $map_marker_data_points is used to store temporary data and prevent duplicate points
22
     * @var array
23
     */
24
    var $map_marker_data_points = array();
25
26
    /**
27
     * @var google_maps_response_codes
28
     * 
29
     */
30
    var $google_maps_response_codes = array('OK', 'ZERO_RESULTS', 'INVALID_REQUEST', 'OVER_QUERY_LIMIT', 'REQUEST_DENIED');
31
32
    /**
33
     * Last Geocoding Status Message
34
     * @var string
35
     */
36
    var $last_status = '';
37
38
    /**
39
     * display_object - display module's object (dom field)
40
     * @var object
41
     */
42
    var $display_object;
43
44
    /**
45
     * relate_object - relate module's object
46
     * @var object
47
     */
48
    var $relate_object;
49
50
    /**
51
     * jjwg_Maps - Maps module's object
52
     * @var object
53
     */
54
    var $bean;
55
    var $jjwg_Maps; // Deprecated reference
56
57
    /**
58
     * jjwg_Address_Cache - Address cache module's object
59
     * @var object
60
     */
61
    var $jjwg_Address_Cache;
62
63
    /**
64
     * smarty object for the generic configuration template
65
     * @var object
66
     */
67
    var $sugarSmarty;
68
69
    
70
    /**
71
     * Constructor
72
     */
73
    function jjwg_MapsController() {
74
        
75
        parent::SugarController();
76
        // Admin Config Setting
77
        $this->configuration();
78
    }
79
    
80
    /**
81
     * Load Configuration Settings using Administration Module
82
     * See jjwg_Maps module for settings
83
     * 
84
     * $GLOBALS['jjwg_config_defaults']
85
     * $GLOBALS['jjwg_config']
86
     */
87
    function configuration() {
88
89
        $this->bean = new jjwg_Maps();
90
        $this->jjwg_Maps = &$this->bean; // Set deprecated reference
91
        $this->settings = $GLOBALS['jjwg_config'];
92
    }
93
    
94
    /**
95
     * action geocoded_counts
96
     * Google Maps - Geocode the Addresses
97
     */
98
    function action_geocoded_counts() {
99
100
        $this->view = 'geocoded_counts';
101
        $GLOBALS['log']->debug(__METHOD__.' START');
102
103
        $this->bean->geocoded_counts = array();
104
        $this->bean->geocoded_headings = array('N/A');
105
        $this->bean->geocoded_module_totals = array();
106
107
        $responses = array('N/A' => '');
108
        foreach ($this->google_maps_response_codes as $code) {
109
            if (!in_array($code, array('OVER_QUERY_LIMIT', 'REQUEST_DENIED'))) {
110
                $responses[$code] = $code;
111
                $this->bean->geocoded_headings[] = $code;
112
            }
113
        }
114
        $responses['Approximate'] = 'APPROXIMATE';
115
        $responses['Empty'] = 'Empty';
116
        $this->bean->geocoded_headings[] = 'Approximate';
117
        $this->bean->geocoded_headings[] = 'Empty';
118
119
        // foreach module
120
        foreach ($this->settings['valid_geocode_modules'] as $module_type) {
121
122
            if (!isset($this->bean->geocoded_counts[$module_type])) {
123
                $this->bean->geocoded_module_totals[$module_type] = 0;
124
            }
125
126
            // Define display object from the necessary classes (utils.php)
127
            $this->display_object = get_module_info($module_type);
128
129
            foreach ($responses as $response => $code) {
130
131
                // Create Simple Count Query
132
                // 09/23/2010: Do not use create_new_list_query() and process_list_query()
133
                // as they will typically exceeded memory allowed.
134
                $query = "SELECT count(*) c FROM " . $this->display_object->table_name .
135
                        " LEFT JOIN " . $this->display_object->table_name . "_cstm " .
136
                        " ON " . $this->display_object->table_name . ".id = " . $this->display_object->table_name . "_cstm.id_c " .
137
                        " WHERE " . $this->display_object->table_name . ".deleted = 0 AND ";
138
                if ($response == 'N/A') {
139
                    $query .= "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = '' OR " .
140
                            $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c IS NULL)";
141
                } else {
142
                    $query .= $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = '" . $code . "'";
143
                }
144
                //var_dump($query);
145
                $count_result = $this->bean->db->query($query);
146
                $count = $this->bean->db->fetchByAssoc($count_result);
147
                if (empty($count)) $count['c'] = 0;
148
                $this->bean->geocoded_counts[$module_type][$response] = $count['c'];
149
            } // end foreach response type
150
            // Get Totals
151
            $this->bean->geocoded_module_totals[$module_type]++;
152
            $query = "SELECT count(*) c FROM " . $this->display_object->table_name . " WHERE " . $this->display_object->table_name . ".deleted = 0";
153
            //var_dump($query);
154
            $count_result = $this->bean->db->query($query);
155
            $count = $this->bean->db->fetchByAssoc($count_result);
156
            $this->bean->geocoded_module_totals[$module_type] = $count['c'];
157
        } // end each module type
158
    }
159
160
    /**
161
     * action geocode_addresses
162
     * Google Maps - Geocode the Addresses
163
     */
164
    function action_geocode_addresses() {
165
166
        $GLOBALS['log']->debug(__METHOD__.' START');
167
168
        if (!empty($_REQUEST['display_module']) && in_array($_REQUEST['display_module'], $this->settings['valid_geocode_modules'])) {
169
            $geocode_modules = array($_REQUEST['display_module']);
170
        } else {
171
            $geocode_modules = $this->settings['valid_geocode_modules'];
172
        }
173
        $geocoding_inc = 0;
174
        $google_geocoding_inc = 0;
175
        // Define Address Cache Object
176
        $this->jjwg_Address_Cache = get_module_info('jjwg_Address_Cache');
177
178
179
        foreach ($geocode_modules as $module_type) {
180
181
            $GLOBALS['log']->debug(__METHOD__.' $module_type: '.$module_type);
182
            // Define display object from the necessary classes (utils.php)
183
            $this->display_object = get_module_info($module_type);
184
185
            // Find the Items to Geocode - Get Geocode Addresses Result
186
            $display_result = $this->bean->getGeocodeAddressesResult($this->display_object->table_name);
187
188
            /* 
189
             * Iterate through the display rows
190
             * We build up an array here to prevent locking issues on some DBs (looking at you MSSQL)
191
             */
192
            $tmpDisplayResults = array();
193
            while ($display = $this->bean->db->fetchByAssoc($display_result)) {
194
                $tmpDisplayResults[] = $display;
195
            }
196
            foreach($tmpDisplayResults as $display){
197
198
                $GLOBALS['log']->debug(__METHOD__.' $display[\'id\': '.$display['id']);
199
                $geocoding_inc++;
200
                $aInfo = array();
201
                $cache_found = false;
202
                
203
                // Get address info array (address, status, lat, lng) from defineMapsAddress()
204
                // This will provide a related address & optionally a status, lat and lng from an account or other object
205
                $aInfo = $this->bean->defineMapsAddress($this->display_object->object_name, $display);
206
                //var_dump($aInfo);
207
                
208
                // Call Controller Method to Define Custom Address Logic
209
                $aInfo = $this->defineMapsAddressCustom($aInfo, $this->display_object->object_name, $display);
210
                //var_dump($aInfo);
211
                
212
                // If needed, check the Address Cache Module for Geocode Info
213
                if (!empty($aInfo['address']) && is_object($this->jjwg_Address_Cache)) {
214
                    $aInfoCache = $this->jjwg_Address_Cache->getAddressCacheInfo($aInfo);
215
                    if (!empty($aInfoCache['address'])) {
216
                        $cache_found = true;
217
                        $aInfo = $aInfoCache;
218
                    }
219
                }
220
221
                // If needed, Google Maps V3. Geocode the current address (status not set)
222
                if (!empty($aInfo['address']) && empty($aInfo['status'])) {
223
                    // Limit Geocode Requests to Google based on $this->settings['google_geocoding_limit']
224
                    if ($google_geocoding_inc < $this->settings['google_geocoding_limit']) {
225
                        $aInfoGoogle = $this->bean->getGoogleMapsGeocode($aInfo['address'], false, false);
226
                        if (!empty($aInfoGoogle)) {
227
                            $aInfo = $aInfoGoogle;
228
                            // Set last status
229
                            $this->last_status = $aInfo['status'];
230
                        }
231
                        $google_geocoding_inc++;
232
                    }
233
                }
234
235
                if (empty($aInfo['status'])) {
236
                    $aInfo['status'] = '';
237
                }
238
                if (empty($aInfo['lat']) || !is_numeric($aInfo['lat'])) {
239
                    $aInfo['lat'] = 0;
240
                }
241
                if (empty($aInfo['lng']) || !is_numeric($aInfo['lng'])) {
242
                    $aInfo['lng'] = 0;
243
                }
244
245
                // Successful geocode
246
                // 'OK' Status
247
                if (!empty($aInfo['address']) && $aInfo['status'] == 'OK' &&
248
                        !($aInfo['lng'] == 0 && $aInfo['lat'] == 0)) {
249
250
                    // Save Geocode $aInfo to custom fields
251
                    $update_result = $this->bean->updateGeocodeInfoByAssocQuery($this->display_object->table_name, $display, $aInfo);
252
                    
253
                    // Save address, lng and lat to cache module - if not already found from cache
254
                    if (!$cache_found) {
255
                        $cache_save_result = $this->jjwg_Address_Cache->saveAddressCacheInfo($aInfo);
256
                    }
257
258
                // Bad Geocode Results - Recorded
259
                // Empty Address - indicates no address, no geocode response
260
                // 'ZERO_RESULTS' - indicates that the geocode was successful but returned no results. 
261
                //     This may occur if the geocode was passed a non-existent address.
262
                // 'INVALID_REQUEST' - generally indicates that the query (address) is missing.
263
                // Also, capture empty $aInfo or address.
264
                } elseif (empty($aInfo) || empty($aInfo['address']) || (!empty($aInfo['address']) &&
265
                        ($aInfo['status'] == 'ZERO_RESULTS' || $aInfo['status'] == 'INVALID_REQUEST' || 
266
                        $aInfo['status'] == 'APPROXIMATE'))) {
267
268
                    if (empty($aInfo['status'])) {
269
                        $aInfo['status'] = 'Empty';
270
                    }
271
                    // Save Geocode $aInfo to custom fields
272
                    $update_result = $this->bean->updateGeocodeInfoByAssocQuery($this->display_object->table_name, $display, $aInfo);
273
274
                // Bad Geocode Results - Stop
275
                // 'OVER_QUERY_LIMIT' - indicates that you are over your quota.
276
                // 'REQUEST_DENIED' - indicates that your request was denied, generally because of lack of a sensor parameter.
277
                } elseif (!empty($aInfo['address']) &&
278
                        ($aInfo['status'] == 'OVER_QUERY_LIMIT' || $aInfo['status'] == 'REQUEST_DENIED')) {
279
280
                    // Set above limit to break/stop processing
281
                    $geocoding_inc = $this->settings['geocoding_limit'] + 1;
282
                } // end if/else
283
                
284
                // Wait 1 Second to Throttle Requests: Rate limit of 10 geocodings per second
285
                if ($geocoding_inc % 10 == 0)
286
                    sleep(1);
287
                
288
                if ($geocoding_inc > $this->settings['geocoding_limit'])
289
                    break;
290
            } // while
291
292
            if ($geocoding_inc > $this->settings['geocoding_limit'])
293
                break;
294
        } // end each module type
295
        
296
        // If not cron processing, then redirect.
297
        if (!isset($_REQUEST['cron'])) {
298
            // Redirect to the Geocoded Counts Display
299
            // contains header and exit
300
            $url = 'index.php?module=jjwg_Maps&action=geocoded_counts';
301
            if (!empty($this->last_status)) {
302
                $url .= '&last_status=' . urlencode($this->last_status);
303
            }
304
            SugarApplication::redirect($url);
305
        }
306
        
307
    }
308
309
    
310
    /**
311
     *  Add a number of display_module objects to a target list
312
     *  Return JSON encoded result count
313
     */
314
    function action_add_to_target_list() {
315
        
316
        $result = array('post' => $_POST);
317
        
318
        // Target List
319
        $list_id = (!empty($_POST['list_id'])) ? $_POST['list_id'] : '';
320
        $list = get_module_info('ProspectLists');
321
        if (!empty($list_id) && is_guid($list_id)) {
322
            $list->retrieve($list_id);
323
            $result['list'] = $list;
324
        }
325
        // Selected Area IDs - Validate
326
        $selected_ids = array();
327
        foreach ($_POST['selected_ids'] as $sel_id) {
328
            if (is_guid($sel_id)) {
329
                $selected_ids[] = $sel_id;
330
            }
331
        }
332
        $result['selected_ids'] = $selected_ids;
333
        
334
        // Display Module Type
335
        $module_type = '';
336
        if (!empty($_POST['display_module']) && in_array($_POST['display_module'], $this->settings['valid_geocode_modules'])) {
337
            $module_type = $_POST['display_module'];
338
            $result['module_type'] = $module_type;
339
            // Define display object
340
            $this->display_object = get_module_info($module_type);
341
        }
342
        
343
        if (!empty($list) && $list_id == $list->id && !empty($selected_ids) && !empty($this->display_object) && 
344
                in_array($this->display_object->module_name, array('Accounts', 'Contacts', 'Leads', 'Prospects', 'Users'))) {
345
            
346
            $object_name = $this->display_object->object_name;
347
            $result['object_name'] = $object_name;
348
            
349
            if ($object_name == 'Account') {
350
                $list->load_relationship('accounts');
351
                foreach ($selected_ids as $sel_id) {
352
                    $list->accounts->add($sel_id);
353
                }
354
            } elseif ($object_name == 'Contact') {
355
                $list->load_relationship('contacts');
356
                foreach ($selected_ids as $sel_id) {
357
                    $list->contacts->add($sel_id);
358
                }
359
            } elseif ($object_name == 'Lead') {
360
                $list->load_relationship('leads');
361
                foreach ($selected_ids as $sel_id) {
362
                    $list->leads->add($sel_id);
363
                }
364
            } elseif ($object_name == 'Prospect') {
365
                $list->load_relationship('prospects');
366
                foreach ($selected_ids as $sel_id) {
367
                    $list->prospects->add($sel_id);
368
                }
369
            } elseif ($object_name == 'User') {
370
                $list->load_relationship('users');
371
                foreach ($selected_ids as $sel_id) {
372
                    $list->users->add($sel_id);
373
                }
374
            }
375
            $result['message'] = 'Target List Updated';
376
        } else {
377
            $result['message'] = 'Target List NOT Updated';
378
        }
379
        
380
        // JSON Encoded $result
381
        header('Content-Type: application/json');
382
        echo @json_encode($result);
383
    }
384
385
    /**
386
     * export addresses in need of geocoding
387
     */
388
    function action_export_geocoding_addresses() {
389
390
        $address_data = array();
391
        $addresses = array();
392
393
        if (!empty($_REQUEST['display_module']) && in_array($_REQUEST['display_module'], $this->settings['valid_geocode_modules'])) {
394
            $module_type = $_REQUEST['display_module'];
395
        } else {
396
            $module_type = $this->settings['valid_geocode_modules'][0];
397
        }
398
        // Define display object
399
        $this->display_object = get_module_info($module_type);
400
401
        // Find the Items to Geocode - Get Geocode Addresses Result
402
        $display_result = $this->bean->getGeocodeAddressesResult($this->display_object->table_name, $this->settings['export_addresses_limit']);
403
404
        $address_data[] = array('address', 'lat', 'lng');
405
        // Iterate through the display rows
406
        while ($display = $this->bean->db->fetchByAssoc($display_result)) {
407
408
            // Get address info array (address, status, lat, lng) from defineMapsAddress()
409
            // This will provide a related address & optionally a status, lat and lng from an account or other object
410
            $aInfo = $this->bean->defineMapsAddress($this->display_object->object_name, $display);
411
            //var_dump($aInfo);
412
            
413
            // Call Method to Define Custom Address Logic
414
            $aInfo = $this->defineMapsAddressCustom($aInfo, $this->display_object->object_name, $display);
415
            //var_dump($aInfo);
416
            
417
            if (!empty($aInfo['address'])) {
418
                $addresses[] = trim($aInfo['address'], ' ,;."\'');
419
        }
420
        }
421
        
422
        $addresses = array_unique($addresses);
423
        foreach ($addresses as $address) {
424
            $address_data[] = array($address, '', '');
425
        }
426
        
427
        $filename = $module_type . '_Addresses_' . date("Ymd") . ".csv";
428
        $this->do_list_csv_output($address_data, $filename);
429
        exit;
430
    }
431
    
432
    /**
433
     * Custom Override for Defining Maps Address
434
     *
435
     * @param $aInfo        address info array(address, status, lat, lng)
436
     * @param $object_name  signular object name
437
     * @param $display      fetched row array
438
     */
439
    function defineMapsAddressCustom($aInfo, $object_name, $display) {
440
        
441
        // Use custom contoller.php with custom logic
442
        return $aInfo;
443
    }
444
    
445
    /**
446
     * 
447
     * Export rows of data as a CSV file
448
     * @param unknown_type $rows
449
     * @param unknown_type $filename
450
     */
451
    private function do_list_csv_output($rows, $filename) {
452
453
        header("Content-type: application/octet-stream");
454
        header("Content-Disposition: attachment; filename=\"$filename\"");
455
        header("Content-Transfer-Encoding: binary");
456
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
457
            // IE cannot download from sessions without a cache 
458
            header('Cache-Control: public');
459
        }
460
        foreach (array_keys($rows) as $key) {
461
            $row = $rows[$key];
462
            echo $this->list_row_to_csv($row);
463
        }
464
    }
465
466
    /**
467
     * 
468
     * Create CSV row for export view
469
     * @param $fields name value pairs
470
     * @param $delimiter
471
     * @param $enclosure
472
     */
473
    private function list_row_to_csv($fields, $delimiter = ',', $enclosure = '"') {
474
475
        $delimiter_esc = preg_quote($delimiter, '/');
476
        $enclosure_esc = preg_quote($enclosure, '/');
477
        $output = array();
478
        foreach ($fields as $field) {
479
            $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
480
                    $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure
481
                    ) : $field;
482
        }
483
484
        return (join($delimiter, $output) . "\n");
485
    }
486
487
    /**
488
     * action geocoding_test
489
     * Google Maps - Geocoding Test
490
     */
491
    function action_geocoding_test() {
492
493
        $this->view = 'geocoding_test';
494
        
495
        if (!empty($_REQUEST['geocoding_address']) && !empty($_REQUEST['process_trigger']) &&
496
                strlen($_REQUEST['geocoding_address']) <= 255) {
497
            $this->bean->geocoding_results = $this->bean->getGoogleMapsGeocode($_REQUEST['geocoding_address'], true, true);
498
        }
499
    }
500
501
    /**
502
     * action config
503
     * Google Maps - Config
504
     */
505
    function action_config() {
506
507
        // Admin Only
508
        if (!empty($GLOBALS['current_user']->is_admin)) {
509
            if (!empty($_REQUEST['submit'])) {
510
                // Post-Get-Redirect
511
                $save_result = $this->bean->saveConfiguration($_REQUEST);
512
                $config_save_notice = ($save_result == true) ? '1' : '0';
513
                SugarApplication::redirect('index.php?module=jjwg_Maps&action=config&config_save_notice='.$config_save_notice);
514
            } else {
515
                $this->view = 'config';
516
            }
517
        } else {
518
            SugarApplication::redirect('index.php?module=jjwg_Maps&action=index');
519
        }
520
    }
521
    
522
    /**
523
     * action reset module geocode info
524
     * Google Maps - geocoded_counts
525
     */
526
    function action_reset_geocoding() {
527
528
        $display_module = $_REQUEST['display_module'];
529
        
530
        // Define display object from the necessary classes (utils.php)
531
        $this->display_object = get_module_info($display_module);
532
533
        // Admin Only
534
        if (!empty($GLOBALS['current_user']->is_admin)) {
535
            if (is_object($this->display_object)) {
536
                $delete_result = $this->bean->deleteAllGeocodeInfoByBeanQuery($this->display_object);
537
                SugarApplication::redirect('index.php?module=jjwg_Maps&action=geocoded_counts');
538
            } else {
539
                $this->view = 'geocoded_counts';
540
            }
541
        } else {
542
            SugarApplication::redirect('index.php?module=jjwg_Maps&action=index');
543
        }
544
    }
545
    
546
    /**
547
     * delete all address cache
548
     * Google Maps - geocoded_counts
549
     */
550
    function action_delete_all_address_cache() {
551
552
        // Define Address Cache Object
553
        $this->jjwg_Address_Cache = get_module_info('jjwg_Address_Cache');
554
        
555
        // Admin Only
556
        if (!empty($GLOBALS['current_user']->is_admin)) {
557
            if (is_object($this->jjwg_Address_Cache)) {
558
                // Post-Get-Redirect
559
                $delete_result = $this->jjwg_Address_Cache->deleteAllAddressCache();
560
                SugarApplication::redirect('index.php?module=jjwg_Maps&action=geocoded_counts');
561
            } else {
562
                $this->view = 'geocoded_counts';
563
            }
564
        } else {
565
            SugarApplication::redirect('index.php?module=jjwg_Maps&action=index');
566
        }
567
    }
568
    
569
    /**
570
     * action quick_radius
571
     * Google Maps - Quick Radius Map
572
     */
573
    function action_quick_radius() {
574
575
        $this->view = 'quick_radius';
576
577
        if (!isset($_REQUEST['distance'])) $_REQUEST['distance'] = $this->settings['map_default_distance'];
578
        if (!isset($_REQUEST['unit_type'])) $_REQUEST['unit_type'] = $this->settings['map_default_unit_type'];
579
580
    }
581
582
    /**
583
     * action map_display
584
     * Google Maps - Output the Page with IFrame to Map Markers
585
     */
586
    function action_quick_radius_display() {
587
588
        $this->view = 'quick_radius_display';
589
    }
590
591
    /**
592
     * action map_display
593
     * Google Maps - Output the Page with IFrame to Map Markers
594
     */
595
    function action_map_display() {
596
597
        $this->view = 'map_display';
598
        if (!isset($_REQUEST['current_post'])) $_REQUEST['current_post'] = '';
599
        
600
        // Bug: 'current_post' too large for iFrame URL used in Google Library calls
601
        $_SESSION['jjwg_Maps']['current_post'] = $_REQUEST['current_post'];
602
        $_REQUEST['current_post'] = 'session';
603
    }
604
605
    /**
606
     * action donate
607
     * Google Maps - Output the Donate Page
608
     */
609
    function action_donate() {
610
611
        $this->view = 'donate';
612
    }
613
614
    /**
615
     * action map_markers
616
     * Google Maps - Output the Map Markers
617
     */
618
    function action_map_markers() {
619
620
        header_remove('X-Frame-Options');
621
        $this->view = 'map_markers';
622
623
        // Define globals for use in the view.
624
        $this->bean->map_center = array();
625
        $this->bean->map_markers = array();
626
        $this->bean->map_markers_groups = array();
627
        $this->bean->custom_markers = array();
628
        $this->bean->custom_areas = array();
629
630
        // Create New Sugar_Smarty Object
631
        $this->sugarSmarty = new Sugar_Smarty();
632
        $this->sugarSmarty->assign("mod_strings", $GLOBALS['mod_strings']);
633
        $this->sugarSmarty->assign("app_strings", $GLOBALS['app_strings']);
634
        $this->sugarSmarty->assign('app_list_strings', $GLOBALS['app_list_strings']);
635
        $this->sugarSmarty->assign('moduleListSingular', $GLOBALS['app_list_strings']['moduleListSingular']);
636
        $this->sugarSmarty->assign('moduleList', $GLOBALS['app_list_strings']['moduleList']);
637
        //echo '<pre>';
638
        //var_dump($_REQUEST);
639
640
        // Related Map Record Defines the Map
641
        if (!empty($_REQUEST['record']) ||
642
                (!empty($_REQUEST['relate_id']) && !empty($_REQUEST['relate_module'])) ||
643
                (!empty($_REQUEST['quick_address']) && !empty($_REQUEST['display_module']))) {
644
645
            // If map 'record' then define map details from current module.
646
            if (@is_guid($_REQUEST['record'])) {
647
                // Get the map object
648
                $map = get_module_info($GLOBALS['currentModule']);
649
                $map->retrieve($_REQUEST['record']);
650
                // Define map variables
651
                $map_parent_type = $map->parent_type;
652
                $map_parent_id = $map->parent_id;
653
                $map_module_type = $map->module_type;
654
                $map_unit_type = $map->unit_type;
655
                $map_distance = $map->distance;
656
            }
657
            // Else if a 'relate_id' use it as the Relate Center Point (Lng/Lat)
658
            else if (@(is_guid($_REQUEST['relate_id']) && !empty($_REQUEST['relate_module']))) {
659
                // Define map variables
660
                $map_parent_type = $_REQUEST['relate_module'];
661
                $map_parent_id = $_REQUEST['relate_id'];
662
                $map_module_type = (!empty($_REQUEST['display_module'])) ? $_REQUEST['display_module'] : $_REQUEST['relate_module'];
663
                $map_distance = (!empty($_REQUEST['distance'])) ? $_REQUEST['distance'] : $this->settings['map_default_distance'];
664
                $map_unit_type = (!empty($_REQUEST['unit_type'])) ? $_REQUEST['unit_type'] : $this->settings['map_default_unit_type'];
665
            }
666
            // Else if a 'quick_address' use it as the Center Point (Lng/Lat)
667
            else if (!empty($_REQUEST['quick_address']) && !empty($_REQUEST['display_module'])) {
668
                // Define map variables / No Parent
669
                $map_parent_type = null;
670
                $map_parent_id = null;
671
                $map_module_type = (!empty($_REQUEST['display_module'])) ? $_REQUEST['display_module'] : $_REQUEST['relate_module'];
672
                $map_distance = (!empty($_REQUEST['distance'])) ? $_REQUEST['distance'] : $this->settings['map_default_distance'];
673
                $map_unit_type = (!empty($_REQUEST['unit_type'])) ? $_REQUEST['unit_type'] : $this->settings['map_default_unit_type'];
674
            }
675
676
            // Define display object, note - 'Accounts_Members' is a special display type
677
            $this->display_object = ($map_module_type == 'Accounts_Members') ? get_module_info('Accounts') : get_module_info($map_module_type);
678
            $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name);
679
            $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']);
680
681
            // If relate module/id object
682
            if (!empty($map_parent_type) && !empty($map_parent_id)) {
683
684
                // Define relate objects
685
                $this->relate_object = get_module_info($map_parent_type);
686
                $this->relate_object->retrieve($map_parent_id);
687
                $mod_strings_related = return_module_language($GLOBALS['current_language'], $this->relate_object->module_name);
688
                $mod_strings_related = array_merge($mod_strings_related, $GLOBALS['mod_strings']);
689
                
690
                // Get the Relate object Assoc Data
691
                $where_conds = $this->relate_object->table_name . ".id = '" . $map_parent_id . "'";
692
                $query = $this->relate_object->create_new_list_query("" . $this->relate_object->table_name . ".assigned_user_id", $where_conds, array(), array(), 0, '', false, $this->relate_object, false);
693
                //var_dump($query);
694
                $relate_result = $this->bean->db->query($query);
695
                $relate = $this->bean->db->fetchByAssoc($relate_result);
696
                // Add Relate (Center Point) Marker
697
                $this->bean->map_center = $this->getMarkerData($map_parent_type, $relate, true, $mod_strings_related);
698
                // Define Center Point
699
                $center_lat = $this->relate_object->jjwg_maps_lat_c;
700
                $center_lng = $this->relate_object->jjwg_maps_lng_c;
701
            }
702
            // Use Quick Address as Center Point
703
            else {
704
                // Geocode 'quick_address'
705
                $aInfo = $this->bean->getGoogleMapsGeocode($_REQUEST['quick_address'], false, true);
706
                // If not status 'OK', then fail here and exit. Note: Inside of iFrame
707
                if (!empty($aInfo['status']) && $aInfo['status'] != 'OK' && preg_match('/[A-Z\_]/', $aInfo['status'])) {
708
                    echo '<br /><br /><div><b>'.$GLOBALS['mod_strings']['LBL_MAP_LAST_STATUS'].': '.$aInfo['status'].'</b></div><br /><br />';
709
                    exit;
710
                }
711
                //var_dump($aInfo);
712
                // Define Marker Data
713
                $aInfo['name'] = $_REQUEST['quick_address'];
714
                $aInfo['id'] = 0;
715
                $aInfo['module'] = ($map_module_type == 'Accounts_Members') ? 'Accounts' : $map_module_type;
716
                $aInfo['address'] = $_REQUEST['quick_address'];
717
                $aInfo['jjwg_maps_address_c'] = $_REQUEST['quick_address'];
718
                $aInfo['jjwg_maps_lat_c'] = $aInfo['lat'];
719
                $aInfo['jjwg_maps_lng_c'] = $aInfo['lng'];
720
                $this->bean->map_center = $this->getMarkerData($map_parent_type, $aInfo, true);
721
                // Define Center Point
722
                $center_lat = $aInfo['lat'];
723
                $center_lng = $aInfo['lng'];
724
            }
725
            //var_dump($aInfo);
726
            // Define $x and $y expressions
727
            $x = '(69.1*((' . $this->display_object->table_name . '_cstm.jjwg_maps_lat_c)-(' . $center_lat . ')))';
728
            $y = '(53.0*((' . $this->display_object->table_name . '_cstm.jjwg_maps_lng_c)-(' . $center_lng . ')) * COS((' . $center_lat . ')/57.1))';
729
            $calc_distance_expression = 'SQRT(' . $x . '*' . $x . '+' . $y . '*' . $y . ')';
730
            if (strtolower($map_unit_type) == 'km' || strtolower($map_unit_type) == 'kilometer') {
731
                $calc_distance_expression .= '*1.609'; // 1 mile = 1.609 km
732
            }
733
734
            // Find the Items to Display
735
            // Assume there is no address at 0,0; it's in the Atlantic Ocean!
736
            $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " .
737
                    "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " .
738
                    " AND " .
739
                    "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')" .
740
                    " AND " .
741
                    "(" . $calc_distance_expression . " < " . $map_distance . ")";
742
            $query = $this->display_object->create_new_list_query('display_object_distance', $where_conds, array(), array(), 0, '', false, $this->display_object, false);
743
            // Add the disply_object_distance into SELECT list
744
            $query = str_replace('SELECT ', 'SELECT (' . $calc_distance_expression . ') AS display_object_distance, ', $query);
745
            if ($map_module_type == 'Contacts') { // Contacts - Account Name
746
                $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id  FROM contacts  ', $query);
747
                $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query);
748
            } elseif ($map_module_type == 'Opportunities') { // Opps - Account Name
749
                $query = str_replace(' FROM opportunities ', ' ,accounts.name AS account_name, accounts.id AS account_id  FROM opportunities  ', $query);
750
                $query = str_replace(' FROM opportunities ', ' FROM opportunities LEFT JOIN accounts_opportunities ON opportunities.id=accounts_opportunities.opportunity_id and accounts_opportunities.deleted = 0 LEFT JOIN accounts ON accounts_opportunities.account_id=accounts.id AND accounts.deleted=0 ', $query);
751
            } elseif ($map_module_type == 'Accounts_Members') { // 'Accounts_Members' is a special display type
752
                $query = str_replace(' AND accounts.deleted=0', ' AND accounts.deleted=0 AND accounts.parent_id = \''.$this->bean->db->quote($map_parent_id).'\'', $query);
753
            }
754
            //var_dump($query);
755
            $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']);
756
            while ($display = $this->bean->db->fetchByAssoc($display_result)) {
757
                if (!empty($map_distance) && !empty($display['id'])) {
758
                    $marker_data_module_type = ($map_module_type == 'Accounts_Members') ? 'Accounts' : $map_module_type;
759
                    $marker_data = $this->getMarkerData($marker_data_module_type, $display, false, $mod_strings_display);
760
                    if (!empty($marker_data)) {
761
                        $this->bean->map_markers[] = $marker_data;
762
                    }
763
                }
764
            }
765
            //var_dump($this->bean->map_markers);
766
            // Next define the Custom Markers and Areas related to this Map
767
            // Define relate and display objects from the necessary classes (utils.php)
768
            @$markers_object = get_module_info('jjwg_Markers');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
769
            @$areas_object = get_module_info('jjwg_Areas');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
770
771
            // Relationship Names: jjwg_maps_jjwg_areas and jjwg_maps_jjwg_markers
772
            // Find the Related Beans: Maps to Markers
773
            if (@(is_object($markers_object) && is_object($map))) {
774
                $related_custom_markers = $map->get_linked_beans('jjwg_maps_jjwg_markers', 'jjwg_Markers');
775
                if ($related_custom_markers) {
776
                    foreach ($related_custom_markers as $marker_bean) {
777
                        $marker_data = $this->getMarkerDataCustom($marker_bean);
778
                        if (!empty($marker_data)) {
779
                            $this->bean->custom_markers[] = $marker_data;
780
                        }
781
                    }
782
                }
783
            }
784
785
            // Find the Related Beans: Maps to Areas
786
            if (@(is_object($areas_object) && is_object($map))) {
787
                $related_custom_areas = $map->get_linked_beans('jjwg_maps_jjwg_areas', 'jjwg_Areas');
788
                if ($related_custom_areas) {
789
                    foreach ($related_custom_areas as $area_bean) {
790
                        $area_data = $this->getAreaDataCustom($area_bean);
791
                        if (!empty($area_data)) {
792
                            $this->bean->custom_areas[] = $area_data;
793
                        }
794
                    }
795
                }
796
            }
797
798
799
            // Map Target List (ProspectLists)
800
        } elseif (!empty($_REQUEST['list_id'])) {
801
802
            $this->bean->map_markers = array();
803
            $this->display_object = get_module_info('ProspectLists');
804
            // Use the Export Query
805
            if (!empty($_REQUEST['list_id'])) {
806
                $this->display_object->retrieve($_REQUEST['list_id']);
807
                if ($this->display_object->id == $_REQUEST['list_id']) {
808
                    $prospect_list_object = $this->display_object;
809
                    $list_id = $this->display_object->id;
810
                }
811
            }
812
            
813
            if (!empty($list_id)) {
814
                
815
                $list_modules = array('Accounts', 'Contacts', 'Leads', 'Users', 'Prospects');
816
                $temp_marker_groups = array();
817
                
818
                foreach ($list_modules as $display_module) {
819
                    
820
                    $this->display_object = get_module_info($display_module);
821
                    $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name);
822
                    $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']);
823
                    
824
                    // Find the Items to Display
825
                    // Assume there is no address at 0,0; it's in the Atlantic Ocean!
826
                    $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " .
827
                            "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " .
828
                            " AND " .
829
                            "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')";
830
                    $query = $this->display_object->create_new_list_query('', $where_conds, array(), array(), 0, '', false, $this->display_object, false);
831
                    if ($display_module == 'Contacts') { // Contacts - Account Name
832
                        $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id  FROM contacts  ', $query);
833
                        $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query);
834
                    }
835
                    // Add List JOIN
836
                    $query = str_replace(' FROM '.$this->display_object->table_name.' ', ' FROM '.$this->display_object->table_name.' '.
837
                            'LEFT JOIN prospect_lists_prospects ON prospect_lists_prospects.related_id = '.$this->display_object->table_name.'.id AND prospect_lists_prospects.deleted=0 '.
838
                            'LEFT JOIN prospect_lists ON prospect_lists_prospects.prospect_list_id = prospect_lists.id AND prospect_lists.deleted=0 ', 
839
                            $query);
840
                    // Restrict WHERE to related type and $list_id
841
                    $query .= ' AND prospect_lists_prospects.related_type = \''.$this->display_object->module_name.'\' AND '.
842
                            'prospect_lists.id = \''.$this->bean->db->quote($list_id).'\'';
843
                    //var_dump($query);
844
                    $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']);
845
                    $display_type_found = false;
846
                    while ($display = $this->bean->db->fetchByAssoc($display_result)) {
847
                        if (!empty($display['id'])) {
848
                            $marker_data = $this->getMarkerData($display_module, $display, false, $mod_strings_display);
849
                            $marker_data['group'] = $GLOBALS['app_list_strings']['moduleList'][$display_module];
850
                            if (!empty($marker_data)) {
851
                                $this->bean->map_markers[] = $marker_data;
852
                            }
853
                            $display_type_found = true;
854
                        }
855
                    }
856
                    if ($display_type_found) {
857
                        $temp_marker_groups[] = $GLOBALS['app_list_strings']['moduleList'][$display_module];
858
                    }
859
                    
860
                }
861
                
862
                $this->bean->map_markers_groups = $temp_marker_groups;
863
            }
864
            
865
            
866
            // Map Records
867
        } elseif (!empty($_REQUEST['uid']) || !empty($_REQUEST['current_post'])) {
868
869
            if (in_array($_REQUEST['display_module'], $this->settings['valid_geocode_modules'])) {
870
                $display_module = $_REQUEST['display_module'];
871
            } else {
872
                $display_module = 'Accounts';
873
            }
874
            if ($_REQUEST['current_post'] == 'session') {
875
                $current_post = $_SESSION['jjwg_Maps']['current_post'];
876
            } else {
877
                $current_post = $_REQUEST['current_post'];
878
            }
879
            $query = '';
880
            $selected_query = '';
881
            $records = array();
882
            $order_by = '';
883
            
884
            $this->display_object = get_module_info($display_module);
885
            $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name);
886
            $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']);
887
888
            if (!empty($_REQUEST['uid'])) {
889
                // Several records selected or this page
890
                $records = explode(',', $_REQUEST['uid']);
891
            } elseif (!empty($current_post)) {
892
                // Select all records (advanced search)
893
                $search_array = generateSearchWhere($display_module, $current_post);
894
                //var_dump($search_array);
895
                if (!empty($search_array['where'])) {
896
                    // Related Field Bug: Get relate/link patched 'where' and 'join'
897
                    @$ret_array = create_export_query_relate_link_patch($display_module, $search_array['searchFields'], $search_array['where']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
898
                    if(!empty($ret_array['join'])) {
899
                        @$selected_query = $this->display_object->create_export_query($order_by, $ret_array['where'], $ret_array['join']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
900
                    } else {
901
                        @$selected_query = $this->display_object->create_export_query($order_by, $ret_array['where']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
902
                    }
903
                    // SugarOnDemand JOIN Bug: If $ret_array['join'] is not included in query, force it in!
904
                    if (strpos($ret_array['join'], $selected_query) === false) {
905
                        $selected_query = str_replace(' where ', $ret_array['join'].' where ', $selected_query);
906
                    }
907
                    // Avoiding subquery. Let's just record the record ID's for later
908
                    $selected_result = $this->bean->db->limitQuery($selected_query, 0, $this->settings['map_markers_limit']);
909
                    while ($display = $this->bean->db->fetchByAssoc($selected_result)) {
910
                        $records[] = $display['id'];
911
                    }
912
                }
913
            }
914
            //var_dump($records);
915
916
            // Find the Items to Display
917
            // Assume there is no address at 0,0; it's in the Atlantic Ocean!
918
            $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " .
919
                    "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " .
920
                    " AND " .
921
                    "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')";
922
            $query = $this->display_object->create_new_list_query('', $where_conds, array(), array(), 0, '', false, $this->display_object, false);
923
            if ($display_module == 'Contacts') { // Contacts - Account Name
924
                $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id  FROM contacts  ', $query);
925
                $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query);
926
            } elseif ($display_module == 'Opportunities') { // Opps - Account Name
927
                $query = str_replace(' FROM opportunities ', ' ,accounts.name AS account_name, accounts.id AS account_id  FROM opportunities  ', $query);
928
                $query = str_replace(' FROM opportunities ', ' FROM opportunities LEFT JOIN accounts_opportunities ON opportunities.id=accounts_opportunities.opportunity_id and accounts_opportunities.deleted = 0 LEFT JOIN accounts ON accounts_opportunities.account_id=accounts.id AND accounts.deleted=0 ', $query);
929
            }
930
            //var_dump($query);
931
            
932
            $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']);
933
            $this->bean->map_markers = array();
934
            while ($display = $this->bean->db->fetchByAssoc($display_result)) {
935
                if (!empty($search_array['where'])) { // Select all records (advanced search) with where clause
936
                    if (in_array($display['id'], $records)) {
937
                        $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display);
938
                    }
939
                } elseif (!empty($_REQUEST['uid'])) { // Several records selected or this page selected
940
                    if (in_array($display['id'], $records)) {
941
                        $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display);
942
                    }
943
                } else { // All
944
                    $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display);
945
                }
946
            }
947
        }
948
949
        // Sort marker groups for the view
950
        sort($this->bean->map_markers_groups);
951
        
952
        // Set display object for later use
953
        $this->bean->display_object = $this->display_object;
954
        
955
        // Get Prospect List Array Dropdown
956
        $list = get_module_info('ProspectLists');
957
        $list_query = $list->create_list_query('prospect_lists.name', '1=1', 0);
958
        $list_result = $list->db->query($list_query);
959
        $list_array = array();
960
        while ($alist = $list->db->fetchByAssoc($list_result)) {
961
            if (!empty($alist['name']) && !empty($alist['id'])) {
962
                $list_array[$alist['id']] = $alist['name'];
963
            }
964
        }
965
        $this->bean->list_array = $list_array;
966
    
967
    }
968
    
969
    // end function action_map_markers
970
971
    /**
972
     * Define marker data for marker display view
973
     * @param $module_type bean name
974
     * @param $display bean fields array
975
     * $param $mod_strings_display mod_strings from display module
976
     * TODO: Use a custom defined field for the $marker['group']
977
     */
978
    function getMarkerData($module_type, $display, $center_marker = false, $mod_strings_display = array()) {
979
        
980
//        echo "<pre>";
981
//        print_r($display);
982
//        print_r($mod_strings_display);
983
//        echo "</pre>";
984
        
985
        // Define Marker
986
        $marker = array();
987
        // Set only partial display data for efficiency
988
        $marker['name'] = $display['name'];
989
        // Or, Set all display data for flexibility
990
        //$marker = $display;
991
        if (empty($marker['name'])) {
992
            $marker['name'] = 'N/A';
993
        }
994
        $marker['id'] = $display['id'];
995
        $marker['module'] = $module_type;
996
        $marker['address'] = $display['jjwg_maps_address_c'];
997
        $marker['lat'] = $display['jjwg_maps_lat_c'];
998
        if (!$this->is_valid_lat($marker['lat'])) {
999
            $marker['lat'] = '0';
1000
        }
1001
        $marker['lng'] = $display['jjwg_maps_lng_c'];
1002
        if (!$this->is_valid_lng($marker['lng'])) {
1003
            $marker['lng'] = '0';
1004
        }
1005
        // Define a phone field: phone_office, phone_work, phone_mobile
1006
        if (!empty($display['phone_office'])) {
1007
            $marker['phone'] = $display['phone_office'];
1008
        } elseif (!empty($display['phone_work'])) {
1009
            $marker['phone'] = $display['phone_work'];
1010
        } elseif (!empty($display['phone_mobile'])) {
1011
            $marker['phone'] = $display['phone_mobile'];
1012
        } else {
1013
            $marker['phone'] = '';
1014
        }
1015
1016
        if ($marker['lat'] != '0' && $marker['lng'] != '0') {
1017
            
1018
            // Check to see if marker point already exists and apply offset if needed
1019
            // This often occurs when an address is only defined by city, state, zip.
1020
            $i = 0;
1021
            while (isset($this->map_marker_data_points[(string) $marker['lat']][(string) $marker['lng']]) &&
1022
            $i < $this->settings['map_markers_limit']) {
1023
                $marker['lat'] = (float) $marker['lat'] + (float) $this->settings['map_duplicate_marker_adjustment'];
1024
                $marker['lng'] = (float) $marker['lng'] + (float) $this->settings['map_duplicate_marker_adjustment'];
1025
                $i++;
1026
            }
1027
            // Set Marker Point as Used (true)
1028
            $this->map_marker_data_points[(string) $marker['lat']][(string) $marker['lng']] = true;
1029
            
1030
            if (isset($display['account_name'])) {
1031
                $marker['account_name'] = $display['account_name'];
1032
            }
1033
            if (isset($display['account_id'])) {
1034
                $marker['account_id'] = $display['account_id'];
1035
            }
1036
            $marker['assigned_user_name'] = (isset($display['assigned_user_name'])) ? $display['assigned_user_name'] : '';
1037
            $marker['image'] = (isset($display['marker_image'])) ? $display['marker_image'] : '';
1038
            
1039
            // Define Marker Group
1040
            if (!$center_marker) {
1041
                // Group Field for the Display Module
1042
                $group_field_name = $this->settings['map_markers_grouping_field'][$module_type];
1043
                $group_field_value = $display[$group_field_name];
1044
                // Check for DOM field types (enum type)
1045
                if (isset($this->display_object->field_name_map[$group_field_name]['type']) && 
1046
                        $this->display_object->field_name_map[$group_field_name]['type'] == 'enum') {
1047
                    $group_field_dom = $this->display_object->field_name_map[$group_field_name]['options'];
1048
                    $marker['group'] = $GLOBALS['app_list_strings'][$group_field_dom][$group_field_value];
1049
                } elseif (!empty($display[$group_field_name])) {
1050
                    $marker['group'] = $display[$group_field_name];
1051
                } else {
1052
                    $marker['group'] = $GLOBALS['mod_strings']['LBL_MAP_NULL_GROUP_NAME']; // null group
1053
                }
1054
                if (!in_array($marker['group'], $this->bean->map_markers_groups)) {
1055
                    $this->bean->map_markers_groups[] = $marker['group'];
1056
                }
1057
            }
1058
            
1059
            /**
1060
             *  Define Dates for Meetings
1061
             *  TimeDate.php to_display_date_time()
1062
             *  Note, date time fields are converted to the User's date time settings
1063
             */
1064
            if ($module_type == 'Meetings') {
1065
                require_once('modules/Meetings/Meeting.php');
1066
                require_once('include/TimeDate.php');
1067
                if (!isset($meetingTimeDate) || !is_object($meetingTimeDate)) {
0 ignored issues
show
Bug introduced by
The variable $meetingTimeDate seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
1068
                    $meetingTimeDate = new TimeDate();
1069
                }
1070
                $display['date_start'] = $meetingTimeDate->to_display_date_time($display['date_start'], true, true, $GLOBALS['current_user']);
1071
                $display['date_end'] = $meetingTimeDate->to_display_date_time($display['date_end'], true, true, $GLOBALS['current_user']);
1072
            }
1073
            $current_user_data = get_object_vars($GLOBALS['current_user']);
1074
            $this->sugarSmarty->assign('current_user', $current_user_data);
1075
            $this->sugarSmarty->assign('current_user_address', $this->bean->defineMapsFormattedAddress($current_user_data, 'address'));
1076
            $this->sugarSmarty->assign("mod_strings", $mod_strings_display);
1077
            // Define Maps Info Window HTML by Sugar Smarty Template
1078
            $this->sugarSmarty->assign("module_type", $module_type);
1079
            $this->sugarSmarty->assign("address", $display['jjwg_maps_address_c']);
1080
            $this->sugarSmarty->assign("fields", $display); // display fields array
1081
            // Use @ error suppression to avoid issues with SugarCRM On-Demand
1082
            $marker['html'] = @$this->sugarSmarty->fetch('./custom/modules/jjwg_Maps/tpls/' . $module_type . 'InfoWindow.tpl');
1083
            if (empty($marker['html'])) {
1084
                $marker['html'] = $this->sugarSmarty->fetch('./modules/jjwg_Maps/tpls/' . $module_type . 'InfoWindow.tpl');
1085
            }
1086
            $marker['html'] = preg_replace('/\n\r/', ' ', $marker['html']);
1087
            //var_dump($marker['html']);
1088
            return $marker;
1089
        
1090
        } else {
1091
            return false;
1092
        }
1093
    }
1094
1095
    /**
1096
     * Get Marker Data Custom for Mapping
1097
     * @param $marker_object
1098
     */
1099
    function getMarkerDataCustom($marker_object) {
1100
1101
        // Define Marker
1102
        $marker = array();
1103
        $marker['name'] = $marker_object->name;
1104
        if (empty($marker['name'])) {
1105
            $marker['name'] = 'N/A';
1106
        }
1107
        $marker['id'] = $marker_object->id;
1108
        $marker['lat'] = $marker_object->jjwg_maps_lat;
1109
        if (!$this->is_valid_lat($marker['lat'])) {
1110
            $marker['lat'] = '0';
1111
        }
1112
        $marker['lng'] = $marker_object->jjwg_maps_lng;
1113
        if (!$this->is_valid_lng($marker['lng'])) {
1114
            $marker['lng'] = '0';
1115
        }
1116
        $marker['image'] = $marker_object->marker_image;
1117
        if (empty($marker['image'])) {
1118
            $marker['image'] = 'None';
1119
        }
1120
1121
        if ($marker['lat'] != '0' || $marker['lng'] != '0') {
1122
            
1123
            $fields = array();
1124
            foreach ($marker_object->column_fields as $field) {
1125
                $fields[$field] = $marker_object->$field;
1126
            }
1127
            // Define Maps Info Window HTML by Sugar Smarty Template
1128
            $this->sugarSmarty->assign("module_type", 'jjwg_Markers');
1129
            $this->sugarSmarty->assign("fields", $fields); // display fields array
1130
            // Use @ error suppression to avoid issues with SugarCRM On-Demand
1131
            $marker['html'] = @$this->sugarSmarty->fetch('./custom/modules/jjwg_Markers/tpls/MarkersInfoWindow.tpl');
1132
            if (empty($marker['html'])) {
1133
                $marker['html'] = $this->sugarSmarty->fetch('./modules/jjwg_Markers/tpls/MarkersInfoWindow.tpl');
1134
            }
1135
            $marker['html'] = preg_replace('/\n\r/', ' ', $marker['html']);
1136
            //var_dump($marker['html']);
1137
            return $marker;
1138
            
1139
        } else {
1140
            return false;
1141
        }
1142
    }
1143
1144
    /**
1145
     * Get Area Data Custom for Mapping
1146
     * @param $area_object
1147
     */
1148
    function getAreaDataCustom($area_object) {
1149
1150
        // Define Area
1151
        $area = array();
1152
        $area['name'] = $area_object->name;
1153
        if (empty($area['name'])) {
1154
            $area['name'] = 'N/A';
1155
        }
1156
        $area['id'] = $area_object->id;
1157
        $area['coordinates'] = $area_object->coordinates;
1158
        
1159
        // Check for proper coordinates pattern
1160
        if (preg_match('/^[0-9\s\(\)\,\.\-]+$/', $area_object->coordinates)) {
1161
            
1162
            $fields = array();
1163
            foreach ($area_object->column_fields as $field) {
1164
                $fields[$field] = $area_object->$field;
1165
            }
1166
            // Define Maps Info Window HTML by Sugar Smarty Template
1167
            $this->sugarSmarty->assign("module_type", 'jjwg_Areas');
1168
            $this->sugarSmarty->assign("fields", $fields); // display fields array
1169
            // Use @ error suppression to avoid issues with SugarCRM On-Demand
1170
            $area['html'] = @$this->sugarSmarty->fetch('./custom/modules/jjwg_Areas/tpls/AreasInfoWindow.tpl');
1171
            if (empty($area['html'])) {
1172
                $area['html'] = $this->sugarSmarty->fetch('./modules/jjwg_Areas/tpls/AreasInfoWindow.tpl');
1173
            }
1174
            $area['html'] = preg_replace('/\n\r/', ' ', $area['html']);
1175
            //var_dump($marker['html']);
1176
            return $area;
1177
            
1178
        } else {
1179
            return false;
1180
        }
1181
    }
1182
1183
    /**
1184
     * Check for valid longitude
1185
     * @param $lng float
1186
     */
1187
    function is_valid_lng($lng) {
1188
        return (is_numeric($lng) && $lng >= -180 && $lng <= 180);
1189
    }
1190
1191
    /**
1192
     * Check for valid latitude
1193
     * @param $lat float
1194
     */
1195
    function is_valid_lat($lat) {
1196
        return (is_numeric($lat) && $lat >= -90 && $lat <= 90);
1197
    }
1198
1199
}
1200