@@ -1446,6 +1446,67 @@ function get_covid_hosp_facility($hospital_pks, $collection_weeks, $publication_
14461446 return count ($ epidata ) === 0 ? null : $ epidata ;
14471447}
14481448
1449+ // queries the `covid_hosp_facility` table for hospital discovery
1450+ // $state (optional): 2-letter state abbreviation
1451+ // $ccn (optional): cms certification number (ccn) of the given facility
1452+ // $city (optional): name of
1453+ // $zip (optional): 2-letter state abbreviation
1454+ // $fips_code (optional): 2-letter state abbreviation
1455+ // note: exactly one of the above parameters should be non-null. if more than
1456+ // one is non-null, then only the first filter will be used.
1457+ function get_covid_hosp_facility_lookup ($ state , $ ccn , $ city , $ zip , $ fips_code ) {
1458+ $ epidata = array ();
1459+ $ table = '`covid_hosp_facility` c ' ;
1460+ $ fields = implode (', ' , array (
1461+ 'c.`hospital_pk` ' ,
1462+ 'MAX(c.`state`) `state` ' ,
1463+ 'MAX(c.`ccn`) `ccn` ' ,
1464+ 'MAX(c.`hospital_name`) `hospital_name` ' ,
1465+ 'MAX(c.`address`) `address` ' ,
1466+ 'MAX(c.`city`) `city` ' ,
1467+ 'MAX(c.`zip`) `zip` ' ,
1468+ 'MAX(c.`hospital_subtype`) `hospital_subtype` ' ,
1469+ 'MAX(c.`fips_code`) `fips_code` ' ,
1470+ 'MAX(c.`is_metro_micro`) `is_metro_micro` ' ,
1471+ ));
1472+ // basic query info
1473+ $ group = 'c.`hospital_pk` ' ;
1474+ $ order = "c.`hospital_pk` ASC " ;
1475+ // build the filter
1476+ // these are all fast because the table has indexes on each of these fields
1477+ $ condition = 'FALSE ' ;
1478+ if ($ state !== null ) {
1479+ $ condition = filter_strings ('c.`state` ' , $ state );
1480+ } else if ($ ccn !== null ) {
1481+ $ condition = filter_strings ('c.`ccn` ' , $ ccn );
1482+ } else if ($ city !== null ) {
1483+ $ condition = filter_strings ('c.`city` ' , $ city );
1484+ } else if ($ zip !== null ) {
1485+ $ condition = filter_strings ('c.`zip` ' , $ zip );
1486+ } else if ($ fips_code !== null ) {
1487+ $ condition = filter_strings ('c.`fips_code` ' , $ fips_code );
1488+ }
1489+ // final query using specific issues
1490+ $ query = "SELECT {$ fields } FROM {$ table } WHERE ( {$ condition }) GROUP BY {$ group } ORDER BY {$ order }" ;
1491+ // get the data from the database
1492+ $ fields_string = array (
1493+ 'hospital_pk ' ,
1494+ 'state ' ,
1495+ 'ccn ' ,
1496+ 'hospital_name ' ,
1497+ 'address ' ,
1498+ 'city ' ,
1499+ 'zip ' ,
1500+ 'hospital_subtype ' ,
1501+ 'fips_code ' ,
1502+ );
1503+ $ fields_int = array ('is_metro_micro ' );
1504+ $ fields_float = null ;
1505+ execute_query ($ query , $ epidata , $ fields_string , $ fields_int , $ fields_float );
1506+ // return the data
1507+ return count ($ epidata ) === 0 ? null : $ epidata ;
1508+ }
1509+
14491510// queries a bunch of epidata tables
14501511function get_meta () {
14511512 // query and return metadata
@@ -1956,6 +2017,17 @@ function meta_delphi() {
19562017 $ epidata = get_covid_hosp_facility ($ hospital_pks , $ collection_weeks , $ publication_dates );
19572018 store_result ($ data , $ epidata );
19582019 }
2020+ } else if ($ source === 'covid_hosp_facility_lookup ' ) {
2021+ if (require_any ($ data , array ('state ' , 'ccn ' , 'city ' , 'zip ' , 'fips_code ' ))) {
2022+ $ state = isset ($ _REQUEST ['state ' ]) ? extract_values ($ _REQUEST ['state ' ], 'str ' ) : null ;
2023+ $ ccn = isset ($ _REQUEST ['ccn ' ]) ? extract_values ($ _REQUEST ['ccn ' ], 'str ' ) : null ;
2024+ $ city = isset ($ _REQUEST ['city ' ]) ? extract_values ($ _REQUEST ['city ' ], 'str ' ) : null ;
2025+ $ zip = isset ($ _REQUEST ['zip ' ]) ? extract_values ($ _REQUEST ['zip ' ], 'str ' ) : null ;
2026+ $ fips_code = isset ($ _REQUEST ['fips_code ' ]) ? extract_values ($ _REQUEST ['fips_code ' ], 'str ' ) : null ;
2027+ // get the data
2028+ $ epidata = get_covid_hosp_facility_lookup ($ state , $ ccn , $ city , $ zip , $ fips_code );
2029+ store_result ($ data , $ epidata );
2030+ }
19592031 } else {
19602032 $ data ['message ' ] = 'no data source specified ' ;
19612033 }
0 commit comments