Advertisement
Guest User

Untitled

a guest
Dec 13th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.45 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This creates all the fields and assembles them
  5. * on the ad category sidebar based on either custom forms
  6. * built by the admin or it just defaults to a
  7. * standard form which has been pre-defined.
  8. *
  9. * @global <type> $wpdb
  10. * @param <type> $results
  11. *
  12. *
  13. */
  14.  
  15.  
  16. // queries the db for the custom ad form based on the cat id
  17. if ( !function_exists('cp_show_refine_search') ) :
  18. function cp_show_refine_search($catid) {
  19. global $wpdb;
  20. $fid = '';
  21.  
  22. // get the category ids from all the form_cats fields.
  23. // they are stored in a serialized array which is why
  24. // we are doing a separate select. If the form is not
  25. // active, then don't return any cats.
  26.  
  27. $results = $wpdb->get_results( $sql = $wpdb->prepare( "SELECT ID, form_cats FROM $wpdb->cp_ad_forms WHERE form_status = 'active'" ) );
  28.  
  29. if ( $results ) :
  30.  
  31. // now loop through the recordset
  32. foreach ( $results as $result ) {
  33.  
  34. // put the form_cats into an array
  35. $catarray = unserialize( $result->form_cats );
  36.  
  37. // now search the array for the $catid which was passed in via the cat drop-down
  38. if ( in_array($catid, $catarray) )
  39. $fid = $result->ID; // when there's a catid match, grab the form id
  40. }
  41.  
  42. // now we should have the formid so show the form layout based on the category selected
  43. $sql = $wpdb->prepare( "SELECT f.field_label, f.field_name, f.field_type, f.field_values, f.field_perm, m.field_search, m.meta_id, m.field_pos, m.field_req, m.form_id "
  44. . "FROM $wpdb->cp_ad_fields f "
  45. . "INNER JOIN $wpdb->cp_ad_meta m "
  46. . "ON f.field_id = m.field_id "
  47. . "WHERE m.form_id = %s "
  48. . "AND f.field_type <> 'text area' "
  49. . "AND m.field_search = '1' "
  50. . "ORDER BY m.field_pos ASC",
  51. $fid);
  52.  
  53. $results = $wpdb->get_results( $sql );
  54.  
  55. // echo $sql;
  56. //print_r($results);
  57.  
  58. if ( $results )
  59. echo cp_refine_search_builder($results); // loop through the custom form fields and display them
  60.  
  61. endif;
  62.  
  63. }
  64. endif;
  65.  
  66.  
  67. // loops through the custom fields and builds the custom refine search in the sidebar
  68. if ( !function_exists('cp_refine_search_builder') ) :
  69. function cp_refine_search_builder($results) {
  70. global $wpdb;
  71. $cp_min_price = str_replace( ',', '', $wpdb->get_var( "SELECT min( CAST( meta_value AS UNSIGNED ) ) FROM $wpdb->postmeta WHERE meta_key = 'cp_price'" ) );
  72. $cp_max_price = str_replace( ',', '', $wpdb->get_var( "SELECT max( CAST( meta_value AS UNSIGNED ) ) FROM $wpdb->postmeta WHERE meta_key = 'cp_price'" ) );
  73.  
  74. $locarray = array();
  75. ?>
  76. <script type="text/javascript">
  77. // <![CDATA[
  78. // toggles the refine search field values
  79. jQuery(document).ready(function($) {
  80. $('div.handle').click(function() {
  81. $(this).next('div.element').animate({
  82. height: ['toggle', 'swing'],
  83. opacity: 'toggle' }, 200
  84. );
  85.  
  86. $(this).toggleClass('open', 'close');
  87. return false;
  88. });
  89. <?php foreach ( $_GET as $field => $val ) : ?>
  90. $('.<?php echo esc_js($field); ?> div.handle').toggleClass('open', 'close');
  91. $('.<?php echo esc_js($field); ?> div.element').show();
  92. <?php endforeach; ?>
  93.  
  94. });
  95. // ]]>
  96. </script>
  97.  
  98. <div class="shadowblock_out">
  99.  
  100. <div class="shadowblock">
  101.  
  102. <h2 class="dotted"><?php _e('Refine Results', 'appthemes') ?></h2>
  103. <style>
  104. .element {display: block !important;}
  105. </style>
  106. <ul class="refine">
  107.  
  108. <form action="<?php if ( is_tax( APP_TAX_CAT ) ) echo get_term_link( get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); else bloginfo('wpurl'); ?>" method="get" name="refine-search">
  109. <?php if ( !is_tax( APP_TAX_CAT ) ) { ?>
  110. <input type="hidden" name="s" value="<?php echo esc_attr(cp_get_search_term()); ?>" />
  111. <input type="hidden" name="scat" value="<?php echo esc_attr(cp_get_search_catid()); ?>" />
  112. <?php } ?>
  113.  
  114. <?php
  115. // grab the price and location fields first and put into a separate array
  116. // then remove them from the results array so they don't print out again
  117. foreach ( $results as $key => $value ) {
  118.  
  119. switch ( $value->field_name ) :
  120.  
  121. case 'cp_city':
  122. $locarray[0] = $results[$key];
  123. unset($results[$key]);
  124. break;
  125.  
  126. case 'cp_zipcode':
  127. $locarray[1] = $results[$key];
  128. unset($results[$key]);
  129. break;
  130.  
  131. case 'cp_price':
  132. $locarray[2] = $results[$key];
  133. unset($results[$key]);
  134. break;
  135.  
  136. case 'cp_country':
  137. $locarray[3] = $results[$key];
  138. unset($results[$key]);
  139. break;
  140.  
  141. case 'cp_region':
  142. $locarray[4] = $results[$key];
  143. unset($results[$key]);
  144. break;
  145.  
  146. endswitch;
  147. }
  148.  
  149. // sort array by key so we get the city/zip code first
  150. ksort( $locarray );
  151.  
  152. // echo '<pre>';
  153. // print_r($locarray);
  154. // echo '</pre><br/><br/>';
  155.  
  156. // both zip code and city have been checked
  157. if ( array_key_exists(0, $locarray) && array_key_exists(1, $locarray) ) {
  158. $flabel = sprintf(__("%s or %s", 'appthemes'), $locarray[0]->field_label, $locarray[1]->field_label);
  159. $fname = 'cp_city_zipcode';
  160. } elseif ( array_key_exists(0, $locarray) ) { // must be the city only
  161. $flabel = $locarray[0]->field_label;
  162. $fname = 'cp_city_zipcode';
  163. } elseif ( array_key_exists(1, $locarray) ) { // must be the zip code only
  164. $flabel = $locarray[1]->field_label;
  165. $fname = 'cp_city_zipcode';
  166. }
  167.  
  168. $distance_unit = 'mi' == get_option( 'cp_distance_unit', 'mi' ) ? 'miles' : 'kilometers';
  169. // show the city/zip code field and radius slider bar
  170. if ( array_key_exists(0, $locarray) || array_key_exists(1, $locarray) ) :
  171. ?>
  172. <script type="text/javascript">
  173. // <![CDATA[
  174. jQuery(document).ready(function($) {
  175. $('#dist-slider').slider( {
  176. range: 'min',
  177. min: 0,
  178. max: 100,
  179. value: <?php echo esc_js( isset( $_GET['distance'] ) ? intval( $_GET['distance'] ) : '50' ); ?>,
  180. step: 5,
  181. slide: function(event, ui) {
  182. $('#distance').val(ui.value + ' <?php _e($distance_unit, 'appthemes'); ?>');
  183. }
  184. });
  185. $('#distance').val($('#dist-slider').slider('value') + ' <?php _e($distance_unit, 'appthemes'); ?>');
  186. });
  187. // ]]>
  188. </script>
  189.  
  190. <li class="distance">
  191. <label class="title"><?php echo $flabel; ?></label>
  192. <input name="<?php echo esc_attr( $fname ); ?>" id="<?php echo esc_attr( $fname ); ?>" type="text" minlength="2" value="<?php if(isset($_GET[$fname])) echo esc_attr( $_GET[$fname] ); ?>" class="text" />
  193. <div class="clr"></div>
  194. <label for="distance" class="title"><?php _e('Radius', 'appthemes'); ?>:</label>
  195. <input type="text" id="distance" name="distance" />
  196. <div id="dist-slider"></div>
  197. </li>
  198. <?php
  199.  
  200. endif;
  201.  
  202. // now loop through the other special fields
  203. foreach ( $locarray as $value ) :
  204.  
  205. // show the price field range slider
  206. if ( $value->field_name == 'cp_price' ) {
  207. $curr_symbol = get_option( 'cp_curr_symbol', '$' );
  208. $cp_curr_symbol_pos = get_option( 'cp_curr_symbol_pos', 'left' );
  209. if ( isset( $_GET['amount'] ) )
  210. $amount = explode( ' - ', $_GET['amount'] );
  211. $amount[0] = empty( $amount[0] ) ? $cp_min_price : $amount[0];
  212. $amount[1] = empty( $amount[1] ) ? $cp_max_price : $amount[1];
  213. $amount[0] = str_replace( array( ',', $curr_symbol, ' ' ), '', $amount[0] );
  214. $amount[1] = str_replace( array( ',', $curr_symbol, ' ' ), '', $amount[1] );
  215. ?>
  216.  
  217. <script type="text/javascript">
  218. // <![CDATA[
  219. jQuery(document).ready(function($) {
  220. $('#slider-range').slider( {
  221. range: true,
  222. min: <?php echo esc_js( intval( $cp_min_price ) ); ?>,
  223. max: <?php echo esc_js( intval( $cp_max_price ) ); ?>,
  224. step: 1,
  225. values: [ <?php echo esc_js( "{$amount[0]}, {$amount[1]}" ); ?> ],
  226. slide: function(event, ui) {
  227. <?php switch ( $cp_curr_symbol_pos ) {
  228. case 'left' :
  229. ?>$('#amount').val('<?php echo $curr_symbol; ?>' + ui.values[0] + ' - <?php echo $curr_symbol; ?>' + ui.values[1]);<?php
  230. break;
  231. case 'left_space' :
  232. ?>$('#amount').val('<?php echo $curr_symbol; ?> ' + ui.values[0] + ' - <?php echo $curr_symbol; ?> ' + ui.values[1]);<?php
  233. break;
  234. case 'right' :
  235. ?>$('#amount').val(ui.values[0] + '<?php echo $curr_symbol; ?> - ' + ui.values[1] + '<?php echo $curr_symbol; ?>' );<?php
  236. break;
  237. case 'right_space' :
  238. ?>$('#amount').val(ui.values[0] + ' <?php echo $curr_symbol; ?> - ' + ui.values[1] + ' <?php echo $curr_symbol; ?>' );<?php
  239. break;
  240. } ?>
  241. }
  242. });
  243. <?php switch ( $cp_curr_symbol_pos ) {
  244. case 'left' :
  245. ?>$('#amount').val('<?php echo $curr_symbol; ?>' + $('#slider-range').slider('values', 0) + ' - <?php echo $curr_symbol; ?>' + $('#slider-range').slider('values', 1));<?php
  246. break;
  247. case 'left_space' :
  248. ?>$('#amount').val('<?php echo $curr_symbol; ?> ' + $('#slider-range').slider('values', 0) + ' - <?php echo $curr_symbol; ?> ' + $('#slider-range').slider('values', 1));<?php
  249. break;
  250. case 'right' :
  251. ?>$('#amount').val($('#slider-range').slider('values', 0) + '<?php echo $curr_symbol; ?> - ' + $('#slider-range').slider('values', 1) + '<?php echo $curr_symbol; ?>');<?php
  252. break;
  253. case 'right_space' :
  254. ?>$('#amount').val($('#slider-range').slider('values', 0) + ' <?php echo $curr_symbol; ?> - ' + $('#slider-range').slider('values', 1) + ' <?php echo $curr_symbol; ?>');<?php
  255. break;
  256. } ?>
  257.  
  258. });
  259. // ]]>
  260. </script>
  261. <p>Insert a Min and Max price e.g. £5 - £30</p>
  262. <li class="amount">
  263. <label class="title"><?php echo esc_html( translate( $value->field_label, 'appthemes' ) ); ?>:</label>
  264. <input type="text" id="amount" name="amount" />
  265. <div id="slider-range"></div>
  266. </li>
  267. <?php
  268.  
  269. }
  270.  
  271. if ( 'cp_region' == $value->field_name || 'cp_country' == $value->field_name )
  272. echo cp_refine_fields($value->field_label, $value->field_name, $value->field_values);
  273.  
  274.  
  275. // show the state values
  276. // uncomment to include states
  277. // if ( $value->field_name == 'cp_state' )
  278. // echo cp_refine_fields( $value->field_label, $value->field_name, $value->field_values );
  279.  
  280. endforeach;
  281.  
  282.  
  283. // echo '<pre>';
  284. // print_r($results);
  285. // echo'</pre>';
  286.  
  287.  
  288. foreach ( $results as $key => $result ) {
  289.  
  290. switch ( $result->field_type ) :
  291.  
  292. // case 'text box':
  293. case 'radio':
  294. case 'checkbox':
  295. case 'drop-down':
  296.  
  297. echo cp_refine_fields( $result->field_label, $result->field_name, $result->field_values );
  298.  
  299. break;
  300.  
  301. endswitch;
  302. ?>
  303.  
  304. <?php
  305. }
  306. ?>
  307. <div class="pad10"></div>
  308. <button class="obtn btn_orange" type="submit" tabindex="1" id="go" value="Go" name="sa"><?php _e('Refine Results &rsaquo;&rsaquo;', 'appthemes'); ?></button>
  309.  
  310. <input type="hidden" name="refine_search" value="yes" />
  311.  
  312. </form>
  313.  
  314. </ul>
  315.  
  316. <div class="clr"></div>
  317.  
  318. </div>
  319.  
  320. </div>
  321.  
  322. <?php
  323. }
  324. endif;
  325.  
  326.  
  327. // spit out the field names and values
  328. function cp_refine_fields($label, $name, $values) {
  329. ?>
  330.  
  331. <li class="<?php echo esc_attr( $name ); ?>">
  332. <label class="title"><?php echo esc_html( translate( $label, 'appthemes' ) ); ?></label>
  333.  
  334. <div class="handle close"></div>
  335.  
  336. <div class="element">
  337.  
  338. <?php
  339. $options = explode(',', $values);
  340. $optionCursor = 1;
  341. $checked = '';
  342. ?>
  343.  
  344. <div class="scrollbox">
  345.  
  346. <ol class="checkboxes">
  347.  
  348. <?php
  349. $cur = isset( $_GET[$name] ) ? $_GET[$name] : array();
  350. foreach ( $options as $option ) {
  351. if ( $cur )
  352. $checked = in_array( trim( $option ), $cur ) ? " checked='checked'" : ''; ?>
  353. <li>
  354. <input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( trim( $option ) ); ?>" <?php echo $checked;?>/>&nbsp;<label for="<?php echo esc_attr( $name ); ?>[]"><?php echo esc_html( trim($option) ); ?></label>
  355. </li> <!-- #checkbox -->
  356. <?php } ?>
  357.  
  358. </ol> <!-- #checkbox-wrap -->
  359.  
  360. </div> <!-- #end scrollbox -->
  361.  
  362. </div> <!-- #end element -->
  363.  
  364. <div class="clr"></div>
  365.  
  366. </li>
  367. <?php
  368. }
  369.  
  370. function cp_pre_get_posts( $query ) {
  371. global $wpdb;
  372. if ( $query->is_archive && isset( $query->query_vars['ad_cat'] ) ) {
  373. $meta_query = array();
  374. foreach ( $_GET as $key => $value ) {
  375. if ( $value )
  376. switch ( $key ) {
  377. case 'cp_city_zipcode' :
  378. $region = get_option( 'cp_gmaps_region', 'us' );
  379. $value = urlencode( $value );
  380. $geocode = json_decode( wp_remote_retrieve_body( wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=$value&sensor=false&region=$region" ) ) );
  381. if ( 'OK' == $geocode->status ) {
  382. $query->set( 'app_geo_query', array(
  383. 'lat' => $geocode->results[0]->geometry->location->lat,
  384. 'lng' => $geocode->results[0]->geometry->location->lng,
  385. 'rad' => intval( $_GET['distance'] ),
  386. ) );
  387. } else {
  388. // Google Maps API error
  389. }
  390. break;
  391.  
  392. case 'amount' :
  393. $value = str_replace( array( get_option( 'cp_curr_symbol' ), ' ' ), '', $value );
  394. $value = str_replace( ' ', '', $value );
  395. $meta_query[] = array(
  396. 'key' => 'cp_price',
  397. 'value' => explode( '-', $value ),
  398. 'compare' => 'BETWEEN',
  399. 'type' => 'numeric',
  400. );
  401. break;
  402.  
  403. default :
  404. if ( 'cp_' == substr( $key, 0, 3 ) ) {
  405. $meta_query[] = array(
  406. 'key' => $key,
  407. 'value' => $value,
  408. 'compare' => 'IN'
  409. );
  410. }
  411. break;
  412. }
  413. }
  414. $query->set( 'meta_query', $meta_query );
  415. //var_dump( $_GET );
  416. }
  417.  
  418. return $query;
  419. }
  420. add_filter( 'pre_get_posts', 'cp_pre_get_posts' );
  421.  
  422. function cp_posts_clauses( $clauses, $wp_query ) {
  423. global $wpdb;
  424.  
  425. $geo_query = $wp_query->get( 'app_geo_query' );
  426. if ( !$geo_query )
  427. return $clauses;
  428.  
  429. extract( $geo_query, EXTR_SKIP );
  430.  
  431. $R = 'mi' == get_option( 'cp_distance_unit', 'mi' ) ? 3959 : 6371;
  432. $table = $wpdb->cp_ad_geocodes;
  433.  
  434. $clauses['join'] .= " INNER JOIN $table ON ($wpdb->posts.ID = $table.post_id)";
  435.  
  436. $clauses['where'] .= $wpdb->prepare( " AND ( %d * acos( cos( radians(%f) ) * cos( radians(lat) ) * cos( radians(lng) - radians(%f) ) + sin( radians(%f) ) * sin( radians(lat) ) ) ) < %d", $R, $lat, $lng, $lat, $rad );
  437.  
  438. return $clauses;
  439. }
  440. add_filter( 'posts_clauses', 'cp_posts_clauses', 10, 2 );
  441.  
  442.  
  443. // refine search on custom fields
  444. function custom_search_refine_where($where) {
  445. global $wpdb, $wp_query, $refine_count;
  446.  
  447. $refine_count = 0; // count how many post meta we query
  448. $old_where = $where; // intercept the old where statement
  449.  
  450. if ( is_search() && isset($_GET['s']) && isset($_GET['refine_search']) ) {
  451. $query = '';
  452.  
  453. foreach ( $_GET as $key => $value ) {
  454. if ( $value ) {
  455. switch ( $key ) {
  456. case 'cp_city_zipcode' :
  457. $region = get_option( 'cp_gmaps_region', 'us' );
  458. $value = urlencode( $value );
  459. $geocode = json_decode( wp_remote_retrieve_body( wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=$value&sensor=false&region=$region" ) ) );
  460. if ( 'OK' == $geocode->status ) {
  461. $wp_query->set( 'search_geo_query', array(
  462. 'lat' => $geocode->results[0]->geometry->location->lat,
  463. 'lng' => $geocode->results[0]->geometry->location->lng,
  464. 'rad' => intval( $_GET['distance'] ),
  465. ) );
  466. } else {
  467. // Google Maps API error
  468. }
  469. break;
  470.  
  471. case 'amount' :
  472. $refine_count++;
  473. $value = str_replace( array( get_option( 'cp_curr_symbol' ), ' ' ), '', $value );
  474. $value = str_replace( ' ', '', $value );
  475. $value = explode( '-', $value );
  476.  
  477. $query .= " AND (";
  478. $query .= "(mt".$refine_count.".meta_key = 'cp_price')";
  479. $query .= " AND (CAST(mt".$refine_count.".meta_value AS SIGNED) BETWEEN '$value[0]' AND '$value[1]')";
  480. $query .= ")";
  481. break;
  482.  
  483. default :
  484. if ( 'cp_' == substr( $key, 0, 3 ) ) {
  485. $refine_count++;
  486. if(is_array($value))
  487. $value = implode("','",$value);
  488.  
  489. $query .= " AND (";
  490. $query .= "(mt".$refine_count.".meta_key = '$key')";
  491. $query .= " AND (CAST(mt".$refine_count.".meta_value AS CHAR) IN ('$value'))";
  492. $query .= ")";
  493. }
  494. break;
  495. }
  496. }
  497. }
  498.  
  499. $geo_query = $wp_query->get( 'search_geo_query' );
  500. if ( $geo_query ) {
  501. extract( $geo_query, EXTR_SKIP );
  502. $R = 'mi' == get_option( 'cp_distance_unit', 'mi' ) ? 3959 : 6371;
  503. $query .= $wpdb->prepare( " AND ( %d * acos( cos( radians(%f) ) * cos( radians(lat) ) * cos( radians(lng) - radians(%f) ) + sin( radians(%f) ) * sin( radians(lat) ) ) ) < %d", $R, $lat, $lng, $lat, $rad );
  504. }
  505.  
  506. if ( !empty($query) )
  507. $where .= $query;
  508.  
  509. }
  510.  
  511. return( $where );
  512. }
  513.  
  514.  
  515. // refine search on custom fields
  516. function custom_search_refine_join($join) {
  517. global $wpdb, $wp_query, $refine_count;
  518.  
  519. if ( is_search() && isset($_GET['s']) && isset($_GET['refine_search']) ) {
  520.  
  521. $geo_query = $wp_query->get( 'search_geo_query' );
  522. if ( $geo_query ) {
  523. $table = $wpdb->cp_ad_geocodes;
  524. $join .= " INNER JOIN $table ON ($wpdb->posts.ID = $table.post_id)";
  525. }
  526.  
  527. if ( isset($refine_count) && is_numeric($refine_count) && $refine_count > 0 ) {
  528. for ($i = 1; $i <= $refine_count; $i++) {
  529. $join .= " INNER JOIN $wpdb->postmeta AS mt".$i." ON ($wpdb->posts.ID = mt".$i.".post_id) ";
  530. }
  531. }
  532.  
  533. }
  534.  
  535. return $join;
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement