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

Diff of /contributions/modules/bookimport/bookimport.module

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

revision 1.2, Mon Nov 28 08:24:21 2005 UTC revision 1.3, Fri Jan 27 06:45:17 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: */  // $Id: $
3    
4    /**
5     * @file
6     * Enables import of books from dxml (Drupal Book XML) format files
7     *
8     * Copyright (C) 2006 Djun M. Kim
9     *
10     * This program is free software; you can redistribute it and/or
11     * modify it under the terms of the GNU General Public License as
12     * published by the Free Software Foundation; either version 2 of the
13     * License, or (at your option) any later version.
14     *
15     * See the GNU General Public License version 2 LICENSE file for
16     * full terms and conditions of use.
17     *
18     */
19    
20  /**  /**
21   * Implementation of hook_menu().   * Implementation of hook_menu().
# Line 56  function bookimport_page() { Line 72  function bookimport_page() {
72      print theme('page', $output);      print theme('page', $output);
73  }  }
74    
 /**  
  * Returns a form_radios group of radio buttons for selecting the  
  * import action.  
  */  
75  function _bookimport_form_radios_action() {  function _bookimport_form_radios_action() {
76    return form_radios(t("Action"),      return
77                       'action',          array(
78                       $_POST['edit']['action'],                '#type' => 'radios',
79                       array(t("Update an existing book"),                '#title' => t("Action"),
80                             t("Create a new book")                '#default_value' => variable_get('import_action', 0),
81                             ));                '#options' => array('0' => t("Update an existing book"),
82                                      '1' => t("Create a new book")),
83                  '#description' => t("Choose whether to import into an existing book, or to create a new book."));
84  }  }
85    
86  /**  /**
87   * Converts code of an import action to a text string   * Converts code of an import action to a text string
88   */   */
89  function _bookimport_form_radio2text() {  function _bookimport_form_radio2text() {
90    switch ($_POST['edit']['action']) {      if (isset($_POST['edit']['import_action'])) {
91      case 0:          switch ($_POST['edit']['import_action']) {
92        return 'update';          case 0:
93      case 1:              return 'update';
94        return 'insert';          case 1:
95    }              return 'insert';
96            }
97        }
98        else {
99            print "edit/import_action not set";
100        }
101  }  }
102    
103  /**  /**
# Line 87  function _bookimport_form_radio2text() { Line 106  function _bookimport_form_radio2text() {
106   * import.   * import.
107   */   */
108  function bookimport_page_xml() {  function bookimport_page_xml() {
109  global $error;    global $error;
110    
111    $file = file_check_upload('xml_upload');    $file = file_check_upload('xml_upload');
112    if ($_POST['op_file']['upload']) {    watchdog('bookimport', t("Uploaded file: %file", array('%file' => theme('placeholder', $file->filename))));
113    
114      if ($_POST['op'] == t('Upload')) {
115      // save to temporary location      // save to temporary location
116      $file = file_save_upload('xml_upload');      $file = file_save_upload('xml_upload');
117      // get all nid from file      // get all nid from file
118    }    }
119    if ($_POST['op_file']['delete']) {    if ($_POST['op'] == t('Remove file')) {
120      if ($file) {      if ($file) {
121          print "delete file";
122        file_delete($file->filepath);        file_delete($file->filepath);
123        Unset($file);        Unset($file);
124      }      }
125    }    }
126    
   $group = '';  
   $group2 = '';  
127    if ($file) {    if ($file) {
128      $group2 .= form_item(t('Uploaded file'), sprintf("%s (%d Bytes)", $file->filename, $file->filesize));        $form['file'] =
129      $group2 .= form_submit(t('Remove file'), 'op_file[delete]');            array(
130                    '#type' => 'item',
131                    '#title' => t('Uploaded file'),
132                    '#value' => sprintf("%s (%d Bytes)", $file->filename, $file->filesize)
133                    );
134          $form['op_file']['delete'] =
135              array(
136                    '#type' => 'submit',
137                    '#value' => t('Remove file'),
138                    );
139    }    }
140    
141    $group2 .= form_file(t('File name'), "xml_upload", 40, t('A file containing XML data in UTF-8. Enter file name and press "Upload" to enable import options.'));    $form['group1'] =
142    $group2 .= form_submit(t('Upload'), 'op_file[upload]');      array(
143    $group .= form_group(t('Step 1: Upload a Drupal Book XML file'), $group2);            '#type' => 'fieldset',
144              '#title' => t('Step 1: Upload a Drupal Book XML (dxml) file'),
145    $group .= '<br />';            '#collapsible' => TRUE,
146              '#collapsed' => TRUE,
147              );
148      $form['group1']['xml_upload'] =
149        array(
150              '#type' => 'file',
151              '#title' => t('File name'),
152              '#size' => 40,
153              '#description' => t('A file containing XML data in UTF-8. Enter filename and press "Upload" to enable import options.'),
154              );
155    
156      $form['group1']['op_file']['upload'] =
157        array(
158              '#type' => 'submit',
159              '#value' => t('Upload'),
160              );
161    
162    if ($file) {    if ($file) {
163      $group2 = _bookimport_form_radios_action();      $form['group2'] =
164      $group2 .= form_submit(t('Import data'), 'op_xml[import][i]');        array(
165      $group .= form_group(t('Step 2: Update or Create the book'), $group2);              '#type' => 'fieldset',
166    }              '#title' => t('Step 2: Update or Create the book'),
167    $group  = form($group, 'post', 0, array("enctype" => "multipart/form-data"));              '#collapsible' => TRUE,
168                '#collapsed' => TRUE,
169                );
170    
171        $form['group2']['import_action'] =
172            array(
173                  '#type' => 'radios',
174                  '#title' => t("Action"),
175                  '#default_value' => variable_get('import_action', 0),
176                  '#options' => array('0' => t("Update an existing book"),
177                                      '1' => t("Create a new book")),
178                  '#description' => t("Choose whether to import into an existing book, or to create a new book."),
179                  );
180    
181        $form['group2']['op_xml']['import'] =
182          array(
183                '#type' => 'submit',
184                '#value' => t('Import data'),
185                );
186    
187    $output .= $group;    }
188      $form['#method'] = 'post';
189      $form['#action'] = 'import/book';
190      $form['#attributes'] = array("enctype" => "multipart/form-data");
191      $output = drupal_get_form('bookimport_page_xml', $form);
192    
193    if ($_POST['op_xml']['import']) {    if ($_POST['op'] == t('Import data')) {
194        bookimport_importxml();        bookimport_importxml();
195    }    }
196    
# Line 139  global $error; Line 205  global $error;
205   * parameters have been set up.   * parameters have been set up.
206   */   */
207  function bookimport_importxml() {  function bookimport_importxml() {
208      define('DEBUG', 0);
209      define('DEBUG', 0);    if (DEBUG > 0) {
210        print "<pre>";
211        print "Op = $op<br />";
212      }
213    
214      $edit = $_POST['edit'];    $edit = $_POST['edit'];
215      $op = _bookimport_form_radio2text();    $op = _bookimport_form_radio2text();
216    
     $fileh = file_check_upload('xml_upload');  
     $xmlfile = $fileh->filepath;  
217    
     // global hash provides mapping from old nid to new  
     $new_nid = array();  
     $parent_of = array();  
   
     // Create the XML parser  
     if (!$xml = xml_parser_create()) {  
         drupal_set_message("Can't create XML parser\n");  
         drupal_goto('import/book/xml');  
     }  
218    
219      require_once('node.php');    $fileh = file_check_upload('xml_upload');
220      require_once('save_node.php');    $xmlfile = $fileh->filepath;
221      require_once('drupal_export_parser.php');  
222      // global hash provides mapping from old nid to new
223      // Create the helper class (event handlers)    $new_nid = array();
224      $obj = new drupal_export_parser('', 'save');    $parent_of = array();
225      $obj->op = $op;  
226      // Create the XML parser
227      if (!$xml = xml_parser_create()) {
228        drupal_set_message("Can't create XML parser\n");
229        drupal_goto('import/book/xml');
230      }
231    
232      require_once('node.php');  // Drupal node defined as PHP object
233      require_once('save_node.php');  // callback that saves nodes to the database
234      require_once('drupal_export_parser.php');
235    
236      // Create the helper class (event handlers)
237      $parser = new drupal_export_parser('', 'save'); // register callback
238      $parser->op = $op;
239    
240      xml_set_object($xml, $obj);    xml_set_object($xml, $parser);
241      xml_set_element_handler($xml, 'start_element', 'end_element');    xml_set_element_handler($xml, 'start_element', 'end_element');
242      xml_set_character_data_handler($xml, 'character_data');    xml_set_character_data_handler($xml, 'character_data');
243      xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);    xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
244    
245      // Open the XML file and parse it    // Open the XML file and parse it
246      if (!$fp = fopen($xmlfile, 'r')) {    if (!$fp = fopen($xmlfile, 'r')) {
247          drupal_set_message("Can't read XML file $xmlfile\n");      drupal_set_message("Can't read XML file $xmlfile\n");
248          drupal_goto('import/book/xml');      drupal_goto('import/book/xml');
249      }    }
250    
251      while ($data = fread($fp, 4096)) {    while ($data = fread($fp, 4096)) {
252          if (!xml_parse($xml, $data, feof($fp))) {      if (!xml_parse($xml, $data, feof($fp))) {
253              drupal_set_message("Can't parse XML data\n");        drupal_set_message("Can't parse XML data\n");
254              drupal_goto('import');        drupal_goto('import');
         }  
255      }      }
256      fclose($fp);    }
257      fclose($fp);
258    
259      // Clean up    // Clean up
260      xml_parser_free($xml);    xml_parser_free($xml);
261    
262      if ($op == 'insert') {    make_links($parent_of, $new_nid);
         make_links($parent_of, $new_nid);  
     }  
263    
264      drupal_set_message("Finished import of file: $xmlfile");    if (DEBUG > 1) {
265      drupal_goto('import');        print "</pre>";
266      };
267      drupal_set_message("Finished import of file: $xmlfile");
268      drupal_goto('import');
269    
270  }  }
   
 ?>  

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.2