node_export.pages.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. /**
  3. * @file
  4. * The Node export pages file.
  5. *
  6. * Functions for page/form interfaces.
  7. */
  8. /**
  9. * Handles the bits of the form that are specific to the token module.
  10. */
  11. function node_export_settings_token_bits(&$form, $key) {
  12. if (module_exists('token')) {
  13. $form[$key . '_token_help'] = array(
  14. '#title' => t('Replacement patterns'),
  15. '#type' => 'fieldset',
  16. '#collapsible' => TRUE,
  17. '#collapsed' => TRUE,
  18. );
  19. $form[$key . '_token_help']['help'] = array(
  20. '#theme' => 'token_tree',
  21. '#token_types' => array('node_export_filename'),
  22. );
  23. }
  24. else {
  25. $form[$key]['#description'] = t(
  26. 'Get the <a href="@token">token</a> module for more options.',
  27. array('@token' => url('http://www.drupal.org/project/token'))
  28. );
  29. }
  30. }
  31. /**
  32. * Menu callback to configure module settings.
  33. */
  34. function node_export_settings($form, &$form_state) {
  35. $types = node_type_get_names();
  36. menu_rebuild();
  37. $form['basic'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('General settings'),
  40. );
  41. $format_handlers = node_export_format_handlers();
  42. $format_options = array();
  43. foreach ($format_handlers as $format_handler => $format) {
  44. $display = $format['#title'];
  45. if (!empty($format['#description'])) {
  46. $display .= '<div><small>';
  47. $display .= $format['#description'];
  48. $display .= '</small></div>';
  49. }
  50. $format_options[$format_handler] = $display;
  51. $format_settings = array();
  52. if (!empty($format['#settings_callback'])) {
  53. if (!empty($format['#file']) && is_file($format['#file'])) {
  54. require_once $format['#file'];
  55. }
  56. $format_form = call_user_func($format['#settings_callback'], $form, $form_state);
  57. $format_settings = $format_settings + $format_form;
  58. }
  59. }
  60. $selected_formats = variable_get('node_export_format', array('drupal'));
  61. if (!count(array_filter($selected_formats))) {
  62. $selected_formats = array('drupal');
  63. }
  64. if (count($format_options) > 1) {
  65. $form['basic']['node_export_format'] = array(
  66. '#type' => 'checkboxes',
  67. '#title' => t('Format to use when exporting a node'),
  68. '#default_value' => $selected_formats,
  69. '#options' => $format_options,
  70. '#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."),
  71. );
  72. }
  73. else {
  74. $format = key($format_options);
  75. $form['basic']['node_export_format'] = array(
  76. '#type' => 'value',
  77. '#value' => array($format => $format),
  78. );
  79. }
  80. $form['basic'][] = $format_settings;
  81. $form['basic']['node_export_code'] = array(
  82. '#type' => 'radios',
  83. '#title' => t('Node export code delivery'),
  84. '#options' => array(
  85. 'all' => t('All of the below options on a page'),
  86. 'copy' => t('Textarea filled with export code'),
  87. 'file' => t('Text file download'),
  88. ),
  89. '#default_value' => variable_get('node_export_code', 'all'),
  90. );
  91. $form['basic']['filename'] = array(
  92. '#type' => 'fieldset',
  93. '#title' => t('Filename settings'),
  94. '#collapsible' => TRUE,
  95. '#collapsed' => TRUE,
  96. );
  97. $form['basic']['filename']['node_export_filename'] = array(
  98. '#type' => 'textarea',
  99. '#title' => t('Filename pattern'),
  100. '#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]'),
  101. '#rows' => 1,
  102. '#wysiwyg' => FALSE,
  103. );
  104. node_export_settings_token_bits($form['basic']['filename'], 'node_export_filename');
  105. $form['basic']['filename']['node_export_file_list'] = array(
  106. '#type' => 'textfield',
  107. '#title' => t('Node ID list max'),
  108. '#default_value' => variable_get('node_export_file_list', 10),
  109. '#size' => 6,
  110. '#maxlength' => 30,
  111. '#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.'),
  112. );
  113. $form['basic']['node_export_existing'] = array(
  114. '#type' => 'radios',
  115. '#title' => t('When importing a node that already exists'),
  116. '#options' => array(
  117. 'new' => t('Create a new node'),
  118. 'revision' => t('Create a new revision of the existing node'),
  119. 'skip' => t('Skip the node'),
  120. ),
  121. '#description' => t('UUIDs are used to uniquely identify nodes.'),
  122. '#default_value' => variable_get('node_export_existing', 'new'),
  123. );
  124. $form['publishing'] = array(
  125. '#type' => 'fieldset',
  126. '#title' => t('Reset values on import'),
  127. );
  128. foreach ($types as $type => $name) {
  129. $form['publishing'][$type] = array(
  130. '#type' => 'fieldset',
  131. '#title' => $name,
  132. '#description' => t('Reset these values when importing nodes of type @s.', array('@s' => $name)),
  133. '#collapsible' => TRUE,
  134. '#collapsed' => TRUE,
  135. );
  136. $form['publishing'][$type]['node_export_reset_status_' . $type] = array(
  137. '#type' => 'checkbox',
  138. '#title' => t('Published'),
  139. '#default_value' => variable_get('node_export_reset_status_' . $type, FALSE),
  140. '#description' => t('Set to unpublished'),
  141. );
  142. $form['publishing'][$type]['node_export_reset_promote_' . $type] = array(
  143. '#type' => 'checkbox',
  144. '#title' => t('Promoted to front page'),
  145. '#default_value' => variable_get('node_export_reset_promote_' . $type, FALSE),
  146. '#description' => t('Set to not promoted'),
  147. );
  148. $form['publishing'][$type]['node_export_reset_sticky_' . $type] = array(
  149. '#type' => 'checkbox',
  150. '#title' => t('Sticky at top of lists'),
  151. '#default_value' => variable_get('node_export_reset_sticky_' . $type, FALSE),
  152. '#description' => t('Set to not sticky'),
  153. );
  154. $form['publishing'][$type]['node_export_reset_author_' . $type] = array(
  155. '#type' => 'checkbox',
  156. '#title' => t('Author (will set to user performing the import)'),
  157. '#default_value' => variable_get('node_export_reset_author_' . $type, TRUE),
  158. '#description' => t('Recommended since User IDs could be different on import site and the wrong user may gain permission to edit the node.'),
  159. );
  160. $form['publishing'][$type]['node_export_reset_created_' . $type] = array(
  161. '#type' => 'checkbox',
  162. '#title' => t('Created time (<em>Authored on</em> date/time)'),
  163. '#default_value' => variable_get('node_export_reset_created_' . $type, TRUE),
  164. );
  165. $form['publishing'][$type]['node_export_reset_changed_' . $type] = array(
  166. '#type' => 'checkbox',
  167. '#title' => t('Changed time (<em>Last updated</em> date/time)'),
  168. '#default_value' => variable_get('node_export_reset_changed_' . $type, TRUE),
  169. );
  170. $form['publishing'][$type]['node_export_reset_revision_timestamp_'. $type] = array(
  171. '#type' => 'checkbox',
  172. '#title' => t('Revision changed time'),
  173. '#default_value' => variable_get('node_export_reset_revision_timestamp_'. $type, TRUE),
  174. );
  175. $form['publishing'][$type]['node_export_reset_last_comment_timestamp_'. $type] = array(
  176. '#type' => 'checkbox',
  177. '#title' => t('Last comment time (date/time the last comment was made)'),
  178. '#default_value' => variable_get('node_export_reset_last_comment_timestamp_'. $type, TRUE),
  179. );
  180. $form['publishing'][$type]['node_export_reset_menu_' . $type] = array(
  181. '#type' => 'checkbox',
  182. '#title' => t('Menu link'),
  183. '#default_value' => variable_get('node_export_reset_menu_' . $type, TRUE),
  184. );
  185. $form['publishing'][$type]['node_export_reset_path_' . $type] = array(
  186. '#type' => 'checkbox',
  187. '#title' => t('URL path'),
  188. '#default_value' => variable_get('node_export_reset_path_' . $type, TRUE),
  189. );
  190. $form['publishing'][$type]['node_export_reset_book_mlid_' . $type] = array(
  191. '#type' => 'checkbox',
  192. '#title' => t('Book menu link'),
  193. '#default_value' => variable_get('node_export_reset_book_mlid_' . $type, TRUE),
  194. '#description' => t('Prevents Book node imports causing errors.'),
  195. );
  196. }
  197. $form['file'] = array(
  198. '#type' => 'fieldset',
  199. '#title' => t('File fields'),
  200. );
  201. $types = node_type_get_names();
  202. $form['file']['node_export_file_types'] = array(
  203. '#type' => 'checkboxes',
  204. '#title' => t('Files exported for content types'),
  205. '#default_value' => variable_get('node_export_file_types', array()),
  206. '#options' => $types,
  207. '#description' => t('Which content types should export file fields?'),
  208. );
  209. $textarea_delivery = $form['basic']['node_export_code']['#default_value'];
  210. $mode_message_display = ($textarea_delivery != 'file');
  211. $form['file']['node_export_file_mode'] = array(
  212. '#type' => 'radios',
  213. '#title' => t('File export mode'),
  214. '#default_value' => variable_get('node_export_file_mode', 'inline'),
  215. '#options' => array(
  216. 'inline' => t('Inline Base64'),
  217. 'local' => t('Local file export'),
  218. 'remote' => t('Remote file export, URL')
  219. ),
  220. '#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>'),
  221. );
  222. $form['file']['node_export_file_assets_path'] = array(
  223. '#type' => 'textfield',
  224. '#title' => t('Local file field assets path'),
  225. '#size' => 60,
  226. '#maxlength' => 255,
  227. '#default_value' => variable_get('node_export_file_assets_path', ''),
  228. '#description' => t(
  229. 'Optionally, copy files to this path when the node is exported.
  230. The primary advantage of this is to divert exported files into a
  231. safe location so they can be committed to source control (eg: SVN,
  232. CVS, Git). <em>Tip: For install profile developers, setting this
  233. path to <code>profiles/my_profile/node_export_assets</code> may be
  234. useful.</em>'
  235. ),
  236. '#required' => FALSE,
  237. '#states' => array(
  238. 'visible' => array(
  239. ':input[name=node_export_file_mode]' => array('value' => 'local'),
  240. ),
  241. ),
  242. );
  243. $form['file']['node_export_file_supported_fields'] = array(
  244. '#type' => 'textfield',
  245. '#title' => t('Supported file field types'),
  246. '#default_value' => variable_get('node_export_file_supported_fields', 'file, image'),
  247. '#maxlength' => 512,
  248. '#description' => t('Comma seperated list of file field types to detect for export/import.'),
  249. );
  250. return system_settings_form($form);
  251. }
  252. /**
  253. * Export GUI function.
  254. *
  255. * @param $nodes
  256. * A node, an array of nodes, or an array of nids.
  257. * @param $format
  258. * The node export format.
  259. * @param $delivery
  260. * The code delivery format, if NULL will use settings.
  261. * @return
  262. * The node export form or nothing if calling function to serve file.
  263. */
  264. function node_export_gui($nodes = NULL, $format = NULL, $delivery = NULL) {
  265. // Get the $code_string.
  266. if ($nodes) {
  267. // $nodes passed in, get the code_string.
  268. $result = node_export($nodes, $format);
  269. if ($result['success']) {
  270. $code_string = $result['output'];
  271. }
  272. else {
  273. foreach ($result['output'] as $output) {
  274. drupal_set_message($output, 'error');
  275. }
  276. return;
  277. }
  278. $nids = node_export_nodes_to_nids($nodes);
  279. $format = $result['format'];
  280. }
  281. elseif (!empty($_SESSION['node_export'])) {
  282. // Nids and code string supplied from session.
  283. $session_data = array_shift($_SESSION['node_export']);
  284. $code_string = $session_data['code_string'];
  285. $nids = $session_data['nids'];
  286. $format = $session_data['format'];
  287. }
  288. $delivery = $delivery ? $delivery : variable_get('node_export_code', 'all');
  289. if ($delivery != 'file') {
  290. if (is_object($nodes)) {
  291. // Single node, output straight away.
  292. drupal_set_title(t('Node export of !title', array('!title' => $nodes->title)));
  293. return drupal_get_form('node_export_form', $nids, $code_string, $format);
  294. }
  295. elseif ($nodes) {
  296. // Node operation, add to session and redirect.
  297. $_SESSION['node_export'][] = array(
  298. 'code_string' => $code_string,
  299. 'nids' => $nids,
  300. 'format' => $format,
  301. );
  302. drupal_goto('admin/content/node_export');
  303. }
  304. elseif (!$nodes) {
  305. // No $nodes passed, but $code_string came from session.
  306. return drupal_get_form('node_export_form', $nids, $code_string, $format);
  307. }
  308. }
  309. else {
  310. // Get file.
  311. node_export_get_file($nids, $code_string, $format);
  312. }
  313. }
  314. /**
  315. * Convert a node, nodes, or nids into an array of nids.
  316. */
  317. function node_export_nodes_to_nids($nodes) {
  318. if (is_object($nodes)) {
  319. $nids = array($nodes->nid);
  320. }
  321. else {
  322. $nids = array();
  323. foreach ($nodes as $node) {
  324. if (is_object($node)) {
  325. $nids[] = $node->nid;
  326. }
  327. elseif (is_numeric($node)) {
  328. $nids[] = $node;
  329. }
  330. }
  331. }
  332. return $nids;
  333. }
  334. /**
  335. * Export form.
  336. *
  337. * @param $form
  338. * The form array.
  339. * @param $form_state
  340. * The form state.
  341. * @param $nids
  342. * An array of node ids that are being exported.
  343. * @param $code_string
  344. * The Node export code.
  345. * @param $format
  346. * The Node export format.
  347. *
  348. * @return
  349. * The built form array.
  350. */
  351. function node_export_form($form, &$form_state, $nids, $code_string, $format) {
  352. $form = array();
  353. if (variable_get('node_export_code', 'all') == 'all') {
  354. $form['nids'] = array(
  355. '#type' => 'hidden',
  356. '#value' => $nids,
  357. );
  358. $form['format'] = array(
  359. '#type' => 'hidden',
  360. '#value' => $format,
  361. );
  362. $form['download_file'] = array(
  363. '#type' => 'submit',
  364. '#value' => t('Download file'),
  365. );
  366. }
  367. $form['export'] = array(
  368. '#type' => 'textarea',
  369. '#title' => t('Node export code'),
  370. '#default_value' => $code_string,
  371. '#rows' => 30,
  372. '#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.'),
  373. '#attributes' => array(
  374. 'wrap' => 'off',
  375. ),
  376. '#wysiwyg' => FALSE,
  377. );
  378. return $form;
  379. }
  380. /**
  381. * Export form submit function.
  382. *
  383. * File download was requested.
  384. */
  385. function node_export_form_submit($form, &$form_state) {
  386. // Get file.
  387. $nids = $form_state['values']['nids'];
  388. $code_string = $form_state['values']['export'];
  389. $format = $form_state['values']['format'];
  390. node_export_get_file($nids, $code_string, $format);
  391. }
  392. /**
  393. * Generate text file.
  394. *
  395. * @param $nids
  396. * An array of node ids.
  397. * @param $code_string
  398. * The text output.
  399. * @param $format
  400. * The format used.
  401. */
  402. function node_export_get_file($nids, $code_string, $format = NULL) {
  403. $filename_data = array();
  404. $filename_data['node-count'] = count($nids);
  405. $filename_data['timestamp'] = REQUEST_TIME;
  406. $filename_data['format'] = $format ? $format : 'export';
  407. // Add a list of nids
  408. if (count($nids) <= variable_get('node_export_file_list', 10)) {
  409. $filename_data['nid-list'] = '[' . implode(',', $nids) . ']';
  410. }
  411. $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]');
  412. $data = array('node_export_filename' => (object)$filename_data);
  413. $name = token_replace($name, $data);
  414. $formats = node_export_format_handlers();
  415. $format_data = $formats[$format];
  416. $mime = !empty($format_data['#mime']) ? $format_data['#mime'] : 'text/plain';
  417. header('Content-type: ' . $mime);
  418. header('Content-Disposition: attachment; filename="' . $name . '"');
  419. print($code_string);
  420. // Clean exit.
  421. module_invoke_all('exit');
  422. exit;
  423. }
  424. /**
  425. * Import Form.
  426. *
  427. * @param $form
  428. * The form array.
  429. * @param $form_state
  430. * The form state.
  431. *
  432. * @return
  433. * The built form array.
  434. */
  435. function node_export_import_form($form, &$form_state) {
  436. // Initialise to prevent notices
  437. $values = array(
  438. 'file' => FALSE,
  439. 'code' => FALSE,
  440. );
  441. $form = array();
  442. $form['#attributes'] = array(
  443. 'enctype' => "multipart/form-data",
  444. );
  445. $form['#prefix'] = "<p>";
  446. $form['#prefix'] .= t('You may import content by pasting or uploading the code exported from Node export.') . " ";
  447. $form['#prefix'] .= t("Some values may be reset during imports depending on Node export's configuration.");
  448. $form['#prefix'] .= "</p>";
  449. $form['upload'] = array(
  450. '#type' => 'fieldset',
  451. '#title' => t('Upload file'),
  452. '#collapsible' => TRUE,
  453. '#collapsed' => !$values['file'],
  454. );
  455. $form['upload']['file'] = array(
  456. '#type' => 'file',
  457. '#description' => t('To clear this field, <a href="!reset">reset the form</a>.', array('!reset' => url($_GET['q'])))
  458. );
  459. $form['paste'] = array(
  460. '#type' => 'fieldset',
  461. '#title' => t('Paste code'),
  462. '#collapsible' => TRUE,
  463. '#collapsed' => !$values['code'],
  464. );
  465. $form['paste']['code'] = array(
  466. '#type' => 'textarea',
  467. '#default_value' => '',
  468. '#rows' => 30,
  469. '#description' => t('Paste the code of a node export here.'),
  470. '#wysiwyg' => FALSE,
  471. );
  472. $form['actions'] = array('#type' => 'actions');
  473. $form['actions']['submit'] = array(
  474. '#type' => 'submit',
  475. '#value' => t('Import'),
  476. );
  477. $form['actions']['reset'] = array(
  478. '#markup' => l(t('Reset the form'), $_GET['q']),
  479. );
  480. return $form;
  481. }
  482. /**
  483. * Validate function for import form.
  484. */
  485. function node_export_import_form_validate($form, &$form_state) {
  486. if (
  487. $form_state['clicked_button']['#id'] == 'edit-submit' &&
  488. !$_FILES['files']['name']['file'] &&
  489. !$form_state['values']['code']
  490. ) {
  491. drupal_set_message(t('Please upload a file or paste code to import.'), 'error');
  492. form_set_error('', NULL);
  493. }
  494. }
  495. /**
  496. * Submit function for import form.
  497. *
  498. * @todo: is there a way to get the contents of the file without using
  499. * file_save_upload()?
  500. */
  501. function node_export_import_form_submit($form, &$form_state) {
  502. if ($_FILES['files']['name']['file']) {
  503. $original = $_FILES['files']['name']['file'];
  504. $save = file_save_upload('file', array('file_validate_extensions' => array()));
  505. if (!$save) {
  506. drupal_set_message(t('Error: Node export could not save file.'), 'error');
  507. }
  508. else {
  509. $save->original = $original;
  510. form_set_value($form['upload']['file'], serialize($save), $form_state);
  511. }
  512. }
  513. if ($form_state['values']['file']) {
  514. $file = unserialize($form_state['values']['file']);
  515. if (file_exists($file->uri)) {
  516. $code_string = file_get_contents($file->uri);
  517. unlink($file->uri);
  518. }
  519. file_delete($file);
  520. }
  521. elseif ($form_state['values']['code']) {
  522. $code_string = trim($form_state['values']['code']);
  523. }
  524. if (isset($code_string)) {
  525. $result = node_export_import($code_string);
  526. // Output the status or error messages.
  527. foreach ($result['output'] as $output) {
  528. drupal_set_message($output, ($result['success'] ? 'status' : 'error'));
  529. }
  530. // We need to send this user somewhere, and we know they have permission
  531. // for this page:
  532. drupal_goto('node/add/node_export');
  533. }
  534. }