You are here

function _office_hours_field_widget_hours in Office Hours 7

Gets the (limited) hours of a day.

Mimics date_hours() function, but that function does not support limiting the hours. The limits are set in the Widget settings form, and used in the Widget form.

2 calls to _office_hours_field_widget_hours()
office_hours_field_settings_form in includes/office_hours.field.inc
Implements hook_field_settings_form().
office_hours_field_widget_form in includes/office_hours.widget.inc
Implements hook_field_widget_form().

File

./office_hours.module, line 107
Creates a field and widget for inserting working or office hours per day

Code

function _office_hours_field_widget_hours($settings) {

  // Get the valid hours. Date API doesn't provide a straight method for this.
  $hoursformat = $settings['hoursformat'] == 1 ? 'g' : 'H';

  // 12 or 24 format.
  $required = FALSE;
  $start = $settings['limitstart'] == '' ? 0 : max(0, $settings['limitstart']);
  $start = (int) $start / 100;
  $end = $settings['limitend'] == '' ? 2300 : min(2300, $settings['limitend']);
  $end = (int) $end / 100;

  // Begin modified copy from date_hours().
  $hours = array();
  if ($hoursformat == 'h' || $hoursformat == 'g') {

    // 12-hour format.
    $min = 1;
    $max = 24;
    for ($i = $min; $i <= $max; $i++) {
      if ($i >= $start && $i <= $end || $end - $start >= 11) {
        $hour = $i <= 12 ? $i : $i - 12;
        $hours[$hour * 100] = $hour < 10 && ($hoursformat == 'H' || $hoursformat == 'h') ? "0{$hour}" : $hour;
      }
    }
    $hours = array_unique($hours);
  }
  else {
    $min = $start;
    $max = $end;
    for ($i = $min; $i <= $max; $i++) {
      $hour = $i;
      $hours[$hour * 100] = $hour < 10 && ($hoursformat == 'H' || $hoursformat == 'h') ? "0{$hour}" : $hour;
    }
  }
  $none = array(
    '' => '',
  );
  $hours = !$required ? $none + $hours : $hours;

  // End modified copy from date_hours().
  return $hours;
}