From c81c6654a2e23623361f3e926a58c57779ce0d22 Mon Sep 17 00:00:00 2001 From: phl0 Date: Thu, 19 Oct 2023 14:00:20 +0200 Subject: [PATCH] Add Gridmaster award map --- application/controllers/Awards.php | 98 +++++++++ .../language/english/gridsquares_lang.php | 3 +- application/language/english/menu_lang.php | 1 + application/models/Gridmaster_model.php | 138 ++++++++++++ application/models/Logbook_model.php | 7 +- application/views/awards/gridmaster/index.php | 83 ++++++++ application/views/interface_assets/header.php | 2 + .../L.MaidenheadColouredGridmasterMap.js | 201 ++++++++++++++++++ assets/js/sections/gridmaster.js | 134 ++++++++++++ 9 files changed, 664 insertions(+), 3 deletions(-) create mode 100644 application/models/Gridmaster_model.php create mode 100644 application/views/awards/gridmaster/index.php create mode 100644 assets/js/leaflet/L.MaidenheadColouredGridmasterMap.js create mode 100644 assets/js/sections/gridmaster.js diff --git a/application/controllers/Awards.php b/application/controllers/Awards.php index 663518e4..f1624333 100644 --- a/application/controllers/Awards.php +++ b/application/controllers/Awards.php @@ -523,6 +523,104 @@ class Awards extends CI_Controller { $this->load->view('awards/details', $data); } + public function gridmaster() { + $data['page_title']= lang('menu_us_gridmaster'); + + $this->load->model('bands'); + $this->load->model('gridmap_model'); + $this->load->model('stations'); + + $data['homegrid']= explode(',', $this->stations->find_gridsquare()); + + $data['modes'] = $this->gridmap_model->get_worked_modes(); + $data['bands']= $this->bands->get_worked_bands(); + $data['sats_available']= $this->bands->get_worked_sats(); + + $data['layer']= $this->optionslib->get_option('option_map_tile_server'); + + $data['attribution']= $this->optionslib->get_option('option_map_tile_server_copyright'); + + $data['gridsquares_gridsquares']= lang('gridsquares_gridsquares'); + $data['gridsquares_gridsquares_worked']= lang('gridsquares_gridsquares_worked'); + $data['gridsquares_gridsquares_confirmed']= lang('gridsquares_gridsquares_confirmed'); + + $footerData = []; + $footerData['scripts']= [ + 'assets/js/leaflet/geocoding.js', + 'assets/js/leaflet/L.MaidenheadColouredGridmasterMap.js', + 'assets/js/sections/gridmaster.js?' + ]; + + $this->load->view('interface_assets/header',$data); + $this->load->view('awards/gridmaster/index'); + $this->load->view('interface_assets/footer',$footerData); + } + + public function getGridmasterGridsjs() { + $this->load->model('gridmaster_model'); + + $array_grid_4char = array(); + + $array_grid_4char_confirmed = array(); + + $grid_4char = ""; + + $grid_4char_confirmed = ""; + + $query = $this->gridmaster_model->get_confirmed(); + + if ($query && $query->num_rows() > 0) { + foreach ($query->result() as $row) { + $grid_4char_confirmed = strtoupper(substr($row->GRID_SQUARES,0,4)); + + if(!in_array($grid_4char_confirmed, $array_grid_4char_confirmed)){ + array_push($array_grid_4char_confirmed, $grid_4char_confirmed); + } + + } + } + + $query = $this->gridmaster_model->get_worked(); + + if ($query && $query->num_rows() > 0) { + foreach ($query->result() as $row) { + + $grid_four = strtoupper(substr($row->GRID_SQUARES,0,4)); + + if(!in_array($grid_four, $array_grid_4char)){ + array_push($array_grid_4char, $grid_four); + } + + } + } + + $vucc_grids = $this->gridmaster_model->get_vucc_confirmed(); + + foreach($vucc_grids as $key) { + $grid_four_confirmed = strtoupper(substr($key,0,4)); + + if(!in_array($grid_four_confirmed, $array_grid_4char_confirmed)){ + array_push($array_grid_4char_confirmed, $grid_four_confirmed); + } + } + + $vucc_grids = $this->gridmaster_model->get_vucc_worked(); + + foreach($vucc_grids as $key) { + $grid_four = strtoupper(substr($key,0,4)); + + if(!in_array($grid_four, $array_grid_4char)){ + array_push($array_grid_4char, $grid_four); + } + } + + $data['grid_4char_confirmed'] = ($array_grid_4char_confirmed); + $data['grid_4char'] = ($array_grid_4char); + + header('Content-Type: application/json'); + echo json_encode($data); + } + /* Handles showing worked Sigs Adif fields: my_sig diff --git a/application/language/english/gridsquares_lang.php b/application/language/english/gridsquares_lang.php index ae5e2867..b1a3162d 100644 --- a/application/language/english/gridsquares_lang.php +++ b/application/language/english/gridsquares_lang.php @@ -21,7 +21,8 @@ $lang['gridsquares_confirmation'] = 'Confirmation'; $lang['gridsquares_button_plot'] = 'Plot'; $lang['gridsquares_gridsquares'] = 'Gridsquares'; +$lang['gridsquares_gridsquares_worked'] = 'Gridsquares worked'; $lang['gridsquares_gridsquares_confirmed'] = 'Gridsquares confirmed'; $lang['gridsquares_gridsquares_not_confirmed'] = 'Gridsquares not confirmed'; $lang['gridsquares_gridsquares_total_worked'] = 'Total gridsquares worked'; -$lang['gridsquares_gridsquares_total_activated'] = 'Total gridsquares activated'; \ No newline at end of file +$lang['gridsquares_gridsquares_total_activated'] = 'Total gridsquares activated'; diff --git a/application/language/english/menu_lang.php b/application/language/english/menu_lang.php index 4d6acd63..52a06af3 100644 --- a/application/language/english/menu_lang.php +++ b/application/language/english/menu_lang.php @@ -42,6 +42,7 @@ $lang['menu_pota'] = 'POTA'; $lang['menu_sig'] = 'SIG'; $lang['menu_sota'] = 'SOTA'; $lang['menu_us_counties'] = 'US Counties'; +$lang['menu_us_gridmaster'] = 'US Gridmaster'; $lang['menu_vucc'] = 'VUCC'; $lang['menu_was'] = 'WAS'; $lang['menu_wwff'] = 'WWFF'; diff --git a/application/models/Gridmaster_model.php b/application/models/Gridmaster_model.php new file mode 100644 index 00000000..6bf9d7b7 --- /dev/null +++ b/application/models/Gridmaster_model.php @@ -0,0 +1,138 @@ +load->model('logbooks_model'); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; + + $sql = 'SELECT distinct substring(COL_GRIDSQUARE,1,4) as GRID_SQUARES FROM ' + .$this->config->item('table_name') + .' WHERE station_id in ('.$location_list.')' + ." and COL_LOTW_QSL_RCVD = 'Y'" + ." and COL_PROP_MODE = 'SAT'" + .' AND substring(COL_GRIDSQUARE,1,4) in (\''.implode('\',\'', $this->us_grids).'\')'; + return $this->db->query($sql); + } + + function get_worked() { + $CI =& get_instance(); + $CI->load->model('logbooks_model'); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; + + $sql = 'SELECT distinct substring(COL_GRIDSQUARE,1,4) as GRID_SQUARES FROM ' + .$this->config->item('table_name') + .' WHERE station_id in ('.$location_list.')' + ." and COL_PROP_MODE = 'SAT'" + .' AND substring(COL_GRIDSQUARE,1,4) in (\''.implode('\',\'', $this->us_grids).'\')'; + return $this->db->query($sql); + } + + function get_vucc_confirmed() { + $CI =& get_instance(); + $CI->load->model('logbooks_model'); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; + + $sql = 'SELECT distinct COL_VUCC_GRIDS as VUCC_GRIDS FROM ' + .$this->config->item('table_name') + .' WHERE station_id in ('.$location_list.')' + ." and COL_LOTW_QSL_RCVD = 'Y'" + ." and COL_VUCC_GRIDS != ''" + ." and COL_VUCC_GRIDS IS NOT NULL" + ." and COL_PROP_MODE = 'SAT'"; + $query = $this->db->query($sql); + $vucc_grids = []; + foreach ($query->result() as $row) { + $grids = explode(',', $row->VUCC_GRIDS); + foreach ($grids as $grid) { + if (in_array(substr($grid, 0, 4), $this->us_grids)) { + if (!in_array(substr($grid, 0, 4), $vucc_grids)) { + $vucc_grids[] = substr($grid, 0, 4); + } + } + } + } + return $vucc_grids; + } + + function get_vucc_worked() { + $CI =& get_instance(); + $CI->load->model('logbooks_model'); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; + + $sql = 'SELECT distinct COL_VUCC_GRIDS as VUCC_GRIDS FROM ' + .$this->config->item('table_name') + .' WHERE station_id in ('.$location_list.')' + ." and COL_VUCC_GRIDS != ''" + ." and COL_VUCC_GRIDS IS NOT NULL" + ." and COL_PROP_MODE = 'SAT'"; + $query = $this->db->query($sql); + $vucc_grids = []; + foreach ($query->result() as $row) { + $grids = explode(',', $row->VUCC_GRIDS); + foreach ($grids as $grid) { + if (in_array(substr($grid, 0, 4), $this->us_grids)) { + if (!in_array(substr($grid, 0, 4), $vucc_grids)) { + $vucc_grids[] = substr($grid, 0, 4); + } + } + } + } + return $vucc_grids; + } + +} diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index cff40679..d0917603 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -420,14 +420,17 @@ class Logbook_model extends CI_Model { $this->db->where($sql); } - if ($mode != 'All') { + if ($mode != 'All' && $mode != '') { $this->db->where("(COL_MODE='" . $mode . "' OR COL_SUBMODE='" . $mode ."')"); } $this->db->order_by("COL_TIME_ON", "desc"); $this->db->limit(500); - return $this->db->get($this->config->item('table_name')); + $result = $this->db->get($this->config->item('table_name')); + log_message('debug', 'SQL: '.$this->db->last_query()); + return $result; + //return $this->db->get($this->config->item('table_name')); } public function activated_grids_qso_details($searchphrase, $band, $mode){ diff --git a/application/views/awards/gridmaster/index.php b/application/views/awards/gridmaster/index.php new file mode 100644 index 00000000..4a390d0b --- /dev/null +++ b/application/views/awards/gridmaster/index.php @@ -0,0 +1,83 @@ + + + +
+ +
+ +

+ + session->flashdata('message')) { ?> + +
+

session->flashdata('message'); ?>

+
+ +
+ +
+
+
+
+
Latitude:
+
+
Longitude:
+
+
Gridsquare:
+
+
Distance:
+
+
Bearing:
+
+
+ diff --git a/application/views/interface_assets/header.php b/application/views/interface_assets/header.php index fcd2af2f..4bf71553 100644 --- a/application/views/interface_assets/header.php +++ b/application/views/interface_assets/header.php @@ -147,6 +147,8 @@ + + diff --git a/assets/js/leaflet/L.MaidenheadColouredGridmasterMap.js b/assets/js/leaflet/L.MaidenheadColouredGridmasterMap.js new file mode 100644 index 00000000..081814ea --- /dev/null +++ b/assets/js/leaflet/L.MaidenheadColouredGridmasterMap.js @@ -0,0 +1,201 @@ +/* + * L.Maidenhead displays a Maidenhead Locator of lines on the map. + */ + +L.Maidenhead = L.LayerGroup.extend({ + + + options: { + // Line and label color + color: 'rgba(255, 0, 0, 0.4)', + + // Redraw on move or moveend + redraw: 'move' + }, + + initialize: function (options) { + L.LayerGroup.prototype.initialize.call(this); + L.Util.setOptions(this, options); + }, + + onAdd: function (map) { + this._map = map; + var grid = this.redraw(); + this._map.on('viewreset '+ this.options.redraw, function () { + grid.redraw(); + }); + + this.eachLayer(map.addLayer, map); + }, + + onRemove: function (map) { + // remove layer listeners and elements + map.off('viewreset '+ this.options.redraw, this.map); + this.eachLayer(this.removeLayer, this); + }, + + redraw: function () { + var d3 = new Array(20, 10, 10, 1, 1, 1, 1, 1, 1, 1, 1 / 24, 1 / 24, 1 / 24, 1 / 24, 1 / 24, 1 / 240, 1 / 240, 1 / 240, 1 / 240, 1 / 240 / 24, 1 / 240 / 24); + var lat_cor = new Array(0, 8, 8, 8, 2.5, 2.2, 6, 8, 8, 8, 1.4, 2.5, 3, 3.5, 4, 4, 3.5, 3.5, 3, 1.8, 1.6); // Used for gridsquare text offset + var bounds = map.getBounds(); + var zoom = map.getZoom(); + console.log(zoom); + var unit = d3[zoom]; + var lcor = lat_cor[zoom]; + var w = bounds.getWest(); + var e = bounds.getEast(); + var n = bounds.getNorth(); + var s = bounds.getSouth(); + var field_lat_cor = new Array(0, 8, 8, 9, 8, 7, 6, 8, 8, 8, 1.4, 2.5, 3, 3.5, 4, 4, 3.5, 3.5, 3, 1.8, 1.6); // Used for field text offset + var field_cor = field_lat_cor[zoom]; + if (zoom==1) {var c = 2;} else {var c = 0.2;} // Height offset + if (n > 85) n = 85; + if (s < -85) s = -85; + var left = Math.floor(w/(unit*2))*(unit*2); + var right = Math.ceil(e/(unit*2))*(unit*2); + var top = Math.ceil(n/unit)*unit; + var bottom = Math.floor(s/unit)*unit; + this.eachLayer(this.removeLayer, this); + + for (var lon = left; lon < right; lon += (unit*2)) { + if (lon > -180 || lon < 180) { + for (var lat = bottom; lat < top; lat += unit) { + var bounds = [[lat,lon],[lat+unit,lon+(unit*2)]]; + var locator = this._getLocator(lon,lat); + + if(grid_four.includes(locator)) { + + if(grid_four_confirmed.includes(locator)) { + var rectConfirmed = L.rectangle(bounds, {className: 'grid-rectangle grid-confirmed', color: 'rgba(144,238,144, 0.6)', weight: 1, fillOpacity: 1, fill:true, interactive: false}); + this.addLayer(rectConfirmed); + } else { + var rectWorked = L.rectangle(bounds, {className: 'grid-rectangle grid-worked', color: this.options.color, weight: 1, fillOpacity: 1, fill:true, interactive: false}) + this.addLayer(rectWorked); + } + // Controls text on grid on various zoom levels + this.addLayer(this._getLabel(lon+unit-(unit/lcor),lat+(unit/2)+(unit/lcor*c))); + } + } + } + } + // Added this to print fields and field name, while still showing worked/confirmed gridsquares + unit = 10; + var left = Math.floor(w / (unit * 2)) * (unit * 2); + var right = Math.ceil(e / (unit * 2)) * (unit * 2); + var top = Math.ceil(n / unit) * unit; + var bottom = Math.floor(s / unit) * unit; + for (var lon = left; lon < right; lon += (unit * 2)) { + for (var lat = bottom; lat < top; lat += unit) { + var bounds = [[lat, lon], [lat + unit, lon + (unit * 2)]]; + + this.addLayer(L.rectangle(bounds, { + className: 'grid-rectangle', + color: this.options.color, + weight: 1, + fill: false, + interactive: false + })); + } + } + return this; + }, + + _getLabel: function(lon,lat) { + var title_size = new Array(0, 10, 14, 16, 8.5, 13, 14, 16, 24, 36, 12, 14, 20, 36, 60, 12, 20, 36, 60, 12, 24); // Controls text size on labels + var zoom = map.getZoom(); + var size = title_size[zoom]+'px'; + var title = ''; + var locator = this._getLocator(lon,lat); + if (zoom != 3) { + title = '' + locator + ''; + } + var myIcon = L.divIcon({className: 'my-div-icon', html: title}); + var marker = L.marker([lat,lon], {icon: myIcon}, clickable=false); + if (zoom == 4 || zoom == 3) { + marker.bindTooltip(locator); + } + if (typeof gridsquaremap !== 'undefined' && gridsquaremap == true) { + marker.on('click', function(event) { + spawnGridsquareModal(locator); + }); + } + return marker; + }, + + _getLocator: function(lon,lat) { + var ydiv_arr=new Array(10, 1, 1/24, 1/240, 1/240/24); + var d1 = "ABCDEFGHIJKLMNOPQR".split(""); + var d2 = "ABCDEFGHIJKLMNOPQRSTUVWX".split(""); + var d4 = new Array(0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5); + var locator = ""; + var x = lon; + var y = lat; + var precision = d4[map.getZoom()]; + while (x < -180) {x += 360;} + while (x > 180) {x -=360;} + x = x + 180; + y = y + 90; + locator = locator + d1[Math.floor(x/20)] + d1[Math.floor(y/10)]; + for (var i=0; i<4; i=i+1) { + if (precision > i+1) { + rlon = x%(ydiv_arr[i]*2); + rlat = y%(ydiv_arr[i]); + if ((i%2)==0) { + locator += Math.floor(rlon/(ydiv_arr[i+1]*2)) +""+ Math.floor(rlat/(ydiv_arr[i+1])); + } else { + locator += d2[Math.floor(rlon/(ydiv_arr[i+1]*2))] +""+ d2[Math.floor(rlat/(ydiv_arr[i+1]))]; + } + } + } + return locator; + }, + + /* + Need this for the field printing, while showing worked/confirmed grids + */ + _getLabel2: function(lon,lat) { + var title_size = new Array(0, 10, 12, 16, 20, 26, 26, 16, 24, 36, 12, 14, 20, 36, 60, 12, 20, 36, 60, 12, 24); + var zoom = map.getZoom(); + var size = title_size[zoom]+'px'; + var title = '' + this._getLocator2(lon,lat) + ''; + var myIcon = L.divIcon({className: 'my-div-icon', html: title}); + var marker = L.marker([lat,lon], {icon: myIcon}, clickable=false); + return marker; + }, + + /* + Need this for the field printing, while showing worked/confirmed grids + */ + _getLocator2: function(lon,lat) { + var ydiv_arr=new Array(10, 1, 1/24, 1/240, 1/240/24); + var d1 = "ABCDEFGHIJKLMNOPQR".split(""); + var d2 = "ABCDEFGHIJKLMNOPQRSTUVWX".split(""); + var d4 = new Array(0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5); + var locator = ""; + var x = lon; + var y = lat; + var precision = d4[map.getZoom()]; + while (x < -180) {x += 360;} + while (x > 180) {x -=360;} + x = x + 180; + y = y + 90; + locator = locator + d1[Math.floor(x/20)] + d1[Math.floor(y/10)]; + for (var i=0; i<4; i=i+1) { + if (precision > i+1) { + rlon = x%(ydiv_arr[i]*2); + rlat = y%(ydiv_arr[i]); + if ((i%2)==0) { + locator += Math.floor(rlon/(ydiv_arr[i+1]*2)) +""+ Math.floor(rlat/(ydiv_arr[i+1])); + } else { + locator += d2[Math.floor(rlon/(ydiv_arr[i+1]*2))] +""+ d2[Math.floor(rlat/(ydiv_arr[i+1]))]; + } + } + } + return locator; + }, + +}); + +L.maidenhead = function (options) { + return new L.Maidenhead(options); +}; diff --git a/assets/js/sections/gridmaster.js b/assets/js/sections/gridmaster.js new file mode 100644 index 00000000..ed2c99f4 --- /dev/null +++ b/assets/js/sections/gridmaster.js @@ -0,0 +1,134 @@ +var map; +var grid_two = ''; +var grid_four = ''; +var grid_six = ''; +var grid_two_confirmed = ''; +var grid_four_confirmed = ''; +var grid_six_confirmed = ''; + +function gridPlot(form) { + $(".ld-ext-right-plot").addClass('running'); + $(".ld-ext-right-plot").prop('disabled', true); + $('#plot').prop("disabled", true); + // If map is already initialized + var container = L.DomUtil.get('gridsquare_map'); + + if(container != null){ + container._leaflet_id = null; + container.remove(); + $("#gridmapcontainer").append('
'); + } + + ajax_url = site_url + '/awards/getGridmasterGridsjs'; + + $.ajax({ + url: ajax_url, + type: 'get', + success: function (data) { + $('.cohidden').show(); + $(".ld-ext-right-plot").removeClass('running'); + $(".ld-ext-right-plot").prop('disabled', false); + $('#plot').prop("disabled", false); + grid_four = data.grid_4char; + grid_four_confirmed = data.grid_4char_confirmed; + var layer = L.tileLayer(jslayer, { + maxZoom: 12, + attribution: jsattribution, + id: 'mapbox.streets' + }); + + map = L.map('gridsquare_map', { + layers: [layer], + center: [38, -95], + zoom: 5, + minZoom: 5, + maxZoom: 5, + dragging: false, + fullscreenControl: true, + fullscreenControlOptions: { + position: 'topleft' + }, + }); + + var printer = L.easyPrint({ + tileLayer: layer, + sizeModes: ['Current'], + filename: 'myMap', + exportOnly: true, + hideControlContainer: true + }).addTo(map); + + /*Legend specific*/ + var legend = L.control({ position: "topright" }); + + legend.onAdd = function(map) { + var div = L.DomUtil.create("div", "legend"); + div.innerHTML += "

" + gridsquares_gridsquares + "

"; + div.innerHTML += '' + gridsquares_gridsquares_worked + ' ('+(grid_four.length)+')
'; + div.innerHTML += '' + gridsquares_gridsquares_confirmed + ' ('+grid_four_confirmed.length+')
'; + return div; + }; + + legend.addTo(map); + + var maidenhead = L.maidenhead().addTo(map); + map.on('mousemove', onMapMove); + + }, + error: function (data) { + }, + }); +} + +function spawnGridsquareModal(loc_4char) { + var ajax_data = ({ + 'Searchphrase': loc_4char, + 'Band': 'SAT', + 'Type': 'VUCC' + }) + $.ajax({ + url: base_url + 'index.php/awards/qso_details_ajax', + type: 'post', + data: ajax_data, + success: function (html) { + BootstrapDialog.show({ + title: lang_general_word_qso_data, + cssClass: 'qso-dialog', + size: BootstrapDialog.SIZE_WIDE, + nl2br: false, + message: html, + onshown: function(dialog) { + + $('[data-toggle="tooltip"]').tooltip(); + $('.contacttable').DataTable({ + "pageLength": 25, + responsive: false, + ordering: false, + "scrollY": "550px", + "scrollCollapse": true, + "paging": false, + "scrollX": true, + dom: 'Bfrtip', + buttons: [ + 'csv' + ] + }); + // change color of csv-button if dark mode is chosen + if (isDarkModeTheme()) { + $(".buttons-csv").css("color", "white"); + } + }, + buttons: [{ + label: 'Close', + action: function(dialogItself) { + dialogItself.close(); + } + }] + }); + } + }); +} + +$(document).ready(function(){ + gridPlot(this.form); +})