You are here

node_import.module in Node import 6

Same filename and directory in other branches
  1. 5 node_import.module

Let users import content from CSV or TSV file.

File

node_import.module
View source
<?php

/**
 * @file
 * Let users import content from CSV or TSV file.
 */

/**
 * Implementation of hook_perm().
 */
function node_import_perm() {
  return array(
    'import content',
    'administer imports',
  );
}

/**
 * Implementation of hook_menu().
 */
function node_import_menu() {
  $items = array();
  $items['admin/content/node_import'] = array(
    'title' => 'Import content',
    'description' => 'Import content from a <abbr title="Comma Separated Values">CSV</abbr> or <abbr title="Tab Separated Values">TSV</abbr> file.',
    'access callback' => 'user_access',
    'access arguments' => array(
      'import content',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_list_tasks_form',
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/list'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'title' => 'List',
  );
  $items['admin/content/node_import/list/tasks'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'title' => 'Tasks',
  );
  $items['admin/content/node_import/list/files'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Files',
    'access callback' => 'user_access',
    'access arguments' => array(
      'import content',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_list_files_form',
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/add'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'New import',
    'access callback' => 'user_access',
    'access arguments' => array(
      'import content',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_add_form',
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/settings'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Settings',
    'weight' => 10,
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer imports',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_settings_form',
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/%node_import'] = array(
    'type' => MENU_CALLBACK,
    'title' => 'Importing...',
    'access callback' => 'node_import_access',
    'access arguments' => array(
      3,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_view_form',
      3,
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/%node_import/view'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'title' => 'View',
    'weight' => -10,
  );
  $items['admin/content/node_import/%node_import/continue'] = array(
    'type' => MENU_CALLBACK,
    'title' => 'Continue import',
    'access callback' => 'node_import_access',
    'access arguments' => array(
      3,
    ),
    'page callback' => 'node_import_js',
    'page arguments' => array(
      3,
    ),
    'file' => 'node_import.inc',
  );
  $items['admin/content/node_import/%node_import/debug'] = array(
    'type' => MENU_CALLBACK,
    'title' => 'Debug report',
    'access callback' => 'node_import_access',
    'access arguments' => array(
      3,
    ),
    'page callback' => 'node_import_debug_report',
    'page arguments' => array(
      3,
    ),
    'file' => 'node_import.admin.inc',
  );
  $items['admin/content/node_import/%node_import/delete'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Delete',
    'weight' => 10,
    'access callback' => 'node_import_access',
    'access arguments' => array(
      3,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_import_delete_form',
      3,
    ),
    'file' => 'node_import.admin.inc',
  );

  //TODO: %node_import/clone ? %node_import/edit ?
  return $items;
}

/**
 * Load an import task by id. Used in hook_menu().
 *
 * @param $taskid
 *   Integer.
 *
 * @return
 *   FALSE or the task object.
 *
 * @ingroup node_import_tasks
 */
function node_import_load($taskid) {
  if (isset($taskid)) {
    $values = db_fetch_array(db_query("SELECT * FROM {node_import_tasks} WHERE taskid = %d", $taskid));
    if ($values !== FALSE) {
      foreach (array(
        'file_options',
        'headers',
        'map',
        'defaults',
        'options',
      ) as $key) {
        $values[$key] = unserialize($values[$key]);
      }
      $values['file'] = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $values['fid']));
      module_invoke_all('node_import_task', $values, 'load');
      return $values;
    }
  }
  return FALSE;
}

/**
 * Check access to an import task. Used in hook_menu().
 *
 * @param $task
 *   Array.
 *
 * @return
 *   TRUE if the current user can access the task.
 *
 * @ingroup node_import_tasks
 */
function node_import_access($task) {
  global $user;
  if (user_access('administer imports') || $task['uid'] == $user->uid) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_cron().
 */
function node_import_cron() {
  module_load_include('inc', 'node_import');
  $unit = variable_get('node_import:cron:unit', 'rows');
  $count = variable_get('node_import:cron:count', 25);
  node_import_do_all_tasks($unit, $count);
}

/**
 * Implementation of hook_theme().
 */
function node_import_theme() {
  return array(
    'node_import_file_select' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'node_import.admin.inc',
    ),
    'node_import_imports_list' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'node_import.admin.inc',
    ),
    'node_import_file_format_select' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'node_import.admin.inc',
    ),
    'node_import_sample_data' => array(
      'arguments' => array(
        'data' => NULL,
      ),
      'file' => 'node_import.admin.inc',
    ),
    'node_import_field_table' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'node_import.admin.inc',
    ),
  );
}

Functions

Namesort descending Description
node_import_access Check access to an import task. Used in hook_menu().
node_import_cron Implementation of hook_cron().
node_import_load Load an import task by id. Used in hook_menu().
node_import_menu Implementation of hook_menu().
node_import_perm Implementation of hook_perm().
node_import_theme Implementation of hook_theme().