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