From f1ad1d1d6eed4ceb4aafaaaf38b10c114b0e80ab Mon Sep 17 00:00:00 2001 From: AndreasK79 Date: Thu, 7 May 2020 17:35:58 +0200 Subject: [PATCH] Added pushing of QSOs to QRZ. --- application/config/migration.php | 2 +- .../040_add_qrz_api_to_stationprofile.php | 20 +++ application/models/Logbook_model.php | 154 ++++++++++++++++++ application/models/Stations.php | 3 + application/views/station_profile/create.php | 12 ++ application/views/station_profile/edit.php | 6 + 6 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 application/migrations/040_add_qrz_api_to_stationprofile.php diff --git a/application/config/migration.php b/application/config/migration.php index f19e068f..31de43d6 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 39; +$config['migration_version'] = 40; /* |-------------------------------------------------------------------------- diff --git a/application/migrations/040_add_qrz_api_to_stationprofile.php b/application/migrations/040_add_qrz_api_to_stationprofile.php new file mode 100644 index 00000000..263d43d1 --- /dev/null +++ b/application/migrations/040_add_qrz_api_to_stationprofile.php @@ -0,0 +1,20 @@ +dbforge->add_column('station_profile', $fields); + } + + public function down() + { + $this->dbforge->drop_column('station_profile', 'qrzapikey'); + } +} \ No newline at end of file diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index cb15d790..44838071 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -322,6 +322,160 @@ class Logbook_model extends CI_Model { // Add QSO to database $this->db->insert($this->config->item('table_name'), $data); + + // Push qso to qrz if apikey is set + if ($apikey = $this->exists_qrz_api_key($data['station_id'])) { + $this->push_qso_to_qrz($data, $apikey); + } + } + + function exists_qrz_api_key($station_id) { + $sql = 'select qrzapikey from station_profile + where station_id = ' . $station_id; + + $query = $this->db->query($sql); + + $result = $query->row(); + + if ($result) { + return $result->qrzapikey; + } + else { + return false; + } + } + + function push_qso_to_qrz($data, $apikey) { + $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; + + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_POST, true); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt( $ch, CURLOPT_HEADER, 0); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true); + + //var_dump($adif); + //var_dump($data); + $response = curl_exec( $ch ); + //var_dump($response); + } + + function create_adif_from_data($data) { + $adif = '' . $data['COL_CALL']; + $adif .= '' . $data['COL_BAND']; + $adif .= '' . $data['COL_MODE']; + + if($data['COL_FREQ'] != "0") { + $freq_in_mhz = $data['COL_FREQ'] / 1000000; + $adif .= '' . $freq_in_mhz; + } + + $date_on = strtotime($data['COL_TIME_ON']); + $new_date = date('Ymd', $date_on); + $adif .= '' . $new_date; + $time_on = strtotime($data['COL_TIME_ON']); + $new_on = date('His', $time_on); + $adif .= '' . $new_on; + $time_off = strtotime($data['COL_TIME_OFF']); + $new_off = date('His', $time_off); + $adif .= '' . $new_off; + $adif .= '' . $data['COL_RST_RCVD']; + $adif .= '' . $data['COL_RST_SENT']; + + if ($data['COL_QSL_RCVD']) { + $adif .= '' . $data['COL_QSL_RCVD']; + } + + $adif .= '' . $data['COL_QSL_SENT']; + $adif .= '' . $data['COL_COUNTRY']; + $adif .= '' . $data['COL_STATION_CALLSIGN']; + $adif .= '' . $data['COL_DXCC']; + $adif .= '' . $data['COL_CQZ']; + //$adif .= '' . $data['COL_ITUZ']; -- not yet implemented + + $adif .= '' . $data['COL_LOTW_QSL_SENT']; + $adif .= '' . $data['COL_LOTW_QSL_RCVD']; + + if($data['COL_IOTA']) { + $adif .= '' . $data['COL_IOTA']; + } + + if($data['COL_GRIDSQUARE']) { + $adif .= '' . $data['COL_GRIDSQUARE']; + } + + if($data['COL_SOTA_REF']) { + $adif .= '' . $data['COL_SOTA_REF']; + } + + if($data['COL_SAT_NAME']) { + if($data['COL_SAT_MODE'] != 0 || $data['COL_SAT_MODE'] !="") { + $adif .= '' . $data['COL_SAT_MODE']; + $adif .= 'sat_name:' . strlen($data['COL_SAT_NAME']) . '>' . $data['COL_SAT_NAME']; + } + } + + if($data['COL_STATE']) { + $adif .= '' . $data['COL_STATE']; + } + + if($data['COL_PROP_MODE']) { + $adif .= '' . $data['COL_PROP_MODE']; + } + + if($data['COL_NAME']) { + $adif .= '' . $data['COL_NAME']; + } + + if($data['COL_OPERATOR']) { + $adif .= '' . $data['COL_OPERATOR']; + } + + if($data['COL_MY_CITY']) { + $adif .= '' . $data['COL_MY_CITY']; + } + + if($data['COL_MY_COUNTRY']) { + $adif .= '' . $data['COL_MY_COUNTRY']; + } + + if($data['COL_MY_DXCC']) { + $adif .= '' . $data['COL_MY_DXCC']; + } + + if($data['COL_MY_IOTA']) { + $adif .= '' . $data['COL_MY_IOTA']; + } + + if($data['COL_MY_SOTA_REF']) { + $adif .= '' . $data['COL_MY_SOTA_REF']; + } + + if($data['COL_MY_CQ_ZONE']) { + $adif .= '' . $data['COL_MY_CQ_ZONE']; + } + + if($data['COL_MY_ITU_ZONE']) { + $adif .= '' . $data['COL_MY_ITU_ZONE']; + } + + if($data['COL_MY_CNTY']) { + $adif .= '' . $data['COL_MY_CNTY']; + } + + if(strpos($data['COL_MY_GRIDSQUARE'], ',') !== false ) { + $adif .= '' . $data['COL_MY_GRIDSQUARE']; + } + + $adif .= ''; + return $adif; } /* Edit QSO */ diff --git a/application/models/Stations.php b/application/models/Stations.php index b6816cf8..b13d1472 100644 --- a/application/models/Stations.php +++ b/application/models/Stations.php @@ -45,6 +45,8 @@ class Stations extends CI_Model { 'station_cq' => xss_clean($this->input->post('station_cq', true)), 'station_itu' => xss_clean($this->input->post('station_itu', true)), 'state' => xss_clean($this->input->post('station_state', true)), + 'eqslqthnickname' => xss_clean($this->input->post('eqslnickname', true)), + 'qrzapikey' => xss_clean($this->input->post('qrzapikey', true)), ); $this->db->insert('station_profile', $data); @@ -65,6 +67,7 @@ class Stations extends CI_Model { 'station_itu' => xss_clean($this->input->post('station_itu', true)), 'state' => xss_clean($this->input->post('station_state', true)), 'eqslqthnickname' => xss_clean($this->input->post('eqslnickname', true)), + 'qrzapikey' => xss_clean($this->input->post('qrzapikey', true)), ); $this->db->where('station_id', xss_clean($this->input->post('station_id', true))); diff --git a/application/views/station_profile/create.php b/application/views/station_profile/create.php index e7c34b8b..44165387 100644 --- a/application/views/station_profile/create.php +++ b/application/views/station_profile/create.php @@ -153,6 +153,18 @@ Station SOTA Reference. +
+ + + eQSL QTH Nickname. +
+ +
+ + + QRZ.com logbook api key +
+ diff --git a/application/views/station_profile/edit.php b/application/views/station_profile/edit.php index e53335f8..19b43e81 100644 --- a/application/views/station_profile/edit.php +++ b/application/views/station_profile/edit.php @@ -160,6 +160,12 @@ eQSL QTH Nickname. +
+ + qrzapikey; } ?>"> + QRZ.com logbook api key +
+