You are here

function date_copy_convert_events in Date 6

Same name and namespace in other branches
  1. 5.2 date_copy/date_copy.module \date_copy_convert_events()
  2. 5 date_copy.module \date_copy_convert_events()
1 call to date_copy_convert_events()
date_copy_import_event_form_submit in date_copy/date_copy.module
Event import processing.

File

date_copy/date_copy.module, line 535

Code

function date_copy_convert_events($source_type, $target_type, $date_field, $description_field, $limit, $start = 0, $delete_old, $start_nid, $timezones) {

  // Get info about the field we are importing into
  $field = content_fields($date_field);

  // Get date tz handling, could be date, site, GMT, or none.
  $tz_handling = $field['tz_handling'];

  // Get event tz handling, could be event, site, or user.
  $event_tz_handling = variable_get('event_timezone_display', 'event');

  // Check which version of the Event module this database was built in.
  $event_version = 1;
  if (db_column_exists('event', 'has_time')) {
    $event_version = 2;
  }
  $rows = array();
  if ($start_nid) {
    $where = " AND n.nid >= {$start_nid} ";
  }
  if (!($result = db_query_range("SELECT * FROM {event} e INNER JOIN {node} n ON e.nid=n.nid WHERE n.type = '%s' {$where} ORDER BY n.nid", array(
    $source_type,
    $start_nid,
  ), $start, $limit))) {
    return array();
  }
  while ($event = db_fetch_object($result)) {
    $source_nid = $event->nid;
    $event_node = node_load($source_nid, NULL, TRUE);

    // Creating new nodes or converting existing ones??
    if ($target_type != $source_type) {
      $target_node = new stdClass();
      $target_node->nid = 0;
      $target_node->type = $target_type;
      foreach ($event_node as $key => $val) {
        if ($key != 'nid' && $key != 'type') {
          $target_node->{$key} = $val;
        }
      }
    }
    else {
      $target_node = $event_node;
    }
    if ($description_field != 'body') {
      $target_node->{$description_field} = array(
        0 => array(
          'value' => $event_node->body,
        ),
      );
      unset($target_node->body);
    }
    $timezone = !empty($event->timezone) && $tz_handling == 'date' && $event_tz_handling == 'event' ? $timezones[$event->timezone] : date_default_timezone_name();
    if ($event_version == 1) {

      // Version 1 stores the UTC value in the database as a timestamp.
      $date = array(
        0 => array(),
      );
      $data[0]['timezone'] = $timezone;
      $start = date_make_date($event->event_start, 'UTC', DATE_UNIX);
      date_timezone_set($start, timezone_open($timezone));
      $data[0]['offset'] = date_offset_get($start);
      $end = date_make_date($event->event_end, 'UTC', DATE_UNIX);
      date_timezone_set($end, timezone_open($timezone));
      $data[0]['offset2'] = date_offset_get($end);

      // If the original event had the wrong offset, the 'UTC' value it
      // created will also be wrong, correct it here.
      if ($event_node->start_offset != date_offset_get($start) || $event_node->end_offset != date_offset_get($end)) {
        $adj = $event_node->start_offset - date_offset_get($start);
        date_timezone_set($start, timezone_open('UTC'));
        date_modify($start, $adj . ' seconds');
        $adj = $event_node->end_offset - date_offset_get($end);
        date_timezone_set($end, timezone_open('UTC'));
        date_modify($end, $adj . ' seconds');
      }
      $data[0]['value'] = date_format($start, date_type_format($field['type']));
      $data[0]['value2'] = date_format($end, date_type_format($field['type']));
    }
    else {

      // Version 2 stores the local value in the database as a datetime field.
      $date = array(
        0 => array(),
      );
      $data[0]['timezone'] = $timezone;
      $start = date_make_date($event->event_start, $timezone, DATE_DATETIME);
      $data[0]['offset'] = date_offset_get($start);
      $end = date_make_date($event->event_end, $timezone, DATE_DATETIME);
      $data[0]['offset2'] = date_offset_get($end);
      date_timezone_set($start, timezone_open('UTC'));
      date_timezone_set($end, timezone_open('UTC'));
      $data[0]['value'] = date_format($start, date_type_format($field['type']));
      $data[0]['value2'] = date_format($end, date_type_format($field['type']));
    }
    $target_node->{$date_field} = $data;
    $event_fields = array(
      'event_start',
      'event_end',
      'timezone',
      'start_offset',
      'start_format',
      'start_time_format',
      'end_offset',
      'end_format',
      'end_time_format',
      'event_node_title',
    );
    foreach ($event_fields as $e) {
      unset($target_node->{$e});
    }
    node_save($target_node);
    if ($target_type != $source_type) {
      watchdog('date_copy', t('!type: created %title.', array(
        '!type' => t($target_type),
        '%title' => $target_node->title,
      )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $target_node->nid));
      if ($delete_old) {
        node_delete($source_nid);
      }
    }
    else {
      watchdog('date_copy', t('!type: updated %title.', array(
        '!type' => t($target_type),
        '%title' => $target_node->title,
      )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $target_node->nid));
    }
    $new_field = $target_node->{$date_field};
    $rows[] = array(
      l($target_node->title, 'node/' . $target_node->nid),
      $source_nid,
      $target_node->nid,
      $new_field[0]['value'],
      $new_field[0]['value2'],
    );
  }
  return $rows;
}