Advertisement
saqib-ali

reportsFilter.php

Aug 24th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.91 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Reports Filter
  4.  */
  5. get_header(); ?>
  6.  
  7.     <div id="breadcrumbs">
  8.        
  9.         <?php
  10.             if(function_exists('bcn_display')) {
  11.                 bcn_display();
  12.             }
  13.         ?>
  14.        
  15.     </div>
  16.    
  17.     <section id="content-full">
  18.        
  19.         <h2>Research Reports</h2>
  20.        
  21.         <p>Filter the reports below:</p>
  22.        
  23.         <?php
  24.             // check if the form var is set and not equal to "all"
  25.             function searchParamSet($param) {
  26.                 if(isset($param) && !empty($param) && $param != "all") {
  27.                     return true;
  28.                 } else {
  29.                     return false;
  30.                 }
  31.             }
  32.        
  33.             $condition = $_POST['field_52d684eaf18a3'];
  34.             $research  = $_POST['field_52d684ecf18a4'];
  35.             $furtherArgs = array();
  36.            
  37.             // If the form has been submitted then query the reports
  38.             // using the search parameters
  39.             if(searchParamSet($condition) || searchParamSet($research)) {
  40.                 if(searchParamSet($condition) && searchParamSet($research)) {
  41.                     $furtherArgs = array(
  42.                         'relation' => 'AND'
  43.                     );
  44.                 }
  45.                 if(searchParamSet($condition)) {
  46.                     $furtherArgs[] = array(
  47.                         'key' => 'condition',
  48.                         'value' => $condition,
  49.                         'compare' => 'LIKE'
  50.                     );
  51.                 }
  52.                 if(searchParamSet($research)) {
  53.                     $furtherArgs[] = array(
  54.                         'key' => 'type_of_research',
  55.                         'value' => $research,
  56.                         'compare' => 'LIKE'
  57.                     );
  58.                 }
  59.             }
  60.          
  61.          
  62.             /*    
  63.                 $args = array(
  64.                     'numberposts' => -1,
  65.                     'post_type' => 'research_report',
  66.                     'meta_query' => array(
  67.                         'relation' => 'AND',
  68.                         array(
  69.                             'key' => 'location',
  70.                             'value' => 'Melbourne',
  71.                             'compare' => '='
  72.                         ),
  73.                         array(
  74.                             'key' => 'attendees',
  75.                             'value' => 100,
  76.                             'type' => 'NUMERIC',
  77.                             'compare' => '>'
  78.                         )
  79.                     )
  80.                 );
  81.             */
  82.            
  83.             $args = array(
  84.                 'numberposts' => -1,
  85.                 'post_type'  => 'research_report',
  86.                 'orderby'    => 'title',
  87.                 'order'      => 'ASC',
  88.                 'meta_query' => $furtherArgs
  89.             );
  90.             /*
  91.                 echo '<pre>';
  92.                 var_dump($args);
  93.                 echo '</pre>';
  94.             */
  95.             // get results
  96.             $the_query = new WP_Query( $args );
  97.         ?>
  98.         <? // Javascript to search+highlight any search terms ?>
  99.         <script type="text/javascript">
  100.             /* Make a new :Contains method that is case-insensitive */
  101.             jQuery.expr[':'].Contains = function(a, i, m) {
  102.                 return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
  103.             };
  104.             /*
  105.                 highlight v4
  106.                
  107.                 Highlights arbitrary terms.
  108.                
  109.                 <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
  110.                
  111.                 MIT license.
  112.                
  113.                 Johann Burkard
  114.                 <http://johannburkard.de>
  115.                 <mailto:jb@eaio.com>
  116.             */
  117.            
  118.             jQuery.fn.highlight=function(c){function e(b,c){var d=0;if(3==b.nodeType){var a=b.data.toUpperCase().indexOf(c);if(0<=a){d=document.createElement("span");d.className="highlight";a=b.splitText(a);a.splitText(c.length);var f=a.cloneNode(!0);d.appendChild(f);a.parentNode.replaceChild(d,a);d=1}}else if(1==b.nodeType&&b.childNodes&&!/(script|style)/i.test(b.tagName))for(a=0;a<b.childNodes.length;++a)a+=e(b.childNodes[a],c);return d}return this.length&&c&&c.length?this.each(function(){e(this,c.toUpperCase())}): this};jQuery.fn.removeHighlight=function(){return this.find("span.highlight").each(function(){this.parentNode.firstChild.nodeName;with(this.parentNode)replaceChild(this.firstChild,this),normalize()}).end()};
  119.            
  120.             $(document).ready(function() {
  121.                 var reportsList  = $('.reports-list');
  122.                 var reportsItems = $('.reports-list li');
  123.                
  124.                 var textSearch = function(searchTerm) {
  125.                     if (searchTerm == "") {
  126.                         reportsItems.removeHighlight();
  127.                         reportsItems.removeClass("visible");
  128.                         reportsItems.addClass("visible");
  129.                     } else {
  130.                         var foundin = $('li:Contains("'+ searchTerm +'")').closest("li");
  131.                        
  132.                         // Hide all of the search results
  133.                         reportsList.addClass("text-search");
  134.                         // Show all of the search results that contain said text
  135.                         reportsItems.removeClass("visible");
  136.                         foundin.addClass("visible");
  137.                         // Highlight all occurences of the search term
  138.                         reportsItems.removeHighlight();
  139.                         reportsItems.highlight(searchTerm);
  140.                     }
  141.                 }
  142.                
  143.                 // run the search on page load
  144.                 textSearch("<?= $_POST['searchTerm'] ?>");
  145.                
  146.                 // run the search when the user changes the text input
  147.                 window.addEventListener('input', function(e){
  148.                     textSearch($('#searchTerm').val());
  149.                 }, false);
  150.             });
  151.         </script>
  152.         <? // Hide the search field for those without JavaScript ?>
  153.         <noscript>
  154.             <style type="text/css">
  155.                 .text-search { display: none; }
  156.             </style>
  157.         </noscript>
  158.        
  159.         <form method="post" class="search-filter">
  160.             <div class="form-line text-search">
  161.                 <label class="label">Search:</label>
  162.                 <div class="controls">
  163.                     <input type="text" name="searchTerm" id="searchTerm" placeholder="Leave this blank to show all" class="textinput textinput-r" value="<?= ((!empty($_POST['searchTerm'])) ? $_POST['searchTerm'] : "") ?>" />
  164.                 </div>
  165.             </div>
  166.             <div class="form-line">
  167.                 <label class="label">Condition:</label>
  168.                 <div class="controls">
  169.                     <?php
  170.                         // Show the condition options in a dropdown box
  171.                         $field_key = "field_52d684eaf18a3";
  172.                         $field = get_field_object($field_key);
  173.                         if($field) {
  174.                             echo '<select name="', $field['key'] ,'" class="textinput textinput-r">';
  175.                                 // Create a default "ALL" option
  176.                                 echo '<option value="all">ALL</option>';
  177.                                 // Show all of the other options
  178.                                 foreach($field['choices'] as $k => $v) {
  179.                                     echo '<option value="', $k ,'"', (($condition == $k) ? "selected=\"selected\"" : "") ,'>', $v ,'</option>';
  180.                                 }
  181.                             echo '</select>';
  182.                         }
  183.                     ?>
  184.                 </div>
  185.             </div>
  186.             <div class="form-line">
  187.                 <label class="label">Research Type:</label>
  188.                 <div class="controls">
  189.                     <?php
  190.                         // Show the research options in a dropdown box
  191.                         $field2_key = "field_52d684ecf18a4";
  192.                         $field2 = get_field_object($field2_key);
  193.                         if($field2) {
  194.                             echo '<select name="', $field2['key'] ,'" class="textinput textinput-r">';
  195.                                 // Create a default "ALL" option
  196.                                 echo '<option value="all">ALL</option>';
  197.                                 // Show all of the other options
  198.                                 foreach($field2['choices'] as $k => $v) {
  199.                                     echo '<option value="', $k ,'"', (($research == $k) ? "selected=\"selected\"" : "") ,'>', $v ,'</option>';
  200.                                 }
  201.                             echo '</select>';
  202.                         }
  203.                     ?>
  204.                 </div>
  205.             </div>
  206.             <div class="form-line">
  207.                 <div class="controls">
  208.                     <input type="submit" name="submit_search" value="Search" class="btn btn-b" />
  209.                 </div>
  210.             </div>
  211.         </form>
  212.        
  213.         <ul class="reports-list">
  214.             <?php if( $the_query->have_posts() ): ?>
  215.                 <?php while($the_query->have_posts()) : $the_query->the_post(); ?>
  216.                     <li>
  217.                         <h2 class="report-title"><?php the_title(); ?></h2>
  218.                         <div class="report-content"><?php the_content(); ?></div>
  219.                     </li>
  220.                 <?php endwhile; ?>
  221.             <?php else: ?>
  222.                 <li>No reports matched your query.</li>
  223.             <?php endif; ?>
  224.         </ul>
  225.    
  226.     </section><!-- End Content-->
  227.        
  228.         <div style="clear: both"></div>
  229.        
  230.     </div>
  231.    
  232. </section><!-- page_body -->
  233.  
  234. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement