2020-05-12 17:11:29 +00:00
< ? php if ( ! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/*
Controller to interact with the QRZ . com API
*/
class Qrz extends CI_Controller {
// Show frontend if there is one
public function index () {
$this -> config -> load ( 'config' );
}
2020-05-20 17:43:13 +00:00
/*
* Upload QSO to QRZ . com
* When called from the url cloudlog / qrz / upload , the function loops through all station_id ' s with a qrz api key defined .
* All QSOs not previously uploaded , will then be uploaded , one at a time
*/
2020-05-12 17:11:29 +00:00
public function upload () {
2020-05-20 17:43:13 +00:00
$this -> setOptions ();
2020-05-12 17:11:29 +00:00
$this -> load -> model ( 'logbook_model' );
$station_ids = $this -> logbook_model -> get_station_id_with_qrz_api ();
if ( $station_ids ) {
2021-11-25 19:43:31 +00:00
foreach ( $station_ids as $station ) {
$qrz_api_key = $station -> qrzapikey ;
if ( $this -> mass_upload_qsos ( $station -> station_id , $qrz_api_key )) {
2020-10-28 18:03:30 +00:00
echo " QSOs have been uploaded to QRZ.com. " ;
log_message ( 'info' , 'QSOs have been uploaded to QRZ.com.' );
2020-05-20 17:43:13 +00:00
} else {
echo " No QSOs found for upload. " ;
log_message ( 'info' , 'No QSOs found for upload.' );
}
2020-05-12 17:11:29 +00:00
}
} else {
2020-10-28 18:03:30 +00:00
echo " No station profiles with a QRZ API Key found. " ;
log_message ( 'error' , " No station profiles with a QRZ API Key found. " );
2020-05-12 17:11:29 +00:00
}
}
2020-05-20 17:43:13 +00:00
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 qrz .
* Adif is build for each qso , and then uploaded , one at a time
*/
2020-05-12 17:11:29 +00:00
function mass_upload_qsos ( $station_id , $qrz_api_key ) {
2020-05-20 17:43:13 +00:00
$i = 0 ;
2020-05-12 17:11:29 +00:00
$data [ 'qsos' ] = $this -> logbook_model -> get_qrz_qsos ( $station_id );
2020-08-08 13:52:59 +00:00
$errormessages = array ();
2020-05-12 17:11:29 +00:00
2020-11-21 13:25:02 +00:00
$CI =& get_instance ();
$CI -> load -> library ( 'AdifHelper' );
2020-05-12 17:11:29 +00:00
if ( $data [ 'qsos' ]) {
2020-11-21 13:25:02 +00:00
foreach ( $data [ 'qsos' ] -> result () as $qso ) {
$adif = $CI -> adifhelper -> getAdifLine ( $qso );
2020-06-04 19:56:32 +00:00
2020-11-21 13:25:02 +00:00
if ( $qso -> COL_QRZCOM_QSO_UPLOAD_STATUS == 'M' ) {
2020-06-04 19:56:32 +00:00
$result = $this -> logbook_model -> push_qso_to_qrz ( $qrz_api_key , $adif , true );
} else {
$result = $this -> logbook_model -> push_qso_to_qrz ( $qrz_api_key , $adif );
}
2023-07-05 12:05:02 +00:00
if ( ( $result [ 'status' ] == 'OK' ) || ( ( $result [ 'status' ] == 'error' ) && ( $result [ 'message' ] == 'STATUS=FAIL&REASON=Unable to add QSO to database: duplicate&EXTENDED=' )) ){
2020-11-21 13:25:02 +00:00
$this -> markqso ( $qso -> COL_PRIMARY_KEY );
2020-05-20 17:43:13 +00:00
$i ++ ;
2023-07-05 12:05:02 +00:00
} elseif ( ( $result [ 'status' ] == 'error' ) && ( substr ( $result [ 'message' ], 0 , 11 ) == 'STATUS=AUTH' )) {
log_message ( 'error' , 'QRZ upload failed for qso: Call: ' . $qso -> COL_CALL . ' Band: ' . $qso -> COL_BAND . ' Mode: ' . $qso -> COL_MODE . ' Time: ' . $qso -> COL_TIME_ON );
log_message ( 'error' , 'QRZ upload failed with the following message: ' . $result [ 'message' ]);
log_message ( 'error' , 'QRZ upload stopped for Station_ID: ' . $station_id );
2023-07-05 12:20:39 +00:00
$errormessages [] = $result [ 'message' ] . ' Call: ' . $qso -> COL_CALL . ' Band: ' . $qso -> COL_BAND . ' Mode: ' . $qso -> COL_MODE . ' Time: ' . $qso -> COL_TIME_ON ;
2023-07-05 12:05:02 +00:00
break ; /* If key is invalid, immediate stop syncing for more QSOs of this station */
2020-08-08 06:47:02 +00:00
} else {
2020-11-21 13:25:02 +00:00
log_message ( 'error' , 'QRZ upload failed for qso: Call: ' . $qso -> COL_CALL . ' Band: ' . $qso -> COL_BAND . ' Mode: ' . $qso -> COL_MODE . ' Time: ' . $qso -> COL_TIME_ON );
2020-08-08 06:47:02 +00:00
log_message ( 'error' , 'QRZ upload failed with the following message: ' . $result [ 'message' ]);
2020-11-21 13:25:02 +00:00
$errormessages [] = $result [ 'message' ] . ' Call: ' . $qso -> COL_CALL . ' Band: ' . $qso -> COL_BAND . ' Mode: ' . $qso -> COL_MODE . ' Time: ' . $qso -> COL_TIME_ON ;
2020-05-12 17:11:29 +00:00
}
}
2020-08-08 13:52:59 +00:00
$result [ 'status' ] = 'OK' ;
$result [ 'count' ] = $i ;
$result [ 'errormessages' ] = $errormessages ;
return $result ;
2020-05-12 17:11:29 +00:00
} else {
2020-08-08 13:52:59 +00:00
$result [ 'status' ] = 'Error' ;
$result [ 'count' ] = $i ;
$result [ 'errormessages' ] = $errormessages ;
return $result ;
2020-05-12 17:11:29 +00:00
}
}
2020-05-20 17:43:13 +00:00
/*
* Function marks QSO with given primarykey as uploaded to qrz
*/
2020-05-12 17:11:29 +00:00
function markqso ( $primarykey ) {
$this -> logbook_model -> mark_qrz_qsos_sent ( $primarykey );
}
2020-05-20 17:43:13 +00:00
/*
* Used for displaying the uid for manually selecting log for upload to qrz
*/
public function export () {
$this -> load -> model ( 'stations' );
2020-09-15 16:56:15 +00:00
$data [ 'page_title' ] = " QRZ Logbook " ;
2020-05-20 17:43:13 +00:00
2021-11-25 19:18:44 +00:00
$data [ 'station_profiles' ] = $this -> stations -> all_of_user ();
2020-05-20 17:43:13 +00:00
$data [ 'station_profile' ] = $this -> stations -> stations_with_qrz_api_key ();
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'qrz/export' );
$this -> load -> view ( 'interface_assets/footer' );
}
/*
* Used for ajax - function when selecting log for upload to qrz
*/
public function upload_station () {
$this -> setOptions ();
$this -> load -> model ( 'stations' );
2021-09-05 15:11:58 +00:00
2020-05-20 17:43:13 +00:00
$postData = $this -> input -> post ();
$this -> load -> model ( 'logbook_model' );
2020-11-21 13:25:02 +00:00
$result = $this -> logbook_model -> exists_qrz_api_key ( $postData [ 'station_id' ]);
$qrz_api_key = $result -> qrzapikey ;
2020-05-20 17:43:13 +00:00
header ( 'Content-type: application/json' );
2020-08-08 13:52:59 +00:00
$result = $this -> mass_upload_qsos ( $postData [ 'station_id' ], $qrz_api_key );
if ( $result [ 'status' ] == 'OK' ) {
2020-05-20 17:43:13 +00:00
$stationinfo = $this -> stations -> stations_with_qrz_api_key ();
$info = $stationinfo -> result ();
$data [ 'status' ] = 'OK' ;
$data [ 'info' ] = $info ;
2020-08-08 13:52:59 +00:00
$data [ 'infomessage' ] = $result [ 'count' ] . " QSOs are now uploaded to QRZ.com " ;
$data [ 'errormessages' ] = $result [ 'errormessages' ];
2020-05-20 17:43:13 +00:00
echo json_encode ( $data );
} else {
$data [ 'status' ] = 'Error' ;
2020-10-28 18:03:30 +00:00
$data [ 'info' ] = 'Error: No QSOs found to upload.' ;
2020-08-08 13:52:59 +00:00
$data [ 'errormessages' ] = $result [ 'errormessages' ];
2020-05-20 17:43:13 +00:00
echo json_encode ( $data );
}
}
2021-09-05 15:11:58 +00:00
public function mark_qrz () {
// Set memory limit to unlimited to allow heavy usage
ini_set ( 'memory_limit' , '-1' );
$station_id = $this -> security -> xss_clean ( $this -> input -> post ( 'station_profile' ));
$this -> load -> model ( 'adif_data' );
$data [ 'qsos' ] = $this -> adif_data -> export_custom ( $this -> input -> post ( 'from' ), $this -> input -> post ( 'to' ), $station_id );
$this -> load -> model ( 'logbook_model' );
foreach ( $data [ 'qsos' ] -> result () as $qso )
{
$this -> logbook_model -> mark_qrz_qsos_sent ( $qso -> COL_PRIMARY_KEY );
}
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'qrz/mark_qrz' , $data );
$this -> load -> view ( 'interface_assets/footer' );
}
}