diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 01cfccedf..418149602 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -728,6 +728,74 @@ class API extends CI_Controller { } + // API function to check if a grid is in the logbook already + function logbook_get_worked_grids() { + $arr = array(); + header('Content-type: application/json'); + $this->load->model('api_model'); + $obj = json_decode(file_get_contents("php://input"), true); + if ($obj === NULL) { + echo json_encode(['status' => 'failed', 'reason' => "wrong JSON"]); + die(); + } + // Check rate limit + $identifier = isset($obj['key']) ? $obj['key'] : null; + $this->check_rate_limit('logbook_get_worked_grids', $identifier); + + if(!isset($obj['key']) || $this->api_model->authorize($obj['key']) == 0) { + http_response_code(401); + echo json_encode(['status' => 'failed', 'reason' => "missing api key"]); + die(); + } + if(!isset($obj['logbook_public_slug'])) { + http_response_code(400); + echo json_encode(['status' => 'failed', 'reason' => "missing fields"]); + return; + } + if($obj['logbook_public_slug'] != "") { + $logbook_slug = $obj['logbook_public_slug']; + if(isset($obj['band'])) { + $band = $obj['band']; + } else { + $band = null; + } + if(isset($obj['cnfm'])) { + $cnfm = $obj['cnfm']; + } else { + $cnfm = null; + } + $this->load->model('logbooks_model'); + if($this->logbooks_model->public_slug_exists($logbook_slug)) { + $logbook_id = $this->logbooks_model->public_slug_exists_logbook_id($logbook_slug); + if($logbook_id != false) + { + $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($logbook_id); + if (!$logbooks_locations_array) { + http_response_code(404); + echo json_encode(['status' => 'failed', 'reason' => "Empty Logbook"]); + die(); + } + } else { + http_response_code(404); + echo json_encode(['status' => 'failed', 'reason' => $logbook_slug." has no associated station locations"]); + die(); + } + $this->load->model('logbook_model'); + + $arr = $this->api_model->get_grids_worked_in_logbook($logbooks_locations_array, $band, $cnfm); + http_response_code(201); + echo json_encode($arr); + + } else { + http_response_code(404); + echo json_encode(['status' => 'failed', 'reason' => "logbook not found"]); + die(); + } + + } + + } + /* ENDPOINT for Rig Control */ function radio() { diff --git a/application/models/Api_model.php b/application/models/Api_model.php index b97d4753c..eda1493dc 100644 --- a/application/models/Api_model.php +++ b/application/models/Api_model.php @@ -178,5 +178,68 @@ class API_Model extends CI_Model { "; return $this->db->query($sql, $binding); + + } + + function get_grids_worked_in_logbook($StationLocationsArray = null, $band = null, $cnfm = null) { + $grid_array = []; + if ($StationLocationsArray == null) { + $this->load->model('logbooks_model'); + $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + } else { + $logbooks_locations_array = $StationLocationsArray; + } + + $bindings = []; + $subsql = ''; + $band = ($band == 'All') ? null : $band; + if ($band != null && $band != 'SAT') { + $subsql .= ' AND COL_BAND = ? AND COL_PROP_MODE != "SAT"'; + $bindings[] = $band; + } else if ($band == 'SAT') { + $subsql .= ' AND COL_SAT_NAME != ""'; + } + switch ($cnfm) { + case 'qsl': + $subsql .= ' AND COL_QSL_RCVD = "Y"'; + break; + case 'lotw': + $subsql .= ' AND COL_LOTW_QSL_RCVD = "Y"'; + break; + case 'eqsl': + $subsql .= ' AND COL_EQSL_QSL_RCVD = "Y"'; + break; + } + + $ids = array_map('intval', $logbooks_locations_array); + $sql = 'SELECT DISTINCT UPPER(SUBSTR(COL_GRIDSQUARE, 1, 4)) AS gridsquare FROM ' . $this->config->item('table_name') . ' thcv '; + $sql .= ' WHERE COL_GRIDSQUARE <> "" AND CHAR_LENGTH(COL_GRIDSQUARE) >= 4'; + $sql .= ' AND station_id IN (' . implode(',', $ids) . ')'; + $sql .= $subsql; + $sql .= ' ORDER BY gridsquare ASC;'; + $query = $this->db->query($sql,$bindings); + foreach($query->result() as $line) { + $grid_array[] = $line->gridsquare; + } + // Get and add VUCC grids + $sql = 'SELECT DISTINCT UPPER(COL_VUCC_GRIDS) AS vuccgrids FROM ' . $this->config->item('table_name') . ' thcv '; + $sql .= ' WHERE COL_VUCC_GRIDS <> ""'; + $sql .= ' AND station_id IN (' . implode(',', $ids) . ')'; + $sql .= $subsql; + $sql .= ' ORDER BY vuccgrids ASC;'; + $query = $this->db->query($sql,$bindings); + foreach($query->result() as $line) { + $vucc_grids = explode(',', $line->vuccgrids); + foreach ($vucc_grids as $vucc_grid) { + if (strlen($vucc_grid) >= 4) { + $grid = substr($vucc_grid, 0, 4); + if (! in_array($grid, $grid_array)) { + $grid_array[] = $grid; + } + } + } + } + sort ($grid_array); + return $grid_array; } }