Skip to content

Commit

Permalink
Added Pagination to Search Location Results awesomemotive#5
Browse files Browse the repository at this point in the history
  • Loading branch information
EkoJR committed Apr 7, 2017
1 parent f88edae commit 890da7e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 22 deletions.
63 changes: 58 additions & 5 deletions classes/simplemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ function display_map( $atts ) {

$to_display .= '<div id="simplemap" style="' . $hidemap . 'width: ' . $map_width . '; height: ' . $map_height . ';"></div>';
$to_display .= '<div id="results" style="' . $hidelist . 'width: ' . $map_width . ';"></div>';
$to_display .= '<div id="sm_page_footer" style="' . $hidelist . 'width: ' . $map_width . '; text-align: center; display: none;">';
$to_display .= '<a href="javascript:prevPage()" id="btn_prev" class="location-search-prev-page nav-previous alignleft">Prev</a>';
$to_display .= '<div id="sm_page_num" class="location-search-page-num">Page 1</div>';
$to_display .= '<a href="javascript:nextPage()" id="btn_next" class="location-search-next-page nav-next alignright">Next</a>';
$to_display .= '</div>';
$to_display .= '<script type="text/javascript">';
$to_display .= '(function($) { ';

Expand Down Expand Up @@ -215,7 +220,7 @@ function location_search_form( $atts ) {
$form_field_names = $this->get_form_field_names_from_shortcode_atts( $search_fields );

// Form onsubmit, action, and method values.
$on_submit = apply_filters( 'sm-location-search-onsubmit', ' onsubmit="searchLocations( 1 ); return false; "', $post->ID );
$on_submit = apply_filters( 'sm-location-search-onsubmit', ' onsubmit="searchOnSubmit(); return false; "', $post->ID );
$action = apply_filters( 'sm-locaiton-search-action', get_permalink(), $post->ID );
$method = apply_filters( 'sm-location-search-method', 'post', $post->ID );

Expand Down Expand Up @@ -427,6 +432,8 @@ function location_search_form( $atts ) {

// Hidden value for limit.
$location_search .= "<input type='hidden' id='location_search_limit' value='" . $limit_value . "' />";
$location_search .= "<input type='hidden' id='location_search_page_num' value='1' />";
$location_search .= "<input type='hidden' id='location_search_num_of_pages' value='0' />";

// Hidden value set to true if we got here via search.
$location_search .= "<input type='hidden' id='location_is_search_results' name='sm-location-search' value='" . $is_sm_search . "' />";
Expand Down Expand Up @@ -954,7 +961,9 @@ function searchLocations( is_search ) {
searchData.lat = document.getElementById('location_search_default_lat').value;
searchData.lng = document.getElementById('location_search_default_lng').value;
searchData.limit = document.getElementById('location_search_limit').value;
searchData.searching = document.getElementById('location_is_search_results').value;
searchData.page_num = document.getElementById('location_search_page_num').value;
searchData.num_of_pages = document.getElementById('location_search_num_of_pages').value;
searchData.searching = is_search;

// Do SimpleMap Taxonomies
<?php
Expand Down Expand Up @@ -1075,7 +1084,6 @@ function searchLocations( is_search ) {
if ( 'none' != autoload || is_search ) {
if ( 'all' == autoload && is_search != 1 ) {
searchData.radius = 0;
searchData.limit = 0;
}

if (! searchData.center) {
Expand All @@ -1099,7 +1107,7 @@ function searchLocationsNear(searchData) {
if ( units == 'km' ) {
searchData.radius = parseInt( searchData.radius ) / 1.609344;
}
} else if ( autoload == 'all' ) {
} else if ( autoload == 'all' && searchData.searching != 1) {
searchData.radius = 0;
} else {
if ( units == 'mi' ) {
Expand All @@ -1124,7 +1132,7 @@ function searchLocationsNear(searchData) {
}
?>

var searchUrl = siteurl + '/?sm-xml-search=1&<?php echo $wpmlquery; ?>lat=' + searchData.center.lat() + '&lng=' + searchData.center.lng() + '&radius=' + searchData.radius + '&namequery=' + searchData.homeAddress + '&query_type=' + searchData.query_type + '&limit=' + searchData.limit + <?php echo $js_tax_string; ?>'&address=' + searchData.address + '&city=' + searchData.city + '&state=' + searchData.state + '&zip=' + searchData.zip + '&pid=<?php echo esc_js( absint( $_GET['smpid'] ) ); ?>';
var searchUrl = siteurl + '/?sm-xml-search=1&<?php echo $wpmlquery; ?>lat=' + searchData.center.lat() + '&lng=' + searchData.center.lng() + '&radius=' + searchData.radius + '&namequery=' + searchData.homeAddress + '&query_type=' + searchData.query_type + '&limit=' + searchData.limit + '&page_num=' + searchData.page_num + '&num_of_pages=' + searchData.num_of_pages + <?php echo $js_tax_string; ?>'&address=' + searchData.address + '&city=' + searchData.city + '&state=' + searchData.state + '&zip=' + searchData.zip + '&pid=<?php echo esc_js( absint( $_GET['smpid'] ) ); ?>';

<?php if ( apply_filters( 'sm-use-updating-image', true ) ) : ?>
// Display Updating Message and hide search results
Expand All @@ -1137,6 +1145,18 @@ function searchLocationsNear(searchData) {

jQuery.get( searchUrl, {}, function(data) {
<?php if ( apply_filters( 'sm-use-updating-image', true ) ) : ?>
number_of_pages = data.shift();
var page_num = parseInt( document.getElementById('location_search_page_num').value );
//var num_of_pages = parseInt( document.getElementById('location_search_num_of_pages').value );
jQuery("#sm_page_num").html( "Page " + page_num + " of " + number_of_pages );

document.getElementById('location_search_num_of_pages').value = number_of_pages;
if ( 1 < number_of_pages ) {
document.getElementById('sm_page_footer').style.display = 'block';
} else {
document.getElementById('sm_page_footer').style.display = 'none';
}
data = data.shift();
// Hide Updating Message
if ( jQuery( "#simplemap-updating" ).is(":visible") ) {
jQuery( "#simplemap-updating" ).hide();
Expand Down Expand Up @@ -1615,6 +1635,39 @@ function createSidebarEntry(marker, locationData, searchData) {
});
return div;
}
function prevPage() {
var page_num = parseInt( document.getElementById('location_search_page_num').value );

if (page_num > 1) {
page_num--;
document.getElementById('location_search_page_num').value = page_num;
//jQuery("#sm_page_num").html( "Page " + page_num + " of " + num_of_pages );

searchLocations( 0 );
}
}

function nextPage() {
var page_num = parseInt( document.getElementById('location_search_page_num').value );
var num_of_pages = parseInt( document.getElementById('location_search_num_of_pages').value );


if ( page_num < num_of_pages ) {
page_num++;
document.getElementById('location_search_page_num').value = page_num;
//jQuery("#sm_page_num").html( "Page " + page_num + " of " + num_of_pages );

searchLocations( 0 );
}
}
function searchOnSubmit() {
document.getElementById('location_search_page_num').value = 1;
document.getElementById('location_search_num_of_pages').value = 0;

searchLocations( 1 );
var num_of_pages = parseInt( document.getElementById('location_search_num_of_pages').value );
//jQuery("#sm_page_num").html( "Page 1 of " + num_of_pages );
}
<?php
die();
}
Expand Down
52 changes: 35 additions & 17 deletions classes/xml-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ function init_search() {
remove_filter( 'the_title', 'at_title_check' );

$defaults = array(
'lat' => false,
'lng' => false,
'radius' => false,
'namequery' => false,
'query_type' => 'distance',
'address' => false,
'city' => false,
'state' => false,
'zip' => false,
'onlyzip' => false,
'country' => false,
'limit' => false,
'pid' => 0,
'lat' => false,
'lng' => false,
'radius' => false,
'namequery' => false,
'query_type' => 'distance',
'address' => false,
'city' => false,
'state' => false,
'zip' => false,
'onlyzip' => false,
'country' => false,
'limit' => false,
'page_num' => 0,
'num_of_pages' => 0,
'pid' => 0,
);
$input = array_filter( array_intersect_key( $_GET, $defaults ) ) + $defaults;

Expand Down Expand Up @@ -130,8 +132,23 @@ function init_search() {
GROUP BY
posts.ID
$distance_having
ORDER BY " . apply_filters( 'sm-location-sort-order', $distance_order . ' posts.post_name ASC', $input ) . " " . $limit;
ORDER BY " . apply_filters( 'sm-location-sort-order', $distance_order . ' posts.post_name ASC', $input ) . " " . $limit . " OFFSET " . ( absint( $input['limit'] ) * ( absint( $input['page_num'] ) - 1 ) );

$input['limit'] = intval( $input['limit'] );
$input['page_num'] = intval( $input['page_num'] );
$num_of_pages = intval( $input['num_of_pages'] );

$total_locations = 0;
if ( 1 === $input['page_num'] ) {
$total_sql = '';
$total_sql = substr( $sql, 0, strrpos( $sql, ' LIMIT' ) );

$total_locations = sizeof( $wpdb->get_results( $total_sql ) );
if ( 0 === $input['limit'] ) {
$input['limit'] = $total_locations;
}
$num_of_pages = (int) ceil( $total_locations / $input['limit'] );
}
$sql = apply_filters( 'sm-xml-search-locations-sql', $sql );

// TODO: Consider using this to generate the marker node attributes in print_xml().
Expand Down Expand Up @@ -202,18 +219,19 @@ function init_search() {
}

$locations = apply_filters( 'sm-xml-search-locations', $locations );
$this->print_json( $locations, $smtaxes );
$this->print_json( $locations, $smtaxes, $num_of_pages );
}
}

// Prints the JSON output
function print_json( $dataset, $smtaxes ) {
function print_json( $locations, $smtaxes, $num_of_pages ) {
header( 'Status: 200 OK', false, 200 );
header( 'Content-type: application/json' );
do_action( 'sm-xml-search-headers' );

do_action( 'sm-print-json', $dataset, $smtaxes );
do_action( 'sm-print-json', $locations, $smtaxes );

$dataset = array( $num_of_pages, $locations );
echo json_encode( $dataset );
die();
}
Expand Down
8 changes: 8 additions & 0 deletions inc/styles/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ font-weight:bold;
vertical-align:top;
}

.location-search-prev-page {}

.location-search-next-page {}

.location-search-page-num {
display: inline-block;
}

.no-linebreak {
white-space:nowrap;
}
Expand Down
10 changes: 10 additions & 0 deletions inc/styles/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ font-weight:bold;
vertical-align:top;
}

.location-search-prev-page {
}

.location-search-next-page {
}

.location-search-page-num {
display: inline-block;
}

.no-linebreak {
white-space:nowrap;
}
Expand Down

0 comments on commit 890da7e

Please sign in to comment.