2021-02-07 00:03:05 +01:00
< ? php
class Counties extends CI_Model
{
/*
* Fetches worked and confirmed counties
*/
function get_counties_array () {
$countiesArray = $this -> get_counties_summary ();
if ( isset ( $countiesArray )) {
return $countiesArray ;
} else {
return 0 ;
}
return 0 ;
}
/*
* Returns a result of worked / confirmed US Counties , grouped by STATE
* QSL card and EQSL is valid for award . Satellite does not count .
* No band split , as it only count the number of counties in the award .
*/
function get_counties_summary () {
2021-09-09 22:43:29 +02:00
$CI =& get_instance ();
$CI -> load -> model ( 'logbooks_model' );
$logbooks_locations_array = $CI -> logbooks_model -> list_logbook_relationships ( $this -> session -> userdata ( 'active_station_logbook' ));
2021-11-14 17:11:59 +01:00
if ( ! $logbooks_locations_array ) {
return null ;
}
2021-09-09 22:43:29 +02:00
$location_list = " ' " . implode ( " ',' " , $logbooks_locations_array ) . " ' " ;
2021-02-07 00:03:05 +01:00
2022-09-07 09:44:22 +02:00
$this -> load -> model ( 'bands' );
$bandslots = $this -> bands -> get_worked_bands ( 'uscounties' );
$bandslots_list = " ' " . implode ( " ',' " , $bandslots ) . " ' " ;
2026-03-24 16:04:23 +00:00
// Normalize county/state values so imported variants group consistently.
$normalizedCountyExpression = " LOWER(TRIM(SUBSTRING_INDEX(COL_CNTY, ',', -1))) " ;
$normalizedStateExpression = " UPPER(TRIM(CASE WHEN COALESCE(COL_STATE, '') <> '' THEN COL_STATE WHEN COL_CNTY REGEXP '^[A-Za-z] { 2},' THEN SUBSTRING_INDEX(COL_CNTY, ',', 1) ELSE '' END)) " ;
$normalizedStateExpressionOuter = " UPPER(TRIM(CASE WHEN COALESCE(thcv.COL_STATE, '') <> '' THEN thcv.COL_STATE WHEN thcv.COL_CNTY REGEXP '^[A-Za-z] { 2},' THEN SUBSTRING_INDEX(thcv.COL_CNTY, ',', 1) ELSE '' END)) " ;
$sql = " select count(distinct " . $normalizedCountyExpression . " ) countycountworked, coalesce(x.countycountconfirmed, 0) countycountconfirmed, " . $normalizedStateExpressionOuter . " as COL_STATE
2021-02-07 00:03:05 +01:00
from " . $this->config ->item('table_name') . " thcv
left outer join (
2026-03-24 16:04:23 +00:00
select count ( distinct " . $normalizedCountyExpression . " ) countycountconfirmed , " . $normalizedStateExpression . " as COL_STATE
2021-02-07 00:03:05 +01:00
from " . $this->config ->item('table_name') .
2021-09-09 22:43:29 +02:00
" where station_id in ( " . $location_list . " ) " .
2022-09-07 09:44:22 +02:00
" and col_band in ( " . $bandslots_list . " ) " .
2021-02-07 15:22:03 +01:00
" and COL_DXCC in ('291', '6', '110')
2021-02-07 00:03:05 +01:00
and coalesce ( COL_CNTY , '' ) <> ''
2026-03-24 16:04:23 +00:00
and " . $normalizedStateExpression . " <> ''
2021-02-07 00:03:05 +01:00
and COL_BAND != 'SAT'
and ( col_qsl_rcvd = 'Y' or col_eqsl_qsl_rcvd = 'Y' )
2026-03-24 16:04:23 +00:00
group by " . $normalizedStateExpression . "
2021-02-07 00:03:05 +01:00
order by COL_STATE
2026-03-24 16:04:23 +00:00
) x on " . $normalizedStateExpressionOuter . " = x . COL_STATE
2021-09-09 22:43:29 +02:00
where station_id in ( " . $location_list . " ) " .
2022-09-07 09:44:22 +02:00
" and col_band in ( " . $bandslots_list . " ) " .
2021-02-07 15:22:03 +01:00
" and COL_DXCC in ('291', '6', '110')
2021-02-07 00:03:05 +01:00
and coalesce ( COL_CNTY , '' ) <> ''
2026-03-24 16:04:23 +00:00
and " . $normalizedStateExpressionOuter . " <> ''
2021-02-07 00:03:05 +01:00
and COL_BAND != 'SAT'
2026-03-24 16:04:23 +00:00
group by " . $normalizedStateExpressionOuter . " , countycountconfirmed
order by " . $normalizedStateExpressionOuter ;
2021-02-07 00:03:05 +01:00
$query = $this -> db -> query ( $sql );
return $query -> result_array ();
}
2021-02-07 15:22:03 +01:00
/*
* Makes a list of all counties in given state
*/
function counties_details ( $state , $type ) {
if ( $type == 'worked' ) {
$counties = $this -> get_counties ( $state , 'none' );
} else if ( $type == 'confirmed' ) {
$counties = $this -> get_counties ( $state , 'confirmed' );
}
if ( ! isset ( $counties )) {
return 0 ;
} else {
ksort ( $counties );
return $counties ;
}
}
function get_counties ( $state , $confirmationtype ) {
2021-09-09 22:43:29 +02:00
$CI =& get_instance ();
$CI -> load -> model ( 'logbooks_model' );
$logbooks_locations_array = $CI -> logbooks_model -> list_logbook_relationships ( $this -> session -> userdata ( 'active_station_logbook' ));
2021-11-14 17:11:59 +01:00
if ( ! $logbooks_locations_array ) {
return null ;
}
2021-09-09 22:43:29 +02:00
$location_list = " ' " . implode ( " ',' " , $logbooks_locations_array ) . " ' " ;
2021-02-07 15:22:03 +01:00
2022-09-07 09:44:22 +02:00
$this -> load -> model ( 'bands' );
$bandslots = $this -> bands -> get_worked_bands ( 'uscounties' );
$bandslots_list = " ' " . implode ( " ',' " , $bandslots ) . " ' " ;
2026-03-24 16:04:23 +00:00
$normalizedCountySelect = " TRIM(SUBSTRING_INDEX(COL_CNTY, ',', -1)) " ;
$normalizedCountyOrder = " LOWER(TRIM(SUBSTRING_INDEX(COL_CNTY, ',', -1))) " ;
$normalizedStateExpression = " UPPER(TRIM(CASE WHEN COALESCE(COL_STATE, '') <> '' THEN COL_STATE WHEN COL_CNTY REGEXP '^[A-Za-z] { 2},' THEN SUBSTRING_INDEX(COL_CNTY, ',', 1) ELSE '' END)) " ;
$sql = " select MIN( " . $normalizedCountySelect . " ) as COL_CNTY, " . $normalizedStateExpression . " as COL_STATE
2021-02-07 15:22:03 +01:00
from " . $this->config ->item('table_name') . " thcv
2021-09-09 22:43:29 +02:00
where station_id in ( " . $location_list . " ) " .
2022-09-07 09:44:22 +02:00
" and col_band in ( " . $bandslots_list . " ) " .
2021-02-07 15:22:03 +01:00
" and COL_DXCC in ('291', '6', '110')
and coalesce ( COL_CNTY , '' ) <> ''
2026-03-24 16:04:23 +00:00
and " . $normalizedStateExpression . " <> ''
2021-02-07 15:22:03 +01:00
and COL_BAND != 'SAT' " ;
if ( $state != 'All' ) {
2026-03-24 16:04:23 +00:00
$sql .= " and " . $normalizedStateExpression . " = " . $this -> db -> escape ( strtoupper ( $state ));
2021-02-07 15:22:03 +01:00
}
if ( $confirmationtype != 'none' ) {
$sql .= " and (col_qsl_rcvd='Y' or col_eqsl_qsl_rcvd='Y') " ;
}
2026-03-24 16:04:23 +00:00
$sql .= " group by " . $normalizedCountyOrder . " , " . $normalizedStateExpression ;
$sql .= " order by " . $normalizedStateExpression . " , " . $normalizedCountySelect ;
2021-02-07 15:22:03 +01:00
$query = $this -> db -> query ( $sql );
return $query -> result_array ();
}
2021-02-07 00:03:05 +01:00
}