Skip to content

Commit

Permalink
Merge pull request #97 from awethemes/improve
Browse files Browse the repository at this point in the history
Fixed #95
  • Loading branch information
anhskohbo committed Oct 19, 2018
2 parents 5bd39c4 + d3c5c67 commit 8e679f4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 63 deletions.
21 changes: 17 additions & 4 deletions inc/Admin/Calendar/Abstract_Scheduler.php
Expand Up @@ -281,11 +281,24 @@ protected function filter_events( $events ) {
* @return \AweBooking\Support\Collection
*/
protected function query_room_types( $query = [] ) {
if ( $this->request->filled( 'only' ) ) {
$query['post__in'] = wp_parse_id_list( $this->request->get( 'only' ) );
}

if ( $this->request->filled( 'hotel' ) ) {
$query['meta_query'][] = [
'key' => '_hotel_id',
'value' => absint( $this->request->get( 'hotel' ) ),
'type' => 'numeric',
'compare' => '=',
];
}

$wp_query_args = apply_filters( 'abrs_scheduler_query_room_types', wp_parse_args( $query, [
'post_type' => Constants::ROOM_TYPE,
'post_status' => 'publish',
'no_found_rows' => true,
'posts_per_page' => 250,
'post_type' => Constants::ROOM_TYPE,
'post_status' => 'publish',
'no_found_rows' => true,
'posts_per_page' => 250,
]), $this );

// Create the WP_Query room types.
Expand Down
110 changes: 51 additions & 59 deletions inc/Admin/Calendar/Booking_Scheduler.php
Expand Up @@ -34,61 +34,10 @@ class Booking_Scheduler extends Abstract_Scheduler {
/**
* {@inheritdoc}
*/
public function prepare( Request $request ) {
$rooms_only = null;
if ( $request->filled( 'only' ) ) {
$rooms_only = wp_parse_id_list( $request['only'] );
}

protected function create_scheduler() {
// Query the list of room type to display.
$this->room_types = $this->query_room_types([
'post__in' => $rooms_only,
]);

parent::prepare( $request );
}

/**
* {@inheritdoc}
*/
protected function setup() {
$this->events = $this->setup_events( $this->scheduler );
}

/**
* {@inheritdoc}
*
* @var \AweBooking\Support\Collection $events
*/
protected function filter_events( $events ) {
return $events->reject( function ( $e ) {
if ( ( ! $e instanceof State_Event && ! $e instanceof Booking_Event ) || 0 === (int) $e->get_value() ) {
return true;
}
$this->room_types = $this->query_room_types();

// Only display the "blocked" in state events.
$blocked_state = array_keys( abrs_get_blocked_states() );
if ( $e instanceof State_Event && ! in_array( $e->get_state(), $blocked_state ) ) {
return true;
}

return false;
})->each( function( $e ) {
/* @var \AweBooking\Calendar\Event\Event $e */
$end_date = $e->get_end_date();

if ( '23:59:00' === $end_date->format( 'H:i:s' ) ) {
$e->set_end_date( $end_date->addMinute() );
}

return $e;
})->values();
}

/**
* {@inheritdoc}
*/
protected function create_scheduler() {
// Get all rooms indexed by room type ID.
$all_rooms = $this->room_types
->keyBy( 'id' )
Expand Down Expand Up @@ -123,6 +72,36 @@ protected function create_scheduler() {
return $scheduler;
}

/**
* {@inheritdoc}
*
* @var \AweBooking\Support\Collection $events
*/
protected function filter_events( $events ) {
return $events->reject( function ( $e ) {
if ( ( ! $e instanceof State_Event && ! $e instanceof Booking_Event ) || 0 === (int) $e->get_value() ) {
return true;
}

// Only display the "blocked" in state events.
$blocked_state = array_keys( abrs_get_blocked_states() );
if ( $e instanceof State_Event && ! in_array( $e->get_state(), $blocked_state ) ) {
return true;
}

return false;
})->each( function( $e ) {
/* @var \AweBooking\Calendar\Event\Event $e */
$end_date = $e->get_end_date();

if ( '23:59:00' === $end_date->format( 'H:i:s' ) ) {
$e->set_end_date( $end_date->addMinute() );
}

return $e;
})->values();
}

/**
* Display the legends.
*
Expand All @@ -149,6 +128,16 @@ protected function display_toolbar() {
$this->template( 'toolbar/datepicker.php' );
}

/**
* Display the main toolbars.
*
* @return void
*/
protected function display_main_toolbar() {
echo '<div class="abrs-spacer"></div>';
$this->template( 'main-toolbar/hotel-filter.php' );
}

/**
* Display the actions.
*
Expand All @@ -168,22 +157,25 @@ protected function display_actions() { ?>
* @return void
*/
protected function display_divider_column( $day, $scheduler ) {
$matrix = $this->get_matrix( $scheduler->get_uid() );
if ( empty( $matrix ) ) {
$matrices = $this->get_matrix( $scheduler->get_uid() );

if ( empty( $matrices ) ) {
return;
}

$available = 0;
foreach ( $matrix as $item ) {
$left = $scheduler->count();
foreach ( $matrices as $item ) {
/* @var \AweBooking\Support\Collection $item */
if ( 0 === $item->get( $day->format( 'Y-m-d' ) ) ) {
$available++;
$left--;
}
}

$available = $scheduler->count() - $left;

/* translators: Available rooms */
$title = sprintf( _nx( '%s room available', '%s rooms available', $available, 'awebooking' ), esc_html( $available ) );
echo sprintf( '<div class="scheduler-flex--center"><strong title="' . esc_attr( $title ) . '">%1$s/%2$s</strong></div>', esc_html( $available ), esc_html( $scheduler->count() ) );
echo sprintf( '<div class="scheduler-flex--center"><strong class="tippy" title="' . esc_attr( $title ) . '">%1$s/%2$s</strong></div>', esc_html( $left ), esc_html( $scheduler->count() ) );
}

/**
Expand Down
10 changes: 10 additions & 0 deletions inc/Admin/Calendar/Pricing_Scheduler.php
Expand Up @@ -53,6 +53,16 @@ protected function display_toolbar() {
$this->template( 'toolbar/datepicker.php' );
}

/**
* Display the main toolbars.
*
* @return void
*/
protected function display_main_toolbar() {
echo '<div class="abrs-spacer"></div>';
$this->template( 'main-toolbar/hotel-filter.php' );
}

/**
* Display the actions.
*
Expand Down
34 changes: 34 additions & 0 deletions inc/Admin/Calendar/views/main-toolbar/hotel-filter.php
@@ -0,0 +1,34 @@
<?php
if ( ! abrs_multiple_hotels() ) {
return;
}

?>

<div class="abrs-ptb1">
<select name="hotel_id" id="hotel_id">
<option value=""><?php esc_html_e( 'Filter by Hotel', 'awebooking' ) ?></option>
<?php foreach ( abrs_list_hotels() as $hotel ) : ?>
<option value="<?php echo esc_attr( $hotel->get_id() ); ?>" <?php selected( isset( $_GET['hotel'] ) ? absint( $_GET['hotel'] ) : 0, $hotel->get_id() ); ?>><?php echo esc_html( $hotel->get( 'name' ) ); ?></option>
<?php endforeach; ?>
</select>
</div>

<script>
(function($, awebooking) {
'use strict';

$(function() {
var plugin = window.awebooking || {};

$('#hotel_id').on('change', function (e) {
var hotel_id = this.value;

setTimeout(function() {
window.location.href = plugin.utils.addQueryArgs({ hotel: hotel_id });
}, 500);
});
});

})(jQuery);
</script>

0 comments on commit 8e679f4

Please sign in to comment.