You are here

function _opening_hours_instance_update in Opening hours 7

Same name and namespace in other branches
  1. 6 includes/opening_hours.pages.inc \_opening_hours_instance_update()

Helper function to update an existing instance.

Also updates propagated copies, if applicable.

Parameters

object $instance: The updated instance object.

object $original: Original instance data as currently stored in the database.

Return value

object Returns $instance.

1 call to _opening_hours_instance_update()
opening_hours_instance_id_api_page in includes/opening_hours.pages.inc
The CRUD API for a specific instance ID.

File

includes/opening_hours.pages.inc, line 162
Page callbacks for the opening hours module.

Code

function _opening_hours_instance_update($instance, $original) {

  // Coerce category_tid and notice to NULL if they're empty.
  $instance->category_tid = is_numeric($instance->category_tid) ? $instance->category_tid : NULL;
  $instance->notice = !empty($instance->notice) ? $instance->notice : NULL;

  // Handling of root instances vs. propagated copies is very different.
  if (empty($original->original_instance_id)) {

    // If instance used to repeat, and it's repeat settings have changed,
    // we need to clean out possibly outdated propagated instances.
    if (!empty($original->repeat_rule) && ($instance->repeat_rule != $original->repeat_rule || $instance->repeat_end_date != $original->repeat_end_date)) {

      // If the event no longer repeats, or the repeat rule changed,
      // wipe all propagated copies.
      if (empty($instance->repeat_rule) || $instance->repeat_rule != $original->repeat_rule) {
        db_query("\n          DELETE FROM {opening_hours}\n          WHERE original_instance_id = :id\n        ", array(
          ':id' => $instance->instance_id,
        ));
      }
      else {
        db_query("\n          DELETE FROM {opening_hours}\n          WHERE original_instance_id = :id\n          AND date > :end_date\n        ", array(
          ':id' => $instance->instance_id,
          ':end_date' => $instance->repeat_end_date,
        ));
      }
    }
  }
  else {

    // There are three different scenarios here. Applying changes to the
    // entire chain of propagated events, to the actual instance and any
    // subsequent versions, or just the one instance.
    if ($_REQUEST['propagateChanges'] == 'all') {

      // TODO: Implement this.
    }
    elseif ($_REQUEST['propagateChanges'] == 'future') {
      db_query("\n        UPDATE {opening_hours} SET original_instance_id = NULL\n        WHERE instance_id = :id\n      ", array(
        ':id' => $instance->instance_id,
      ));
      db_query("\n        UPDATE {opening_hours} SET original_instance_id = :id\n        WHERE original_instance_id = :prev_id AND date > :date\n      ", array(
        ':id' => $instance->instance_id,
        ':prev_id' => $instance->original_instance_id,
        ':date' => $instance->date,
      ));

      // Stop propagating the original instance, since otherwise it
      // re-propagate on next cron run, nullifying these changes.
      opening_hours_repeat_stop_propagation($instance->original_instance_id);
      unset($instance->original_instance_id);
    }
    else {
      $instance->customised = 1;
    }
  }

  // drupal_write_record will try to write an empty string to the repeat
  // end date if it has an empty-ish value, which will fail, so be sure
  // to unset it first in this case.
  if (empty($instance->repeat_end_date)) {
    unset($instance->repeat_end_date);
  }

  // Once that's all done, first update the original instance, and then
  // any propagated copies.
  drupal_write_record('opening_hours', $instance, array(
    'instance_id',
  ));
  $query = db_query("\n    UPDATE {opening_hours}\n    SET start_time = :start, end_time = :end, category_tid = :tid, notice = :notice\n    WHERE customised = 0 AND original_instance_id = :id\n  ", array(
    ':start' => $instance->start_time,
    ':end' => $instance->end_time,
    ':tid' => $instance->category_tid,
    ':notice' => $instance->notice,
    ':id' => $instance->instance_id,
  ));

  // Propagate changes if instance has a repeat rule.
  if (!empty($instance->repeat_rule)) {
    opening_hours_repeat_instance_propagate($instance);
  }
  return $instance;
}