From 20235d9874d6bd736f9ca2ebdf1211c8af6414f1 Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Thu, 27 Jun 2024 17:45:29 +0200 Subject: [PATCH] moved hrdlog upload from controller to model --- application/controllers/Hrdlog.php | 121 ++++----------------------- application/models/Hrdlog_model.php | 103 +++++++++++++++++++++++ application/models/Logbook_model.php | 2 +- 3 files changed, 120 insertions(+), 106 deletions(-) create mode 100644 application/models/Hrdlog_model.php diff --git a/application/controllers/Hrdlog.php b/application/controllers/Hrdlog.php index 721ea69cd..814f12810 100644 --- a/application/controllers/Hrdlog.php +++ b/application/controllers/Hrdlog.php @@ -22,100 +22,10 @@ class Hrdlog extends CI_Controller { } public function upload() { - $this->setOptions(); - // set the last run in cron table for the correct cron id - $this->load->model('cron_model'); - $this->cron_model->set_last_run($this->router->class.'_'.$this->router->method); - - $this->load->model('logbook_model'); - - $station_ids = $this->logbook_model->get_station_id_with_hrdlog_code(); - - if ($station_ids) { - foreach ($station_ids as $station) { - $hrdlog_username = $station->hrdlog_username; - $hrdlog_code = $station->hrdlog_code; - if ($this->mass_upload_qsos($station->station_id, $hrdlog_username, $hrdlog_code)) { - echo "QSOs have been uploaded to hrdlog.net."; - log_message('info', 'QSOs have been uploaded to hrdlog.net.'); - } else { - echo "No QSOs found for upload."; - log_message('info', 'No QSOs found for upload.'); - } - } - } else { - echo "No station profiles with a hrdlog Code found."; - log_message('error', "No station profiles with a hrdlog Code found."); - } - } - - function setOptions() { - $this->config->load('config'); - ini_set('memory_limit', '-1'); - ini_set('display_errors', 1); - ini_set('display_startup_errors', 1); - error_reporting(E_ALL); - } - - /* - * Function gets all QSOs from given station_id, that are not previously uploaded to hrdlog. - * Adif is build for each qso, and then uploaded, one at a time - */ - function mass_upload_qsos($station_id, $hrdlog_username, $hrdlog_code) { - $i = 0; - $data['qsos'] = $this->logbook_model->get_hrdlog_qsos($station_id); - $errormessages = array(); - - $this->load->library('AdifHelper'); - - if ($data['qsos']) { - foreach ($data['qsos']->result() as $qso) { - $adif = $this->adifhelper->getAdifLine($qso); - - if ($qso->COL_HRDLOG_QSO_UPLOAD_STATUS == 'M') { - $result = $this->logbook_model->push_qso_to_hrdlog($hrdlog_username, $hrdlog_code, $adif, true); - } else { - $result = $this->logbook_model->push_qso_to_hrdlog($hrdlog_username, $hrdlog_code, $adif); - } - - if (($result['status'] == 'OK') || (($result['status'] == 'error') || ($result['status'] == 'duplicate'))) { - $this->markqso($qso->COL_PRIMARY_KEY); - $i++; - $result['status'] = 'OK'; - } elseif ((substr($result['status'], 0, 11) == 'auth_error')) { - log_message('error', 'hrdlog upload failed for qso: Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON); - log_message('error', 'hrdlog upload failed with the following message: ' . $result['message']); - log_message('error', 'hrdlog upload stopped for Station_ID: ' . $station_id); - $errormessages[] = $result['message'] . 'Invalid HRDLog-Code, stopped at Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON; - $result['status'] = 'Error'; - break; /* If key is invalid, immediate stop syncing for more QSOs of this station */ - } else { - log_message('error', 'hrdlog upload failed for qso: Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON); - log_message('error', 'hrdlog upload failed with the following message: ' . $result['message']); - $result['status'] = 'Error'; - $errormessages[] = $result['message'] . ' Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON; - } - } - if ($i == 0) { - $result['status']='Error'; - } - $result['count'] = $i; - $result['errormessages'] = $errormessages; - return $result; - } else { - $result['status'] = 'Error'; - $result['count'] = $i; - $result['errormessages'] = $errormessages; - return $result; - } - } - - /* - * Function marks QSO with given primarykey as uploaded to hrdlog - */ - function markqso($primarykey) { - $this->logbook_model->mark_hrdlog_qsos_sent($primarykey); + $this->load->model('Hrdlog_model'); + $this->Hrdlog_model->upload(); + } /* @@ -139,8 +49,10 @@ class Hrdlog extends CI_Controller { */ public function upload_station() { if (!($this->config->item('disable_manual_hrdlog'))) { - $this->setOptions(); - $this->load->model('stations'); + $this->load->model('stations'); + $this->load->model('Hrdlog_model'); + + $this->Hrdlog_model->setOptions(); $postData = $this->input->post(); @@ -149,19 +61,19 @@ class Hrdlog extends CI_Controller { $hrdlog_username = $result->hrdlog_username; $hrdlog_code = $result->hrdlog_code; header('Content-type: application/json'); - $result = $this->mass_upload_qsos($postData['station_id'], $hrdlog_username, $hrdlog_code); + $result = $this->Hrdlog_model->mass_upload_qsos($postData['station_id'], $hrdlog_username, $hrdlog_code); if ($result['status'] == 'OK') { $stationinfo = $this->stations->stations_with_hrdlog_code(); $info = $stationinfo->result(); $data['status'] = 'OK'; $data['info'] = $info; - $data['infomessage'] = $result['count'] . " QSOs are now uploaded to hrdlog"; + $data['infomessage'] = sprintf(_ngettext("%d QSO is now uploaded to HRDlog", "%d QSOs are now uploaded to HRDlog", $result['count']), $result['count']); $data['errormessages'] = $result['errormessages']; echo json_encode($data); } else { $data['status'] = 'Error'; - $data['info'] = 'No QSOs found to upload.'; + $data['info'] = __("No QSOs found to upload."); $data['errormessages'] = $result['errormessages']; echo json_encode($data); } @@ -177,17 +89,16 @@ class Hrdlog extends CI_Controller { $station_id = $this->security->xss_clean($this->input->post('station_profile')); $this->load->model('adif_data'); + $this->load->model('logbook_model'); $data['qsos'] = $this->adif_data->export_custom($this->input->post('from'), $this->input->post('to'), $station_id); - $this->load->model('logbook_model'); - - if (isset($data['qsos'])) { - foreach ($data['qsos']->result() as $qso) - { - $this->logbook_model->mark_hrdlog_qsos_sent($qso->COL_PRIMARY_KEY); + if (isset($data['qsos'])) { + foreach ($data['qsos']->result() as $qso) + { + $this->logbook_model->mark_hrdlog_qsos_sent($qso->COL_PRIMARY_KEY); + } } - } $this->load->view('interface_assets/header', $data); $this->load->view('hrdlog/mark_hrdlog', $data); diff --git a/application/models/Hrdlog_model.php b/application/models/Hrdlog_model.php new file mode 100644 index 000000000..ec21cd20b --- /dev/null +++ b/application/models/Hrdlog_model.php @@ -0,0 +1,103 @@ +setOptions(); + + // set the last run in cron table for the correct cron id + $this->load->model('cron_model'); + $this->cron_model->set_last_run($this->router->class . '_' . $this->router->method); + + $this->load->model('logbook_model'); + + $station_ids = $this->logbook_model->get_station_id_with_hrdlog_code(); + + if ($station_ids) { + foreach ($station_ids as $station) { + $hrdlog_username = $station->hrdlog_username; + $hrdlog_code = $station->hrdlog_code; + $u_result = $this->mass_upload_qsos($station->station_id, $hrdlog_username, $hrdlog_code); + if ($u_result['count'] > 0) { + $msg = "HRDlog: QSOs have been uploaded to hrdlog.net for the station callsign ".$station->station_callsign." (ID: ".$station->station_id.")"; + echo $msg; + } else { + $msg = "HRDlog: No QSOs found to upload for the station callsign ".$station->station_callsign." (ID: ".$station->station_id.")"; + echo $msg; + } + log_message('debug', $msg); + } + } else { + $msg = "HRDlog: No station profiles with HRDlog Credentials found."; + echo $msg; + log_message('debug', $msg); + } + } + + function setOptions() { + $this->config->load('config'); + ini_set('memory_limit', '-1'); + ini_set('display_errors', 1); + ini_set('display_startup_errors', 1); + error_reporting(E_ALL); + } + + /* + * Function gets all QSOs from given station_id, that are not previously uploaded to hrdlog. + * Adif is build for each qso, and then uploaded, one at a time + */ + function mass_upload_qsos($station_id, $hrdlog_username, $hrdlog_code) { + $i = 0; + $data['qsos'] = $this->logbook_model->get_hrdlog_qsos($station_id); + $errormessages = array(); + + $this->load->library('AdifHelper'); + + if ($data['qsos']) { + foreach ($data['qsos']->result() as $qso) { + $adif = $this->adifhelper->getAdifLine($qso); + + if ($qso->COL_HRDLOG_QSO_UPLOAD_STATUS == 'M') { + $result = $this->logbook_model->push_qso_to_hrdlog($hrdlog_username, $hrdlog_code, $adif, true); + } else { + $result = $this->logbook_model->push_qso_to_hrdlog($hrdlog_username, $hrdlog_code, $adif); + } + + if (($result['status'] == 'OK') || (($result['status'] == 'error') || ($result['status'] == 'duplicate'))) { + $this->markqso($qso->COL_PRIMARY_KEY); + $i++; + $result['status'] = 'OK'; + } elseif ((substr($result['status'], 0, 11) == 'auth_error')) { + log_message('error', 'hrdlog upload failed for qso: Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON); + log_message('error', 'hrdlog upload failed with the following message: ' . $result['message']); + log_message('error', 'hrdlog upload stopped for Station_ID: ' . $station_id); + $errormessages[] = $result['message'] . 'Invalid HRDLog-Code, stopped at Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON; + $result['status'] = 'Error'; + break; /* If key is invalid, immediate stop syncing for more QSOs of this station */ + } else { + log_message('error', 'hrdlog upload failed for qso: Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON); + log_message('error', 'hrdlog upload failed with the following message: ' . $result['message']); + $result['status'] = 'Error'; + $errormessages[] = $result['message'] . ' Call: ' . $qso->COL_CALL . ' Band: ' . $qso->COL_BAND . ' Mode: ' . $qso->COL_MODE . ' Time: ' . $qso->COL_TIME_ON; + } + } + if ($i == 0) { + $result['status'] = 'Error'; + } + $result['count'] = $i; + $result['errormessages'] = $errormessages; + } else { + $result['status'] = 'Error'; + $result['count'] = $i; + $result['errormessages'] = $errormessages; + } + return $result; + } + + /* + * Function marks QSO with given primarykey as uploaded to hrdlog + */ + function markqso($primarykey) { + $this->logbook_model->mark_hrdlog_qsos_sent($primarykey); + } +} diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 0fb40b7cf..68886958b 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -1923,7 +1923,7 @@ class Logbook_model extends CI_Model { * Function returns all the station_id's with HRDLOG Code */ function get_station_id_with_hrdlog_code() { - $sql = 'SELECT station_id, hrdlog_username, hrdlog_code + $sql = 'SELECT station_id, hrdlog_username, hrdlog_code, station_callsign FROM station_profile WHERE coalesce(hrdlog_username, "") <> "" AND coalesce(hrdlog_code, "") <> ""';