Last active
March 21, 2017 10:08
-
-
Save JacquesJahnichen/f700ca75fb1906f8abb138ffb38b3686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: vivi-search-events-nodates | |
* | |
* This is template will display a login form | |
*/ | |
add_action('get_header', 'ck_do_login_form'); | |
// Début de page pour supprimer les commentaires présents sur les pages par défault | |
function ck_do_login_form() { | |
add_filter('comments_open', '__return_false'); | |
add_filter('comments_array', '__return_empty_array'); | |
} | |
/* | |
* Add custom CSS | |
*/ | |
function custom_load_custom_style_sheet() { | |
wp_enqueue_style( 'custom-stylesheet-grille', CHILD_URL . '/lib/vivi-search.css', array(), PARENT_THEME_VERSION ); | |
} | |
/* | |
* Add Title page | |
*/ | |
function wsm_show_blog_page_title_text() { | |
echo '<center><h1 class="entry-title">' . get_the_title() . '</h1></center>'; | |
} | |
/* | |
* Form | |
*/ | |
function prefix_searchwp_form( $query ) { | |
?> | |
<div class="search-form-workshops" > | |
<form class="searchwp-form-worksops" action="/ateliers-passes/" method="get"> | |
<input class="search-form-criteria" type="text" id="searchwpquery" name="searchwpquery" value="<?php echo stripslashes (esc_attr( $query )); ?>" /> | |
<select name="search-criteria-departement" class="search-form-criteria" > | |
<option value="">France</option> | |
<?php | |
$taxonomy = array('espresso_event_categories'); | |
$args = array('orderby'=>'name', 'hide_empty' => false, 'exclude' => array('71','81','297'), 'exclude_tree' => array('73','300'),); | |
$ee_terms = get_terms($taxonomy, $args); | |
foreach($ee_terms as $term){ | |
if (preg_match('/^[a-zA-Z]/', $term->name) == 1 && ($term->name != "France") && ($term->name != "Paris")){ | |
$html_option = ""; | |
$html_option .= '<option value="' . $term->term_id . '" '; | |
// Select default value | |
if ($term->term_id == $_GET['search-criteria-departement']) { | |
$html_option .= ' selected="selected" '; | |
} | |
$html_option .= ' >'. $term->name . '</option>'; | |
echo $html_option; | |
} | |
} | |
?> | |
</select> | |
<select class="search-form-criteria" name="search-criteria-categorie" id="ee_filter_cat" > | |
<option value="" >Toutes les disciplines</option> | |
<!-- Get termes option : https://codex.wordpress.org/Function_Reference/get_terms --> | |
<?php | |
$taxonomy = array('espresso_event_categories'); | |
$args = array('orderby'=>'name','hide_empty'=>true, 'exclude' => array('73'), 'exclude_tree' => array('71','89'),); | |
$ee_terms = get_terms($taxonomy, $args); | |
foreach($ee_terms as $term){ | |
$html_catégorie = ""; | |
$html_catégorie .= '<option value="' . $term->term_id . '" '; | |
// Select default value | |
if ($term->term_id == $_GET['search-criteria-categorie']) { | |
$html_catégorie .= ' selected="selected" '; | |
} | |
$html_catégorie .= ' >'. $term->name . '</option>'; | |
echo $html_catégorie; | |
} | |
?> | |
</select> | |
<div class="search-form-more" > | |
<div style="display:none;"> | |
<select name="criteria_age" class="search-form-criteria" ></select> | |
<select name="criteria_niveau" class="search-form-criteria" ></select> | |
<select name="criteria_frequence" class="search-form-criteria" ></select> | |
</div> | |
<select name="criteria_statut" class="search-form-criteria" > | |
<?php | |
$html_statut_option = '<option value="avenir"'; | |
if ($_GET['criteria_statut'] == "avenir") { | |
$html_statut_option .= ' selected="selected"'; | |
} | |
$html_statut_option .= '>A venir</option>'; | |
echo $html_statut_option; | |
?> | |
<?php | |
$html_statut_option = '<option value="tout"'; | |
if ($_GET['criteria_statut'] == "tout") { | |
$html_statut_option .= ' selected="selected"'; | |
} | |
$html_statut_option .= '>Tout (A venir, complet et passé)</option>'; | |
echo $html_statut_option; | |
?> | |
</select> | |
</div> | |
<div class="search-form-submit" > | |
<button type="submit" ><?php echo __( 'Rechercher', 'text-domain' ); ?></button> | |
</div> | |
</form> | |
</div> | |
<?php | |
} | |
function mySearchEnginePostsPerPage() { | |
return 12; // 10 posts per page | |
} | |
/* | |
* Custom loop | |
*/ | |
function prefix_do_search_loop() { | |
global $wp_query, $post; | |
// Return early if SearchWP is disabled. | |
if ( ! class_exists( 'SearchWP' ) ) | |
return; | |
$searchwpquery = isset( $_GET['searchwpquery'] ) ? sanitize_text_field( $_GET['searchwpquery'] ) : ''; | |
$searchwppage = get_query_var( 'paged' ); | |
if( empty( $searchwppage ) ) | |
$searchwppage = 1; | |
// Load the custom SearchWP form. | |
prefix_searchwp_form( $searchwpquery ); | |
// Do nothing if no search has been performed. | |
if ( empty( $searchwpquery ) ){ | |
get_all_events(); | |
return; | |
} | |
// Instantiate SearchWP. | |
$engine = SearchWP::instance(); | |
// Set the supplemental search engine name. | |
$supplementalSearchEngineName = 'evenements'; | |
// perform the search | |
$posts = $engine->search( $supplementalSearchEngineName, $searchwpquery, $searchwppage ); | |
// prep pagiation | |
$original = array( 'max_num_pages' => $wp_query->max_num_pages, 'found_posts' => $wp_query->found_posts ); | |
$wp_query->max_num_pages = $engine->maxNumPages; | |
$wp_query->found_posts = $engine->foundPosts; | |
// trick subsequent function calls into thinking we're not on a page but in a loop | |
$wp_query-> is_singular = $wp_query->is_single = $wp_query->is_page = false; | |
//$posts = array_reverse($posts); | |
// output our loop | |
if( !empty( $posts ) ) | |
{ | |
// loop through posts | |
foreach ( $posts as $post ) | |
{ | |
setup_postdata( $post ); | |
html_event($post); | |
} | |
if( 'numeric' === genesis_get_option( 'posts_nav' ) || ( 'numeric' !== genesis_get_option( 'posts_nav' ) && $searchwppage < 2 ) ) | |
do_action( 'genesis_after_endwhile' ); | |
} else { | |
?> | |
<div style="height:400px;text-aligne:center;display:block;padding-top:30px;"> | |
<h3> Aucun résultat associé </h3> | |
</div> | |
<?php | |
} | |
$wp_query->max_num_pages = $original['max_num_pages']; | |
$wp_query->found_posts = $original['found_posts']; | |
} | |
/* | |
* Present event | |
*/ | |
function html_event($post){ | |
global $post; | |
$date_format = get_option( 'date_format' ); | |
$time_format = 'H\hi'; | |
// Get people | |
EE_Registry::instance()->load_helper( 'People_View' ); | |
EE_Registry::instance()->load_helper( 'Venue_View' ); | |
$people = EEH_People_View::get_people_for_event(); | |
?> | |
<div class="event-presentation" > | |
<div class="event-img"> | |
<?php | |
echo '<a href="' . get_permalink() . '">'; | |
echo the_post_thumbnail('post-image'); //Add in featured image | |
echo '</a>'; | |
?> | |
</div> | |
<div class="event-title"> | |
<h2 id="post-<?php the_ID(); ?>" > | |
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> | |
<?php the_title(); ?></a> | |
</h2> | |
</div> | |
<div class="event-text-information"> | |
<?php echo do_shortcode( '[yasr_pro_average_comments_ratings size="small"]' ); ?><?php the_field( "tse_event_custom_level" )?>-<?php the_field( "tse_event_custom_age" )?> | |
</div> | |
<div class="event-text"> | |
<?php | |
foreach ( $people as $type => $persons ) : | |
if(strtolower( $type == "Organisme")): | |
foreach ( $persons as $person ) : | |
if ( $person instanceof EE_Person ) : ?> | |
<a class="eea-people-addon-link-to-person" href="<?php echo get_permalink( $person->ID() ); ?>" title="<?php printf( __('Click here to view more info about %s', 'event_espresso' ), $person->full_name() ); ?>"><?php echo ucfirst(strtolower($person->full_name())); ?></a> | |
<?php | |
endif; | |
endforeach; | |
endif; | |
endforeach; | |
?> | |
</div> | |
<div class="event-description"> | |
<?php | |
echo the_excerpt(); | |
?> | |
</div> | |
<div class="event-date"> | |
<?php | |
$datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $post->ID, false, false, 1 ); | |
if (sizeof($datetimes) > 0){ | |
$datetime = end( $datetimes ); | |
$startdate = date_i18n( $date_format . ' ' . $time_format, strtotime( $datetime->start_date_and_time('Y-m-d', 'H:i:s') ) ); | |
echo "Prochaine date : " .$startdate; | |
} | |
?> | |
</div> | |
<div class="event-more"> | |
<?php | |
echo '<a href="' . get_permalink() . '" style="float:left;border:1px;">En savoir plus</a>'; | |
?> | |
</div> | |
</div> | |
<?php | |
} | |
/* | |
* Get global request to have all the filter available | |
*/ | |
// https://searchwp.com/docs/hooks/searchwp_query_join/ | |
// https://searchwp.com/docs/kb/using-custom-field-prioritize-search-results/ | |
function my_searchwp_query_main_join( $sql, $engine ) { | |
global $wpdb; | |
// Filter - only the futur event | |
if ($_GET['criteria_statut'] == "avenir"){ | |
$sql .= " INNER JOIN {$wpdb->prefix}esp_datetime ON {$wpdb->posts}.ID = {$wpdb->prefix}esp_datetime.EVT_ID "; | |
$sql .= " INNER JOIN {$wpdb->prefix}esp_datetime_ticket ON {$wpdb->prefix}esp_datetime.DTT_ID = {$wpdb->prefix}esp_datetime_ticket.DTT_ID "; | |
$sql .= " INNER JOIN {$wpdb->prefix}esp_ticket ON {$wpdb->prefix}esp_datetime_ticket.TKT_ID = {$wpdb->prefix}esp_ticket.TKT_ID "; | |
} | |
// Filter - taxonomy | |
if ( !empty($_GET['search-criteria-departement']) || !empty($_GET['search-criteria-categorie']) ){ | |
$sql .= " LEFT JOIN {$wpdb->prefix}term_relationships ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id) "; | |
} | |
return $sql; | |
} | |
function my_searchwp_query_conditions( $sql, $engine ) { | |
global $wpdb; | |
// Filter - only the futur event | |
if ($_GET['criteria_statut'] == "avenir"){ | |
$sql .= " AND {$wpdb->prefix}esp_datetime.DTT_EVT_start > '" . current_time( 'mysql', TRUE ) . "' "; | |
$sql .= " AND {$wpdb->prefix}esp_ticket.TKT_sold < {$wpdb->prefix}esp_ticket.TKT_qty "; | |
} | |
// Filter - taxonomy departement | |
if (!empty($_GET['search-criteria-departement'])){ | |
$sql .= " AND ( {$wpdb->prefix}term_relationships.term_taxonomy_id = " . $_GET['search-criteria-departement'] . " ) "; | |
} | |
// Filter - taxonomy arts | |
if (!empty($_GET['search-criteria-categorie'])){ | |
$sql .= " AND ( {$wpdb->prefix}term_relationships.term_taxonomy_id = " . $_GET['search-criteria-categorie'] . " ) "; | |
} | |
return $sql; | |
} | |
function my_searchwp_query_orderby( $orderby ) { | |
global $wpdb; | |
// Only the futur event - Order by next datetimes | |
if ($_GET['criteria_statut'] == "avenir"){ | |
$new_orderby = " ORDER BY {$wpdb->prefix}esp_datetime.DTT_EVT_start ASC, " . str_replace( 'ORDER BY', '', $orderby ) ; | |
// All the events - Order by post_title | |
} else { | |
$new_orderby = " ORDER BY {$wpdb->posts}.post_title ASC, " . str_replace( 'ORDER BY', '', $orderby ) ; | |
} | |
return $new_orderby; | |
} | |
// Actions & Filters | |
remove_action('genesis_loop', 'genesis_do_loop'); // Remove the standard loop | |
add_action( 'wp_enqueue_scripts', 'custom_load_custom_style_sheet' ); | |
add_action( 'genesis_before_content', 'wsm_show_blog_page_title_text', 2 ); | |
add_filter( 'searchwp_posts_per_page', 'mySearchEnginePostsPerPage' ); | |
add_action( 'genesis_loop', 'prefix_do_search_loop'); // Add custom loop | |
//add_filter( 'searchwp_include', 'my_searchwp_taxonomy_limiter'); | |
add_filter( 'searchwp_query_main_join', 'my_searchwp_query_main_join', 10, 2 ); | |
add_filter( 'searchwp_where', 'my_searchwp_query_conditions', 10, 2 ); | |
add_filter( 'searchwp_query_orderby', 'my_searchwp_query_orderby' ); | |
genesis(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment