You are here

export.module in Node export 5

Same filename and directory in other branches
  1. 5.2 export.module
  2. 6 export.module

File

export.module
View source
<?php

/**
 * Implementation of hook_help().
 */
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;
  }
}

/**
* Implementation of hook_perm().
*/
function export_perm() {
  return array(
    'export node',
    'export own nodes',
    'import nodes',
  );
}

/**
 * Implementation of hook_menu().
 */
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;

  // Check basic permissions first.
  $access = user_access('export node') || $user->uid && $node->uid == $user->uid && user_access('export own nodes');

  // Check additional conditions
  $access = $access && (export_is_permitted($node->type) && filter_access($node->format) && node_access('create', $node->type));

  // Let other modules alter this - for exmple to only allow some users
  // to export specific nodes or types.
  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()));
}

/**
* menu callback to configure module settings.
*/
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),
    );
  }

  // Need the variable default key to be something that's never a valid node type.
  $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);
}

/**
 * Implementation of hook_node_type().
 */
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;
  }
}

/**
 *  Exports a node - return export code in a textarea
 */
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',
        ));

        // fill in the default values
        foreach (array(
          'status',
          'moderate',
          'promote',
          'sticky',
          'revision',
        ) as $key) {
          $node->{$key} = in_array($key, $node_options);
        }
      }

      // Let other modules do special fixing up.
      // The function signature is: hook_export_node_alter(&$node, $original_node, $method)
      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;
    }
  }
}

/**
 *  Import Node UI
 */
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);
  }
}

/**
 *  Import 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;
}

Functions

Namesort descending Description
export_access
export_help Implementation of hook_help().
export_import_form Import Form
export_is_permitted
export_menu Implementation of hook_menu().
export_node_export Exports a node - return export code in a textarea
export_node_import Import Node UI
export_node_type Implementation of hook_node_type().
export_perm Implementation of hook_perm().
export_settings menu callback to configure module settings.