Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / node_export / node_export.pages.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * The Node export pages file.
6
 *
7
 * Functions for page/form interfaces.
8
 */
9

    
10
/**
11
 * Handles the bits of the form that are specific to the token module.
12
 */
13
function node_export_settings_token_bits(&$form, $key) {
14
  if (module_exists('token')) {
15
    $form[$key . '_token_help'] = array(
16
      '#title' => t('Replacement patterns'),
17
      '#type' => 'fieldset',
18
      '#collapsible' => TRUE,
19
      '#collapsed' => TRUE,
20
    );
21
    $form[$key . '_token_help']['help'] = array(
22
      '#theme' => 'token_tree',
23
      '#token_types' => array('node_export_filename'),
24
    );
25
  }
26
  else {
27
    $form[$key]['#description'] = t(
28
      'Get the <a href="@token">token</a> module for more options.',
29
      array('@token' => url('http://www.drupal.org/project/token'))
30
    );
31
  }
32
}
33

    
34
/**
35
 * Menu callback to configure module settings.
36
 */
37
function node_export_settings($form, &$form_state) {
38
  $types = node_type_get_names();
39

    
40
  menu_rebuild();
41

    
42
  $form['basic'] = array(
43
    '#type' => 'fieldset',
44
    '#title' => t('General settings'),
45
  );
46

    
47
  $format_handlers = node_export_format_handlers();
48
  $format_options = array();
49
  foreach ($format_handlers as $format_handler => $format) {
50
    $display = $format['#title'];
51
    if (!empty($format['#description'])) {
52
      $display .= '<div><small>';
53
      $display .= $format['#description'];
54
      $display .= '</small></div>';
55
    }
56
    $format_options[$format_handler] = $display;
57

    
58
    $format_settings = array();
59
    if (!empty($format['#settings_callback'])) {
60
      if (!empty($format['#file']) && is_file($format['#file'])) {
61
        require_once $format['#file'];
62
      }
63
      $format_form = call_user_func($format['#settings_callback'], $form, $form_state);
64
      $format_settings = $format_settings + $format_form;
65
    }
66

    
67
  }
68
  $selected_formats = variable_get('node_export_format', array('drupal'));
69
  if (!count(array_filter($selected_formats))) {
70
    $selected_formats = array('drupal');
71
  }
72
  if (count($format_options) > 1) {
73
    $form['basic']['node_export_format'] = array(
74
      '#type' => 'checkboxes',
75
      '#title' => t('Format to use when exporting a node'),
76
      '#default_value' => $selected_formats,
77
      '#options' => $format_options,
78
      '#description' => t("If you select multiple formats, they will all be available to the user.  If you select none, or the format handler is not found, it will use the default 'Drupal var export'.  This does not affect imports, the required import format will be used automatically."),
79
    );
80
  }
81
  else {
82
    $format = key($format_options);
83
    $form['basic']['node_export_format'] = array(
84
      '#type' => 'value',
85
      '#value' => array($format => $format),
86
    );
87
  }
88

    
89
  $form['basic'][] = $format_settings;
90

    
91
  $form['basic']['node_export_code'] = array(
92
    '#type' => 'radios',
93
    '#title' => t('Node export code delivery'),
94
    '#options' => array(
95
      'all' => t('All of the below options on a page'),
96
      'copy' => t('Textarea filled with export code'),
97
      'file' => t('Text file download'),
98
    ),
99
    '#default_value' => variable_get('node_export_code', 'all'),
100
  );
101

    
102
  $form['basic']['filename'] = array(
103
    '#type' => 'fieldset',
104
    '#title' => t('Filename settings'),
105
    '#collapsible' => TRUE,
106
    '#collapsed' => TRUE,
107
  );
108

    
109
  $form['basic']['filename']['node_export_filename'] = array(
110
    '#type' => 'textarea',
111
    '#title' => t('Filename pattern'),
112
    '#default_value' => variable_get('node_export_filename', 'node-export[node_export_filename:nid-list]([node_export_filename:node-count]-nodes).[node_export_filename:timestamp].[node_export_filename:format]'),
113
    '#rows' => 1,
114
    '#wysiwyg' => FALSE,
115
  );
116
  node_export_settings_token_bits($form['basic']['filename'], 'node_export_filename');
117

    
118
  $form['basic']['filename']['node_export_file_list'] = array(
119
    '#type' => 'textfield',
120
    '#title' => t('Node ID list max'),
121
    '#default_value' => variable_get('node_export_file_list', 10),
122
    '#size' => 6,
123
    '#maxlength' => 30,
124
    '#description' => t('If there are more than this many nodes, the [node_export_filename:nid-list] token for the filename will not be built.  This is to prevent very long filenames.'),
125
  );
126

    
127
  $form['basic']['node_export_existing'] = array(
128
    '#type' => 'radios',
129
    '#title' => t('When importing a node that already exists'),
130
    '#options' => array(
131
      'new' => t('Create a new node'),
132
      'revision' => t('Create a new revision of the existing node'),
133
      'skip' => t('Skip the node'),
134
    ),
135
    '#description' => t('UUIDs are used to uniquely identify nodes.'),
136
    '#default_value' => variable_get('node_export_existing', 'new'),
137
  );
138

    
139
  $form['publishing'] = array(
140
    '#type' => 'fieldset',
141
    '#title' => t('Reset values on import'),
142
  );
143

    
144
  foreach ($types as $type => $name) {
145
    $form['publishing'][$type] = array(
146
      '#type' => 'fieldset',
147
      '#title' => $name,
148
      '#description' => t('Reset these values when importing nodes of type @s.', array('@s' => $name)),
149
      '#collapsible' => TRUE,
150
      '#collapsed' => TRUE,
151
    );
152
    $form['publishing'][$type]['node_export_reset_status_' . $type] = array(
153
      '#type' => 'checkbox',
154
      '#title' => t('Published'),
155
      '#default_value' => variable_get('node_export_reset_status_' . $type, FALSE),
156
      '#description' => t('Set to unpublished'),
157
    );
158
    $form['publishing'][$type]['node_export_reset_promote_' . $type] = array(
159
      '#type' => 'checkbox',
160
      '#title' => t('Promoted to front page'),
161
      '#default_value' => variable_get('node_export_reset_promote_' . $type, FALSE),
162
      '#description' => t('Set to not promoted'),
163
    );
164
    $form['publishing'][$type]['node_export_reset_sticky_' . $type] = array(
165
      '#type' => 'checkbox',
166
      '#title' => t('Sticky at top of lists'),
167
      '#default_value' => variable_get('node_export_reset_sticky_' . $type, FALSE),
168
      '#description' => t('Set to not sticky'),
169
    );
170
    $form['publishing'][$type]['node_export_reset_author_' . $type] = array(
171
      '#type' => 'checkbox',
172
      '#title' => t('Author (will set to user performing the import)'),
173
      '#default_value' => variable_get('node_export_reset_author_' . $type, TRUE),
174
      '#description' => t('Recommended since User IDs could be different on import site and the wrong user may gain permission to edit the node.'),
175
    );
176
    $form['publishing'][$type]['node_export_reset_created_' . $type] = array(
177
      '#type' => 'checkbox',
178
      '#title' => t('Created time (<em>Authored on</em> date/time)'),
179
      '#default_value' => variable_get('node_export_reset_created_' . $type, TRUE),
180
    );
181
    $form['publishing'][$type]['node_export_reset_changed_' . $type] = array(
182
      '#type' => 'checkbox',
183
      '#title' => t('Changed time (<em>Last updated</em> date/time)'),
184
      '#default_value' => variable_get('node_export_reset_changed_' . $type, TRUE),
185
    );
186
    $form['publishing'][$type]['node_export_reset_revision_timestamp_'. $type] = array(
187
      '#type' => 'checkbox',
188
      '#title' => t('Revision changed time'),
189
      '#default_value' => variable_get('node_export_reset_revision_timestamp_'. $type, TRUE),
190
    );
191
    $form['publishing'][$type]['node_export_reset_last_comment_timestamp_'. $type] = array(
192
      '#type' => 'checkbox',
193
      '#title' => t('Last comment time (date/time the last comment was made)'),
194
      '#default_value' => variable_get('node_export_reset_last_comment_timestamp_'. $type, TRUE),
195
    );
196
    $form['publishing'][$type]['node_export_reset_menu_' . $type] = array(
197
      '#type' => 'checkbox',
198
      '#title' => t('Menu link'),
199
      '#default_value' => variable_get('node_export_reset_menu_' . $type, TRUE),
200
    );
201
    $form['publishing'][$type]['node_export_reset_path_' . $type] = array(
202
      '#type' => 'checkbox',
203
      '#title' => t('URL path'),
204
      '#default_value' => variable_get('node_export_reset_path_' . $type, TRUE),
205
    );
206
    $form['publishing'][$type]['node_export_reset_book_mlid_' . $type] = array(
207
      '#type' => 'checkbox',
208
      '#title' => t('Book menu link'),
209
      '#default_value' => variable_get('node_export_reset_book_mlid_' . $type, TRUE),
210
      '#description' => t('Prevents Book node imports causing errors.'),
211
    );
212
  }
213

    
214
  $form['file'] = array(
215
    '#type' => 'fieldset',
216
    '#title' => t('File fields'),
217
  );
218

    
219
  $types = node_type_get_names();
220
  $form['file']['node_export_file_types'] = array(
221
    '#type' => 'checkboxes',
222
    '#title' => t('Files exported for content types'),
223
    '#default_value' => variable_get('node_export_file_types', array()),
224
    '#options' => $types,
225
    '#description' => t('Which content types should export file fields?'),
226
  );
227

    
228
  $textarea_delivery = $form['basic']['node_export_code']['#default_value'];
229

    
230
  $mode_message_display = ($textarea_delivery != 'file');
231

    
232
  $form['file']['node_export_file_mode'] = array(
233
    '#type' => 'radios',
234
    '#title' => t('File export mode'),
235
    '#default_value' => variable_get('node_export_file_mode', 'inline'),
236
    '#options' => array(
237
      'inline' => t('Inline Base64'),
238
      'local' => t('Local file export'),
239
      'remote' => t('Remote file export, URL')
240
     ),
241
    '#description' => t('Should file exports be inline inside the export code, a local path to the file, or a URL?  Inline Base64 is the easiest option to use but can sometimes exceed PHP post limits, local and remote modes are more useful for power users.  <em>NOTE: Remote mode only works with a public files directory.</em>'),
242
  );
243

    
244
  $form['file']['node_export_file_assets_path'] = array(
245
    '#type' => 'textfield',
246
    '#title' => t('Local file field assets path'),
247
    '#size' => 60,
248
    '#maxlength' => 255,
249
    '#default_value' => variable_get('node_export_file_assets_path', ''),
250
    '#description' => t(
251
      'Optionally, copy files to this path when the node is exported.
252
      The primary advantage of this is to divert exported files into a
253
      safe location so they can be committed to source control (eg: SVN,
254
      CVS, Git).  <em>Tip: For install profile developers, setting this
255
      path to <code>profiles/my_profile/node_export_assets</code> may be
256
      useful.</em>'
257
    ),
258
    '#required' => FALSE,
259
    '#states' => array(
260
      'visible' => array(
261
        ':input[name=node_export_file_mode]' => array('value' => 'local'),
262
      ),
263
    ),
264
  );
265

    
266
  $form['file']['node_export_file_supported_fields'] = array(
267
    '#type' => 'textfield',
268
    '#title' => t('Supported file field types'),
269
    '#default_value' => variable_get('node_export_file_supported_fields', 'file, image'),
270
    '#maxlength' => 512,
271
    '#description' => t('Comma seperated list of file field types to detect for export/import.'),
272
  );
273

    
274
  return system_settings_form($form);
275
}
276

    
277
/**
278
 *  Export GUI function.
279
 *
280
 *  @param $nodes
281
 *    A node, an array of nodes, or an array of nids.
282
 *  @param $format
283
 *    The node export format.
284
 *  @param $delivery
285
 *    The code delivery format, if NULL will use settings.
286
 *  @return
287
 *    The node export form or nothing if calling function to serve file.
288
 */
289
function node_export_gui($nodes = NULL, $format = NULL, $delivery = NULL) {
290
  // Get the $code_string.
291
  if ($nodes) {
292
    // $nodes passed in, get the code_string.
293
    $result = node_export($nodes, $format);
294
    if ($result['success']) {
295
      $code_string = $result['output'];
296
    }
297
    else {
298
      foreach ($result['output'] as $output) {
299
        drupal_set_message($output, 'error');
300
      }
301
      return;
302
    }
303
    $nids = node_export_nodes_to_nids($nodes);
304
    $format = $result['format'];
305
  }
306
  elseif (!empty($_SESSION['node_export'])) {
307
    // Nids and code string supplied from session.
308
    $session_data = array_shift($_SESSION['node_export']);
309
    $code_string = $session_data['code_string'];
310
    $nids = $session_data['nids'];
311
    $format = $session_data['format'];
312
  }
313

    
314
  $delivery = $delivery ? $delivery : variable_get('node_export_code', 'all');
315
  if ($delivery != 'file') {
316
    if (is_object($nodes)) {
317
      // Single node, output straight away.
318
      drupal_set_title(t('Node export of !title', array('!title' => $nodes->title)));
319
      return drupal_get_form('node_export_form', $nids, $code_string, $format);
320
    }
321
    elseif ($nodes) {
322
      // Node operation, add to session and redirect.
323
      $_SESSION['node_export'][] = array(
324
        'code_string' => $code_string,
325
        'nids' => $nids,
326
        'format' => $format,
327
      );
328
      drupal_goto('admin/content/node_export');
329
    }
330
    elseif (!$nodes) {
331
      // No $nodes passed, but $code_string came from session.
332
      return drupal_get_form('node_export_form', $nids, $code_string, $format);
333
    }
334
  }
335
  else {
336
    // Get file.
337
    node_export_get_file($nids, $code_string, $format);
338
  }
339
}
340

    
341
/**
342
 * Convert a node, nodes, or nids into an array of nids.
343
 */
344
function node_export_nodes_to_nids($nodes) {
345
  if (is_object($nodes)) {
346
    $nids = array($nodes->nid);
347
  }
348
  else {
349
    $nids = array();
350
    foreach ($nodes as $node) {
351
      if (is_object($node)) {
352
        $nids[] = $node->nid;
353
      }
354
      elseif (is_numeric($node)) {
355
        $nids[] = $node;
356
      }
357
    }
358
  }
359
  return $nids;
360
}
361

    
362
/**
363
 * Export form.
364
 *
365
 * @param $form
366
 *   The form array.
367
 * @param $form_state
368
 *   The form state.
369
 * @param $nids
370
 *   An array of node ids that are being exported.
371
 * @param $code_string
372
 *   The Node export code.
373
 * @param $format
374
 *   The Node export format.
375
 *
376
 * @return
377
 *   The built form array.
378
 */
379
function node_export_form($form, &$form_state, $nids, $code_string, $format) {
380
  $form = array();
381
  if (variable_get('node_export_code', 'all') == 'all') {
382
    $form['nids'] = array(
383
      '#type' => 'hidden',
384
      '#value' => $nids,
385
    );
386
    $form['format'] = array(
387
      '#type' => 'hidden',
388
      '#value' => $format,
389
    );
390
    $form['download_file'] = array(
391
      '#type' => 'submit',
392
      '#value' => t('Download file'),
393
    );
394
  }
395
  $form['export'] = array(
396
    '#type' => 'textarea',
397
    '#title' => t('Node export code'),
398
    '#default_value' => $code_string,
399
    '#rows' => 30,
400
    '#description' => t('Copy this code and then on the site you want to import to, go to the <em>Node export: import</em> link under <em>Add content</em>, and paste it in there.'),
401
    '#attributes' => array(
402
      'wrap' => 'off',
403
    ),
404
    '#wysiwyg' => FALSE,
405
  );
406
  return $form;
407
}
408

    
409
/**
410
 * Export form submit function.
411
 *
412
 * File download was requested.
413
 */
414
function node_export_form_submit($form, &$form_state) {
415
  // Get file.
416
  $nids = $form_state['values']['nids'];
417
  $code_string = $form_state['values']['export'];
418
  $format = $form_state['values']['format'];
419
  node_export_get_file($nids, $code_string, $format);
420
}
421

    
422
/**
423
 * Generate text file.
424
 *
425
 * @param $nids
426
 *   An array of node ids.
427
 * @param $code_string
428
 *   The text output.
429
 * @param $format
430
 *   The format used.
431
 */
432
function node_export_get_file($nids, $code_string, $format = NULL) {
433
  $filename_data = array();
434
  $filename_data['node-count'] = count($nids);
435
  $filename_data['timestamp'] = REQUEST_TIME;
436
  $filename_data['format'] = $format ? $format : 'export';
437

    
438
  // Add a list of nids
439
  if (count($nids) <= variable_get('node_export_file_list', 10)) {
440
    $filename_data['nid-list'] = '[' . implode(',', $nids) . ']';
441
  }
442
  $name = variable_get('node_export_filename', 'node-export[node_export_filename:nid-list]([node_export_filename:node-count]-nodes).[node_export_filename:timestamp].[node_export_filename:format]');
443
  $data = array('node_export_filename' => (object)$filename_data);
444
  $name = token_replace($name, $data);
445

    
446
  $formats = node_export_format_handlers();
447
  $format_data = $formats[$format];
448
  $mime = !empty($format_data['#mime']) ? $format_data['#mime'] : 'text/plain';
449

    
450
  header('Content-type: ' . $mime);
451
  header('Content-Disposition: attachment; filename="' . $name . '"');
452
  print($code_string);
453
  // Clean exit.
454
  module_invoke_all('exit');
455
  exit;
456
}
457

    
458
/**
459
 * Import Form.
460
 *
461
 *  @param $form
462
 *    The form array.
463
 *  @param $form_state
464
 *    The form state.
465
 *
466
 *  @return
467
 *    The built form array.
468
 */
469
function node_export_import_form($form, &$form_state) {
470

    
471
  // Initialise to prevent notices
472
  $values = array(
473
    'file' => FALSE,
474
    'code' => FALSE,
475
  );
476

    
477
  $form = array();
478
  $form['#attributes'] = array(
479
    'enctype' => "multipart/form-data",
480
  );
481
  $form['#prefix'] = "<p>";
482
  $form['#prefix'] .= t('You may import content by pasting or uploading the code exported from Node export.') . " ";
483
  $form['#prefix'] .= t("Some values may be reset during imports depending on Node export's configuration.");
484
  $form['#prefix'] .= "</p>";
485

    
486
  $form['upload'] = array(
487
    '#type' => 'fieldset',
488
    '#title' => t('Upload file'),
489
    '#collapsible' => TRUE,
490
    '#collapsed' => !$values['file'],
491
  );
492
  $form['upload']['file'] = array(
493
    '#type' => 'file',
494
    '#description' => t('To clear this field, <a href="!reset">reset the form</a>.', array('!reset' => url($_GET['q'])))
495
  );
496
  $form['paste'] = array(
497
    '#type' => 'fieldset',
498
    '#title' => t('Paste code'),
499
    '#collapsible' => TRUE,
500
    '#collapsed' => !$values['code'],
501
  );
502
  $form['paste']['code'] = array(
503
    '#type' => 'textarea',
504
    '#default_value' => '',
505
    '#rows' => 30,
506
    '#description' => t('Paste the code of a node export here.'),
507
    '#wysiwyg' => FALSE,
508
  );
509

    
510
  $form['actions'] = array('#type' => 'actions');
511
  $form['actions']['submit'] = array(
512
    '#type' => 'submit',
513
    '#value' => t('Import'),
514
  );
515
  $form['actions']['reset'] = array(
516
    '#markup' => l(t('Reset the form'), $_GET['q']),
517
  );
518

    
519
  return $form;
520
}
521

    
522
/**
523
 * Validate function for import form.
524
 */
525
function node_export_import_form_validate($form, &$form_state) {
526
  if (
527
    $form_state['clicked_button']['#id'] == 'edit-submit' &&
528
    !$_FILES['files']['name']['file'] &&
529
    !$form_state['values']['code']
530
  ) {
531
    drupal_set_message(t('Please upload a file or paste code to import.'), 'error');
532
    form_set_error('', NULL);
533
  }
534
}
535

    
536
/**
537
 * Submit function for import form.
538
 *
539
 * @todo: is there a way to get the contents of the file without using
540
 * file_save_upload()?
541
 */
542
function node_export_import_form_submit($form, &$form_state) {
543
  if ($_FILES['files']['name']['file']) {
544
    $original = $_FILES['files']['name']['file'];
545
    $save = file_save_upload('file', array('file_validate_extensions' => array()));
546
    if (!$save) {
547
      drupal_set_message(t('Error: Node export could not save file.'), 'error');
548
    }
549
    else {
550
      $save->original = $original;
551
      form_set_value($form['upload']['file'], serialize($save), $form_state);
552
    }
553
  }
554

    
555
  if ($form_state['values']['file']) {
556
    $file = unserialize($form_state['values']['file']);
557
    if (file_exists($file->uri)) {
558
      $code_string = file_get_contents($file->uri);
559
      unlink($file->uri);
560
    }
561
    file_delete($file);
562
  }
563
  elseif ($form_state['values']['code']) {
564
    $code_string = trim($form_state['values']['code']);
565
  }
566

    
567
  if (isset($code_string)) {
568
    $result = node_export_import($code_string);
569
    // Output the status or error messages.
570
    foreach ($result['output'] as $output) {
571
      drupal_set_message($output, ($result['success'] ? 'status' : 'error'));
572
    }
573
    // We need to send this user somewhere, and we know they have permission
574
    // for this page:
575
    drupal_goto('node/add/node_export');
576
  }
577

    
578
}
579

    
580