2020-10-28 23:20:03 +01:00
< ? php if ( ! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/*
Controller for QSL Cards
*/
class Qsl extends CI_Controller {
function __construct () {
parent :: __construct ();
2024-08-16 10:08:44 +02:00
if ( ! $this -> user_model -> authorize ( 2 )) { $this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " )); redirect ( 'dashboard' ); }
2025-07-05 13:34:06 +02:00
if (( $this -> config -> item ( 'disable_qsl' ) ? ? false )) { $this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " )); redirect ( 'dashboard' ); exit ; }
2020-10-28 23:20:03 +01:00
}
2020-11-04 14:46:42 +00:00
// Default view when loading controller.
2020-10-28 23:20:03 +01:00
public function index () {
2020-11-04 14:46:42 +00:00
2024-02-01 11:58:34 +01:00
$this -> load -> model ( 'qsl_model' );
2024-02-24 10:23:58 +01:00
$this -> load -> library ( 'Genfunctions' );
2026-06-11 23:06:23 +02:00
$folder_name = $this -> paths -> getUserdataPath ( 'qsl_card' , 'p' );
2024-02-24 10:23:58 +01:00
$data [ 'storage_used' ] = $this -> genfunctions -> sizeFormat ( $this -> genfunctions -> folderSize ( $folder_name ));
2020-11-04 14:46:42 +00:00
2020-10-28 23:20:03 +01:00
// Render Page
2024-06-08 11:01:59 +02:00
$data [ 'page_title' ] = __ ( " QSL Cards " );
2020-10-28 23:20:03 +01:00
$data [ 'qslarray' ] = $this -> qsl_model -> getQsoWithQslList ();
2025-07-05 13:34:06 +02:00
$footerData = [];
$footerData [ 'scripts' ] = [
2026-02-13 16:58:57 +01:00
'assets/js/sections/qsl.js' ,
2025-07-05 13:34:06 +02:00
];
2020-10-28 23:20:03 +01:00
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'qslcard/index' );
2025-07-05 13:34:06 +02:00
$this -> load -> view ( 'interface_assets/footer' , $footerData );
}
2020-10-28 23:20:03 +01:00
public function upload () {
// Render Page
2024-06-08 11:01:59 +02:00
$data [ 'page_title' ] = __ ( " Upload QSL Cards " );
2020-10-28 23:20:03 +01:00
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'qslcard/upload' );
$this -> load -> view ( 'interface_assets/footer' );
}
2020-11-04 14:46:42 +00:00
// Deletes QSL Card
2020-10-28 23:20:03 +01:00
public function delete () {
2024-08-16 10:08:44 +02:00
if ( ! $this -> user_model -> authorize ( 2 )) { $this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " )); redirect ( 'dashboard' ); }
2020-10-28 23:20:03 +01:00
$id = $this -> input -> post ( 'id' );
$this -> load -> model ( 'Qsl_model' );
$this -> Qsl_model -> deleteQsl ( $id );
}
public function uploadqsl () {
2024-08-16 10:08:44 +02:00
if ( ! $this -> user_model -> authorize ( 2 )) { $this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " )); redirect ( 'dashboard' ); }
2020-10-28 23:20:03 +01:00
$qsoid = $this -> input -> post ( 'qsoid' );
if ( isset ( $_FILES [ 'qslcardfront' ]) && $_FILES [ 'qslcardfront' ][ 'name' ] != " " && $_FILES [ 'qslcardfront' ][ 'error' ] == 0 )
{
2020-10-29 20:18:51 +01:00
$result [ 'front' ] = $this -> uploadQslCardFront ( $qsoid );
2023-06-30 11:54:39 +02:00
} else {
$result [ 'front' ][ 'status' ] = '' ;
2020-10-29 20:18:51 +01:00
}
if ( isset ( $_FILES [ 'qslcardback' ]) && $_FILES [ 'qslcardback' ][ 'name' ] != " " && $_FILES [ 'qslcardback' ][ 'error' ] == 0 )
{
$result [ 'back' ] = $this -> uploadQslCardBack ( $qsoid );
2023-06-30 11:54:39 +02:00
} else {
$result [ 'back' ][ 'status' ] = '' ;
2020-10-28 23:20:03 +01:00
}
2020-11-01 12:36:02 +01:00
header ( " Content-type: application/json " );
echo json_encode ([ 'status' => $result ]);
2020-10-28 23:20:03 +01:00
}
2020-10-29 20:18:51 +01:00
function uploadQslCardFront ( $qsoid ) {
2024-02-01 11:58:34 +01:00
$this -> load -> model ( 'Qsl_model' );
2026-06-13 09:48:20 +00:00
$this -> load -> model ( 'logbook_model' );
$this -> load -> library ( 'upload_guard' );
if ( ! $this -> logbook_model -> check_qso_is_accessible ( $qsoid )) {
return array ( 'error' => __ ( " You're not allowed to do that! " ));
}
2026-06-11 23:06:23 +02:00
$config [ 'upload_path' ] = $this -> paths -> getUserdataPath ( 'qsl_card' , 'p' );
2022-04-01 17:39:23 +01:00
$config [ 'allowed_types' ] = 'jpg|gif|png|jpeg|JPG|PNG' ;
2020-10-28 23:20:03 +01:00
$array = explode ( " . " , $_FILES [ 'qslcardfront' ][ 'name' ]);
$ext = end ( $array );
2026-06-13 09:48:20 +00:00
$config [ 'file_name' ] = $qsoid . '_' . time () . '_' . bin2hex ( random_bytes ( 8 )) . '.' . $ext ;
// Refuse the upload if it would leave the storage volume too full
if ( ! $this -> upload_guard -> has_free_space ( $config [ 'upload_path' ], $_FILES [ 'qslcardfront' ][ 'size' ])) {
return array ( 'error' => __ ( " Not enough free disk space to store the QSL card. " ));
}
2020-10-28 23:20:03 +01:00
$this -> load -> library ( 'upload' , $config );
if ( ! $this -> upload -> do_upload ( 'qslcardfront' )) {
// Upload of QSL card Failed
$error = array ( 'error' => $this -> upload -> display_errors ());
return $error ;
}
else {
//Upload of QSL card was successful
$data = $this -> upload -> data ();
2026-06-13 09:48:20 +00:00
if ( ! $this -> upload_guard -> is_real_image ( $data [ 'full_path' ])) {
unlink ( $data [ 'full_path' ]);
return array ( 'error' => __ ( " The uploaded file is not a valid image. " ));
}
2020-10-28 23:20:03 +01:00
// Now we need to insert info into database about file
$filename = $data [ 'file_name' ];
2020-11-01 12:36:02 +01:00
$insertid = $this -> Qsl_model -> saveQsl ( $qsoid , $filename );
2020-10-28 23:20:03 +01:00
2020-11-01 12:36:02 +01:00
$result [ 'status' ] = 'Success' ;
$result [ 'insertid' ] = $insertid ;
$result [ 'filename' ] = $filename ;
return $result ;
2020-10-28 23:20:03 +01:00
}
}
2020-10-29 20:18:51 +01:00
function uploadQslCardBack ( $qsoid ) {
2024-02-01 11:58:34 +01:00
$this -> load -> model ( 'Qsl_model' );
2026-06-13 09:48:20 +00:00
$this -> load -> model ( 'logbook_model' );
$this -> load -> library ( 'upload_guard' );
if ( ! $this -> logbook_model -> check_qso_is_accessible ( $qsoid )) {
return array ( 'error' => __ ( " You're not allowed to do that! " ));
}
2026-06-11 23:06:23 +02:00
$config [ 'upload_path' ] = $this -> paths -> getUserdataPath ( 'qsl_card' , 'p' );
2022-04-01 17:39:23 +01:00
$config [ 'allowed_types' ] = 'jpg|gif|png|jpeg|JPG|PNG' ;
2020-10-29 20:18:51 +01:00
$array = explode ( " . " , $_FILES [ 'qslcardback' ][ 'name' ]);
$ext = end ( $array );
2026-06-13 09:48:20 +00:00
$config [ 'file_name' ] = $qsoid . '_' . time () . '_' . bin2hex ( random_bytes ( 8 )) . '.' . $ext ;
if ( ! $this -> upload_guard -> has_free_space ( $config [ 'upload_path' ], $_FILES [ 'qslcardback' ][ 'size' ])) {
return array ( 'error' => __ ( " Not enough free disk space to store the QSL card. " ));
}
2020-10-29 20:18:51 +01:00
$this -> load -> library ( 'upload' , $config );
if ( ! $this -> upload -> do_upload ( 'qslcardback' )) {
// Upload of QSL card Failed
$error = array ( 'error' => $this -> upload -> display_errors ());
return $error ;
}
else {
//Upload of QSL card was successful
$data = $this -> upload -> data ();
2026-06-13 09:48:20 +00:00
if ( ! $this -> upload_guard -> is_real_image ( $data [ 'full_path' ])) {
unlink ( $data [ 'full_path' ]);
return array ( 'error' => __ ( " The uploaded file is not a valid image. " ));
}
2020-10-29 20:18:51 +01:00
// Now we need to insert info into database about file
$filename = $data [ 'file_name' ];
2020-11-01 12:36:02 +01:00
$insertid = $this -> Qsl_model -> saveQsl ( $qsoid , $filename );
2020-10-29 20:18:51 +01:00
2020-11-01 12:36:02 +01:00
$result [ 'status' ] = 'Success' ;
$result [ 'insertid' ] = $insertid ;
$result [ 'filename' ] = $filename ;
return $result ;
2020-10-29 20:18:51 +01:00
}
}
2021-07-22 07:55:34 +02:00
function loadSearchForm () {
$data [ 'filename' ] = $this -> input -> post ( 'filename' );
$this -> load -> view ( 'qslcard/searchform' , $data );
}
function searchQsos () {
$this -> load -> model ( 'Qsl_model' );
$callsign = $this -> input -> post ( 'callsign' );
$data [ 'results' ] = $this -> Qsl_model -> searchQsos ( $callsign );
$data [ 'filename' ] = $this -> input -> post ( 'filename' );
$this -> load -> view ( 'qslcard/searchresult' , $data );
}
function addQsoToQsl () {
2026-05-20 19:34:11 +02:00
$qsoid = $this -> input -> post ( 'qsoid' , TRUE );
$filename = $this -> input -> post ( 'filename' , TRUE );
2021-07-22 07:55:34 +02:00
$this -> load -> model ( 'Qsl_model' );
$insertid = $this -> Qsl_model -> addQsotoQsl ( $qsoid , $filename );
header ( " Content-type: application/json " );
$result [ 'status' ] = 'Success' ;
$result [ 'insertid' ] = $insertid ;
$result [ 'filename' ] = $filename ;
echo json_encode ( $result );
}
2020-11-04 14:46:42 +00:00
2023-06-30 23:36:35 +02:00
function viewQsl () {
$cleanid = $this -> security -> xss_clean ( $this -> input -> post ( 'id' ));
$this -> load -> model ( 'Qsl_model' );
$data [ 'qslimages' ] = $this -> Qsl_model -> getQslForQsoId ( $cleanid );
$this -> load -> view ( 'qslcard/qslcarousel' , $data );
}
2024-04-03 05:27:51 +00:00
}