| 1 |
<?php
|
| 2 |
// $Id: clone.pages.inc,v 1.2 2008/05/04 00:23:21 pwolanin Exp $
|
| 3 |
// $Name: $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Menu callback to configure module settings.
|
| 7 |
*/
|
| 8 |
function clone_settings() {
|
| 9 |
|
| 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" tab when viewing a node, bypassing the normal confirmation form.'),
|
| 26 |
);
|
| 27 |
|
| 28 |
$form['publishing'] = array(
|
| 29 |
'#type' => 'fieldset',
|
| 30 |
'#title' => t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'),
|
| 31 |
);
|
| 32 |
$types = node_get_types('names');
|
| 33 |
|
| 34 |
foreach ($types as $type => $name) {
|
| 35 |
$form['publishing']['clone_reset_'. $type] = array(
|
| 36 |
'#type' => 'checkbox',
|
| 37 |
'#title' => t('@s: reset publishing options when cloned', array('@s' => $name)),
|
| 38 |
'#default_value' => variable_get('clone_reset_'. $type, FALSE),
|
| 39 |
);
|
| 40 |
}
|
| 41 |
// Need the variable default key to be something that's never a valid node type.
|
| 42 |
$form['omit'] = array(
|
| 43 |
'#type' => 'fieldset',
|
| 44 |
'#title' => t('Content types that are not to be cloned - omitted due to incompatibility'),
|
| 45 |
);
|
| 46 |
$form['omit']['clone_omitted'] = array(
|
| 47 |
'#type' => 'checkboxes',
|
| 48 |
'#title' => t('Omitted content types'),
|
| 49 |
'#default_value' => variable_get('clone_omitted', array()),
|
| 50 |
'#options' => $types,
|
| 51 |
'#description' => t('Select any node types which should <em>never</em> be cloned. Typically you should will want to select here all node types for which cloning fails (e.g. image nodes).'),
|
| 52 |
);
|
| 53 |
|
| 54 |
return system_settings_form($form);
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Menu callback: prompt the user to confirm the operation
|
| 59 |
*/
|
| 60 |
function clone_node_check($node) {
|
| 61 |
|
| 62 |
$method = variable_get('clone_method', 'prepopulate');
|
| 63 |
|
| 64 |
switch ($method) {
|
| 65 |
case 'save-edit':
|
| 66 |
if (variable_get('clone_nodes_without_confirm', FALSE)) {
|
| 67 |
$new_nid = clone_node_save($node->nid);
|
| 68 |
drupal_goto('node/'. $new_nid .'/edit');
|
| 69 |
}
|
| 70 |
else {
|
| 71 |
return drupal_get_form('clone_node_confirm', $node);
|
| 72 |
}
|
| 73 |
break;
|
| 74 |
case 'prepopulate':
|
| 75 |
default:
|
| 76 |
include_once(drupal_get_path('module', 'node') .'/node.pages.inc');
|
| 77 |
return clone_node_prepopulate($node);
|
| 78 |
break;
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* form builder: prompt the user to confirm the operation
|
| 84 |
*/
|
| 85 |
function clone_node_confirm($form_state, $node) {
|
| 86 |
|
| 87 |
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
| 88 |
return confirm_form($form,
|
| 89 |
t('Are you sure you want to clone %title?', array('%title' => $node->title)),
|
| 90 |
'node/'. $node->nid, '<p>'. t('This action will create a new node. You willl then be redirected to the edit page for the new node.') .'</p>',
|
| 91 |
t('Clone'), t('Cancel'));
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Handle confirm form submission
|
| 96 |
*/
|
| 97 |
function clone_node_confirm_submit($form, &$form_state) {
|
| 98 |
if ($form_state['values']['confirm']) {
|
| 99 |
$new_nid = clone_node_save($form_state['values']['nid']);
|
| 100 |
}
|
| 101 |
$form_state['redirect'] = 'node/'. $new_nid .'/edit';
|
| 102 |
}
|
| 103 |
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Clones a node - prepopulate a node editing form
|
| 107 |
*/
|
| 108 |
function clone_node_prepopulate($original_node) {
|
| 109 |
if (isset($original_node->nid)) {
|
| 110 |
global $user;
|
| 111 |
|
| 112 |
if (clone_is_permitted($original_node->type)) {
|
| 113 |
$node = drupal_clone($original_node);
|
| 114 |
|
| 115 |
$node->nid = NULL;
|
| 116 |
$node->vid = NULL;
|
| 117 |
$node->name = $user->name;
|
| 118 |
$node->uid = $user->uid;
|
| 119 |
$node->created = NULL;
|
| 120 |
$node->menu = NULL;
|
| 121 |
if (isset($node->book['mlid'])) {
|
| 122 |
$node->book['mlid'] = NULL;
|
| 123 |
}
|
| 124 |
$node->path = NULL;
|
| 125 |
$node->files = array();
|
| 126 |
$node->title = t('Clone of !title', array('!title' => $node->title));
|
| 127 |
drupal_set_title(check_plain($node->title));
|
| 128 |
|
| 129 |
if (variable_get('clone_reset_'. $node->type, FALSE)) {
|
| 130 |
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
|
| 131 |
// Fill in the default values.
|
| 132 |
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) {
|
| 133 |
$node->$key = in_array($key, $node_options);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
// Let other modules do special fixing up.
|
| 137 |
// The function signature is: hook_clone_node_alter(&$node, $original_node, $method)
|
| 138 |
// Where $method is either 'prepopulate' or 'save-edit'.
|
| 139 |
drupal_alter("clone_node", $node, $original_node, "prepopulate");
|
| 140 |
|
| 141 |
return drupal_get_form($node->type .'_node_form', $node);
|
| 142 |
}
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Clones a node by directly saving it.
|
| 148 |
*/
|
| 149 |
function clone_node_save($nid) {
|
| 150 |
if (is_numeric($nid)) {
|
| 151 |
global $user;
|
| 152 |
|
| 153 |
$node = node_load($nid);
|
| 154 |
if (isset($node->nid) && clone_is_permitted($node->type)) {
|
| 155 |
$original_node = drupal_clone($node);
|
| 156 |
|
| 157 |
$node->nid = NULL;
|
| 158 |
$node->vid = NULL;
|
| 159 |
$node->name = $user->name;
|
| 160 |
$node->uid = $user->uid;
|
| 161 |
$node->created = NULL;
|
| 162 |
$node->menu = NULL;
|
| 163 |
$node->book['mlid'] = NULL;
|
| 164 |
$node->path = NULL;
|
| 165 |
$node->files = array();
|
| 166 |
$node->title = t('Clone of !title', array('!title' => $node->title));
|
| 167 |
|
| 168 |
if (variable_get('clone_reset_'. $node->type, FALSE)) {
|
| 169 |
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
|
| 170 |
// Fill in the default values.
|
| 171 |
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) {
|
| 172 |
$node->$key = in_array($key, $node_options);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
// Let other modules do special fixing up.
|
| 176 |
// The function signature is: hook_clone_node_alter(&$node, $original_node, $method)
|
| 177 |
// Where $method is either 'prepopulate' or 'save-edit'.
|
| 178 |
drupal_alter("clone_node", $node, $original_node, "save-edit");
|
| 179 |
|
| 180 |
node_save($node);
|
| 181 |
return $node->nid;
|
| 182 |
}
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|