2023-03-17 17:54:35 +00:00
< ? php if ( ! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/* Controller to interact with the webADIF API */
class Webadif extends CI_Controller {
2026-01-08 09:48:34 +01:00
function __construct () {
parent :: __construct ();
if ( ! $this -> user_model -> authorize ( 2 )) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
}
2024-05-19 11:56:53 +00:00
/*
* Upload QSO to webADIF
* When called from the url wavelog / webadif / upload , the function loops through all station_id ' s with a webADIF
* api key defined .
* All QSOs not previously uploaded , will then be uploaded , one at a time
*/
public function upload () {
2023-03-17 17:54:35 +00:00
$this -> setOptions ();
$this -> load -> model ( 'logbook_model' );
$station_ids = $this -> logbook_model -> get_station_id_with_webadif_api ();
if ( $station_ids ) {
foreach ( $station_ids as $station ) {
$webadif_api_key = $station -> webadifapikey ;
$webadif_api_url = $station -> webadifapiurl ;
2023-07-31 09:44:27 +00:00
if ( $this -> mass_upload_qsos ( $station -> station_id , $webadif_api_key , $webadif_api_url , true )) { // When called via cron it is trusted
2023-03-17 17:54:35 +00:00
echo " QSOs have been uploaded to QO-100 Dx Club. " ;
log_message ( 'info' , 'QSOs have been uploaded to QO-100 Dx Club.' );
} else {
echo " No QSOs found for upload. " ;
log_message ( 'info' , 'No QSOs found for upload.' );
}
}
} else {
echo " No station profiles with a QO-100 Dx Club API Key found. " ;
log_message ( 'error' , " No station profiles with a QO-100 Dx Club API Key found. " );
}
}
2024-05-19 11:56:53 +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 webADIF consumer .
* Adif is build for each qso , and then uploaded , one at a time
*/
function mass_upload_qsos ( $station_id , $webadif_api_key , $webadif_api_url , $trusted = false ) {
$i = 0 ;
$data [ 'qsos' ] = $this -> logbook_model -> get_webadif_qsos ( $station_id , null , null , $trusted );
$errormessages = array ();
2024-09-11 10:09:43 +02:00
if ( ! $this -> load -> is_loaded ( 'AdifHelper' )) {
$this -> load -> library ( 'AdifHelper' );
}
2023-03-17 17:54:35 +00:00
if ( $data [ 'qsos' ]) {
foreach ( $data [ 'qsos' ] -> result () as $qso ) {
2024-05-19 11:56:53 +00:00
$adif = $this -> adifhelper -> getAdifLine ( $qso );
2023-03-17 17:54:35 +00:00
$result = $this -> logbook_model -> push_qso_to_webadif ( $webadif_api_url , $webadif_api_key , $adif );
if ( $result ) {
2023-03-20 19:15:05 +00:00
$this -> logbook_model -> mark_webadif_qsos_sent ([ $qso -> COL_PRIMARY_KEY ]);
2023-03-17 17:54:35 +00:00
$i ++ ;
} else {
$errorMessage = 'QO-100 Dx Club upload failed for qso: Call: ' . $qso -> COL_CALL . ' Band: ' . $qso -> COL_BAND . ' Mode: ' . $qso -> COL_MODE . ' Time: ' . $qso -> COL_TIME_ON ;
log_message ( 'error' , $errorMessage );
$errormessages [] = $errorMessage ;
}
}
$result = [];
$result [ 'status' ] = 'OK' ;
$result [ 'count' ] = $i ;
$result [ 'errormessages' ] = $errormessages ;
return $result ;
} else {
$result = [];
$result [ 'status' ] = 'Error' ;
$result [ 'count' ] = $i ;
$result [ 'errormessages' ] = $errormessages ;
return $result ;
}
2024-05-19 11:56:53 +00:00
}
/*
* Used for displaying the uid for manually selecting log for upload to webADIF consumer
*/
public function export () {
2025-01-02 10:22:23 +01:00
if ( ! $this -> user_model -> authorize ( 2 ) || ! clubaccess_check ( 9 )) { $this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " )); redirect ( 'dashboard' ); }
2024-05-19 11:56:53 +00:00
$this -> load -> model ( 'stations' );
2024-06-08 11:01:59 +02:00
$data [ 'page_title' ] = __ ( " QO-100 Dx Club Upload " );
2024-05-19 11:56:53 +00:00
$data [ 'station_profiles' ] = $this -> stations -> stations_with_webadif_api_key ();
$data [ 'station_profile' ] = $this -> stations -> stations_with_webadif_api_key ();
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'webadif/export' );
$this -> load -> view ( 'interface_assets/footer' );
}
/*
* Used for ajax - function when selecting log for upload to webADIF consumer
*/
public function upload_station () {
$this -> setOptions ();
$postData = $this -> input -> post ();
$this -> load -> model ( 'stations' );
if ( ! $this -> stations -> check_station_is_accessible ( $postData [ 'station_id' ])) {
return ;
}
$this -> load -> model ( 'logbook_model' );
$result = $this -> logbook_model -> exists_webadif_api_key ( $postData [ 'station_id' ]);
$webadif_api_key = $result -> webadifapikey ;
$webadif_api_url = $result -> webadifapiurl ;
header ( 'Content-type: application/json' );
$result = $this -> mass_upload_qsos ( $postData [ 'station_id' ], $webadif_api_key , $webadif_api_url );
if ( $result [ 'status' ] == 'OK' ) {
$stationinfo = $this -> stations -> stations_with_webadif_api_key ();
$info = $stationinfo -> result ();
$data [ 'status' ] = 'OK' ;
$data [ 'info' ] = $info ;
$data [ 'infomessage' ] = $result [ 'count' ] . " QSOs are now uploaded to QO-100 Dx Club " ;
$data [ 'errormessages' ] = $result [ 'errormessages' ];
echo json_encode ( $data );
} else {
$data [ 'status' ] = 'Error' ;
$data [ 'info' ] = 'Error: No QSOs found to upload.' ;
$data [ 'errormessages' ] = $result [ 'errormessages' ];
echo json_encode ( $data );
}
}
2023-03-17 17:54:35 +00:00
public function mark_webadif () {
// Set memory limit to unlimited to allow heavy usage
ini_set ( 'memory_limit' , '-1' );
2024-06-08 11:01:59 +02:00
$data [ 'page_title' ] = __ ( " QO-100 Dx Club Upload " );
2023-03-17 17:54:35 +00:00
$station_id = $this -> security -> xss_clean ( $this -> input -> post ( 'station_profile' ));
2023-03-20 13:39:35 +00:00
$from = $this -> security -> xss_clean ( $this -> input -> post ( 'from' ));
$to = $this -> security -> xss_clean ( $this -> input -> post ( 'to' ));
2023-03-17 17:54:35 +00:00
2023-03-20 13:39:35 +00:00
$this -> load -> model ( 'logbook_model' );
2023-03-17 17:54:35 +00:00
2023-03-20 13:39:35 +00:00
$data [ 'qsos' ] = $this -> logbook_model -> get_webadif_qsos (
$station_id ,
$from ,
$to
);
2023-03-17 17:54:35 +00:00
if ( $data [ 'qsos' ] !== null ) {
2023-03-20 19:15:05 +00:00
$qsoIDs = [];
2023-03-17 17:54:35 +00:00
foreach ( $data [ 'qsos' ] -> result () as $qso ) {
2023-03-20 19:15:05 +00:00
$qsoIDs [] = $qso -> COL_PRIMARY_KEY ;
}
$batchSize = 500 ;
while ( $qsoIDs !== []) {
$slice = array_slice ( $qsoIDs , 0 , $batchSize );
$qsoIDs = array_slice ( $qsoIDs , $batchSize );
$this -> logbook_model -> mark_webadif_qsos_sent ( $slice );
2023-03-17 17:54:35 +00:00
}
}
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'webadif/mark_webadif' , $data );
$this -> load -> view ( 'interface_assets/footer' );
}
}