View source
<?php
function clone_help($section) {
switch ($section) {
case 'admin/help#clone':
$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>';
$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". Once you click this tab you have <em>already</em> created a new node that is a copy of the node you were viewing, and you will be redirected to an edit screen for that new node.') . '</p>';
return $output;
}
}
function clone_perm() {
return array(
'clone node',
'clone own nodes',
);
}
function clone_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/clone',
'title' => t('Clone module'),
'description' => t('Allows users to clone (copy then edit) an existing node.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'clone_settings',
'access' => user_access('administer site configuration'),
);
}
else {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ($node->nid) {
$items[] = array(
'path' => 'node/' . $node->nid . '/clone',
'title' => t('Clone'),
'callback' => 'clone_node_check',
'access' => clone_access_cloning($node),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
}
}
}
return $items;
}
function clone_access_cloning($node) {
global $user;
$access = user_access('clone node') || $user->uid && $node->uid == $user->uid && user_access('clone own nodes');
$access = $access && node_access('view', $node);
$access = $access && (clone_is_permitted($node->type) && filter_access($node->format) && node_access('create', $node->type));
foreach (module_implements('clone_access_alter') as $module) {
$function = $module . '_clone_access_alter';
$function($access, $node);
}
return $access;
}
function clone_is_permitted($type) {
$omitted = variable_get('clone_omitted', array());
return empty($omitted[$type]);
}
function clone_settings() {
$form['heading'] = array(
'#value' => '<b>' . t('Configuration options for the clone module:') . '</b>',
);
$form['clone_nodes_without_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Clone nodes without requiring confirmation'),
'#default_value' => variable_get('clone_nodes_without_confirm', FALSE),
'#description' => t('If this is set, a new node will be created immediately upon clicking the "clone" tab when viewing a node.'),
);
$form['publishing'] = array(
'#type' => 'fieldset',
'#title' => t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'),
);
$types = node_get_types('names');
foreach ($types as $type => $name) {
$form['publishing']['clone_reset_' . $type] = array(
'#type' => 'checkbox',
'#title' => t('@s: reset publishing options when cloned', array(
'@s' => $name,
)),
'#default_value' => variable_get('clone_reset_' . $type, FALSE),
);
}
$types = array_merge(array(
'!' => "<" . t("none") . ">",
), $types);
$form['clone_omitted'] = array(
'#type' => 'select',
'#title' => t('Omitted content types'),
'#default_value' => variable_get('clone_omitted', array(
'!',
)),
'#options' => $types,
'#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).'),
'#multiple' => TRUE,
);
$form['clone_method'] = array(
'#type' => 'value',
'#value' => 'save-edit',
);
return system_settings_form($form);
}
function clone_node_type($op, $type_obj) {
switch ($op) {
case 'delete':
variable_del('clone_reset_' . $type_obj->type);
break;
case 'update':
if (!empty($type_obj->old_type) && $type_obj->old_type != $type_obj->type) {
if (variable_get('clone_reset_' . $type_obj->old_type, FALSE)) {
variable_del('clone_reset_' . $type_obj->old_type);
variable_set('clone_reset_' . $type_obj->type, TRUE);
}
}
break;
}
}
function clone_node_check() {
if (variable_get('clone_nodes_without_confirm', FALSE)) {
clone_node(arg(1));
return;
}
return drupal_get_form('clone_node_confirm');
}
function clone_node_confirm() {
$node = node_load(arg(1));
$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'));
}
function clone_node_confirm_submit($form_id, $form_values) {
if ($form_values['confirm']) {
clone_node($form_values['nid']);
}
return '';
}
function clone_node($nid) {
if (is_numeric($nid)) {
global $user;
$node = node_load($nid);
if (isset($node->nid) && clone_is_permitted($node->type)) {
$original_node = drupal_clone($node);
$node->nid = NULL;
$node->vid = NULL;
$node->name = $user->name;
$node->uid = $user->uid;
$node->created = 0;
$node->menu = NULL;
$node->path = NULL;
$node->files = array();
if (module_exists('imagefield') || module_exists('filefield')) {
$content_type = module_invoke('content', 'types', $node->type);
foreach ($content_type['fields'] as $data) {
if ($data['type'] == 'file' || $data['type'] == 'image') {
$key = $data['field_name'];
if (isset($node->{$key})) {
$node->{$key} = array();
}
}
}
}
$node->title = t('Clone of !title', array(
'!title' => $node->title,
));
$node->clone_from_original_nid = $original_node->nid;
if (variable_get('clone_reset_' . $node->type, FALSE)) {
$node_options = variable_get('node_options_' . $node->type, array(
'status',
'promote',
));
foreach (array(
'status',
'moderate',
'promote',
'sticky',
'revision',
) as $key) {
$node->{$key} = in_array($key, $node_options);
}
}
foreach (module_implements('clone_node_alter') as $module) {
$function = $module . '_clone_node_alter';
$function($node, $original_node, "save-edit");
}
node_save($node);
drupal_goto('node/' . $node->nid . '/edit');
}
}
}