From f7a9c3507b0b77cbf04ea04c41598e668da77397 Mon Sep 17 00:00:00 2001 From: AndreasK79 Date: Tue, 12 May 2020 19:11:29 +0200 Subject: [PATCH] Mass upload for QRZ.com, suitable for a cron job. --- application/controllers/Qrz.php | 61 ++++++++++++++++++++++++ application/models/Logbook_model.php | 69 ++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 application/controllers/Qrz.php diff --git a/application/controllers/Qrz.php b/application/controllers/Qrz.php new file mode 100644 index 00000000..dd2b190d --- /dev/null +++ b/application/controllers/Qrz.php @@ -0,0 +1,61 @@ +config->load('config'); + } + + // Upload QSO to QRZ.com + public function upload() { + + $this->config->load('config'); + ini_set('memory_limit', '-1'); + ini_set('display_errors', 1); + ini_set('display_startup_errors', 1); + error_reporting(E_ALL); + + $this->load->model('logbook_model'); + + $station_ids = $this->logbook_model->get_station_id_with_qrz_api(); + + if ($station_ids) { + foreach ($station_ids as $station_id) { + $qrz_api_key = $this->logbook_model->exists_qrz_api_key($station_id); + $this->mass_upload_qsos($station_id, $qrz_api_key); + } + } else { + echo "No station_id's with a QRZ API Key found"; + log_message('info', "No station_id's with a QRZ API Key found"); + } + + } + + function mass_upload_qsos($station_id, $qrz_api_key) { + $data['qsos'] = $this->logbook_model->get_qrz_qsos($station_id); + + if ($data['qsos']) { + foreach ($data['qsos'] as $qso) { + $adif = $this->logbook_model->create_adif_from_data($qso); + $result = $this->logbook_model->push_qso_to_qrz($qrz_api_key, $adif); + if ($result) { + $this->markqso($qso['COL_PRIMARY_KEY']); + } + } + echo "QSOs has been uploaded to QRZ.com."; + log_message('info', 'QSOs has been uploaded to QRZ.com.'); + } else { + echo "No QSOs found for upload."; + log_message('info', 'No QSOs found for upload.'); + } + } + + function markqso($primarykey) { + $this->logbook_model->mark_qrz_qsos_sent($primarykey); + } +} diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 7f0605f3..23431d91 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -326,7 +326,8 @@ class Logbook_model extends CI_Model { // Push qso to qrz if apikey is set if ($apikey = $this->exists_qrz_api_key($data['station_id'])) { - IF ($this->push_qso_to_qrz($data, $apikey)) { + $adif = $this->create_adif_from_data($data); + IF ($this->push_qso_to_qrz($apikey, $adif)) { $data['COL_QRZCOM_QSO_UPLOAD_STATUS'] = 'Y'; $data['COL_QRZCOM_QSO_UPLOAD_DATE'] = date("Y-m-d H:i:s", strtotime("now")); } @@ -336,6 +337,9 @@ class Logbook_model extends CI_Model { $this->db->insert($this->config->item('table_name'), $data); } + /* + * Function checks if a QRZ API Key exists in the table with the given station id + */ function exists_qrz_api_key($station_id) { $sql = 'select qrzapikey from station_profile where station_id = ' . $station_id; @@ -352,11 +356,13 @@ class Logbook_model extends CI_Model { } } - function push_qso_to_qrz($data, $apikey) { + /* + * Function uploads a QSO to QRZ with the API given. + * $adif contains a line with the QSO in the ADIF format. QSO ends with an + */ + function push_qso_to_qrz($apikey, $adif) { $url = 'http://logbook.qrz.com/api'; // TODO: Move this to database - $adif = $this->create_adif_from_data($data); - $post_data['KEY'] = $apikey; $post_data['ACTION'] = 'INSERT'; $post_data['ADIF'] = $adif; @@ -383,6 +389,26 @@ class Logbook_model extends CI_Model { curl_close($ch); } + /* + * Function marks QSOs as uploaded to QRZ. + * $primarykey is the unique id for that QSO in the logbook + */ + function mark_qrz_qsos_sent($primarykey) { + $data = array( + 'COL_QRZCOM_QSO_UPLOAD_DATE' => date("Y-m-d H:i:s", strtotime("now")), + 'COL_QRZCOM_QSO_UPLOAD_STATUS' => 'Y', + ); + + $this->db->where('COL_PRIMARY_KEY', $primarykey); + + $this->db->update($this->config->item('table_name'), $data); + + return true; + } + + /* + * Function is used to build an ADIF string from an array that contains the QSO data + */ function create_adif_from_data($data) { $adif = '' . $data['COL_CALL']; $adif .= '' . $data['COL_BAND']; @@ -797,6 +823,41 @@ class Logbook_model extends CI_Model { return $query; } + /* + * Function returns the QSOs from the logbook, which have not been either marked as uploaded to qrz, or has been modified with an edit + */ + function get_qrz_qsos($station_id){ + $sql = 'select * from ' . $this->config->item('table_name') . + ' where station_id = ' . $station_id . + ' and (COL_QRZCOM_QSO_UPLOAD_STATUS = NULL + or COL_QRZCOM_QSO_UPLOAD_STATUS = "" + or COL_QRZCOM_QSO_UPLOAD_STATUS = "M" + or COL_QRZCOM_QSO_UPLOAD_STATUS = "N")'; + + $query = $this->db->query($sql); + return $query->result_array(); + } + /* + * Function returns all the station_id's with QRZ API Key's + */ + function get_station_id_with_qrz_api() { + $sql = 'select station_id from station_profile + where coalesce(qrzapikey, "") <> ""'; + + $query = $this->db->query($sql); + + $result = $query->row(); + + if ($result) { + return $result; + } + else { + return null; + } + } + + + function get_last_qsos($num) { $CI =& get_instance();