-
Notifications
You must be signed in to change notification settings - Fork 0
/
processclone.pages.inc
271 lines (240 loc) · 9.42 KB
/
processclone.pages.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @file
* Additional functions for Node_Clone module.
*/
/**
* Menu callback to configure module settings.
*/
function processclone_settings($form, &$form_state) {
$form['basic'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
);
$form['basic']['processclone_method'] = array(
'#type' => 'radios',
'#title' => t('Method to use when cloning a node'),
'#options' => array('prepopulate' => t('Pre-populate the node form fields'), 'save-edit' => t('Save as a new node then edit')),
'#default_value' => variable_get('processclone_method', 'prepopulate'),
);
$form['basic']['processclone_nodes_without_confirm'] = array(
'#type' => 'radios',
'#title' => t('Confirmation mode when using the "Save as a new node then edit" method'),
'#default_value' => (int)variable_get('processclone_nodes_without_confirm', 0),
'#options' => array(t('Require confirmation (recommended)'), t('Bypass confirmation')),
'#description' => t('A new node may be saved immediately upon clicking the "clone" link when viewing a node, bypassing the normal confirmation form.'),
'#states' => array(
// Only show this field when the clone method is save-edit.
'visible' => array(
':input[name="processclone_method"]' => array('value' => 'save-edit'),
),
),
);
$form['basic']['processclone_menu_links'] = array(
'#type' => 'radios',
'#title' => t('Clone menu links'),
'#options' => array(0 => t('No'), 1 => t('Yes')),
'#default_value' => (int) variable_get('processclone_menu_links', 0),
'#description' => t('Should any menu link for a node also be cloned?'),
);
$form['basic']['processclone_use_node_type_name'] = array(
'#type' => 'checkbox',
'#title' => t('Use node type name in clone link'),
'#default_value' => (int) variable_get('processclone_use_node_type_name', 0),
'#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".'),
);
$form['publishing'] = array(
'#type' => 'fieldset',
'#title' => t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'),
);
$types = node_type_get_names();
foreach ($types as $type => $name) {
$form['publishing']['processclone_reset_' . $type] = array(
'#type' => 'checkbox',
'#title' => t('@s: reset publishing options when cloned', array('@s' => $name)),
'#default_value' => variable_get('processclone_reset_' . $type, FALSE),
);
}
// Need the variable default key to be something that's never a valid node type.
$form['omit'] = array(
'#type' => 'fieldset',
'#title' => t('Content types that are not to be cloned - omitted due to incompatibility'),
);
$form['omit']['processclone_omitted'] = array(
'#type' => 'checkboxes',
'#title' => t('Omitted content types'),
'#default_value' => variable_get('processclone_omitted', array()),
'#options' => $types,
'#description' => t('Select any node types which should <em>never</em> be cloned. In other words, all node types where cloning will fail.'),
);
return system_settings_form($form);
}
/**
* Menu callback: prompt the user to confirm the operation
*/
function processclone_node_check($node) {
$method = variable_get('processclone_method', 'prepopulate');
switch ($method) {
case 'save-edit':
if (variable_get('processclone_nodes_without_confirm', FALSE)) {
$new_nid = processclone_node_save($node->nid);
$options = array();
if (!empty($_GET['process-clone-destination'])) {
$options['query']['destination'] = $_GET['process-clone-destination'];
}
drupal_goto('node/' . $new_nid . '/edit', $options);
}
else {
return drupal_get_form('processclone_node_confirm', $node);
}
break;
case 'prepopulate':
default:
return processclone_node_prepopulate($node);
break;
}
}
/**
* form builder: prompt the user to confirm the operation
*/
function processclone_node_confirm($form, &$form_state, $node) {
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
return confirm_form($form,
t('Are you sure you want to clone %title?', array('%title' => $node->title)),
'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>',
t('Clone'), t('Cancel'));
}
/**
* Handle confirm form submission
*/
function processclone_node_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$new_nid = processclone_node_save($form_state['values']['nid']);
}
$form_state['redirect'] = 'node/' . $new_nid . '/edit';
}
/**
* Create a new menu link cloned from another node.
*
* Returns NULL if no existing link, or links are not to be cloned.
*/
function processclone_node_processclone_menu_link($node) {
if (variable_get('processclone_menu_links', FALSE) && function_exists('menu_node_prepare')) {
// This will fetch the existing menu link if the node had one.
menu_node_prepare($node);
if (!empty($node->menu['mlid'])) {
$old_link = $node->menu;
$link['link_title'] = t('Clone of !title', array('!title' => $old_link['link_title']));
$link['plid'] = $old_link['plid'];
$link['menu_name'] = $old_link['menu_name'];
$link['weight'] = $old_link['weight'];
$link['module'] = $old_link['module'];
// Use the value -1 because it casts to boolean TRUE in function
// menu_form_node_form_alter() in menu.module so the node form checkbox
// is selected, but in function menu_link_save() no existing link will
// match.
$link['mlid'] = -1;
$link['has_children'] = 0;
$link['hidden'] = $old_link['hidden'];
$link['customized'] = $old_link['customized'];
$link['options'] = $old_link['options'];
$link['expanded'] = $old_link['expanded'];
$link['description'] = $old_link['description'];
$link['language'] = isset($old_link['language']) ? $old_link['language'] : NULL;
// This is needed to get the link saved in function menu_node_save() when
// using the save-edit method.
$link['enabled'] = TRUE;
return $link;
}
}
return NULL;
}
/**
* Clones a node - prepopulate a node editing form
*/
function processclone_node_prepopulate($original_node) {
if (isset($original_node->nid)) {
if (processclone_is_permitted($original_node->type)) {
global $user;
if (!isset($user->uid)) {
// go with the default rule to allow for the node to be saved with a null
$account = null;
}
else {
// use the current user
$account = $user;
}
$node = _processclone_node_prepare($original_node, TRUE, $account);
drupal_set_title($node->title);
// Let other modules do special fixing up.
$context = array('method' => 'prepopulate', 'original_node' => $original_node);
drupal_alter('processclone_node', $node, $context);
// Make sure the file defining the node form is loaded.
$form_state = array();
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
return drupal_build_form($node->type .'_node_form', $form_state);
}
}
}
/**
* Clones a node by directly saving it.
*/
function processclone_node_save($nid, $account = NULL) {
if (is_numeric($nid)) {
$original_node = node_load($nid);
if (isset($original_node->nid) && processclone_is_permitted($original_node->type)) {
$node = _processclone_node_prepare($original_node, TRUE, $account);
// Let other modules do special fixing up.
$context = array('method' => 'save-edit', 'original_node' => $original_node);
drupal_alter('processclone_node', $node, $context);
node_save($node);
if (module_exists('rules')) {
rules_invoke_event('processclone_node', $node, $original_node);
}
return $node->nid;
}
}
}
/**
* Prepares a node to be cloned.
*/
function _processclone_node_prepare($original_node, $prefix_title = FALSE, $account = NULL) {
$node = clone $original_node;
if (!isset($account->uid)) {
$account = $GLOBALS['user'];
}
$node->nid = NULL;
$node->vid = NULL;
$node->tnid = NULL;
$node->log = NULL;
// Also handle modules that attach a UUID to the node.
$node->uuid = NULL;
$node->vuuid = NULL;
// Anyonmymous users don't have a name.
// @see: drupal_anonymous_user().
$node->name = isset($account->name) ? $account->name : NULL;
$node->uid = $account->uid;
$node->created = NULL;
$node->menu = processclone_node_processclone_menu_link($original_node);
if (isset($node->book['mlid'])) {
$node->book['mlid'] = NULL;
$node->book['has_children'] = 0;
}
$node->path = NULL;
$node->files = array();
if ($prefix_title) {
$node->title = t('Clone of !title', array('!title' => $node->title));
}
// Add an extra property as a flag.
$node->processclone_from_original_nid = $original_node->nid;
if (variable_get('processclone_reset_'. $node->type, FALSE)) {
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
// Fill in the default values.
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) {
// Cast to int since that's how they need to be saved to the DB.
$node->$key = (int) in_array($key, $node_options);
}
}
return $node;
}