You are here

function rooms_booking_edit_form_submit in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Form API submit callback for the Booking form.

@todo remove hard-coded link

1 string reference to 'rooms_booking_edit_form_submit'
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 1246
Rooms editing UI.

Code

function rooms_booking_edit_form_submit(&$form, &$form_state) {

  // Get the client name and client id.
  $client = explode(':', $form_state['values']['client']);
  $client_name = $client[0];
  $commerce_customer_id = rooms_booking_find_customer_by_name($client_name);

  // Save customer in rooms_customers table.
  db_merge('rooms_customers')
    ->key(array(
    'name' => $client_name,
  ))
    ->fields(array(
    'name' => $client_name,
    'commerce_customer_id' => $commerce_customer_id,
  ))
    ->execute();

  // Get customer id from rooms_customers table.
  $client_id = db_select('rooms_customers')
    ->fields('rooms_customers', array(
    'id',
  ))
    ->condition('name', $client_name, '=')
    ->execute()
    ->fetchField();

  // Put them back in $form_state so the entity controller builds the entity.
  $form_state['values']['name'] = $client_name;
  $form_state['values']['customer_id'] = $client_id;

  // We also need appropriate named variables for start and end date.
  // It's simpler to do this than change all the other code for now.
  $form_state['values']['start_date'] = $form_state['values']['rooms_start_date'];
  $form_state['values']['end_date'] = $form_state['values']['rooms_end_date'];
  $unit_id = 0;
  if (isset($form_state['values']['unit_id'])) {
    $unit_id = $form_state['values']['unit_id'];
  }

  // If we are dealing with a new booking.
  if ($form_state['rooms_booking']->booking_id == '') {
    $booking = rooms_booking_create(array(
      'type' => $form_state['rooms_booking']->type,
    ));
    $form_state['rooms_booking'] = $booking;
    $booking = entity_ui_controller('rooms_booking')
      ->entityFormSubmitBuildEntity($form, $form_state);
    $booking->is_new = isset($booking->is_new) ? $booking->is_new : 0;
  }
  else {
    $booking = entity_ui_controller('rooms_booking')
      ->entityFormSubmitBuildEntity($form, $form_state);

    // Get the unit id from the booking to be used further down as it is not in
    // the form_values because of how unit ids are assigned.
    $unit_id = $booking->unit_id;
  }

  // Add in created and changed times.
  $booking->created = !empty($booking->date) ? strtotime($booking->date) : REQUEST_TIME;
  $booking->changed = time();

  // Add in the booking owner.
  if ($account = user_load_by_name($booking->owner_name)) {
    $booking->uid = $account->uid;
  }
  else {
    $booking->uid = 0;
  }
  if (isset($form_state['input']['price'])) {
    $booking->price = $form_state['input']['price'] * 100;
  }
  $unit = rooms_unit_load($unit_id);

  // Store selected unit options.
  foreach (rooms_unit_get_unit_options($unit) as $option) {
    $option_name = rooms_options_machine_name($option['name']);
    if (isset($form_state['values'][$option_name])) {
      if ($form_state['values'][$option_name] == '1') {
        $booking->data[$option_name] = 1;
        if (isset($form_state['values'][$option_name . ':quantity'])) {
          $booking->data[$option_name . ':quantity'] = $form_state['values'][$option_name . ':quantity'];
        }
      }
    }
  }

  // Store children ages in booking's data field.
  if (isset($form_state['values']['data']['childrens_age'])) {
    $booking->data['childrens_age'] = $form_state['values']['data']['childrens_age'];
  }

  // Save booking.
  $booking
    ->save();

  // Add the booking to $form_state to be altered by other submit handlers.
  $form_state['booking'] = $booking;

  // We have a unit defined so lets block availability there.
  if ($unit_id != 0) {
    if ($booking->rooms_av_update == ROOMS_UPDATED) {
      drupal_set_message(t('Unit availability has been updated.'));
    }
    else {
      drupal_set_message(t('Unit availability could not be updated.'), 'error');
    }
  }
  $form_state['redirect'] = 'admin/rooms/bookings';
}