View source
<?php
interface AvailabilityAgentFilterInterface {
public function applyFilter();
public static function availabilitySearchParameters();
public static function availabilitySearchForm(&$form, &$form_state);
public static function availabilitySearchFormValidate(&$form, &$form_state);
public static function availabilityChangeSearchForm(&$form, &$form_state);
public static function availabilityChangeSearchFormValidate(&$form, &$form_state);
}
abstract class AvailabilityAgentFilterBase implements AvailabilityAgentFilterInterface {
protected $units;
protected $parameters;
public function __construct(array $units, array $parameters) {
$this->units = $units;
$this->parameters = $parameters;
}
protected function intersectUnits($filtered_units) {
$filtered_keys = array_keys($filtered_units);
$unit_keys = array_keys($this->units);
$keys_units = array_intersect($filtered_keys, $unit_keys);
$results = array();
foreach ($keys_units as $key) {
$results[$key] = $this->units[$key];
}
return $results;
}
public static function availabilitySearchParameters() {
return array();
}
public static function availabilitySearchForm(&$form, &$form_state) {
}
public static function availabilitySearchFormValidate(&$form, &$form_state) {
}
public static function availabilityChangeSearchForm(&$form, &$form_state) {
}
public static function availabilityChangeSearchFormValidate(&$form, &$form_state) {
}
}
class AvailabilityAgentSizeFilter extends AvailabilityAgentFilterBase {
public function applyFilter() {
$group_size = isset($this->parameters['group_size']) ? $this->parameters['group_size'] : 0;
$group_size_children = isset($this->parameters['group_size_children']) ? $this->parameters['group_size_children'] : 0;
$unit_types = isset($this->parameters['unit_types']) ? $this->parameters['unit_types'] : NULL;
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'rooms_unit')
->propertyOrderBy('max_sleeps', 'ASC')
->propertyCondition('max_sleeps', $group_size, '>=')
->propertyCondition('max_children', $group_size_children, '>=')
->propertyCondition('bookable', 1);
if ($unit_types != NULL) {
$query
->propertyCondition('type', $unit_types);
}
$results = $query
->execute();
if (count($results) == 0) {
return ROOMS_SIZE_FAILURE;
}
if (empty($this->units)) {
return $results['rooms_unit'];
}
else {
return $this
->intersectUnits($results['rooms_unit']);
}
}
}
class AvailabilityAgentDateFilter extends AvailabilityAgentFilterBase {
public function applyFilter() {
$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;
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);
$results = $query
->execute();
foreach ($results['rooms_unit'] as $key => $unit) {
$unit = rooms_unit_load($unit->unit_id);
$rc = new UnitCalendar($unit->unit_id);
$states = $rc
->getStates($start_date, $end_date, $confirmed);
$state_diff = array_diff($states, $valid_states);
if ($this->parameters['revert_valid_states']) {
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 {
return $this
->intersectUnits($results['rooms_unit']);
}
}
}
class AvailabilityAgentSingleUnitFilter extends AvailabilityAgentFilterBase {
public function applyFilter() {
if (variable_get('rooms_presentation_style') == ROOMS_INDIVIDUAL && isset($_GET['rooms_id']) && ($requested_unit = rooms_unit_load($_GET['rooms_id']))) {
foreach ($this->units as $unit) {
if ($unit->unit_id != $requested_unit->unit_id) {
unset($this->units[$unit->unit_id]);
}
}
if (empty($this->units)) {
drupal_set_message(t('Unfortunately @unit_name is not available - try other dates if possible', array(
'@unit_name' => $requested_unit->name,
)), 'warning');
}
}
return $this->units;
}
}
class AvailabilityAgentCommerceFilter extends AvailabilityAgentFilterBase {
public function applyFilter() {
global $user;
$start_date = isset($this->parameters['start_date']) ? $this->parameters['start_date'] : date_create()
->setTimestamp(0);
$end_date = isset($this->parameters['end_date']) ? $this->parameters['end_date'] : date_create()
->setTimestamp(2147483647);
if (empty($this->units)) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'rooms_unit')
->propertyOrderBy('max_sleeps', 'ASC')
->propertyCondition('bookable', 1);
$results = $query
->execute();
if (count($results)) {
$results = $results['rooms_unit'];
}
}
else {
$results = $this->units;
}
$orders = array();
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'commerce_order')
->propertyCondition('status', array(
'cart',
'checkout_checkout',
'checkout_review',
'checkout_payment',
'checkout_complete',
))
->execute();
if (isset($result['commerce_order'])) {
$orders = commerce_order_load_multiple(array_keys($result['commerce_order']));
}
foreach ($orders as $order) {
$wrapper = entity_metadata_wrapper('commerce_order', $order);
if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
foreach ($wrapper->commerce_line_items as $line_item) {
if (in_array($line_item->type
->value(), commerce_product_line_item_types())) {
if ($line_item->commerce_product
->value()->type == 'rooms_product') {
if (isset($line_item->rooms_booking_dates)) {
$rooms_booking_dates = $line_item->rooms_booking_dates
->value();
$s_date = new DateTime($rooms_booking_dates['value']);
$e_date = new DateTime($rooms_booking_dates['value2']);
if ($this
->checkInRange($s_date, $e_date, $start_date, $end_date)) {
if (isset($results[$line_item->rooms_booked_unit_id
->value()])) {
unset($results[$line_item->rooms_booked_unit_id
->value()]);
}
}
}
}
}
}
}
}
if (empty($this->units)) {
return $results;
}
else {
return $this
->intersectUnits($results);
}
}
private function checkInRange($line_item_start_date, $line_item_end_date, $start_date, $end_date) {
if (!($line_item_start_date < $start_date && $line_item_end_date < $start_date || $line_item_start_date > $end_date && $line_item_end_date > $end_date)) {
return TRUE;
}
else {
return FALSE;
}
}
}