You are here

import.module in Import 6

Same filename and directory in other branches
  1. 8.2 import.module
  2. 7 import.module

File

import.module
View source
<?php

/**
 * invokes hook_import()
 */
function import_stage_import() {
  module_invoke_all('import_stage');
  return;
}

/**
 * invokes hook_import_process() and contains a context variable for use with
 * the batch api
 *
 * @param $data
 *   An array containing the following keys that correspond to data staged in
 *   the {import} table:
 *    - impid
 *    - type
 *
 * @param $context
 *   The context array used for batch processing
 *  
 */
function import_process_import($data, &$context) {
  module_invoke_all('import_process', $data);
  $context['message'] = t('Processing import') . ' ' . $data->type . ' ' . $data->impid;
  return;
}

/*
 * Implementation of hook_menu().
 */
function import_menu() {
  $items = array();
  $items['admin/build/import'] = array(
    'title' => 'Import',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'import_admin_form',
    ),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'administer nodes',
    ),
  );
  return $items;
}

/**
 * Stages content into {import}
 *
 * @param $args
 *   An array that accepts $args['impid'] and $args['type']
 */
function import_stage($args) {
  if (array_key_exists('impid', $args) && array_key_exists('type', $args)) {
    db_query("INSERT INTO {import} VALUES('%s', '%s')", $args['impid'], $args['type']);
  }
  else {
    drupal_set_message(t("There was an error staging your data.  Please provide an array with impid and type keys."));
  }
  return;
}

/**
 * A function menat to be called by modules implementing the Import API to
 * indicate success of an imported item.
 *
 * @param $data
 *   An array containing impid and type so that we know what we are working with.
 * @param $msg
 *   A string that serves as an optional message to associate with succesful import.
 * @todo implement message
 */
function import_pass($data, $msg = '') {
  db_query("INSERT INTO {import_pass} VALUES('%s', '%s')", $data->impid, $data->type);
  db_query("DELETE from {import} where impid = '%s' AND type = '%s'", $data->impid, $data->type);
}

/**
 * A function menat to be called by modules implementing the Import API to
 * indicate failure of an imported item.
 *
 * @param $data
 *   An array containing impid and type so that we know what we are working with.
 * @param $msg
 *   A string that serves as an optional message to associate with succesful import.
 */
function import_fail($data, $msg = '') {
  db_query("INSERT INTO {import_fail} VALUES('%s','%s','%s')", $data->impid, $data->type, $msg);
  db_query("DELETE from {import} where impid = '%s' AND type = '%s'", $data->impid, $data->type);
}

/**
 * Generates a form containing the stage and process buttons for admins to use.
 */
function import_admin_form() {
  $form['stage'] = array(
    '#type' => 'button',
    '#value' => t('Stage Data for Import'),
    '#executes_submit_callback' => TRUE,
  );

  // only display the import buttons if there is something staged for import
  $import = db_query("SELECT DISTINCT type from {import}");
  $types = array();
  while ($row = db_fetch_object($import)) {
    $types[] = $row->type;
  }
  if (count($types) > 0) {
    foreach ($types as $type) {
      $form['process_' . $type] = array(
        '#type' => 'button',
        '#value' => t('Process') . ' ' . $type,
        '#executes_submit_callback' => TRUE,
      );
    }
  }
  return $form;
}

/**
 * Submit handler for the import_admin_form
 */
function import_admin_form_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#value'] == t('Stage Data for Import')) {
    import_stage_import();
    $import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import}"));
    drupal_set_message(t('%total items are staged for Import', array(
      '%total' => $import->total,
    )));
  }
  else {
    $type_array = explode(' ', $form_state['clicked_button']['#value']);
    $type = array_pop($type_array);
    $import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import} WHERE type = '%s'", $type));
    if ($import->total > 0) {
      $operations = array();
      $result = db_query("Select impid, type from {import} WHERE type = '%s'", $type);
      while ($row = db_fetch_object($result)) {
        $operations[] = array(
          'import_process_import',
          array(
            $row,
          ),
        );
      }
      $batch = array(
        'operations' => $operations,
        'title' => t('Importing'),
        'init_message' => t('Import is starting to process %total %type items', array(
          '%total' => $import->total,
          '%type' => $type,
        )),
        'progress_message' => t('Processed @current out of @total.'),
        'error_message' => t('Import has encountered an error.'),
      );
      batch_set($batch);
    }
    else {
      drupal_set_message(t('%total items are staged for Import', array(
        '%total' => $import->total,
      )));
    }
  }
}

Functions

Namesort descending Description
import_admin_form Generates a form containing the stage and process buttons for admins to use.
import_admin_form_submit Submit handler for the import_admin_form
import_fail A function menat to be called by modules implementing the Import API to indicate failure of an imported item.
import_menu
import_pass A function menat to be called by modules implementing the Import API to indicate success of an imported item.
import_process_import invokes hook_import_process() and contains a context variable for use with the batch api
import_stage Stages content into {import}
import_stage_import invokes hook_import()