View source
<?php
function import_stage_import() {
module_invoke_all('import_stage');
return;
}
function import_process_import($data, &$context) {
module_invoke_all('import_process', $data);
$context['message'] = t('Processing import') . ' ' . $data->type . ' ' . $data->impid;
return;
}
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;
}
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;
}
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);
}
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);
}
function import_admin_form() {
$form['stage'] = array(
'#type' => 'button',
'#value' => t('Stage Data for Import'),
'#executes_submit_callback' => TRUE,
);
$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;
}
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,
)));
}
}
}