clone.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * @file
  4. * Allow users to make a copy of an item of content (a node) and then edit that copy.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function clone_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#clone':
  12. $output = '<p>' . t('The clone module allows users to make a copy of an existing node and then edit that copy. The authorship is set to the current user, the menu and url aliases are reset, and the words "Clone of" are inserted into the title to remind you that you are not editing the original node.') . '</p>';
  13. $output .= '<p>' . t('Users with the "clone node" permission can utilize this functionality. A new tab will appear on node pages with the word "Clone".') . '</p>';
  14. return $output;
  15. case 'node/%/clone':
  16. $method = variable_get('clone_method', 'prepopulate');
  17. if ($method == 'prepopulate') {
  18. return t('This clone will not be saved to the database until you submit.');
  19. }
  20. }
  21. }
  22. /**
  23. * Implementation of hook_permission().
  24. */
  25. function clone_permission() {
  26. return array(
  27. 'clone node' => array('title' => t('Clone any node')),
  28. 'clone own nodes' => array('title' => t('Clone own nodes.')),
  29. );
  30. }
  31. /**
  32. * Implementation of hook_menu().
  33. */
  34. function clone_menu() {
  35. $items['admin/config/content/clone'] = array(
  36. 'access arguments' => array('administer site configuration'),
  37. 'page callback' => 'drupal_get_form',
  38. 'page arguments' => array('clone_settings'),
  39. 'title' => 'Node clone module',
  40. 'file' => 'clone.pages.inc',
  41. 'description' => 'Allows users to clone (copy then edit) an existing node.',
  42. );
  43. $items['node/%node/clone/%clone_token'] = array(
  44. 'access callback' => 'clone_access_cloning',
  45. 'access arguments' => array(1, TRUE, 3),
  46. 'page callback' => 'clone_node_check',
  47. 'page arguments' => array(1),
  48. 'title' => 'Clone content',
  49. 'title callback' => 'clone_action_link_title',
  50. 'title arguments' => array(1),
  51. 'weight' => 5,
  52. 'file' => 'clone.pages.inc',
  53. 'type' => MENU_LOCAL_ACTION,
  54. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  55. );
  56. return $items;
  57. }
  58. function clone_token_to_arg($arg = NULL, $map = NULL, $index = NULL) {
  59. // Supply CSRF token if needed.
  60. if (variable_get('clone_nodes_without_confirm', FALSE)) {
  61. return drupal_get_token('clone_access_cloning');
  62. }
  63. return 'confirm';
  64. }
  65. function clone_access_cloning($node, $check_token = FALSE, $token = FALSE) {
  66. global $user;
  67. // Check CSRF token if needed.
  68. if ($check_token) {
  69. if (!$token || $token !== clone_token_to_arg()) {
  70. return FALSE;
  71. }
  72. }
  73. // Check basic permissions first.
  74. $access = clone_is_permitted($node->type) && (user_access('clone node') || ($user->uid && ($node->uid == $user->uid) && user_access('clone own nodes')));
  75. // Make sure the user can view the original node content, and create a new one..
  76. $access = $access && node_access('view', $node) && node_access('create', $node->type);
  77. // Let other modules alter this.
  78. drupal_alter("clone_access", $access, $node);
  79. return $access;
  80. }
  81. function clone_is_permitted($type) {
  82. $omitted = variable_get('clone_omitted', array());
  83. return empty($omitted[$type]);
  84. }
  85. /**
  86. * Menu title callback.
  87. */
  88. function clone_action_link_title($node) {
  89. // A hack to present a shorter title in contextual links.
  90. if (current_path() != 'node/' . $node->nid) {
  91. return t('Clone');
  92. }
  93. if (variable_get('clone_use_node_type_name', 0)) {
  94. return t('Clone this !type', array('!type' => drupal_strtolower(node_type_get_name($node))));
  95. }
  96. return t('Clone content');
  97. }
  98. /**
  99. * Implementation of hook_node_type_delete().
  100. */
  101. function clone_node_type_delete($info) {
  102. variable_del('clone_reset_' . $info->type);
  103. }
  104. /**
  105. * Implementation of hook_node_type_update().
  106. */
  107. function clone_node_type_update($info) {
  108. if (!empty($info->old_type) && $info->old_type != $info->type) {
  109. if (variable_get('clone_reset_' . $info->old_type, FALSE)) {
  110. variable_del('clone_reset_' . $info->old_type);
  111. variable_set('clone_reset_' . $info->type, TRUE);
  112. }
  113. }
  114. }
  115. /**
  116. * Implements hook_views_api.
  117. */
  118. function clone_views_api() {
  119. return array(
  120. 'api' => 3,
  121. 'path' => drupal_get_path('module', 'clone') . '/views',
  122. );
  123. }
  124. /**
  125. * Implementation of hook_admin_paths().
  126. */
  127. function clone_admin_paths() {
  128. if (variable_get('node_admin_theme')) {
  129. $paths = array(
  130. 'node/*/clone/*' => TRUE,
  131. );
  132. return $paths;
  133. }
  134. }
  135. /**
  136. * Implements hook_form_BASE_FORM_ID_alter().
  137. */
  138. function clone_form_node_form_alter(&$form, $form_state, $form_id) {
  139. // Add the clone_from_original_nid value for node forms triggered by cloning.
  140. // This will make sure the clone_from_original_nid property is still
  141. // attached to the node when passing through hook_node_insert().
  142. if (!empty($form['#node']->clone_from_original_nid)) {
  143. $form['clone_from_original_nid'] = array(
  144. '#type' => 'value',
  145. '#value' => $form['#node']->clone_from_original_nid,
  146. );
  147. }
  148. }
  149. /**
  150. * Implements hook_form_FORM_ID_alter().
  151. */
  152. function clone_form_node_admin_content_alter(&$form, $form_state, $form_id) {
  153. $destination = drupal_get_destination();
  154. // The property attribute changes in the $form array depending on the user role.
  155. $property = isset($form['admin']['nodes']['#options']) ? '#options' : '#rows';
  156. if (empty($form['admin']['nodes'][$property])) {
  157. return;
  158. }
  159. // Expose a Clone operation on each node.
  160. foreach ($form['admin']['nodes'][$property] as $nid => &$row) {
  161. $node = node_load($nid);
  162. if (clone_access_cloning($node)) {
  163. $row['operations']['data']['#links']['clone'] = array(
  164. 'title' => t('clone'),
  165. 'href' => 'node/' . $nid . '/clone/' . clone_token_to_arg(),
  166. 'query' => $destination,
  167. );
  168. }
  169. }
  170. }
  171. /**
  172. * Implements hook_action_info().
  173. */
  174. function clone_action_info() {
  175. return array(
  176. 'clone_action_clone' => array(
  177. 'type' => 'node',
  178. 'label' => t('Clone item'),
  179. 'configurable' => TRUE,
  180. 'hooks' => array('any' => TRUE),
  181. 'triggers' => array('any'),
  182. ),
  183. );
  184. }
  185. /**
  186. * Action callback.
  187. */
  188. function clone_action_clone($original_node, $context) {
  189. module_load_include('inc', 'clone', 'clone.pages');
  190. if (clone_is_permitted($original_node->type)) {
  191. $val = !empty($context['clone_context']) ? $context['clone_context'] : array();
  192. $node = _clone_node_prepare($original_node, !empty($val['prefix_title']));
  193. if (isset($val['substitute_from']) && strlen($val['substitute_from']) && isset($val['substitute_to'])) {
  194. $i = (!empty($val['substitute_case_insensitive']) ? 'i' : '');
  195. $pattern = '#'. strtr($val['substitute_from'], array('#' => '\#')) . '#' . $i;
  196. foreach (array('title') as $property) {
  197. $new = preg_replace($pattern, $val['substitute_to'], $node->{$property});
  198. if ($new) {
  199. $node->{$property} = $new;
  200. }
  201. }
  202. foreach (array('body') as $property) {
  203. foreach($node->{$property} as $lang => $row) {
  204. foreach ($row as $delta => $data) {
  205. foreach (array('value', 'summary') as $key) {
  206. if (isset($node->{$property}[$lang][$delta][$key])) {
  207. $new = preg_replace($pattern, $val['substitute_to'], $node->{$property}[$lang][$delta][$key]);
  208. if ($new) {
  209. $node->{$property}[$lang][$delta][$key] = $new;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. // Let other modules do special fixing up.
  218. $context = array('method' => 'action', 'original_node' => $original_node);
  219. drupal_alter('clone_node', $node, $context);
  220. node_save($node);
  221. if (module_exists('rules')) {
  222. rules_invoke_event('clone_node', $node, $original_node);
  223. }
  224. }
  225. else {
  226. drupal_set_message(t('Clone failed for %title : not permitted for nodes of type %type', array('%title' => $original_node->title, '%type' => $original_node->type)), 'warning');
  227. }
  228. }
  229. /**
  230. * Action form.
  231. */
  232. function clone_action_clone_form($context) {
  233. $form['clone_context'] = array(
  234. '#tree' => TRUE,
  235. );
  236. $form['clone_context']['prefix_title'] = array(
  237. '#title' => t('Prefix title'),
  238. '#type' => 'checkbox',
  239. '#description' => t('Should cloned node tiles be prefixed?'),
  240. '#default_value' => isset($context['clone_context']['prefix_title']) ? $context['clone_context']['prefix_title'] : 1,
  241. );
  242. $form['clone_context']['substitute_from'] = array(
  243. '#title' => t('Substitute from string'),
  244. '#type' => 'textfield',
  245. '#description' => t('String (or regex) to substitute from in title and body.'),
  246. '#default_value' => isset($context['clone_context']['substitue_from']) ? $context['clone_context']['substitue_from'] : '',
  247. );
  248. $form['clone_context']['substitute_to'] = array(
  249. '#title' => t('Substitute to string'),
  250. '#type' => 'textfield',
  251. '#description' => t('String (or regex) to substitute to in title and body.'),
  252. '#default_value' => isset($context['clone_context']['substitue_to']) ? $context['clone_context']['substitue_to'] : '',
  253. );
  254. $form['clone_context']['substitute_case_insensitive'] = array(
  255. '#title' => t('Case insensitive substitution'),
  256. '#type' => 'checkbox',
  257. '#description' => t('Should the substituion match be case insensitive?'),
  258. '#default_value' => isset($context['clone_context']['substitute_case_insensitive']) ? $context['clone_context']['substitute_case_insensitive'] : NULL,
  259. );
  260. return $form;
  261. }
  262. /**
  263. * Action form submit.
  264. */
  265. function clone_action_clone_submit($form, $form_state) {
  266. return array('clone_context' => $form_state['values']['clone_context']);
  267. }