/[drupal]/contributions/modules/import/import.module
ViewVC logotype

Contents of /contributions/modules/import/import.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (show annotations) (download) (as text)
Wed Apr 8 23:33:18 2009 UTC (7 months, 3 weeks ago) by cyberswat
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.19: +26 -12 lines
File MIME type: text/x-php
Removed the cron hooks to allow for menu options and selective firing of importing different types.  Created a node example module
1 <?php
2 // $Id: import.module,v 1.19 2009/04/08 21:42:11 cyberswat Exp $
3
4 /**
5 * invokes hook_import()
6 */
7 function import_stage_import() {
8 module_invoke_all('import_stage');
9 return;
10 }
11 /**
12 * invokes hook_import_process() and contains a context variable for use with
13 * the batch api
14 *
15 * @param $data
16 * An array containing the following keys that correspond to data staged in
17 * the {import} table:
18 * - impid
19 * - type
20 *
21 * @param $context
22 * The context array used for batch processing
23 *
24 */
25 function import_process_import($data, &$context) {
26 module_invoke_all('import_process',$data);
27 $context['message'] = t('Processing import') .' '. $data->type .' '. $data->impid;
28 return;
29 }
30 /*
31 * Implementation of hook_menu().
32 */
33 function import_menu() {
34 $items = array();
35 $items['admin/build/import'] = array(
36 'title' => 'Import',
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('import_admin_form'),
39 'type' => MENU_NORMAL_ITEM,
40 'access arguments' => array('administer nodes'),
41 );
42 return $items;
43 }
44
45 /**
46 * Stages content into {import}
47 *
48 * @param $args
49 * An array that accepts $args['impid'] and $args['type']
50 */
51 function import_stage($args) {
52 if(array_key_exists('impid',$args) && array_key_exists('type',$args)) {
53 db_query("INSERT INTO {import} VALUES('%s', '%s')", $args['impid'], $args['type']);
54 } else {
55 drupal_set_message(t("There was an error staging your data. Please provide an array with impid and type keys."));
56 }
57 return;
58 }
59
60 /**
61 * A function menat to be called by modules implementing the Import API to
62 * indicate success of an imported item.
63 *
64 * @param $data
65 * An array containing impid and type so that we know what we are working with.
66 * @param $msg
67 * A string that serves as an optional message to associate with succesful import.
68 * @todo implement message
69 */
70 function import_pass($data, $msg='') {
71 db_query("INSERT INTO {import_pass} VALUES('%s', '%s')", $data->impid, $data->type);
72 db_query("DELETE from {import} where impid = '%s' AND type = '%s'", $data->impid, $data->type);
73 }
74
75 /**
76 * A function menat to be called by modules implementing the Import API to
77 * indicate failure of an imported item.
78 *
79 * @param $data
80 * An array containing impid and type so that we know what we are working with.
81 * @param $msg
82 * A string that serves as an optional message to associate with succesful import.
83 */
84 function import_fail($data, $msg='') {
85 db_query("INSERT INTO {import_fail} VALUES('%s','%s','%s')", $data->impid, $data->type, $msg);
86 db_query("DELETE from {import} where impid = '%s' AND type = '%s'", $data->impid, $data->type);
87 }
88
89 /**
90 * Generates a form containing the stage and process buttons for admins to use.
91 */
92 function import_admin_form() {
93 $form['stage'] = array(
94 '#type' => 'button',
95 '#value' => t('Stage Data for Import'),
96 '#executes_submit_callback' => TRUE
97 );
98
99 // only display the import buttons if there is something staged for import
100 $import = db_query("SELECT DISTINCT type from {import}");
101 $types = array();
102 while ($row = db_fetch_object($import)) {
103 $types[] = $row->type;
104 }
105 if (count($types) > 0) {
106 foreach ($types as $type) {
107 $form['process_'. $type] = array(
108 '#type' => 'button',
109 '#value' => t('Process') .' '. $type,
110 '#executes_submit_callback' => TRUE
111 );
112 }
113 }
114 return $form;
115 }
116
117 /**
118 * Submit handler for the import_admin_form
119 */
120 function import_admin_form_submit($form, &$form_state) {
121
122 if ($form_state['clicked_button']['#value'] == t('Stage Data for Import')) {
123 import_stage_import();
124 $import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import}"));
125 drupal_set_message(t('%total items are staged for Import', array('%total' => $import->total)));
126 }
127 else {
128 $type_array = explode(' ', $form_state['clicked_button']['#value']);
129 $type = array_pop($type_array);
130 $import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import} WHERE type = '%s'", $type));
131 if($import->total > 0) {
132 $operations = array();
133 $result = db_query("Select impid, type from {import} WHERE type = '%s'", $type);
134 while ($row = db_fetch_object($result)) {
135 $operations[] = array('import_process_import', array($row));
136 }
137 $batch = array(
138 'operations' => $operations,
139 'title' => t('Importing'),
140 'init_message' => t('Import is starting to process %total %type items', array('%total' => $import->total, '%type' => $type)),
141 'progress_message' => t('Processed @current out of @total.'),
142 'error_message' => t('Import has encountered an error.'),
143 );
144
145 batch_set($batch);
146 }
147 else {
148 drupal_set_message(t('%total items are staged for Import', array('%total' => $import->total)));
149 }
150 }
151 }

  ViewVC Help
Powered by ViewVC 1.1.2