You are here

function opening_hours_repeat_instance_propagate in Opening hours 7

Same name and namespace in other branches
  1. 6 opening_hours.module \opening_hours_repeat_instance_propagate()

Propagates a repeating instance.

Makes copies of the event each time it repeats until either the repeat rule ends or two years have passed.

3 calls to opening_hours_repeat_instance_propagate()
opening_hours_cron in ./opening_hours.module
Implements hook_cron().
opening_hours_crud_api_page in includes/opening_hours.pages.inc
The CRUD API for communication with Backbone.
_opening_hours_instance_update in includes/opening_hours.pages.inc
Helper function to update an existing instance.

File

./opening_hours.module, line 396
Opening hours module.

Code

function opening_hours_repeat_instance_propagate(&$instance) {

  // Maximum limit is about two years in the future.
  $limit = $_SERVER['REQUEST_TIME'] + 365 * 86400;

  // Set up the increment for the repeat rule.
  if ($instance->repeat_rule == 'weekly') {
    $increment = 7 * 86400;
  }
  if (!empty($instance->repeat_end_date)) {

    // Use noon on the date when converting to timestamp to dodge
    // daylight savings issues.
    $end_date = strtotime($instance->repeat_end_date . 'T12:00:00');

    // If the end date is before the limit, it becomes the new limit.
    if ($end_date && $end_date < $limit) {
      $limit = $end_date;
    }
  }

  // Bail if we don't have an increment.
  if (empty($increment) || $increment < 2) {
    return;
  }
  $current_date = strtotime($instance->date . 'T12:00:00');

  // Figure out how far the instance has already been propagated, and
  // start there.
  $start_point_date = db_query('
    SELECT MAX(date) FROM {opening_hours} WHERE original_instance_id = :id
  ', array(
    ':id' => $instance->instance_id,
  ))
    ->fetchField();
  if ($start_point_date) {
    $start_point_date = strtotime($start_point_date . 'T12:00:00');

    // If our start point is later than the current date, use that when
    // iterating, so we don't generate duplicate entries.
    if ($start_point_date > $current_date) {
      $current_date = $start_point_date;
    }
  }
  for ($current_date += $increment; $current_date < $limit; $current_date += $increment) {

    // Generate the new propagated instance.
    $propagated = (object) array(
      'nid' => $instance->nid,
      'date' => date('Y-m-d', $current_date),
      'start_time' => $instance->start_time,
      'end_time' => $instance->end_time,
      'original_instance_id' => $instance->instance_id,
      'customised' => 0,
    );

    // Propagate the category_tid, if set.
    if (!empty($instance->category_tid)) {
      $propagated->category_tid = $instance->category_tid;
    }

    // Propagate the notice, if set.
    if (!empty($instance->notice)) {
      $propagated->notice = $instance->notice;
    }
    drupal_write_record('opening_hours', $propagated);
  }
}