You are here

function bat_type_add_units_form_submit in Booking and Availability Management Tools for Drupal 7

Submit handler for unit add form.

File

modules/bat_unit/bat_type.admin.inc, line 849
BatType editing UI.

Code

function bat_type_add_units_form_submit($form, &$form_state) {
  global $user;

  // The Type this unit is associated too.
  $type_id = $form_state['values']['type_id'];

  // The unit bundle this unit belongs to.
  $unit_type = $form_state['values']['unit_type'];

  // Sanity check.
  if (!($type = bat_type_load($type_id))) {
    drupal_set_message(t('Could not load Bat Unit!'), 'warning');
    return;
  }
  $total_units = count(bat_unit_load_multiple(FALSE, array(
    'type_id' => $type_id,
  )));

  // Create units.
  for ($i = $total_units + 1; $i <= $form_state['values']['units'] + $total_units; $i++) {
    $unit = bat_unit_create(array(
      'type' => $unit_type,
    ));
    $unit->name = $type->name . ' ' . $i;
    $unit->created = !empty($unit->date) ? strtotime($unit->date) : REQUEST_TIME;
    $unit->type_id = $type_id;
    $unit->default_state = 1;
    $unit->uid = $user->uid;
    $unit
      ->save();
  }
  $form_state['redirect'] = 'admin/bat/config/types/manage/' . $type_id . '/units';
}