function rooms_booking_edit_form_validate in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7
Form API validate callback for the booking form.
1 string reference to 'rooms_booking_edit_form_validate'
- rooms_booking_edit_form in modules/
rooms_booking/ rooms_booking.admin.inc - Form callback: create or edit a booking.
File
- modules/
rooms_booking/ rooms_booking.admin.inc, line 1119 - Rooms editing UI.
Code
function rooms_booking_edit_form_validate(&$form, &$form_state) {
$booking = $form_state['rooms_booking'];
list($start_date, $end_date) = rooms_form_input_get_start_end_dates($form_state);
if ($start_date && $end_date) {
// Convert input format to the same used in the stored dates.
$input_start_date = $start_date
->format('Y-m-d');
$input_end_date = $end_date
->format('Y-m-d');
// If stored dates differ from input dates check that price was re-assigned.
if (isset($booking->start_date) && isset($booking->end_date)) {
if ($input_start_date != $booking->start_date || $input_end_date != $booking->end_date) {
if (!isset($form_state['values']['unit_id']) || !is_numeric($form_state['values']['unit_id'])) {
form_set_error('availability_fieldset', t('A unit must be re-assigned for this booking.'));
}
}
}
}
// Check if unit assigned.
if (!isset($form_state['values']['unit_id']) || $form_state['values']['unit_id'] == '') {
form_set_error('availability_fieldset', t('A unit has not been assigned for this booking.'));
}
elseif (isset($form_state['values']['unit_id']) && $form_state['values']['unit_id'] == '') {
form_set_error('availability_fieldset', t('A unit has not been assigned for this booking.'));
}
if (isset($booking->unit_id)) {
$new_unit_id = $booking->unit_id;
if (isset($form_state['values']['unit_id'])) {
$new_unit_id = $form_state['values']['unit_id'];
}
if ($new_unit_id == $booking->unit_id) {
// We are going to be updating the event. So the removing the old event.
if ($booking->unit_id != 0 && $start_date && $end_date) {
// Create a calendar.
$uc = new UnitCalendar($booking->unit_id);
$adjusted_end_date = clone $end_date;
$adjusted_end_date
->modify('-1 day');
// Create an event representing the event to remove.
$event_id = rooms_availability_assign_id($booking->booking_id, $booking->booking_status);
$be = new BookingEvent($booking->unit_id, $event_id, $start_date, $end_date);
// Check if a locked event is blocking the update.
$states_confirmed = $uc
->getStates($start_date, $adjusted_end_date);
$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,
$be->id,
));
$state_diff_confirmed = array_diff($states_confirmed, $valid_states);
if (count($state_diff_confirmed) > 0) {
$date1 = new DateTime($form['rooms_date_range']['rooms_start_date']['#default_value']);
$date2 = new DateTime($form['rooms_date_range']['rooms_end_date']['#default_value']);
$form['rooms_date_range']['rooms_start_date']['date']['#value'] = $date1
->format($form['rooms_date_range']['rooms_start_date']['#date_format']);
$form['rooms_date_range']['rooms_end_date']['date']['#value'] = $date2
->format($form['rooms_date_range']['rooms_end_date']['#date_format']);
form_set_error('date_range', t('Could not update calendar because a locked event is blocking the update - you need to unlock any locked events in that period.'));
}
// Check if this booking overlap with an unconfirmed booking.
$states_unconfirmed = $uc
->getStates($start_date, $adjusted_end_date, TRUE);
$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(
$be->id,
));
$state_diff_unconfirmed = array_diff($states_unconfirmed, $valid_states);
if (count($state_diff_unconfirmed) > 0) {
$booking_id = rooms_availability_return_id(array_pop($state_diff_unconfirmed));
form_set_error('date_range', t('Change of dates leads to an overlap with an unconfirmed booking, please resolve issue with booking !booking_link first.', array(
'!booking_link' => l($booking_id, 'admin/rooms/bookings/booking/' . $booking_id . '/edit'),
)));
}
}
}
}
// For new bookings check if the owner has an outstanding order, don't run on
// AJAX requests due this could cause message duplication.
if (!isset($booking->customer_id) && strpos(request_uri(), 'system/ajax') === FALSE) {
$customer_name = $form_state['input']['client'];
$commerce_customer_id = rooms_booking_find_customer_by_name($customer_name);
// The statuses that are considered a complete order we could use
// commerce_payment_order_balance($order); but that adds the dependency
// of commerce_payment module and all the orders must have a transaction
// history to have a reliable balance.
$complete_statuses = array(
'complete',
'rooms_unit_confirmed',
);
// Determine the commerce order statuses that identify an outstanding order.
$incomplete_statuses = array_diff(array_keys(commerce_order_statuses()), $complete_statuses);
$orders = commerce_order_load_multiple(array(), array(
'uid' => $commerce_customer_id,
'status' => $incomplete_statuses,
));
$query = new EntityFieldQuery();
$orders = $query
->entityCondition('entity_type', 'commerce_order')
->fieldCondition('commerce_customer_billing', 'profile_id', $commerce_customer_id)
->execute();
// In case of pending orders alert the admin.
if ($orders) {
foreach ($orders['commerce_order'] as $order) {
$order_links[] = l(t('Order #@num', array(
'@num' => $order->order_id,
)), "admin/commerce/orders/{$order->order_id}/edit");
}
$message_params = array(
'!customer_name' => check_plain($customer_name),
'!order_links' => theme('item_list', array(
'items' => $order_links,
)),
);
drupal_set_message(t('The customer !customer_name has an outstanding order: !order_links', $message_params), 'warning');
}
}
$client = explode(':', $form_state['values']['client']);
$client_name = $client[0];
$commerce_customer_id = rooms_booking_find_customer_by_name($client_name);
if ($commerce_customer_id === FALSE) {
form_set_error('client', t('Enter a valid customer.'));
}
// Notify field widgets to validate their data.
entity_form_field_validate('rooms_booking', $form, $form_state);
}