You are here

function office_hours_handler_filter_open::post_execute in Office Hours 7

Run after the view is executed, before the result is cached.

This gives all the handlers some time to modify values. This is primarily used so that handlers that pull up secondary data can put it in the $values so that the raw data can be utilized externally.

Overrides views_handler::post_execute

File

includes/office_hours_handler_filter_open.inc, line 82
Implements Views integration: filter 'open now'.

Class

office_hours_handler_filter_open
@file Implements Views integration: filter 'open now'.

Code

function post_execute(&$values) {

  // A filter in query() might be quicker, but this is easier to write.
  if (!empty($values)) {
    $operators = $this->value;

    // in_operator = multivalue.
    if ($operators == array() || $operators == 'All' || $operators == 'all') {
      return;
    }

    // Copied from office_hours.module.
    $today = (int) idate('w', $_SERVER['REQUEST_TIME']);

    // Get daynumber sun=0 - sat=6.
    $now = date('Gi', $_SERVER['REQUEST_TIME']);

    // 'Gi' format.
    foreach ($values as $key => &$object) {

      // For now, ony for nodes...
      // For now, only for status 'today'...
      $entity_type = 'node';
      $entity_id = $object->nid;

      //      $entity_type = $object->{$this->aliases['entity_type']};
      if (empty($this->definition['is revision'])) {
        $revision_id = FALSE;

        //        $entity_id = $object->{$this->field_alias};
      }
      else {
        $revision_id = $object->{$this->field_alias};

        //        $entity_id = $object->{$this->aliases['entity_id']};
      }
      $entities = entity_load($entity_type, array(
        $entity_id,
      ));
      $entity = array_pop($entities);
      if ($entity) {

        // 1 => t('Open now'),
        // 2 => t('Open today'),
        // 4 => t('Closed now'),
        // 8 => t('Closed today'),
        $bitstatus = 0;

        // Loop over the translations of the field.
        foreach ($entity->{$this->real_field} as $item) {
          unset($record);
          foreach ($item as $langcode => $record) {
            if ($record['day'] == $today && $record['starthours'] <= $now && $record['endhours'] >= $now) {

              // Store is open at this moment.
              $status = 'open_now';
              $bitstatus = $bitstatus | 1;

              // => t('Open now');
              $bitstatus = $bitstatus | 2;

              // => t('Open today');
            }
            elseif ($record['day'] == $today && $record['starthours'] >= $now) {

              // Store is open later today.
              $bitstatus = $bitstatus | 2;

              // => t('Open today');
            }
            elseif ($record['day'] == $today) {

              // Already/still closed.
              $bitstatus = 4;

              // => t('Closed now, was open')
            }
          }
        }
        if (!$bitstatus) {

          // Store is never open today.
          $bitstatus = $bitstatus | 8;

          // => t('Closed today');
        }

        // Remove object from result.
        $selected = FALSE;
        foreach ($operators as $bitcode) {
          if ($bitstatus & $bitcode) {
            $selected = TRUE;
          }
        }
        if (!$selected) {
          unset($values[$key]);
        }
      }
    }
  }
}