2020-02-09 13:20:59 +01:00
< ? php
class was extends CI_Model {
2024-02-18 12:40:04 +00:00
function __construct () {
2024-02-18 14:54:45 +00:00
$this -> load -> library ( 'Genfunctions' );
2024-02-18 12:40:04 +00:00
}
2020-02-09 13:20:59 +01:00
2024-01-31 14:20:48 +00:00
public $stateString = 'AK,AL,AR,AZ,CA,CO,CT,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VA,VT,WA,WI,WV,WY' ;
2020-03-29 00:36:18 +01:00
2024-01-31 14:20:48 +00:00
function get_was_array ( $bands , $postdata ) {
$this -> load -> model ( 'logbooks_model' );
$logbooks_locations_array = $this -> logbooks_model -> list_logbook_relationships ( $this -> session -> userdata ( 'active_station_logbook' ));
2026-03-18 09:00:48 +01:00
if ( $logbooks_locations_array [ 0 ] === - 1 ) {
2024-01-31 14:20:48 +00:00
return null ;
}
2021-09-09 23:04:50 +02:00
2024-01-31 14:20:48 +00:00
$location_list = " ' " . implode ( " ',' " , $logbooks_locations_array ) . " ' " ;
$stateArray = explode ( ',' , $this -> stateString );
$states = array (); // Used for keeping track of which states that are not worked
2024-09-06 12:41:12 +00:00
$qsl = $this -> genfunctions -> gen_qsl_from_postdata ( $postdata );
2024-01-31 14:20:48 +00:00
foreach ( $stateArray as $state ) { // Generating array for use in the table
$states [ $state ][ 'count' ] = 0 ; // Inits each state's count
}
foreach ( $bands as $band ) {
foreach ( $stateArray as $state ) { // Generating array for use in the table
$bandWas [ $state ][ $band ] = '-' ; // Sets all to dash to indicate no result
}
if ( $postdata [ 'worked' ] != NULL ) {
$wasBand = $this -> getWasWorked ( $location_list , $band , $postdata );
foreach ( $wasBand as $line ) {
2025-12-01 14:25:18 +01:00
$state = $this -> normalizeState ( $line -> col_state ); // DC counts as MD
$bandWas [ $state ][ $band ] = '<div class="bg-danger awardsBgWarning"><a href=\'javascript:displayContacts("' . $state . '","' . $band . '","All","All","' . $postdata [ 'mode' ] . '","WAS", "")\'>W</a></div>' ;
$states [ $state ][ 'count' ] ++ ;
2024-01-31 14:20:48 +00:00
}
}
if ( $postdata [ 'confirmed' ] != NULL ) {
$wasBand = $this -> getWasConfirmed ( $location_list , $band , $postdata );
foreach ( $wasBand as $line ) {
2025-12-01 14:25:18 +01:00
$state = $this -> normalizeState ( $line -> col_state ); // DC counts as MD
$bandWas [ $state ][ $band ] = '<div class="bg-success awardsBgSuccess"><a href=\'javascript:displayContacts("' . $state . '","' . $band . '","All","All","' . $postdata [ 'mode' ] . '","WAS", "' . $qsl . '")\'>C</a></div>' ;
$states [ $state ][ 'count' ] ++ ;
2024-01-31 14:20:48 +00:00
}
}
}
// We want to remove the worked states in the list, since we do not want to display them
if ( $postdata [ 'worked' ] == NULL ) {
$wasBand = $this -> getWasWorked ( $location_list , $postdata [ 'band' ], $postdata );
foreach ( $wasBand as $line ) {
unset ( $bandWas [ $line -> col_state ]);
}
}
// We want to remove the confirmed states in the list, since we do not want to display them
if ( $postdata [ 'confirmed' ] == NULL ) {
$wasBand = $this -> getWasConfirmed ( $location_list , $postdata [ 'band' ], $postdata );
foreach ( $wasBand as $line ) {
unset ( $bandWas [ $line -> col_state ]);
}
}
if ( $postdata [ 'notworked' ] == NULL ) {
foreach ( $stateArray as $state ) {
if ( $states [ $state ][ 'count' ] == 0 ) {
unset ( $bandWas [ $state ]);
};
}
}
if ( isset ( $bandWas )) {
return $bandWas ;
}
else {
return 0 ;
}
}
/*
* Function gets worked and confirmed summary on each band on the active stationprofile
*/
2024-09-06 12:41:12 +00:00
function get_was_summary ( $bands , $postdata ) {
$this -> load -> model ( 'logbooks_model' );
$logbooks_locations_array = $this -> logbooks_model -> list_logbook_relationships ( $this -> session -> userdata ( 'active_station_logbook' ));
2021-09-09 23:04:50 +02:00
2026-03-18 09:00:48 +01:00
if ( $logbooks_locations_array [ 0 ] === - 1 ) {
2024-01-31 14:20:48 +00:00
return null ;
}
2021-11-14 17:11:59 +01:00
2021-09-09 23:04:50 +02:00
$location_list = " ' " . implode ( " ',' " , $logbooks_locations_array ) . " ' " ;
2020-08-18 14:41:16 +02:00
2024-01-31 14:20:48 +00:00
foreach ( $bands as $band ) {
$worked = $this -> getSummaryByBand ( $band , $postdata , $location_list );
$confirmed = $this -> getSummaryByBandConfirmed ( $band , $postdata , $location_list );
$wasSummary [ 'worked' ][ $band ] = $worked [ 0 ] -> count ;
$wasSummary [ 'confirmed' ][ $band ] = $confirmed [ 0 ] -> count ;
}
2020-08-18 14:41:16 +02:00
2024-01-31 14:20:48 +00:00
$workedTotal = $this -> getSummaryByBand ( $postdata [ 'band' ], $postdata , $location_list );
$confirmedTotal = $this -> getSummaryByBandConfirmed ( $postdata [ 'band' ], $postdata , $location_list );
2020-10-15 12:24:20 +02:00
2024-01-31 14:20:48 +00:00
$wasSummary [ 'worked' ][ 'Total' ] = $workedTotal [ 0 ] -> count ;
$wasSummary [ 'confirmed' ][ 'Total' ] = $confirmedTotal [ 0 ] -> count ;
2020-10-15 12:24:20 +02:00
2024-01-31 14:20:48 +00:00
return $wasSummary ;
}
2020-08-18 14:41:16 +02:00
2024-09-06 12:41:12 +00:00
function getSummaryByBand ( $band , $postdata , $location_list ) {
$bindings = [];
2025-12-01 14:25:18 +01:00
// DC is combined with MD, so we use CASE to normalize
$sql = " SELECT count(distinct CASE WHEN thcv.col_state = 'DC' THEN 'MD' ELSE thcv.col_state END) as count FROM " . $this -> config -> item ( 'table_name' ) . " thcv " ;
2024-01-31 14:20:48 +00:00
$sql .= " where station_id in ( " . $location_list . " ) " ;
2020-08-19 17:23:24 +02:00
2024-01-31 14:20:48 +00:00
if ( $band == 'SAT' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and thcv.col_prop_mode = ? " ;
$bindings [] = $band ;
2024-01-31 14:20:48 +00:00
} else if ( $band == 'All' ) {
$this -> load -> model ( 'bands' );
2022-09-07 20:40:31 +02:00
$bandslots = $this -> bands -> get_worked_bands ( 'was' );
2024-01-31 14:20:48 +00:00
2022-09-07 20:40:31 +02:00
$bandslots_list = " ' " . implode ( " ',' " , $bandslots ) . " ' " ;
2024-01-31 14:20:48 +00:00
2022-09-07 20:40:31 +02:00
$sql .= " and thcv.col_band in ( " . $bandslots_list . " ) " .
2026-04-01 10:07:23 +02:00
" and (thcv.col_prop_mode !='SAT' or thcv.col_prop_mode is NULL) " ;
2024-01-31 14:20:48 +00:00
} else {
2026-04-01 10:07:23 +02:00
$sql .= " and (thcv.col_prop_mode !='SAT' or thcv.col_prop_mode is NULL) " ;
2024-09-06 12:41:12 +00:00
$sql .= " and thcv.col_band = ? " ;
$bindings [] = $band ;
2024-01-31 14:20:48 +00:00
}
2020-08-19 17:23:24 +02:00
2024-01-31 14:20:48 +00:00
if ( $postdata [ 'mode' ] != 'All' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and (col_mode = ? or col_submode = ?) " ;
$bindings [] = $postdata [ 'mode' ];
$bindings [] = $postdata [ 'mode' ];
2022-01-04 19:13:41 +01:00
}
2024-01-31 14:20:48 +00:00
$sql .= $this -> addStateToQuery ();
2020-08-18 14:41:16 +02:00
2024-09-06 12:41:12 +00:00
$query = $this -> db -> query ( $sql , $bindings );
2020-08-19 17:23:24 +02:00
2024-01-31 14:20:48 +00:00
return $query -> result ();
}
2020-08-19 17:23:24 +02:00
2024-09-06 12:41:12 +00:00
function getSummaryByBandConfirmed ( $band , $postdata , $location_list ) {
$bindings = [];
2025-12-01 14:25:18 +01:00
// DC is combined with MD, so we use CASE to normalize
$sql = " SELECT count(distinct CASE WHEN thcv.col_state = 'DC' THEN 'MD' ELSE thcv.col_state END) as count FROM " . $this -> config -> item ( 'table_name' ) . " thcv " ;
2024-01-31 14:20:48 +00:00
$sql .= " where station_id in ( " . $location_list . " ) " ;
2020-08-18 14:41:16 +02:00
2024-01-31 14:20:48 +00:00
if ( $band == 'SAT' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and thcv.col_prop_mode = ? " ;
$bindings [] = $band ;
2024-01-31 14:20:48 +00:00
} else if ( $band == 'All' ) {
$this -> load -> model ( 'bands' );
2022-09-07 20:40:31 +02:00
$bandslots = $this -> bands -> get_worked_bands ( 'was' );
2024-01-31 14:20:48 +00:00
2022-09-07 20:40:31 +02:00
$bandslots_list = " ' " . implode ( " ',' " , $bandslots ) . " ' " ;
2024-01-31 14:20:48 +00:00
2022-09-07 20:40:31 +02:00
$sql .= " and thcv.col_band in ( " . $bandslots_list . " ) " .
2026-04-01 10:07:23 +02:00
" and (thcv.col_prop_mode !='SAT' or thcv.col_prop_mode is NULL) " ;
2024-01-31 14:20:48 +00:00
} else {
2026-04-01 10:07:23 +02:00
$sql .= " and (thcv.col_prop_mode !='SAT' or thcv.col_prop_mode is NULL) " ;
2024-09-06 12:41:12 +00:00
$sql .= " and thcv.col_band = ? " ;
$bindings [] = $band ;
2024-01-31 14:20:48 +00:00
}
2020-08-19 17:23:24 +02:00
2024-01-31 14:20:48 +00:00
if ( $postdata [ 'mode' ] != 'All' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and (col_mode = ? or col_submode = ?) " ;
$bindings [] = $postdata [ 'mode' ];
$bindings [] = $postdata [ 'mode' ];
2022-01-04 19:13:41 +01:00
}
2020-08-19 17:23:24 +02:00
2024-02-18 14:54:45 +00:00
$sql .= $this -> genfunctions -> addQslToQuery ( $postdata );
2022-01-04 19:13:41 +01:00
2024-01-31 14:20:48 +00:00
$sql .= $this -> addStateToQuery ();
2020-08-18 14:41:16 +02:00
2024-09-06 12:41:12 +00:00
$query = $this -> db -> query ( $sql , $bindings );
2020-08-18 14:41:16 +02:00
2024-01-31 14:20:48 +00:00
return $query -> result ();
}
2020-08-18 14:41:16 +02:00
2024-01-31 14:20:48 +00:00
/*
* Function returns all worked , but not confirmed states
* $postdata contains data from the form , in this case Lotw or QSL are used
*/
function getWasWorked ( $location_list , $band , $postdata ) {
2024-09-06 12:41:12 +00:00
$bindings = [];
2024-01-31 14:20:48 +00:00
$sql = " SELECT distinct col_state FROM " . $this -> config -> item ( 'table_name' ) . " thcv
where station_id in ( " . $location_list . " ) " ;
2020-03-29 00:36:18 +01:00
2021-07-27 12:11:41 +02:00
if ( $postdata [ 'mode' ] != 'All' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and (col_mode = ? or col_submode = ?) " ;
$bindings [] = $postdata [ 'mode' ];
$bindings [] = $postdata [ 'mode' ];
2021-07-27 12:11:41 +02:00
}
2024-01-31 14:20:48 +00:00
$sql .= $this -> addStateToQuery ();
2020-03-29 00:36:18 +01:00
2024-09-06 12:41:12 +00:00
$sql .= $this -> genfunctions -> addBandToQuery ( $band , $bindings );
2020-02-09 13:20:59 +01:00
2024-01-31 14:20:48 +00:00
$sql .= " and not exists (select 1 from " . $this -> config -> item ( 'table_name' ) .
" where station_id in ( " . $location_list . " ) " .
" and col_state = thcv.col_state " ;
2020-02-09 13:20:59 +01:00
2021-07-27 12:11:41 +02:00
if ( $postdata [ 'mode' ] != 'All' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and (col_mode = ? or col_submode = ?) " ;
$bindings [] = $postdata [ 'mode' ];
$bindings [] = $postdata [ 'mode' ];
2021-07-27 12:11:41 +02:00
}
2024-09-06 12:41:12 +00:00
$sql .= $this -> genfunctions -> addBandToQuery ( $band , $bindings );
2020-02-09 13:20:59 +01:00
2024-02-18 14:54:45 +00:00
$sql .= $this -> genfunctions -> addQslToQuery ( $postdata );
2020-03-29 00:36:18 +01:00
2024-01-31 14:20:48 +00:00
$sql .= $this -> addStateToQuery ();
2020-03-29 00:36:18 +01:00
2024-01-31 14:20:48 +00:00
$sql .= " ) " ;
2020-03-29 00:36:18 +01:00
2024-09-06 12:41:12 +00:00
$query = $this -> db -> query ( $sql , $bindings );
2020-03-29 00:36:18 +01:00
2024-01-31 14:20:48 +00:00
return $query -> result ();
}
2020-03-29 00:36:18 +01:00
2024-01-31 14:20:48 +00:00
/*
* Function returns all confirmed states on given band and on LoTW or QSL
* $postdata contains data from the form , in this case Lotw or QSL are used
*/
function getWasConfirmed ( $location_list , $band , $postdata ) {
2024-09-06 12:41:12 +00:00
$bindings = [];
2024-01-31 14:20:48 +00:00
$sql = " SELECT distinct col_state FROM " . $this -> config -> item ( 'table_name' ) . " thcv
where station_id in ( " . $location_list . " ) " ;
2020-03-29 00:36:18 +01:00
2021-07-27 12:11:41 +02:00
if ( $postdata [ 'mode' ] != 'All' ) {
2024-09-06 12:41:12 +00:00
$sql .= " and (col_mode = ? or col_submode = ?) " ;
$bindings [] = $postdata [ 'mode' ];
$bindings [] = $postdata [ 'mode' ];
2021-07-27 12:11:41 +02:00
}
2024-01-31 14:20:48 +00:00
$sql .= $this -> addStateToQuery ();
2024-09-06 12:41:12 +00:00
$sql .= $this -> genfunctions -> addBandToQuery ( $band , $bindings );
2024-01-31 14:20:48 +00:00
2024-02-18 14:54:45 +00:00
$sql .= $this -> genfunctions -> addQslToQuery ( $postdata );
2024-01-31 14:20:48 +00:00
2024-09-06 12:41:12 +00:00
$query = $this -> db -> query ( $sql , $bindings );
2024-01-31 14:20:48 +00:00
return $query -> result ();
}
function addStateToQuery () {
$sql = '' ;
$sql .= " and COL_DXCC in ('291', '6', '110') " ;
2025-12-01 14:25:18 +01:00
// DC is combined with MD for WAS award
$sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY') " ;
2024-01-31 14:20:48 +00:00
return $sql ;
}
2025-12-01 14:25:18 +01:00
/**
* Normalize state code - DC counts as MD for WAS award
*/
function normalizeState ( $state ) {
return ( $state === 'DC' ) ? 'MD' : $state ;
}
2020-02-09 13:20:59 +01:00
}
2021-07-24 14:31:16 +02:00
?>