
<?php
/*
Template Name: Map Page
*/
?>
<?php get_header(); ?>
 
        <div id="container">
            <div id="content">

<?php the_post(); ?>
                
<?php
$city = '';
$postcode = '';
   if(isset($_POST['submit1'])){
       if(!empty($_POST['city'])){
           $city = $_POST['city'];
       }
       if(!empty($_POST['postcode'])){
           $postcode = $_POST['postcode'];
       }
   }
?>
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    
                    <div class="entry-content">
                        <?php the_content(); ?>
                        <form action="" method="post" id="map_form">
<label for="postcode">Entrez votre code postal et votre ville</label>
                            <input type="text" name="postcode" id="postcode" value="<?php echo $postcode; ?>" />
                            <input type="text" name="city" id="city" value="<?php echo $city; ?>" />
                            <input type="submit" value="Ok" name="submit1" />
                        </form>

<?php 
if(empty($city)&&empty($postcode)){
echo do_shortcode('[locations]'); 
}elseif(!empty($city)){
        $loc_cat = get_terms('locations_category');
        foreach ($loc_cat as $v) {
            $name_pieces = explode(',',$v->name);
            foreach ($name_pieces as $va) {
                $va = trim($va);
                if(strtolower($va) == strtolower($city)){
                    $categ = get_term_by('name', $v->name, 'locations_category' );
                }
            }
        }
        echo do_shortcode('[locations category="'.$categ->term_id.'"]');
}elseif(!empty($postcode)){
        $loc_cat = get_terms('locations_category');
        foreach ($loc_cat as $v) {
            $name_pieces = explode(',',$v->name);
            foreach ($name_pieces as $va) {
                $va = trim($va);
                if(is_numeric($va)){
                    $va = substr($va, 0, strlen($va)-2);
                    $postcode1 = substr($postcode, 0,  strlen($postcode)-2);
                    if($va == $postcode1){
                        $categ = get_term_by('name', $v->name, 'locations_category' );
                    }
                }
            }
        }
        echo do_shortcode('[locations category="'.$categ->term_id.'"]');
}
?>


<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>
<?php edit_post_link( __( 'Edit', 'your-theme' ), '<span class="edit-link">', '</span>' ) ?>

                    </div><!-- .entry-content -->
                </div><!-- #post-<?php the_ID(); ?> -->           
 
<?php if ( get_post_custom_values('comments') ) comments_template() // Add a custom field with Name and Value of "comments" to enable comments on this page ?>            
 
            </div><!-- #content -->
			<?php get_sidebar(); ?>
        </div><!-- #container -->
 
<?php get_footer(); ?>
