You are here

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

Form callback for rooms_booking_children_discount form.

1 string reference to 'rooms_booking_children_discount'
rooms_booking_menu in modules/rooms_booking/rooms_booking.module
Implements hook_menu().

File

modules/rooms_booking/rooms_booking.module, line 114
Manage Bookings - Bookings are tied to a customer profile and possible a Unit ID and Order ID.

Code

function rooms_booking_children_discount($form, &$form_state) {
  $form['#attributes']['class'][] = 'rooms-management-form children-discount-form';
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'rooms_booking') . '/css/rooms_booking.css',
  );
  $form['#tree'] = TRUE;
  if (empty($form_state['num_options'])) {
    $options = variable_get('rooms_children_discount_options', array());
    if (empty($options)) {
      $form_state['num_options'] = 1;
    }
    else {
      $form_state['num_options'] = count($options);
    }
  }

  // This string gives us an easy way to translate the sentence used for
  // giving the child discount, and at the same time we can break the string
  // into components to use around our fields with clever useage of "explode".
  // The "<wbr>" is used here to create an invisible boundary between the
  // "option" field suffix and the "start" field prefix.
  $string = t('Give a @amount % discount <wbr> for children ages @start to @end.');
  list($option_prefix, $string) = explode('@amount', $string);
  list($option_suffix, $string) = explode('<wbr>', $string);
  list($start_prefix, $string) = explode('@start', $string);
  list($end_prefix, $end_suffix) = explode('@end', $string);
  for ($i = 1; $i <= $form_state['num_options']; $i++) {
    $form['option'][$i] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'child-discount container-inline',
        ),
      ),
    );
    $form['option'][$i]['discount'] = array(
      '#title' => 'Discount',
      '#title_display' => 'invisible',
      '#type' => 'textfield',
      '#field_prefix' => $option_prefix,
      '#field_suffix' => $option_suffix,
      '#size' => 10,
      '#maxlength' => 10,
      '#required' => TRUE,
      '#default_value' => isset($options[$i]['end']) ? $options[$i]['discount'] : '',
      '#attributes' => array(
        'class' => array(
          'child-discount__amount',
        ),
      ),
      '#element_validate' => array(
        'rooms_element_validate_integer_positive_less_than_100',
      ),
    );
    $form['option'][$i]['start'] = array(
      '#title' => 'Start date',
      '#title_display' => 'invisible',
      '#type' => 'textfield',
      '#field_prefix' => $start_prefix,
      '#size' => 5,
      '#maxlength' => 5,
      '#required' => TRUE,
      '#default_value' => isset($options[$i]['start']) ? $options[$i]['start'] : '',
      '#attributes' => array(
        'class' => array(
          'child-discount__age-start',
        ),
      ),
      '#element_validate' => array(
        'rooms_element_validate_integer_zero_positive',
      ),
    );
    $form['option'][$i]['end'] = array(
      '#title' => 'End date',
      '#title_display' => 'invisible',
      '#type' => 'textfield',
      '#field_prefix' => $end_prefix,
      '#field_suffix' => $end_suffix,
      '#size' => 5,
      '#maxlength' => 5,
      '#required' => TRUE,
      '#default_value' => isset($options[$i]['end']) ? $options[$i]['end'] : '',
      '#attributes' => array(
        'class' => array(
          'child-discount__age-end',
        ),
      ),
      '#element_validate' => array(
        'rooms_element_validate_integer_zero_positive',
      ),
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
    '#tree' => FALSE,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#attributes' => array(
      'class' => array(
        'child-discount__save-button',
      ),
    ),
  );
  $form['actions']['add_option'] = array(
    '#type' => 'submit',
    '#value' => t('Add another discount'),
    '#submit' => array(
      'rooms_booking_children_discount_add_option',
    ),
    '#attributes' => array(
      'class' => array(
        'child-discount__add-button',
      ),
    ),
  );
  if ($form_state['num_options'] > 1) {
    $form['actions']['remove_option'] = array(
      '#type' => 'submit',
      '#value' => t('Remove last discount'),
      '#submit' => array(
        'rooms_booking_children_discount_remove_option',
      ),
      '#limit_validation_errors' => array(),
      '#attributes' => array(
        'class' => array(
          'child-discount__remove-button',
        ),
      ),
    );
  }
  return $form;
}