View source
<?php
function export_help($section) {
switch ($section) {
case 'admin/help#export':
$output = '<p>' . t('The export module allows users to export an existing node and then import it into another Drupal installation.') . '</p>';
$output .= '<p>' . t('Users with the "export node" permission can utilize this functionality. A new tab will appear on node pages with the word "Export".') . '</p>';
return $output;
}
}
function export_perm() {
return array(
'export node',
'export own nodes',
'import nodes',
);
}
function export_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/export',
'title' => t('Export module'),
'description' => t('Allows users to export (copy then edit) an existing node.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'export_settings',
'access' => user_access('administer site configuration'),
);
}
else {
$items[] = array(
'path' => 'admin/content/import',
'title' => t('Import Node'),
'description' => t('Allows users to import a node from another site.'),
'callback' => 'export_node_import',
'access' => user_access('import nodes'),
);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ($node->nid) {
$items[] = array(
'path' => 'node/' . $node->nid . '/export',
'title' => t('Export'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'export_node_export',
$node->nid,
),
'access' => export_access($node),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
}
}
}
return $items;
}
function export_access($node) {
global $user;
$access = user_access('export node') || $user->uid && $node->uid == $user->uid && user_access('export own nodes');
$access = $access && (export_is_permitted($node->type) && filter_access($node->format) && node_access('create', $node->type));
foreach (module_implements('export_access_alter') as $module) {
$function = $module . '_export_access_alter';
$function($access, $node);
}
return $access;
}
function export_is_permitted($type) {
return !in_array($type, variable_get('export_omitted', array()));
}
function export_settings() {
$form['heading'] = array(
'#value' => '<b>' . t('Configuration options for the export module:') . '</b>',
);
$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']['export_reset_' . $type] = array(
'#type' => 'checkbox',
'#title' => t('@s: reset publishing options when exportd', array(
'@s' => $name,
)),
'#default_value' => variable_get('export_reset_' . $type, FALSE),
);
}
$types = array_merge(array(
'!' => "<" . t("none") . ">",
), $types);
$form['export_omitted'] = array(
'#type' => 'select',
'#title' => t('Omitted content types'),
'#default_value' => variable_get('export_omitted', array(
'!',
)),
'#options' => $types,
'#description' => t('Select any node types which should <em>never</em> be exportd. Typically you should will want to select here all node types for which cloning fails (e.g. image nodes).'),
'#multiple' => TRUE,
);
$form['export_method'] = array(
'#type' => 'value',
'#value' => 'prepopulate',
);
return system_settings_form($form);
}
function export_node_type($op, $type_obj) {
switch ($op) {
case 'delete':
variable_del('export_reset_' . $type_obj->type);
break;
case 'update':
if (!empty($type_obj->old_type) && $type_obj->old_type != $type_obj->type) {
if (variable_get('export_reset_' . $type_obj->old_type, FALSE)) {
variable_del('export_reset_' . $type_obj->old_type);
variable_set('export_reset_' . $type_obj->type, TRUE);
}
}
break;
}
}
function export_node_export($nid) {
if (is_numeric($nid)) {
global $user;
$node = node_load($nid);
if (isset($node->nid) && export_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 = NULL;
$node->menu = NULL;
$node->path = NULL;
$node->files = array();
$node->title = t('!title', array(
'!title' => $node->title,
));
drupal_set_title(check_plain($node->title));
if (variable_get('export_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('export_node_alter') as $module) {
$function = $module . '_export_node_alter';
$function($node, $original_node, "prepopulate");
}
$node_code = var_export($node, true);
$node_code = str_replace('stdClass::__set_state(array(', 'node(code(', $node_code);
$form['export'] = array(
'#type' => 'textarea',
'#title' => t('Node Code'),
'#default_value' => $node_code,
'#rows' => 30,
'#description' => t('Copy this code and then on the site you want to import to, go to the Import Node link under Content Management, and paste it in there.'),
);
return $form;
}
}
}
function export_node_import() {
global $user;
$output;
if ($_POST['import_code']) {
$node_code = str_replace('node(code(', 'array(array(', $_POST['import_code']);
$imp = eval("return " . $node_code . ";");
return str_replace('admin/content/import', 'node/add/' . $imp[0]['type'] . '', drupal_get_form($imp[0]['type'] . '_node_form', $imp[0]));
}
else {
return drupal_get_form('export_import_form', $form);
}
}
function export_import_form() {
$form['#description'] = t('You may import a node by cut-and-pasting the results of an export node. If the import is successful you will be taken to the Add Node page with all of the settings of the imported node.');
$form['import_code'] = array(
'#type' => 'textarea',
'#title' => t('Import Code'),
'#default_value' => '',
'#required' => TRUE,
'#description' => t('Cut and paste the results of an Export Node here.'),
);
$form['#redirect'] = FALSE;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}