wickywills
11/21/2017 - 2:29 PM

search.php

<?php
/**
 * Search results page
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */

use Timber\Timber;
use Lumberjack\PostTypes\Post;

$templates = ['search.twig', 'posts.twig', 'generic-page.twig'];
$context = Timber::get_context();

$searchQuery = get_search_query();

/**
 * Breadcrumb
 */
ob_start();
doBreadcrumb();
$context['breadcrumb'] = ob_get_contents();
ob_end_clean();


$bannerImage = get_template_directory_uri() . "/assets/img/jpg/banner-image-compressor.jpg";
$context['banner'] = [
	"heading1"			=> 'Search results for \''.$searchQuery . '\'',
	"image"				=> $bannerImage
];

unset(FWP()->ajax->query_vars);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = Post::posts([
    's'			=> $searchQuery,
	'paged'		=> $paged, 
	'post_type' => array( 'page', 'post', 'listing', 'event' ),
	'meta_query'  => array(
		'relation' => 'OR',
		array(
			'key'     => 'featured',
			'compare' => 'NOT EXISTS',
		),
		array(
			'relation' => 'OR',
			array(
				'key'   => 'featured',
				'value' => 'on',
			),
			array(
				'key'     => 'featured',
				'value'   => 'on',
				'compare' => '!=',
			),
		),
	),
	'orderby'     => array( 'meta_value' => 'DESC', 'date' => 'DESC' ),
]);


/**
 * Filter bar
 */
$filterBarItemsArr[] = [
	"id" => "viewResultsAsMap",
	"title" => "View results as map",
	"link" => "#"
];

$context['filterBar'] = [
	"filters" => true,
	"items" => $filterBarItemsArr
];


/**
 * Filters
 */
$filters = [
	0 => [
		"title"	=> "Filter by what's on",
		"facet"	=> facetwp_display( 'facet', 'whats_on' )
	],
	1 => [
		"title"	=> "Filter by things to do",
		"facet"	=> facetwp_display( 'facet', 'things_to_do' )
	],
	2 => [
		"title"	=> "Filter by food and drink",
		"facet"	=> facetwp_display( 'facet', 'food_drink' )
	],
	3 => [
		"title"	=> "Filter by shopping",
		"facet"	=> facetwp_display( 'facet', 'shopping' )
	],
	4 => [
		"title"	=> "Filter by where to stay",
		"facet"	=> facetwp_display( 'facet', 'where_to_stay' )
	],
	5 => [
		"title"	=> "Filter by who is going",
		"facet"	=> facetwp_display( 'facet', 'by_who_is_going' )
	],
	6 => [
		"title"	=> "Filter by location",
		"facet"	=> facetwp_display( 'facet', 'by_location' )
	],
	7 => [
		"title"	=> "Seasonal",
		"facet"	=> facetwp_display( 'facet', 'seasonal' )
	]
];


// Remove all the filters that aren't needed
foreach( $filters as $key => $filter ){
	
	if( !$filter['facet'] || $filter['facet'] == " " ){
		unset($filters[$key]);
	}
}

$context['filters'] = $filters;
		

foreach($posts as $post) :
	
	// Get all terms relating to the current post
	if( $post->post_type == 'listing' ){
		$currentCats = get_the_terms($post, 'listing_category');
	} elseif ($post->post_type == 'event') {
		$currentCats = get_the_terms($post, 'event_category');
	}
	
	$currentCatsClasses = [];
	$currentCatsNames = [];
	$currentCatsSlugs = [];
	
	// Get the coordinates
	$currentBusinessCoords = get_field('exact_location');
	$currentBusinessLat = $currentBusinessCoords['lat'];
	$currentBusinessLng = $currentBusinessCoords['lng'];
	
	// Build up an array of category ID's (just in case there are more than one on the current business)
	if( is_array($currentCats) ) : foreach ($currentCats as $currentCat) :
		if(!empty($currentCat)) :
			if( $currentCat->parent == 0 ) :
				array_push($currentCatsClasses, 'e-pill-tag--category-'.$currentCat->slug);
				array_push($currentCatsNames, $currentCat->name);
				array_push($currentCatsSlugs, $currentCat->slug);
			endif;
		endif;
	endforeach; endif;
	
	// If it's featured, give the sub cat block a border and a 'featured' tag
	$featured = get_field('featured');
	$isFeatured = false;
	
	if( !empty($featured) ) :
		$isFeatured = true;
	endif;
	
	$fromDate		= (get_field('from_date') ? date("j M Y", strtotime(get_field('from_date'))) : "");
	$toDate			= (get_field('from_date') ? date("j M Y", strtotime(get_field('to_date'))) : "");
	
	// If a thumbnail exists, use that, else use the post featured image
	if( get_field('thumbnail') ) :
		$image = wp_get_attachment_image_url( get_field('thumbnail'), "product-block" );
	else :
		$image = wp_get_attachment_image_url(get_post_thumbnail_id(), "product-block");
	endif;
	
	
	// If a description exists, use that, else use the post excerpt
	if( get_field('thumbnail') ) :
		$description = get_field('short_description');
	else :
		$description = get_the_excerpt();
	endif;
	
	$title			= get_the_title();
	$locality		= get_field('locality');
	$categoryClass	= ( !empty($currentCatsClasses) ? $currentCatsClasses[0] : '' );
	$categorySlug	= ( !empty($currentCatsSlugs) ? $currentCatsSlugs[0] : '' );
	$categoryTitle	= ( !empty($currentCatsNames) ? $currentCatsNames[0] : '' );
	$link			= get_the_permalink();
	$contentImage	= wp_get_attachment_image_url( get_field('thumbnail'), "product-block" );
	$date			= ($fromDate == $toDate ? $fromDate : $fromDate . " - " . $toDate);
	
	$context['subCatItems'][] = [
		"image"				=> $image,
		"title"				=> $title,
		"locality"			=> $locality,
		"description"		=> $description,
		"categoryClass"		=> $categoryClass,
		"categorySlug"		=> $categorySlug,
		"categoryTitle"		=> $categoryTitle,
		"date"				=> $date,
		"link"				=> $link,
		"featured"			=> $isFeatured
	];
	
	$context['markers'][] = [
		"lat"				=> $currentBusinessLat,
		"lng"				=> $currentBusinessLng,
		"type"				=> $categorySlug,
		"content"			=> strip_tags($description),
		"contentImage"		=> $contentImage,
		"categorySlug"		=> $categorySlug,
		"categoryClass"		=> $categoryClass,
		"categoryTitle"		=> $categoryTitle
	];
	
endforeach;


/**
 * Pagination
 */
function doPagination(){
	wp_pagenavi(); 
}

ob_start();
doPagination();
$context['pagination'] = ob_get_contents();
ob_end_clean();

Timber::render($templates, $context);