[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/date/date_tools/ -> date_tools.event.inc (source)

   1  <?php
   2  
   3  /**
   4   *  Event import form.
   5   */
   6  function date_tools_copy_import_event_form($form_state) {
   7    // We can do an import if there are event fields available whether or not the event module is enabled
   8    // so we just check whether the table exists.
   9    if (!db_table_exists('event')) {
  10      drupal_set_message(t('There is no event table in this database. No event import options are available.'));
  11      return array();
  12    }
  13  
  14    if (empty($form_state['values']['step'])) {
  15      $form_state['values']['step'] = 0;
  16    }
  17    $step = intval($form_state['values']['step'] + 1);
  18    $form['step'] = array(
  19      '#type' => 'hidden',
  20      '#value' => $step,
  21    );
  22  
  23    switch ($step) {
  24      case 1: // Select a content type to import into.
  25        $node_types = node_get_types('names');
  26        $form['#prefix'] = '<p>' . t("Create a new CCK content type to import your events into, or, if you do not want to create new nodes for your events, add a date field to the existing event type. Make sure the target content type has a date field that has an optional or required To date so it can accept the From date and To date of the event. If your source event has its own timezone field, make sure you set the target date timezone handling to 'date'. Test the target type by trying to create a node manually and make sure all the right options are available in the form before attempting an import.") . '</p><p><strong>' . t('The import will create new nodes and trigger all related hooks, so you may want to turn off automatic email messaging for this node type while performing the import!') . '</strong></p>';
  27        $source_type_options = array();
  28        $result = db_query("SELECT DISTINCT n.type FROM {event} e INNER JOIN {node} n ON e.nid=n.nid");
  29        while ($arr = db_fetch_array($result)) {
  30          $source_type_options[$arr['type']] = $node_types[$arr['type']];
  31        }
  32        if (sizeof($source_type_options) < 1) {
  33          drupal_set_message(t('There are no event nodes in this database. No event import options are available.'));
  34          return array();
  35        }
  36        $form['source_type'] = array(
  37          '#type' => 'select',
  38          '#options' => $source_type_options,
  39          '#title' => t('Source type'),
  40          '#default_value' => '',
  41        );
  42        $form += date_tools_copy_type_form(TRUE);
  43        $form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
  44  
  45        return $form;
  46  
  47      case 2: // Select the fields to import into.
  48        $type = $form_state['values']['target_type'];
  49        $form['target_type'] = array(
  50          '#value' => $type,
  51          '#type' => 'hidden',
  52        );
  53        $form['source_type'] = array(
  54          '#value' => $form_state['values']['source_type'],
  55          '#type' => 'hidden',
  56        );
  57        $form['fields'] = array(
  58          '#type' => 'fieldset',
  59          '#title' => t('!type Fields', array('!type' => $node_types[$type])),
  60          '#weight' => -1,
  61        );
  62        $form['fields'] += date_tools_copy_type_fields_form($type);
  63        $form['delete_old'] = array('#type' => 'select', '#options' => array(1 => t('Yes'), 0 => t('No')), '#title' => t('Delete original event?'), '#description' => t('Should the original entry be deleted once it has been copied to the new content type? If so, be sure to back up your database first.'));
  64        $form['max'] = array('#type' => 'textfield', '#title' => t('Limit'), '#description' => t('The maximum number of nodes to convert in this pass.'), '#required' => TRUE);
  65        $form['start_nid'] = array('#type' => 'textfield', '#title' => t('Starting nid'), '#default_value' => 0, '#description' => t('Convert nodes with nids greater than or equal to this number.'));
  66        $form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
  67        return $form;
  68    }
  69  }
  70  
  71  /**
  72   *  Event import processing.
  73   */
  74  function date_tools_copy_import_event_form_submit($form, &$form_state) {
  75    $form_state['rebuild'] = TRUE;
  76    extract($form_state['values']);
  77    if ($step != 2) return;
  78  
  79    // workaround to disable drupal messages when nodes are created or deleted
  80    //$messages = drupal_get_messages();
  81  
  82    // The array that maps event timezone zids to timezone names is in
  83    // date_php4_tz_map.inc, need to reverse it so the zid is the key.
  84    require_once('./'. drupal_get_path('module', 'date_php4') .'/date_php4_tz_map.inc');
  85    $timezones = array('' => '');
  86    $map = $timezone_map;
  87    foreach ($map as $zone => $values) {
  88      if (!empty($values['zid'])) {
  89        $timezones[$values['zid']] = $zone;
  90      }
  91    }
  92  
  93    $rows = array();
  94    $i = 0;
  95    // Get $max records, 10 at a time.
  96    $limit = min(10, intval($max));
  97    while ($i < intval($max)) {
  98      $new_rows = date_tools_copy_convert_events($source_type, $target_type, $date_field, $description_field, $limit, $i, $delete_old, $start_nid, $timezones);
  99      $rows = array_merge($rows, $new_rows);
 100      $i += $limit;
 101    }
 102  
 103    // write back the old messages
 104    //$_SESSION['messages'] = $messages;
 105  
 106    if (!empty($rows)) {
 107      drupal_set_message(format_plural(sizeof($rows), '1 event has been converted.', '@count events have been converted.'));
 108      drupal_set_message(theme('table', array(t('Node Title'), t('Original Node ID'), t('New Node ID'), t('Start date'), t('End date')), $rows));
 109    }
 110    else {
 111      drupal_set_message(t('No events have been converted.'));
 112    }
 113    return;
 114  }
 115  
 116  function date_tools_copy_convert_events( $source_type, $target_type, $date_field, $description_field, $limit, $start = 0, $delete_old, $start_nid, $timezones) {
 117  
 118    // Get info about the field we are importing into
 119    $field   = content_fields($date_field);
 120  
 121    // Get date tz handling, could be date, site, GMT, or none.
 122    $tz_handling  = $field['tz_handling'];
 123  
 124    // Get event tz handling, could be event, site, or user.
 125    $event_tz_handling = variable_get('event_timezone_display', 'event');
 126  
 127    // Check which version of the Event module this database was built in.
 128    $event_version = 1;
 129    if (db_column_exists('event', 'has_time')) {
 130      $event_version = 2;
 131    }
 132  
 133    $rows = array();
 134    if ($start_nid) {
 135      // We're skipping the placeholders so make sure this is an integer.
 136      $start_nid = intval($start_nid);
 137      $where = " AND n.nid >= $start_nid ";
 138    }
 139    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)) {
 140      return array();
 141    }
 142    while ($event = db_fetch_object($result)) {
 143  
 144      $source_nid = $event->nid;
 145      $event_node = node_load($source_nid, NULL, TRUE);
 146  
 147      // Creating new nodes or converting existing ones??
 148      if ($target_type != $source_type) {
 149        $target_node = new stdClass();
 150        $target_node->nid = 0;
 151        $target_node->type = $target_type;
 152        foreach ($event_node as $key => $val) {
 153          if ($key != 'nid' && $key != 'type') {
 154            $target_node->$key = $val;
 155          }
 156        }
 157      }
 158      else {
 159        $target_node = $event_node;
 160      }
 161  
 162      if ($description_field != 'body') {
 163        $target_node->$description_field = array(0 => array('value' => $event_node->body));
 164        unset($target_node->body);
 165      }
 166  
 167      // Set the date timezone value.
 168      $timezone = !empty($event->timezone) && $tz_handling == 'date' && $event_tz_handling == 'event' ? $timezones[$event->timezone] : date_default_timezone_name();
 169  
 170      // If this is a deprecated timezone, replace it.
 171      require_once(drupal_get_path('module', 'date_timezone') .'/date_timezone.install');
 172      $timezone = _date_timezone_replacement($timezone);
 173  
 174      // Find the original timezone value (might not be the same as the date timezone).
 175      $event_timezone = !empty($event->timezone) ? $timezones[$event->timezone] : date_default_timezone_name();
 176      // If this is a deprecated timezone, replace it.
 177      $event_timezone = _date_timezone_replacement($event_timezone);
 178  
 179      if ($event_version == 1) {
 180        // Version 1 stores the UTC value in the database as a timestamp.
 181        $date = array(0 => array());
 182        $data[0]['timezone'] = $timezone;
 183        $start = date_make_date($event->event_start, 'UTC', DATE_UNIX);
 184        date_timezone_set($start, timezone_open($timezone));
 185        $data[0]['offset'] = date_offset_get($start);
 186        $end = date_make_date($event->event_end, 'UTC', DATE_UNIX);
 187        date_timezone_set($end, timezone_open($timezone));
 188        $data[0]['offset2'] = date_offset_get($end);
 189  
 190        // If the original event had the wrong offset, the 'UTC' value it
 191        // created will also be wrong, correct it here.
 192        if ($event_node->start_offset != date_offset_get($start) || $event_node->end_offset != date_offset_get($end)) {
 193          $adj = $event_node->start_offset - date_offset_get($start);
 194          date_timezone_set($start, timezone_open('UTC'));
 195          date_modify($start, $adj .' seconds');
 196          $adj = $event_node->end_offset - date_offset_get($end);
 197          date_timezone_set($end, timezone_open('UTC'));
 198          date_modify($end, $adj .' seconds');
 199        }
 200        $data[0]['value'] = date_format($start, date_type_format($field['type']));
 201        $data[0]['value2'] = date_format($end, date_type_format($field['type']));
 202      }
 203      else {
 204        // Version 2 stores the local value in the database as a datetime field.
 205        $date = array(0 => array());
 206        $data[0]['timezone'] = $timezone;
 207        $start = date_make_date($event->event_start, $event_timezone, DATE_DATETIME);
 208        if ($event_timezone != $timezone) {
 209          date_timezone_set($start, timezone_open($timezone));
 210        }
 211        $data[0]['offset'] = date_offset_get($start);
 212        $end = date_make_date($event->event_end, $event_timezone, DATE_DATETIME);
 213        if ($event_timezone != $timezone) {
 214          date_timezone_set($end, timezone_open($timezone));
 215        }
 216        $data[0]['offset2'] = date_offset_get($end);
 217  
 218        date_timezone_set($start, timezone_open('UTC'));
 219        date_timezone_set($end, timezone_open('UTC'));
 220        $data[0]['value'] = date_format($start, date_type_format($field['type']));
 221        $data[0]['value2'] = date_format($end, date_type_format($field['type']));
 222      }
 223  
 224      $target_node->$date_field = $data;
 225      $event_fields = array(
 226        'event_start', 'event_end', 'timezone', 'start_offset',
 227        'start_format', 'start_time_format', 'end_offset',
 228        'end_format', 'end_time_format', 'event_node_title');
 229      foreach ($event_fields as $e) {
 230        unset($target_node->$e);
 231      }
 232  
 233      node_save($target_node);
 234  
 235      if ($target_type != $source_type) {
 236        watchdog('date_tools', '!type: %title has been created.', array(
 237          '!type' => t($target_type),
 238          '%title' => $target_node->title),
 239          WATCHDOG_NOTICE,
 240          l(t('view'), 'node/'. $target_node->nid));
 241        if ($delete_old) {
 242          node_delete($source_nid);
 243        }
 244      }
 245      else {
 246        watchdog('date_tools', '!type: %title has been updated.', array(
 247          '!type' => t($target_type),
 248          '%title' => $target_node->title),
 249          WATCHDOG_NOTICE,
 250          l(t('view'), 'node/'. $target_node->nid));
 251      }
 252      $new_field = $target_node->$date_field;
 253      $rows[] = array(
 254        l($target_node->title,
 255        'node/'. $target_node->nid),
 256        $source_nid,
 257        $target_node->nid,
 258        $new_field[0]['value'],
 259        $new_field[0]['value2']);
 260    }
 261    return $rows;
 262  }
 263  
 264  /**
 265   * A form to select a content type.
 266   */
 267  function date_tools_copy_type_form($target = TRUE) {
 268    $form = array();
 269    $node_types = node_get_types('names');
 270    $fields = content_fields();
 271    $type_options = array();
 272  
 273    // Find out what content types contain date fields and set them up as target options.
 274    foreach ($fields as $field_name => $field) {
 275      if ($field['type'] == 'date' || $field['type'] == 'datestamp' || $field['type'] == 'datetime') {
 276        $type_options[$field['type_name']] = $node_types[$field['type_name']];
 277      }
 278    }
 279  
 280    if (sizeof($type_options) < 1) {
 281      drupal_set_message(t('There are no date fields in this database to import the data into. Please add a date field to the desired node types and be sure to indicate it uses both a "from" and a "to" date.'));
 282      return $form;
 283    }
 284    $type = $target ? 'target_type' : 'source_type';
 285    $label = $target ? t('Target type') : t('Source type');
 286    $form[$type] = array(
 287      '#type' => 'select',
 288      '#options' => $type_options,
 289      '#title' => $label,
 290      '#description' => t('Only content types with date fields appear in this list as possible target types.'),
 291      '#default_value' => '',
 292      );
 293  
 294    // If Content Copy is enabled, offer an import link.
 295    if (module_exists('content_copy')) {
 296      $form['macro'] = array(
 297        '#type' => 'fieldset',
 298        '#title' => t('Add'),
 299        '#description' => t('If your desired target type does not already have a date field, follow this link and select a content type to add a date field to that type.'),
 300        '#collapsible' => TRUE,
 301        '#collapsed' => FALSE,
 302      );
 303      $form['macro']['link'] = array(
 304        '#type' => 'markup',
 305        '#value' => l(t('Add new date field'), 'admin/content/types/import', array('query' => 'macro_file='. drupal_get_path('module', 'date_tools') .'/date_field.php')),
 306      );
 307    }
 308    return $form;
 309  }
 310  
 311  /**
 312   * A form to select fields from a content type.
 313   */
 314  function date_tools_copy_type_fields_form($type, $extended = FALSE) {
 315    $form = array();
 316    $fields = content_fields();
 317    $date_options = array();
 318    $description_options = array('' => '');
 319    $uid_options = array('' => '');
 320    $url_options = array('' => '');
 321    $location_options = array('' => '');
 322  
 323    // Find out what content types contain date fields and set them up as target options.
 324    foreach ($fields as $field_name => $field) {
 325      if ($field['type_name'] == $type) {
 326        if ($field['type'] == 'date' || $field['type'] == 'datestamp' || $field['type'] == 'datetime') {
 327          $date_options[$field_name] = $field['widget']['label'];
 328        }
 329        if ($field['type'] == 'text') {
 330          $description_options[$field_name] = $field['widget']['label'];
 331          $location_options[$field_name] = $field['widget']['label'];
 332          $uid_options[$field_name] = $field['widget']['label'];
 333          $url_options[$field_name] = $field['widget']['label'];
 334        }
 335        if ($field['type'] == 'link') {
 336          $url_options[$field_name] = $field['widget']['label'];
 337        }
 338      }
 339    }
 340    // The body field is also available as an option for the description.
 341    $description_options['body'] = t('body');
 342    if (sizeof($date_options) < 1) {
 343      drupal_set_message(t('There are no date fields in this database to import the data into. Please add a date field to the desired node types and be sure to indicate it uses both a "from" and a "to" date.'));
 344      return $form;
 345    }
 346    $form['date_field'] = array(
 347      '#type' => 'select',
 348      '#options' => $date_options,
 349      '#title' => t('Date field'),
 350      '#default_value' => '',
 351      '#description' => t('The field which will contain the source dates in target content type.'),
 352      );
 353    $form['description_field'] = array(
 354      '#type' => 'select',
 355      '#options' => $description_options,
 356      '#title' => t('Description field'),
 357      '#default_value' => '',
 358      '#description' => t('The text or body field which will contain the source description in the target content type.'),
 359      );
 360    if ($extended) {
 361      $form['url_field'] = array(
 362        '#type' => 'select',
 363        '#options' => $url_options,
 364        '#title' => t('Url field'),
 365        '#default_value' => '',
 366        '#description' => t('The text or link field which will contain the source url in the target content type.'),
 367        );
 368      $form['location_field'] = array(
 369        '#type' => 'select',
 370        '#options' => $location_options,
 371        '#title' => t('Location field'),
 372        '#default_value' => '',
 373        '#description' => t('The text field which will contain the source location text in the target content type.'),
 374        );
 375      $form['uid_field'] = array(
 376        '#type' => 'select',
 377        '#options' => $uid_options,
 378        '#title' => t('Uid field'),
 379        '#default_value' => '',
 380        '#description' => t('The text field which will contain the source uid in the target content type.'),
 381        );
 382    }
 383    return $form;
 384  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7