class AvailabilityAgentDateFilter in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7
Filter by start_date, end_date, valid_states.
Hierarchy
- class \AvailabilityAgentFilterBase implements AvailabilityAgentFilterInterface
- class \AvailabilityAgentDateFilter
Expanded class hierarchy of AvailabilityAgentDateFilter
File
- modules/
rooms_booking/ includes/ rooms_booking.availability_agent_filter.inc, line 205 - Rooms Booking agent filter interfaces and base implementations.
View source
class AvailabilityAgentDateFilter extends AvailabilityAgentFilterBase {
/**
* {@inheritdoc}
*/
public function applyFilter() {
// Check parameters.
$start_date = isset($this->parameters['start_date']) ? $this->parameters['start_date'] : NULL;
$end_date = isset($this->parameters['end_date']) ? $this->parameters['end_date'] : NULL;
$confirmed = isset($this->parameters['confirmed']) ? $this->parameters['confirmed'] : FALSE;
// Start date and end date parameters must be set.
if ($start_date == NULL || $end_date == NULL) {
return $this->units;
}
if (isset($this->parameters['valid_states'])) {
$valid_states = $this->parameters['valid_states'];
}
else {
$valid_states = array_keys(array_filter(variable_get('rooms_valid_availability_states', drupal_map_assoc(array(
ROOMS_AVAILABLE,
ROOMS_ON_REQUEST,
)))));
$valid_states = array_merge($valid_states, array(
ROOMS_UNCONFIRMED_BOOKINGS,
));
}
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'rooms_unit')
->propertyOrderBy('max_sleeps', 'ASC')
->propertyCondition('bookable', 1);
// Execute the query and collect the results.
$results = $query
->execute();
foreach ($results['rooms_unit'] as $key => $unit) {
$unit = rooms_unit_load($unit->unit_id);
// Get a calendar and check availability.
$rc = new UnitCalendar($unit->unit_id);
// We need to make this based on user-set vars.
// Rather than using $rc->stateAvailability we will get the states check
// directly as different states will impact on what products we create.
$states = $rc
->getStates($start_date, $end_date, $confirmed);
$state_diff = array_diff($states, $valid_states);
if ($this->parameters['revert_valid_states']) {
// $valid_states match completely with existing states so remove if we are looking
// for the opposite.
if (count($state_diff) == 0) {
unset($results['rooms_unit'][$key]);
}
}
elseif (count($state_diff) != 0) {
unset($results['rooms_unit'][$key]);
}
}
if (empty($this->units)) {
return $results['rooms_unit'];
}
else {
// Computes the intersection of units and results.
return $this
->intersectUnits($results['rooms_unit']);
}
}
}