bglick of Papercut Interactive
10/30/2018 - 1:03 PM

Zip Code Search (Santek)

<form method="post" action="#results">
	<input type="hidden" name="csrf_token" value="{csrf_token}">
	<input type="text" name="zip_code" placeholder="ENTER A ZIP CODE" class="zip-code-input">
	<input type="hidden" name="state" value="" class="state-field">
	<input type="hidden" name="city" value="" class="city-field">
			
  <div id="state-filter" class="filter-container">
  	<p class="select-btn"><span>STATE</span> <i class="icon-arrow-down"><svg width="20px" height="13px" viewBox="0 0 20 13" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><polygon id="triangle-down-1" class="" fill="#707070" points="0 0.7586207 20 0.7586207 10 12.2175066"></polygon></svg></i></p>
  	<div class="line dropdown state-dropdown">
  		<?php
  		
  		$state_abbrevs = array(
  			'AL'=>'ALABAMA',
  			'AK'=>'ALASKA',
  			'AS'=>'AMERICAN SAMOA',
  			'AZ'=>'ARIZONA',
  			'AR'=>'ARKANSAS',
  			'CA'=>'CALIFORNIA',
  			'CO'=>'COLORADO',
  			'CT'=>'CONNECTICUT',
  			'DE'=>'DELAWARE',
  			'DC'=>'DISTRICT OF COLUMBIA',
  			'FM'=>'FEDERATED STATES OF MICRONESIA',
  			'FL'=>'FLORIDA',
  			'GA'=>'GEORGIA',
  			'GU'=>'GUAM GU',
  			'HI'=>'HAWAII',
  			'ID'=>'IDAHO',
  			'IL'=>'ILLINOIS',
  			'IN'=>'INDIANA',
  			'IA'=>'IOWA',
  			'KS'=>'KANSAS',
  			'KY'=>'KENTUCKY',
  			'LA'=>'LOUISIANA',
  			'ME'=>'MAINE',
  			'MH'=>'MARSHALL ISLANDS',
  			'MD'=>'MARYLAND',
  			'MA'=>'MASSACHUSETTS',
  			'MI'=>'MICHIGAN',
  			'MN'=>'MINNESOTA',
  			'MS'=>'MISSISSIPPI',
  			'MO'=>'MISSOURI',
  			'MT'=>'MONTANA',
  			'NE'=>'NEBRASKA',
  			'NV'=>'NEVADA',
  			'NH'=>'NEW HAMPSHIRE',
  			'NJ'=>'NEW JERSEY',
  			'NM'=>'NEW MEXICO',
  			'NY'=>'NEW YORK',
  			'NC'=>'NORTH CAROLINA',
  			'ND'=>'NORTH DAKOTA',
  			'MP'=>'NORTHERN MARIANA ISLANDS',
  			'OH'=>'OHIO',
  			'OK'=>'OKLAHOMA',
  			'OR'=>'OREGON',
  			'PW'=>'PALAU',
  			'PA'=>'PENNSYLVANIA',
  			'PR'=>'PUERTO RICO',
  			'RI'=>'RHODE ISLAND',
  			'SC'=>'SOUTH CAROLINA',
  			'SD'=>'SOUTH DAKOTA',
  			'TN'=>'TENNESSEE',
  			'TX'=>'TEXAS',
  			'UT'=>'UTAH',
  			'VT'=>'VERMONT',
  			'VI'=>'VIRGIN ISLANDS',
  			'VA'=>'VIRGINIA',
  			'WA'=>'WASHINGTON',
  			'WV'=>'WEST VIRGINIA',
  			'WI'=>'WISCONSIN',
  			'WY'=>'WYOMING'
  		);
  		
  		// on .state-dropdown click, run query for cities, and then display options.
  		$query = ee()->db->select('field_id_40 as state')
      ->from('exp_channel_titles t')
      ->join('exp_channel_data_field_40 state', 't.entry_id = state.entry_id')
      ->where(array(
          't.channel_id' => '9',
      ))
  		->group_by('field_id_40')
     	->get();
  
  		if ($query->num_rows() > 0)
  		{
  			foreach($query->result_array() as $row)
  			{
  				echo '<a data-value="'.$row['state'].'">'.$state_abbrevs[$row['state']].'</a>';
  			}
  		}
  		
  		?>
  	</div>
  </div>
  <div id="city-filter" style="display:none" class="filter-container">
		<p class="select-btn"><span>CITY/TOWN</span> <i class="icon-arrow-down"><svg width="20px" height="13px" viewBox="0 0 20 13" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><polygon id="triangle-down2" class="" fill="#707070" points="0 0.7586207 20 0.7586207 10 12.2175066"></polygon></svg></i></p>
		<div class="line dropdown city-dropdown"></div>
	</div>
  <input type="submit" value="SEARCH">
</form>

			
<?php

	$query = ee()->db->select('field_id_40 as state, field_id_39 as city')
->from('exp_channel_titles t')
->join('exp_channel_data_field_40 state', 't.entry_id = state.entry_id')
->join('exp_channel_data_field_39 city', 't.entry_id = city.entry_id')
->where(array(
		't.channel_id' => '9',
		'state.field_id_40' => '{segment_3}',
))
->group_by('field_id_39')
->get();

if ($query->num_rows() > 0)
{
	foreach($query->result_array() as $row)
	{
		if (!empty($row['city'])){
			echo '<a href="#" data-value="'.$row['city'].'">'.$row['city'].'</a>';
		}
	}
}		

?>
<?php
	$facilities = '';
	$zip = '';
	$collection_type = array();
	$_SESSION['collection_type'] = '';
	$search_criteria = '';

	// ZIP CODE SEARCH
	if (!empty($_POST['zip_code'])){
		$zip = ee()->input->post('zip_code', TRUE);	
		$search_criteria = $zip;
		
		$query = ee()->db->select('field_id_49 as collection, field_id_36 as code, field_id_48 as landfill, field_id_50 as transfer_station, field_id_51 as collection_type')
		->from('exp_channel_titles t')
		->join('exp_channel_data_field_49 c', 't.entry_id = c.entry_id')
		->join('exp_channel_data_field_36 code', 't.entry_id = code.entry_id')
		->join('exp_channel_data_field_48 l', 't.entry_id = l.entry_id')
		->join('exp_channel_data_field_50 tr', 't.entry_id = tr.entry_id')
		->join('exp_channel_data_field_51 ct', 't.entry_id = ct.entry_id')
		->where(array(
				't.channel_id' => '9',
				'field_id_49' => $zip,						
		))
		->or_where(array(
				'field_id_48' => $zip,						
		))
		->or_where(array(
				'field_id_50' => $zip,						
		))
		->get();				
		
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
			{
				$facilities .= $row['code'].'|';
				if (!empty($row['collection_type'])){
					$collection_type[$row['code']] = $row['collection_type'];
				}
			}
			$facilities = rtrim($facilities,"|");
		}	
		$_SESSION['collection_type'] = $collection_type;
		
		// Make Note if there are no results
		if (empty($facilities)){
			$facilities = 'No Results';
		}
		
	}// Close zip code search

	// STATE SEARCH
	elseif (!empty($_POST['state']) && empty($_POST['city'])){
		$state = ee()->input->post('state', TRUE);
		$search_criteria = $state;
		
		$query = ee()->db->select('field_id_40 as state, field_id_36 as code')
		->from('exp_channel_titles t')
		->join('exp_channel_data_field_40 s', 't.entry_id = s.entry_id')
		->join('exp_channel_data_field_36 c', 't.entry_id = c.entry_id')
		->where(array(
				't.channel_id' => '9',
				'field_id_40' => $state,						
		))
		->group_by('field_id_36')
		->get();				
		
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
			{
				$facilities .= $row['code'].'|';
			}
			$facilities = rtrim($facilities,"|");
		}	
	} // Close state search

	elseif (!empty($_POST['state']) && !empty($_POST['city'])){
		$state = ee()->input->post('state', TRUE);
		$city = ee()->input->post('city', TRUE);
		$search_criteria = $city.', '.$state;
		
		$query = ee()->db->select('field_id_39 as city, field_id_40 as state, field_id_36 as code, field_id_51 as collection_type')
		->from('exp_channel_titles t')
		->join('exp_channel_data_field_39 city', 't.entry_id = city.entry_id')
		->join('exp_channel_data_field_40 s', 't.entry_id = s.entry_id')
		->join('exp_channel_data_field_36 c', 't.entry_id = c.entry_id')
		->join('exp_channel_data_field_51 ct', 't.entry_id = ct.entry_id')
		->where(array(
				't.channel_id' => '9',
				'field_id_40' => $state,
				'field_id_39' => $city,	
		))
		->group_by('field_id_36')
		->get();				
		
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
			{
				$facilities .= $row['code'].'|';
				if (!empty($row['collection_type'])){
					$collection_type[$row['code']] = $row['collection_type'];
				}
			}
			$facilities = rtrim($facilities,"|");
		}	
		$_SESSION['collection_type'] = $collection_type;
	} // Close city, state search
	
	// Make note that the user has not entered a search yet
	else {
		$facilities = 'No Queries';
	}
	
?>

<div class="search-wrapper">
	
	<h2 id="results">Results for <?php echo $search_criteria; ?>:</h2>
	
	<div class="results">
	{exp:channel:entries channel="facilities" search:facility_code="<?php echo $facilities; ?>" dynamic="no"}
		<div class="size1of4 search-result">
			<h3><strong>{title}</strong></h3>
			<h4>{facility_service}{if '{facility_service}' == 'Collection'}{embed="find-facility/collection-type" code="{facility_code}"}{/if}</h4>
			<p>
			{facility_address}<br/>
			{city}, {state}<br/>
			{zip}</p>
			<p>
			<span>{hours}</span>
		</p>
			<h4>Contact</h4>
		<a href="tel:{phone}">{phone}</a><br/>
			<a href="mailto:{facility_email}" class="contact-email">{facility_email}</a><br/>
			{if facebook}<a href="{facebook}"><img class="fb" src="/img/fb.svg"></a>{/if}
			<a data-fancybox data-src="#request-service" class="btn-blue contact-btn">request service</a><br/>
			<a data-fancybox data-src="#report-problem" class="btn-green contact-btn">report a problem</a>
		</div>
		{if no_results}
			<p>No Results</p>
		{/if}
	
			
	{/exp:channel:entries}
	
	</div>
</div>
//Ajax for get city filter
$(document).ready(function() {
  // Get Cities
	$('.state-dropdown a').click(function() {
		$state = $(this).data('value');	
		$('.state-field').val($state);
		$('#state-filter .select-btn span').html($(this).html());
		$.ajax({
			url: "/find-facility/get-cities/" + $state,
		})
			.done(function( data ) {
				if ($.trim(data)){//we have cities
					$( ".city-dropdown" ).html( data );
					$('#city-filter .select-btn span').html('CITY/TOWN');
					$('.city-dropdown a').click(function(e) {
						e.preventDefault();
						$city = $(this).data('value');	
						$('.city-field').val($city);
						$('#city-filter .select-btn span').html($(this).html());
					});
					$('#city-filter').show();
				} else {
					$('#city-filter').hide();
					$('.city-field').val('');
				}
			});		
	});
}):