From 61eb0c440cd4dd036044e6fd1f4f77b7434217bf Mon Sep 17 00:00:00 2001 From: phl0 Date: Thu, 4 Sep 2025 13:54:49 +0200 Subject: [PATCH] Add POTA ref import function --- application/controllers/Adif.php | 68 +++++++++++++++++++++++++ application/models/Logbook_model.php | 64 +++++++++++++++++++++++ application/views/adif/import.php | 28 ++++++++++ application/views/adif/pota_success.php | 45 ++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 application/views/adif/pota_success.php diff --git a/application/controllers/Adif.php b/application/controllers/Adif.php index e3386fe02..014275b87 100644 --- a/application/controllers/Adif.php +++ b/application/controllers/Adif.php @@ -355,6 +355,74 @@ class adif extends CI_Controller { $this->load->view('interface_assets/footer'); } } + + public function pota() { + $this->load->model('stations'); + $data['station_profile'] = $this->stations->all_of_user(); + + $data['page_title'] = __("POTA Import"); + $data['tab'] = "potab"; + + $config['upload_path'] = './uploads/'; + $config['allowed_types'] = 'adi|ADI|adif|ADIF'; + + $this->load->library('upload', $config); + + if ( ! $this->upload->do_upload()) { + $data['error'] = $this->upload->display_errors(); + + $data['max_upload'] = ini_get('upload_max_filesize'); + + $this->load->view('interface_assets/header', $data); + $this->load->view('adif/import', $data); + $this->load->view('interface_assets/footer'); + } else { + $data = array('upload_data' => $this->upload->data()); + + ini_set('memory_limit', '-1'); + set_time_limit(0); + + $this->load->model('logbook_model'); + + if (!$this->load->is_loaded('adif_parser')) { + $this->load->library('adif_parser'); + } + + $this->adif_parser->load_from_file('./uploads/'.$data['upload_data']['file_name']); + + $this->adif_parser->initialize(); + $error_count = array(0, 0, 0); + $custom_errors = ""; + while($record = $this->adif_parser->get_record()) + { + if(count($record) == 0) { + break; + }; + + $pota_result = $this->logbook_model->update_pota($record); + if (!empty($pota_result)) { + switch ($pota_result[0]) { + case 0: + $error_count[0]++; + break; + case 1: + $error_count[1]++; + break; + case 2: + $custom_errors .= $pota_result[1]; + $error_count[2]++; + } + } + }; + unlink('./uploads/'.$data['upload_data']['file_name']); + $data['pota_error_count'] = $error_count; + $data['pota_errors'] = $custom_errors; + $data['page_title'] = __("POTA Data Imported"); + $this->load->view('interface_assets/header', $data); + $this->load->view('adif/pota_success'); + $this->load->view('interface_assets/footer'); + } + } } /* End of file adif.php */ diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 06d92d2e3..807b69894 100644 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -4840,6 +4840,55 @@ class Logbook_model extends CI_Model { } } + function update_pota($record) { + $this->load->model('logbooks_model'); + $custom_date_format = $this->session->userdata('user_date_format'); + $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + + if (isset($record['call'])) { + $call = strtoupper($record['call']); + } else { + return array(3, 'Callsign not found'); + } + + // Join date+time + $time_on = date('Y-m-d', strtotime($record['qso_date'])) . " " . date('H:i', strtotime($record['time_on'])); + + // Store Band + if (isset($record['band'])) { + $band = strtolower($record['band']); + } else { + $band = ''; + } + + if (isset($record['mode'])) { + $mode = $record['mode']; + } else { + $mode = ''; + } + + if (isset($record['pota_ref'])) { + $pota_ref = $record['pota_ref']; + } else { + $pota_ref = ''; + } + + if ($pota_ref != '') { + $sql = "SELECT COL_PRIMARY_KEY, COL_POTA_REF FROM ".$this->config->item('table_name')." WHERE COL_CALL = ? AND COL_TIME_ON >= DATE_ADD(DATE_FORMAT(?, '%Y-%m-%d %H:%i' ), INTERVAL -15 MINUTE) AND COL_TIME_ON <= DATE_ADD(DATE_FORMAT(?, '%Y-%m-%d %H:%i' ), INTERVAL +15 MINUTE) AND UPPER(COL_BAND) = ? AND UPPER(COL_MODE) = ? AND station_id IN ?;"; + $check = $this->db->query($sql, array($call, $time_on, $time_on, strtoupper($band), strtoupper($mode), $logbooks_locations_array)); + if ($check->num_rows() != 1) { + return array(2, $result['message'] = "" . date($custom_date_format, strtotime($record['qso_date'])) . "" . date('H:i', strtotime($record['time_on'])) . "" . str_replace('0', 'Ø', $call) . "" . $band . "" . $mode . "".$pota_ref."" . __("QSO could not be matched") . ""); + } else { + if (str_contains(($check->row()->COL_POTA_REF ?? ''), $pota_ref)) { + return array(1, $result['message'] = "" . date($custom_date_format, strtotime($record['qso_date'])) . "" . date('H:i', strtotime($record['time_on'])) . "" . str_replace('0', 'Ø', $call) . "" . $band . "" . $mode . "".$check->row()->COL_POTA_REF."".$pota_ref."" . __("POTA reference already in log") . ""); + } else { + $this->set_pota_ref($check->row()->COL_PRIMARY_KEY, $check->row()->COL_POTA_REF, $pota_ref); + return array(0, $result['message'] = "" . date($custom_date_format, strtotime($record['qso_date'])) . "" . date('H:i', strtotime($record['time_on'])) . "" . str_replace('0', 'Ø', $call) . "" . $band . "" . $mode . "".$check->row()->COL_POTA_REF."".$pota_ref."" . __("QSO updated") . " (" . $check->row()->COL_POTA_REF . ($check->row()->COL_POTA_REF != '' ? ',' : "") . $pota_ref . ")"); + } + } + } + } + function set_dok($key, $dok) { $data = array( 'COL_DARC_DOK' => $dok, @@ -4850,6 +4899,21 @@ class Logbook_model extends CI_Model { return; } + function set_pota_ref($key, $existing_pota, $new_pota) { + if ($existing_pota == '') { + $data = array( + 'COL_POTA_REF' => $new_pota, + ); + } else { + $data = array( + 'COL_POTA_REF' => $existing_pota.",".$new_pota, + ); + } + $this->db->where(array('COL_PRIMARY_KEY' => $key)); + $this->db->update($this->config->item('table_name'), $data); + return; + } + function get_main_mode_from_mode($mode) { return ($this->get_main_mode_if_submode($mode) == null ? $mode : $this->get_main_mode_if_submode($mode)); } diff --git a/application/views/adif/import.php b/application/views/adif/import.php index 849d7c132..26287befa 100644 --- a/application/views/adif/import.php +++ b/application/views/adif/import.php @@ -35,6 +35,15 @@ echo 'false'; } ?>"> +