clone.pages.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @file
  4. * Additional functions for Node_Clone module.
  5. */
  6. /**
  7. * Menu callback to configure module settings.
  8. */
  9. function clone_settings($form, &$form_state) {
  10. $form['basic'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t('General settings'),
  13. );
  14. $form['basic']['clone_method'] = array(
  15. '#type' => 'radios',
  16. '#title' => t('Method to use when cloning a node'),
  17. '#options' => array('prepopulate' => t('Pre-populate the node form fields'), 'save-edit' => t('Save as a new node then edit')),
  18. '#default_value' => variable_get('clone_method', 'prepopulate'),
  19. );
  20. $form['basic']['clone_nodes_without_confirm'] = array(
  21. '#type' => 'radios',
  22. '#title' => t('Confirmation mode when using the "Save as a new node then edit" method'),
  23. '#default_value' => (int)variable_get('clone_nodes_without_confirm', 0),
  24. '#options' => array(t('Require confirmation (recommended)'), t('Bypass confirmation')),
  25. '#description' => t('A new node may be saved immediately upon clicking the "clone" link when viewing a node, bypassing the normal confirmation form.'),
  26. );
  27. $form['basic']['clone_menu_links'] = array(
  28. '#type' => 'radios',
  29. '#title' => t('Clone menu links'),
  30. '#options' => array(0 => t('No'), 1 => t('Yes')),
  31. '#default_value' => (int) variable_get('clone_menu_links', 0),
  32. '#description' => t('Should any menu link for a node also be cloned?'),
  33. );
  34. $form['basic']['clone_use_node_type_name'] = array(
  35. '#type' => 'checkbox',
  36. '#title' => t('Use node type name in clone link'),
  37. '#default_value' => (int) variable_get('clone_use_node_type_name', 0),
  38. '#description' => t('If checked, the link to clone the node will contain the node type name, for example, "Clone this article", otherwise it will read "Clone content".'),
  39. );
  40. $form['publishing'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'),
  43. );
  44. $types = node_type_get_names();
  45. foreach ($types as $type => $name) {
  46. $form['publishing']['clone_reset_' . $type] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('@s: reset publishing options when cloned', array('@s' => $name)),
  49. '#default_value' => variable_get('clone_reset_' . $type, FALSE),
  50. );
  51. }
  52. // Need the variable default key to be something that's never a valid node type.
  53. $form['omit'] = array(
  54. '#type' => 'fieldset',
  55. '#title' => t('Content types that are not to be cloned - omitted due to incompatibility'),
  56. );
  57. $form['omit']['clone_omitted'] = array(
  58. '#type' => 'checkboxes',
  59. '#title' => t('Omitted content types'),
  60. '#default_value' => variable_get('clone_omitted', array()),
  61. '#options' => $types,
  62. '#description' => t('Select any node types which should <em>never</em> be cloned. In other words, all node types where cloning will fail.'),
  63. );
  64. return system_settings_form($form);
  65. }
  66. /**
  67. * Menu callback: prompt the user to confirm the operation
  68. */
  69. function clone_node_check($node) {
  70. $method = variable_get('clone_method', 'prepopulate');
  71. switch ($method) {
  72. case 'save-edit':
  73. if (variable_get('clone_nodes_without_confirm', FALSE)) {
  74. $new_nid = clone_node_save($node->nid);
  75. $options = array();
  76. if (!empty($_GET['node-clone-destination'])) {
  77. $options['query']['destination'] = $_GET['node-clone-destination'];
  78. }
  79. drupal_goto('node/' . $new_nid . '/edit', $options);
  80. }
  81. else {
  82. return drupal_get_form('clone_node_confirm', $node);
  83. }
  84. break;
  85. case 'prepopulate':
  86. default:
  87. return clone_node_prepopulate($node);
  88. break;
  89. }
  90. }
  91. /**
  92. * form builder: prompt the user to confirm the operation
  93. */
  94. function clone_node_confirm($form, &$form_state, $node) {
  95. $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
  96. return confirm_form($form,
  97. t('Are you sure you want to clone %title?', array('%title' => $node->title)),
  98. 'node/' . $node->nid, '<p>' . t('This action will create a new node. You will then be redirected to the edit page for the new node.') . '</p>',
  99. t('Clone'), t('Cancel'));
  100. }
  101. /**
  102. * Handle confirm form submission
  103. */
  104. function clone_node_confirm_submit($form, &$form_state) {
  105. if ($form_state['values']['confirm']) {
  106. $new_nid = clone_node_save($form_state['values']['nid']);
  107. }
  108. $form_state['redirect'] = 'node/' . $new_nid . '/edit';
  109. }
  110. /**
  111. * Create a new menu link cloned from another node.
  112. *
  113. * Returns NULL if no existing link, or links are not to be cloned.
  114. */
  115. function clone_node_clone_menu_link($node) {
  116. if (variable_get('clone_menu_links', FALSE) && function_exists('menu_node_prepare')) {
  117. // This will fetch the existing menu link if the node had one.
  118. menu_node_prepare($node);
  119. if (!empty($node->menu['mlid'])) {
  120. $old_link = $node->menu;
  121. $link['link_title'] = t('Clone of !title', array('!title' => $old_link['link_title']));
  122. $link['plid'] = $old_link['plid'];
  123. $link['menu_name'] = $old_link['menu_name'];
  124. $link['weight'] = $old_link['weight'];
  125. $link['module'] = $old_link['module'];
  126. return $link;
  127. }
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * Clones a node - prepopulate a node editing form
  133. */
  134. function clone_node_prepopulate($original_node) {
  135. if (isset($original_node->nid)) {
  136. if (clone_is_permitted($original_node->type)) {
  137. $node = _clone_node_prepare($original_node, TRUE);
  138. drupal_set_title($node->title);
  139. // Let other modules do special fixing up.
  140. $context = array('method' => 'prepopulate', 'original_node' => $original_node);
  141. drupal_alter('clone_node', $node, $context);
  142. // Make sure the file defining the node form is loaded.
  143. $form_state = array();
  144. $form_state['build_info']['args'] = array($node);
  145. form_load_include($form_state, 'inc', 'node', 'node.pages');
  146. return drupal_build_form($node->type .'_node_form', $form_state);
  147. }
  148. }
  149. }
  150. /**
  151. * Clones a node by directly saving it.
  152. */
  153. function clone_node_save($nid, $account = NULL) {
  154. if (is_numeric($nid)) {
  155. $original_node = node_load($nid);
  156. if (isset($original_node->nid) && clone_is_permitted($original_node->type)) {
  157. $node = _clone_node_prepare($original_node, TRUE, $account);
  158. // Let other modules do special fixing up.
  159. $context = array('method' => 'save-edit', 'original_node' => $original_node);
  160. drupal_alter('clone_node', $node, $context);
  161. node_save($node);
  162. if (module_exists('rules')) {
  163. rules_invoke_event('clone_node', $node, $original_node);
  164. }
  165. return $node->nid;
  166. }
  167. }
  168. }
  169. /**
  170. * Prepares a node to be cloned.
  171. */
  172. function _clone_node_prepare($original_node, $prefix_title = FALSE, $account = NULL) {
  173. $node = clone $original_node;
  174. if (!isset($account->uid)) {
  175. $account = $GLOBALS['user'];
  176. }
  177. $node->nid = NULL;
  178. $node->vid = NULL;
  179. $node->tnid = NULL;
  180. $node->log = NULL;
  181. // Also handle modules that attach a UUID to the node.
  182. $node->uuid = NULL;
  183. $node->vuuid = NULL;
  184. // Anyonmymous users don't have a name.
  185. // @see: drupal_anonymous_user().
  186. $node->name = isset($account->name) ? $account->name : NULL;
  187. $node->uid = $account->uid;
  188. $node->created = NULL;
  189. $node->menu = clone_node_clone_menu_link($original_node);
  190. if (isset($node->book['mlid'])) {
  191. $node->book['mlid'] = NULL;
  192. $node->book['has_children'] = 0;
  193. }
  194. $node->path = NULL;
  195. $node->files = array();
  196. if ($prefix_title) {
  197. $node->title = t('Clone of !title', array('!title' => $node->title));
  198. }
  199. // Add an extra property as a flag.
  200. $node->clone_from_original_nid = $original_node->nid;
  201. if (variable_get('clone_reset_'. $node->type, FALSE)) {
  202. $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
  203. // Fill in the default values.
  204. foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) {
  205. // Cast to int since that's how they need to be saved to the DB.
  206. $node->$key = (int) in_array($key, $node_options);
  207. }
  208. }
  209. return $node;
  210. }