1<?php
2
3#
4# Prints map of all Circuits
5#
6
7# perm check
8$User->check_module_permissions ("circuits", 1, true, false);
9
10# title
11if(isset($_GET['map_specific']) && $_GET['map_specific'] == 'true'){
12    print "<h3>"._('Map of circuits')."</h3>";
13    $circuits_to_map = json_decode($_GET['circuits_to_map'], true);
14}else{
15    print "<h3>"._('Map of all circuits')."</h3>";
16}
17
18
19// fetch all circuits and types. Create a hash of the types to avoid lots of queries
20$circuits = $Tools->fetch_all_circuits();
21$circuit_types = $Tools->fetch_all_objects ("circuitTypes", "ctname");
22$type_hash = [];
23foreach($circuit_types as $t){
24    $type_hash[$t->id] = $t;
25}
26
27//Fetch all locations and store info hash, same as above.
28//This will elimate the need of looping through circuits the first time
29$locations = $Tools->fetch_all_objects("locations");
30$all_locations = [];
31
32foreach($locations as $l){ $all_locations[$l->id] = $l; }
33
34// check
35if ($User->settings->enableLocations=="1" && strlen(Config::get('gmaps_api_key'))==0) {
36    $Result->show("info text-center nomargin", _("Location: Google Maps API key is unset. Please configure config.php \$gmaps_api_key to enable."));
37}
38//elseif ($locA->name!=="/" && $locB->name!=="/") { ?
39elseif(true) {
40    // get all
41    if(sizeof($all_locations)>0) {
42        foreach ($all_locations as $k=>$l) {
43            // map used
44            if(strlen($l->long)==0 && strlen($l->lat)==0 && strlen($l->address)==0 ) {
45                // map not used
46                unset($all_locations[$k]);
47            }
48            // recode
49            elseif (strlen($l->long)==0 && strlen($l->lat)==0 && strlen($l->address)>0) {
50                $latlng = $Tools->get_latlng_from_address ($l->address);
51                if($latlng['lat']==NULL || $latlng['lng']==NULL) {
52                    unset($all_locations[$k]);
53                }
54                else {
55                    // save
56                    $Tools->update_latlng ($l->id, $latlng['lat'], $latlng['lng']);
57                    $all_locations[$k]->lat = $latlng['lat'];
58                    $all_locations[$k]->long = $latlng['lng'];
59                }
60            }
61        }
62    }
63
64    // print
65    if (sizeof($all_locations)>0) { ?>
66        <script type="text/javascript">
67            $(document).ready(function() {
68                // init gmaps
69                var map = new GMaps({
70                  div: '#gmap',
71                  zoom: 15,
72                  lat: '<?php print $all_locations[0]->lat; ?>',
73                  lng: '<?php print $all_locations[0]->long; ?>'
74                });
75
76                var bounds = [];
77                var lineSymbol = {
78                    path: 'M 0,-1 0,1',
79                    strokeOpacity: 1,
80                    scale: 4
81                      };
82
83
84
85
86                // add markers
87                <?php
88                $html        = array();
89
90                $map_marker_location_ids = array();
91                //Instead of using the locations as the base of the loop, use the circuits to put more info on the map
92                //all_locations is now a hash based on database IDs, so no worry about worrying about duplicates in the array
93
94                //Add all locations to map
95                foreach ($all_locations as $k=>$location) {
96                    // description and apostrophe fix
97                    $description = str_replace(array("\r\n","\n","\r"), "<br>", escape_input($location->description));
98                    $description = !empty($description) ? "<span class=\'text-muted\'>".$description."</span>" : "";
99
100                    // Don't generate duplicate map markers
101                    if (!in_array($location->id, $map_marker_location_ids, true)) {
102                        array_push($map_marker_location_ids, $location->id);
103
104                        $html[] = "map.addMarker({";
105                        $html[] = " title: '". escape_input($location->name). "',";
106                        $html[] = " lat: '$location->lat',";
107                        $html[] = " lng: '$location->long',";
108                        $html[] = $k % 2 == 0 ? " icon: 'css/images/red-dot.png'," : " icon: 'css/images/blue-dot.png',";
109                        $html[] = " infoWindow: {";
110                        $html[] = "    content: '<h5><a href=\'".create_link("tools", "locations", $location->id)."\'>". escape_input($location->name). "</a></h5>$description'";
111                        $html[] = "}";
112                        $html[] = "});";
113                    }
114                    /*
115                    if($k % 2 == 0) {
116                        $html[] = "path = [[".$all_locations[$k+1]->lat.", ".$all_locations[$k+1]->long."], [".$all_locations[$k]->lat.", ".$all_locations[$k]->long."]]";
117                        $html[] = "map.drawPolyline({";
118                        $html[] = "  path: path,";
119                        $html[] = "  strokeColor: '#131540',";
120                        $html[] = "  strokeOpacity: 0.6,";
121                        $html[] = "  strokeWeight: 3";
122                        $html[] = "});";
123                    }
124                    */
125                }
126                //Add all circuits to map with type information
127                foreach ($circuits as $circuit) {
128                  //If map_spepcifc is set and its in the array OR it isn't set, map all
129                  if((isset($_GET['map_specific']) && in_array($circuit->id,$circuits_to_map)) || (!isset($_GET['map_specific']))){
130                    $html[] = "path = [[".$all_locations[$circuit->location1]->lat.", ".$all_locations[$circuit->location1]->long."], [".$all_locations[$circuit->location2]->lat.", ".$all_locations[$circuit->location2]->long."]]";
131                    $html[] = "map.drawPolyline({";
132                    $html[] = "  path: path,";
133                    $html[] = "  strokeColor: '".$type_hash[$circuit->type]->ctcolor."',";
134                    if($type_hash[$circuit->type]->ctpattern == "Dotted") { $html[] = "  strokeOpacity: 0,"; }
135                    else{ $html[] = "  strokeOpacity: 0.6,"; }
136                    if($type_hash[$circuit->type]->ctpattern == "Dotted") {
137                      $html[] = " icons:[{
138                        icon: lineSymbol,
139                        offset: '0',
140                        repeat: '20px'
141                        }],"; }
142                    $html[] = "  strokeWeight: 3";
143                    $html[] = "});";
144
145                    print implode("\n", $html);
146                  }
147                }
148                ?>
149
150                // fit zoom
151                <?php if(sizeof($all_locations)>1) { ?>
152                map.fitZoom();
153                <?php } ?>
154                // resize map function
155                <?php if(!isset($height)) { ?>
156                function resize_map () {
157                    var heights = window.innerHeight - 320;
158                    $('#map_overlay').css("height", heights+"px");
159                }
160                resize_map();
161                window.onresize = function() {
162                    resize_map();
163                };
164                <?php } ?>
165            });
166        </script>
167
168        <div style="width:100%; height:<?php print isset($height) ? $height : "1000px";?>;" id="map_overlay">
169        	<div id="gmap" style="width:100%; height:100%;"></div>
170        </div>
171
172
173        <?php
174        print "<hr>";
175        print "<div class='text-right'>";
176        print "<h5>"._('Circuit Type Legend')."</h5>";
177        foreach($circuit_types as $t){
178          print "<span class='badge badge1'  style='color:white;background:$t->ctcolor !important'></i>$t->ctname ($t->ctpattern Line)</span>";
179        }
180        print "</div>";
181        ?>
182
183        <?php
184        # no coordinates
185        }
186        else {
187            $Result->show("info","No Locations with coordinates configured", false);
188        }
189}
190else {
191	$Result->show("danger", _("Location not set"), true);
192}
193