2024-02-26 17:42:46 +01:00
< ? php
class Debug_model extends CI_Model
{
private $userdata_dir ;
private $flag_file ;
private $src_eqsl ;
private $eqsl_dir ;
private $src_qsl ;
private $qsl_dir ;
public function __construct ()
{
$this -> userdata_dir = $this -> config -> item ( 'userdata' );
$this -> flag_file = '.migrated' ; // we use this flag file to determine if the migration already run through
$this -> src_eqsl = 'images/eqsl_card_images' ;
$this -> eqsl_dir = 'eqsl_card' ; // make sure this is the same as in Eqsl_images.php function get_imagePath()
$this -> src_qsl = 'assets/qslcard' ;
$this -> qsl_dir = 'qsl_card' ; // make sure this is the same as in Qsl_model.php function get_imagePath()
}
function migrate_userdata ()
{
$this -> load -> model ( 'Logbook_model' );
$allowed_file_extensions = [ 'jpg' , 'jpeg' , 'gif' , 'png' ];
// ***** EQSL ***** //
// Let's scan the whole folder and get necessary data for each file
foreach ( scandir ( $this -> src_eqsl ) as $file ) {
// Ignore files if they are not jpg, png or gif
$file_extension = strtolower ( pathinfo ( $file , PATHINFO_EXTENSION ));
if ( ! in_array ( $file_extension , $allowed_file_extensions )) continue ;
if ( ! is_readable ( $this -> src_eqsl . '/' . $file )) continue ;
if ( $file != '.' && $file != '..' ) {
// we need the qso_id from the database to get the necessary user_id
$qso_id = $this -> get_qsoid_from_eqsl_filename ( $file ) ? ? '' ;
// check if the qso_id is empty, if yes we create a folder 'not assigned' instead of 'user_id'
if ( ! empty ( $qso_id )) {
// get the user_id for this qso_id
$get_user_id = $this -> Logbook_model -> get_user_id_from_qso ( $qso_id );
// it can happen that the user_id is empty even there is a qso_id (deleted qso or deleted user)
if ( ! empty ( $get_user_id )) {
$user_id = $get_user_id ;
} else {
$user_id = 'not_assigned' ;
2024-04-23 13:34:30 +02:00
}
2024-02-26 17:42:46 +01:00
} else {
$user_id = 'not_assigned' ;
}
// make sure the target path exists
$target = $this -> userdata_dir . '/' . $user_id . '/' . $this -> eqsl_dir ;
if ( ! file_exists ( realpath ( APPPATH . '../' ) . '/' . $target )) {
mkdir ( realpath ( APPPATH . '../' ) . '/' . $target , 0755 , true );
}
// then copy the file
if ( ! copy ( $this -> src_eqsl . '/' . $file , $target . '/' . $file )) {
return false ; // Failed to copy file
}
}
}
// ***** QSL Cards ***** //
2024-04-23 13:34:30 +02:00
2024-02-26 17:42:46 +01:00
// Let's scan the whole folder and get necessary data for each file
foreach ( scandir ( $this -> src_qsl ) as $file ) {
// Ignore files if they are not jpg, png or gif
$file_extension = strtolower ( pathinfo ( $file , PATHINFO_EXTENSION ));
if ( ! in_array ( $file_extension , $allowed_file_extensions )) continue ;
if ( ! is_readable ( $this -> src_qsl . '/' . $file )) continue ;
if ( $file != '.' && $file != '..' ) {
// we need the qso_id from the database to get the necessary user_id
$qso_id = $this -> get_qsoid_from_qsl_filename ( $file ) ? ? '' ;
// check if the qso_id is empty, if yes we create a folder 'not assigned' instead of 'user_id'
if ( ! empty ( $qso_id )) {
// get the user_id for this qso_id
$get_user_id = $this -> Logbook_model -> get_user_id_from_qso ( $qso_id );
// it can happen that the user_id is empty even there is a qso_id (deleted qso or deleted user)
if ( ! empty ( $get_user_id )) {
$user_id = $get_user_id ;
} else {
$user_id = 'not_assigned' ;
}
} else {
$user_id = 'not_assigned' ;
}
// make sure the target path exists
$target = $this -> userdata_dir . '/' . $user_id . '/' . $this -> qsl_dir ;
if ( ! file_exists ( realpath ( APPPATH . '../' ) . '/' . $target )) {
mkdir ( realpath ( APPPATH . '../' ) . '/' . $target , 0755 , true );
}
// then copy the file
if ( ! copy ( $this -> src_qsl . '/' . $file , $target . '/' . $file )) {
return false ; // Failed to copy file
}
}
}
// here we create the 'migrated' flag
if ( ! file_exists ( realpath ( APPPATH . '../' ) . '/' . $this -> userdata_dir . '/' . $this -> flag_file )) {
touch ( realpath ( APPPATH . '../' ) . '/' . $this -> userdata_dir . '/' . $this -> flag_file );
}
return true ;
}
function check_migrated_flag ()
{
if ( ! file_exists ( realpath ( APPPATH . '../' ) . '/' . $this -> userdata_dir . '/' . $this -> flag_file )) {
return false ;
} else {
return true ;
}
}
function get_qsoid_from_eqsl_filename ( $filename )
{
$sql = " SELECT qso_id FROM eQSL_images WHERE image_file = ? " ;
$result = $this -> db -> query ( $sql , $filename );
$row = $result -> row ();
return $row -> qso_id ;
}
function get_qsoid_from_qsl_filename ( $filename )
{
$sql = " SELECT qsoid FROM qsl_images WHERE filename = ? " ;
$result = $this -> db -> query ( $sql , $filename );
$row = $result -> row ();
return $row -> qsoid ;
}
2024-04-23 13:34:30 +02:00
// Returns the number of qso's total on this instance
function count_all_qso () {
$sql = 'SELECT COUNT(*) AS total FROM ' . $this -> config -> item ( 'table_name' ) . ' WHERE station_id IS NOT NULL;' ;
$query = $this -> db -> query ( $sql );
return $query -> row () -> total ;
}
2024-09-16 00:57:27 +02:00
function count_users () {
$sql = 'SELECT COUNT(*) AS total FROM users;' ;
$query = $this -> db -> query ( $sql );
return $query -> row () -> total ;
}
2024-04-23 13:34:30 +02:00
function getMigrationVersion () {
$this -> db -> select_max ( 'version' );
$query = $this -> db -> get ( 'migrations' );
$migration_version = $query -> row ();
if ( $query -> num_rows () == 1 ) {
$migration_version = $query -> row () -> version ;
return $migration_version ;
} else {
return null ;
}
}
2024-04-23 13:52:57 +02:00
public function calls_without_station_id () {
$query = $this -> db -> query ( " select distinct COL_STATION_CALLSIGN from " . $this -> config -> item ( 'table_name' ) . " where station_id is null or station_id = '' " );
$result = $query -> result_array ();
return $result ;
}
2024-02-26 17:42:46 +01:00
}