From 1e863ade5d0f72670ac2f549b189f7bc1415c70e Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Mon, 14 Jul 2025 14:02:20 +0200 Subject: [PATCH] [QSO entry] Custom date format --- application/controllers/Logbook.php | 19 +++++++++- application/models/Logbook_model.php | 52 ++++++++++++++++++++------ application/views/qso/index.php | 34 +++++++++++++++-- assets/js/sections/qso.js | 55 +++++++++++++++++++++++++--- 4 files changed, 136 insertions(+), 24 deletions(-) diff --git a/application/controllers/Logbook.php b/application/controllers/Logbook.php index b264715ce..f9247db90 100644 --- a/application/controllers/Logbook.php +++ b/application/controllers/Logbook.php @@ -76,8 +76,23 @@ class Logbook extends CI_Controller { function json($tempcallsign, $tempband, $tempmode, $tempstation_id = null, $date = "", $count = 5) { session_write_close(); - if (($date ?? '') != '') { - $date=date("Y-m-d",strtotime($date)); + + // Normalize the date only if it's not empty + if (!empty($date)) { + // Get user-preferred date format + if ($this->session->userdata('user_date_format')) { + $date_format = $this->session->userdata('user_date_format'); + } else { + $date_format = $this->config->item('qso_date_format'); + } + $date = urldecode($date); + $dt = DateTime::createFromFormat($date_format, $date); + if ($dt !== false) { + $date = $dt->format('Y-m-d'); // or any normalized format + } else { + // Invalid date for the expected format, handle gracefully + $date = null; + } } // Cleaning for security purposes $callsign = $this->security->xss_clean($tempcallsign); diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index bd0cda7c0..b16c04d5d 100644 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -17,20 +17,48 @@ class Logbook_model extends CI_Model { /* Add QSO to Logbook */ function create_qso() { + // Get user-preferred date format + if ($this->session->userdata('user_date_format')) { + $date_format = $this->session->userdata('user_date_format'); + } else { + $date_format = $this->config->item('qso_date_format'); + } + + // Get input values + $start_date = $this->input->post('start_date'); // e.g., "14/07/2025" + $start_time = $this->input->post('start_time'); // e.g., "08:11:36" + $end_time = $this->input->post('end_time'); // e.g., "00:05:00" (optional) $callsign = trim(str_replace('Ø', '0', $this->input->post('callsign'))); - // Join date+time - $datetime = date("Y-m-d", strtotime($this->input->post('start_date'))) . " " . $this->input->post('start_time'); - if (($this->input->post('end_time') ?? '') != '') { - $datetime_off = date("Y-m-d", strtotime($this->input->post('start_date'))) . " " . $this->input->post('end_time'); - // if time off < time on, and time off is on 00:xx >> add 1 day (concidering start and end are between 23:00 and 00:59) // - $_tmp_datetime_off = strtotime($datetime_off); - if (($_tmp_datetime_off < strtotime($datetime)) && (substr($this->input->post('end_time'), 0, 2) == "00")) { - $datetime_off = date("Y-m-d H:i:s", ($_tmp_datetime_off + 60 * 60 * 24)); - } + + // Parse datetime using createFromFormat + $datetime_obj = DateTime::createFromFormat("$date_format H:i:s", "$start_date $start_time"); + + if ($datetime_obj === false) { + // Handle parse error gracefully (optional: log error) + $datetime = null; + $datetime_off = null; } else { - $datetime_off = $datetime; + $datetime = $datetime_obj->format('Y-m-d H:i:s'); // Standard format for DB + + // Handle end time + if (!empty($end_time)) { + $end_datetime_obj = DateTime::createFromFormat("$date_format H:i:s", "$start_date $end_time"); + + if ($end_datetime_obj === false) { + $datetime_off = $datetime; + } else { + // If time-off is before time-on and hour is 00 → add 1 day + if ($end_datetime_obj < $datetime_obj && str_starts_with($end_time, "00")) { + $end_datetime_obj->modify('+1 day'); + } + $datetime_off = $end_datetime_obj->format('Y-m-d H:i:s'); + } + } else { + $datetime_off = $datetime; + } } + if ($this->input->post('prop_mode') != null) { $prop_mode = $this->input->post('prop_mode'); } else { @@ -181,7 +209,7 @@ class Logbook_model extends CI_Model { // Represent cnty with "state,cnty" only for USA // Others do no need it - + if ($this->input->post('county') && $this->input->post('input_state')) { switch ($dxcc_id) { case 6: @@ -1320,7 +1348,7 @@ class Logbook_model extends CI_Model { } else { // nothing from above? $uscounty = null; } - + } else { $retvals['detail']=__("DXCC has to be Numeric"); return $retvals; diff --git a/application/views/qso/index.php b/application/views/qso/index.php index cf246e6ef..93586d393 100644 --- a/application/views/qso/index.php +++ b/application/views/qso/index.php @@ -1,4 +1,29 @@