You are here

function date_copy_import_ical_form_submit in Date 6

Same name and namespace in other branches
  1. 5.2 date_copy/date_copy.module \date_copy_import_ical_form_submit()
  2. 5 date_copy.module \date_copy_import_ical_form_submit()

File

date_copy/date_copy.module, line 316

Code

function date_copy_import_ical_form_submit($form_id, $form_values) {
  extract($form_values);
  if ($step < 2) {
    return;
  }
  include_once drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  $imported_values = date_ical_import($source_file);
  if (empty($imported_values)) {
    drupal_set_message(t('This is an invalid file.'));
    return;
  }

  // workaround to disable drupal messages when nodes are created or deleted
  $messages = drupal_get_messages();
  $node_types = node_get_types('names');
  $field = content_fields($date_field, $target_type);
  $account = user_load(array(
    'name' => $name,
  ));
  $rows = array();
  switch ($field['type']) {
    case DATE_UNIX:
      $format = 'U';
      break;
    case DATE_ISO:
      $format = DATE_FORMAT_ISO;
      break;
  }
  foreach ($imported_values as $calendars => $calendar) {
    foreach ($calendar['VEVENT'] as $key => $value) {
      if (empty($value['DTEND'])) {
        $value['DTEND'] = $value['DTSTART'];
      }
      $start_date = date_ical_date($value['DTSTART']);
      $end_date = date_ical_date($value['DTEND']);
      $timezone = timezone_name_get(date_timezone_get($start_date));
      $offset = date_offset_get($start_date);
      $offset2 = date_offset_get($end_date);
      date_timezone_set($start_date, 'UTC');
      date_timezone_set($end_date, 'UTC');
      $start = date_limit_value(date_format($start_date, $format), date_granularity($field), $field['type']);
      $end = date_limit_value(date_format($end_date, $format), date_granularity($field), $field['type']);
      $target_node = new stdClass();
      $target_node->nid = 0;
      $target_node->type = $target_type;
      $target_node->name = $name;
      $target_node->uid = $account->uid;
      $target_node->status = $status;
      $target_node->promote = $promote;
      $target_node->sticky = $sticky;
      $target_node->revision = $revision;
      if (module_exists('og')) {
        $target_node->og_public = $og_public;
        $target_node->og_groups = $og_groups;
      }
      $target_node->title = stripslashes($value['SUMMARY']);
      $target_node->{$date_field} = array(
        0 => array(
          'value' => $start,
          'value2' => $end,
          'timezone' => $timezone,
          'offset' => $offset,
          'offset2' => $offset2,
        ),
      );
      if ($description_field == 'body') {
        $target_node->body = stripslashes($value['DESCRIPTION']);
      }
      elseif (!empty($description_field)) {
        $target_node->{$description_field} = array(
          0 => array(
            'value' => stripslashes($value['DESCRIPTION']),
          ),
        );
      }
      if (!empty($uid_field)) {
        $target_node->{$uid_field} = array(
          0 => array(
            'value' => stripslashes($value['UID']),
          ),
        );
      }
      if (!empty($location_field)) {
        $target_node->{$location_field} = array(
          0 => array(
            'value' => stripslashes($value['LOCATION']),
          ),
        );
      }
      if (!empty($url_field)) {
        $target_node->{$url_field} = array(
          0 => array(
            'url' => stripslashes($value['URL']),
            'title' => stripslashes($value['SUMMARY']),
          ),
        );
      }
      $target_node->taxonomy = $taxonomy;
      node_save($target_node);
      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));
      $new_field = $target_node->{$date_field};
      $rows[] = array(
        l($target_node->title, 'node/' . $target_node->nid),
        $target_node->nid,
        $new_field[0]['value'],
        $new_field[0]['value2'],
      );
    }
  }

  // write back the old messages
  $_SESSION['messages'] = $messages;
  if (!empty($rows)) {
    drupal_set_message(t('%limit ical events have been added.', array(
      '%limit' => sizeof($rows),
    )));
    drupal_set_message(theme('table', array(
      t('Title'),
      t('Id'),
      t('Start'),
      t('End'),
    ), $rows));
  }
  else {
    drupal_set_message(t('No ical events have been added.'));
  }
  return;
}