You are here

function date_copy_convert_events in Date 5

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

File

./date_copy.module, line 519

Code

function date_copy_convert_events($source_type, $target_type, $date_field, $description_field, $limit, $start = 0, $delete_old, $start_nid) {
  include_once drupal_get_path('module', 'date_api') . '/date.inc';
  include_once './' . drupal_get_path('module', 'date_api') . '/date_timezones.inc';

  // 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');
  $timezones = date_get_timezones();
  $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);
    }
    $timestamp = $event->event_start;
    $timestamp2 = $event->event_end;
    if ($field['type'] == 'date') {
      $data = array(
        0 => array(
          'value' => date_unset_granularity(date_unix2iso($timestamp), date_granularity_array($field), $field['type']),
          'value2' => date_unset_granularity(date_unix2iso($timestamp2), date_granularity_array($field), $field['type']),
          'timezone' => '',
          'offset' => '',
        ),
      );
    }
    else {
      $data = array(
        0 => array(
          'value' => date_unset_granularity($timestamp, date_granularity_array($field), $field['type']),
          'value2' => date_unset_granularity($timestamp2, date_granularity_array($field), $field['type']),
          'timezone' => '',
          'offset' => '',
        ),
      );
    }
    if ($tz_handling == 'date' && $event_tz_handling == 'event') {
      $data[0]['timezone'] = $timezones[$event->timezone]['timezone'];
      $data[0]['offset'] = date_get_offset($event->timezone, $timestamp);
    }
    else {
      unset($data[0]['timezone']);
      unset($data[0]['offset']);
    }
    $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;
}