You are here

function office_hours_widget in Office Hours 5

Same name and namespace in other branches
  1. 6.2 office_hours.module \office_hours_widget()
  2. 6 office_hours.module \office_hours_widget()

Parameters

$op: What kind of action is being performed. Possible values:

&$node: The node the action is being performed on. This argument is passed by reference for performance only; do not modify it.

$field: The field the action is being performed on.

&$node_field: The contents of the field in this node. Changes to this variable will be saved back to the node object.

Return value

This varies depending on the operation.

  • The "form" operation should return an array of form elements to display.
  • Other operations have no return value.

File

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

Code

function office_hours_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'prepare form values':
      $items = array();
      foreach ($node_field as $key => $item) {
        $day = $item['day'];
        $delta = $day * 2;

        //0,2,4,6,8,10,12
        $deltaplus = $delta + 1;

        //1,3,5,7,9,11,13
        if (!isset($items[$delta])) {
          $items[$delta] = $item;
        }
        else {
          $items[$deltaplus] = $item;
        }
      }
      $node_field = $items;
      break;
    case 'form':
      $ophours = _create_hours_arr($field, TRUE);
      $opdays = array(
        t('Sunday'),
        t('Monday'),
        t('Tuesday'),
        t('Wednesday'),
        t('Thursday'),
        t('Friday'),
        t('Saturday'),
      );
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      $form[$field['field_name']]['#type'] = 'fieldset';
      $form[$field['field_name']]['#title'] = t($field['widget']['label']);
      $form[$field['field_name']]['#description'] = t('Mark the days you want to show and set the hours accordingly');
      $form[$field['field_name']]['#attributes'] = array(
        'class' => 'office-container-inline',
      );
      foreach (range(0, 6) as $day) {

        //day is 0,1,2,3,4,5,6
        if ($field['multiple']) {
          $delta = $day * 2;

          //0,2,4,6,8,10,12
          $deltaplus = $delta + 1;

          //1,3,5,7,9,11,13
        }
        else {
          $delta = $day;
        }
        $form[$field['field_name']][$delta]['day'] = array(
          '#type' => 'value',
          '#value' => $day,
        );
        $form[$field['field_name']][$delta]['daycheck'] = array(
          '#type' => 'checkbox',
          '#title' => $opdays[$day],
          '#description' => $field['widget']['description'],
          '#default_value' => isset($node_field[$delta]['day']) ? 1 : 0,
          '#prefix' => '<div class="office-hours-block">',
        );
        $form[$field['field_name']][$delta]['starthours'] = array(
          '#type' => 'select',
          '#title' => '',
          '#options' => $ophours,
          '#required' => $field['required'],
          '#description' => $field['widget']['description'],
          '#prefix' => '<div class ="oh-toggle">',
          '#default_value' => isset($node_field[$delta]['starthours']) ? $node_field[$delta]['starthours'] : $field['defaultstart'],
        );
        $form[$field['field_name']][$delta]['endhours'] = array(
          '#type' => 'select',
          '#title' => '',
          '#options' => $ophours,
          '#required' => $field['required'],
          '#description' => $field['widget']['description'],
          '#default_value' => isset($node_field[$delta]['endhours']) ? $node_field[$delta]['endhours'] : $node_field['defaultend'],
        );
        if ($field['multiple']) {
          $form[$field['field_name']][$delta]['morelink'] = array(
            '#value' => t('Additional hours block:'),
            '#prefix' => '<span class ="morelink">',
            '#suffix' => '</span>',
          );
          $form[$field['field_name']][$deltaplus]['day'] = array(
            '#type' => 'value',
            '#value' => $day,
          );
          $form[$field['field_name']][$deltaplus]['starthours'] = array(
            // from more link
            '#type' => 'select',
            '#title' => '',
            '#options' => $ophours,
            '#description' => $field['widget']['description'],
            '#prefix' => '<div class ="oh-toggle-more">',
            '#default_value' => isset($node_field[$deltaplus]['starthours']) ? $node_field[$deltaplus]['starthours'] : '',
          );
          $form[$field['field_name']][$deltaplus]['endhours'] = array(
            //from more link
            '#type' => 'select',
            '#title' => '',
            '#options' => $ophours,
            '#description' => $field['widget']['description'],
            '#default_value' => isset($node_field[$deltaplus]['endhours']) ? $node_field[$deltaplus]['endhours'] : '',
            '#suffix' => '</div></div></div>',
          );
        }
      }
      return $form;
    case 'process form values':
      foreach ($node_field as $delta => $item) {
        if ($item['day'] == NULL && ($delta == 0 || $delta == 1)) {

          //adjust for sunday.
          $node_field[$delta]['day'] = '0';
        }
        if ($item['daycheck'] == 0 && $delta % 2 == 0) {
          unset($node_field[$delta]);
        }
        if ($item['starthours'] == 'none' || $item['endhours'] == 'none') {
          unset($node_field[$delta]);
        }
      }
      break;
  }
}