/[drupal]/contributions/modules/tw/tw_import/tw_import_delimited.module
ViewVC logotype

Diff of /contributions/modules/tw/tw_import/tw_import_delimited.module

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

revision 1.1.2.10, Thu Apr 30 21:20:22 2009 UTC revision 1.1.2.11, Fri May 1 01:44:14 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: tw_import_delimited.module,v 1.1.2.9 2009/04/17 19:31:52 mikeryan Exp $  // $Id: tw_import_delimited.module,v 1.1.2.10 2009/04/30 21:20:22 mikeryan Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 8  Line 8 
8    
9  /**  /**
10   * Implementation of hook_tw_form().   * Implementation of hook_tw_form().
  *  
  * @return unknown  
11   */   */
12  function tw_import_delimited_tw_form() {  function tw_import_delimited_tw_form() {
13    ////////////////////////////////////////////////////////////    ////////////////////////////////////////////////////////////
# Line 48  function tw_import_delimited_tw_form() { Line 46  function tw_import_delimited_tw_form() {
46        '#value' => t('Import new file'),        '#value' => t('Import new file'),
47      ),      ),
48    );    );
49    
50    return $fieldsets;    return $fieldsets;
51  }  }
52    
53  /**  /**
54   * Implementation of hook_tw_form_submit   * Implementation of hook_tw_form_submit().
  *  
  * @param unknown_type $values  
  * @return unknown  
55   */   */
56  function tw_import_delimited_tw_form_submit_delimited($values) {  function tw_import_delimited_tw_form_submit_delimited($values) {
57    $file = file_save_upload('delimited');    $file = file_save_upload('delimited');
# Line 65  function tw_import_delimited_tw_form_sub Line 60  function tw_import_delimited_tw_form_sub
60      $dest = _tw_delimited_file_name($file->filename);      $dest = _tw_delimited_file_name($file->filename);
61      if ($dest) {      if ($dest) {
62        file_move($file, $dest, FILE_EXISTS_REPLACE);        file_move($file, $dest, FILE_EXISTS_REPLACE);
63      }      }
64      else {      else {
65        drupal_set_message(t('Could not upload !filename',        drupal_set_message(t('Could not upload !filename',
66          array('!filename' => $file->filename)));          array('!filename' => $file->filename)));
67        return NULL;        return NULL;
68      }      }
# Line 78  function tw_import_delimited_tw_form_sub Line 73  function tw_import_delimited_tw_form_sub
73      if ($headers === FALSE) {      if ($headers === FALSE) {
74        return;        return;
75      }      }
76    
77      // If an explicit table name was not provided...      // If an explicit table name was not provided...
78      if ($values['tablename']) {      if ($values['tablename']) {
79        $tablename = trim($values['tablename']);        $tablename = trim($values['tablename']);
80      } else {      }
81        else {
82        // ... Derive one from the filename (minus extension)        // ... Derive one from the filename (minus extension)
83        $pieces = explode('.', $file->filename);        $pieces = explode('.', $file->filename);
84        $tablename = trim($pieces[0]);        $tablename = trim($pieces[0]);
85      }      }
86    
87      if (!$tablename) {      if (!$tablename) {
88        drupal_set_message(t('Could not derive a table name for !filename',        drupal_set_message(t('Could not derive a table name for !filename',
89          array('!filename' => $file->filename)));          array('!filename' => $file->filename)));
90      }      }
91    
92      // Lowercase table names make views integration much easier      // Lowercase table names make views integration much easier
93      $tablename = strtolower(preg_replace('/[^a-z0-9]/i', '_', $tablename));      $tablename = drupal_strtolower(preg_replace('/[^a-z0-9]/i', '_', $tablename));
94      // Truncate to 63 characters (valid for either MySQL or Postgres)      // Truncate to 63 characters (valid for either MySQL or Postgres)
95      $tablename = substr($tablename, 0, 63);      $tablename = drupal_substr($tablename, 0, 63);
96    
97      // Build a schema definition for the table      // Build a schema definition for the table
98      $schema = array(      $schema = array(
99          'description' => t('Uploaded data file'),          'description' => t('Uploaded data file'),
# Line 110  function tw_import_delimited_tw_form_sub Line 106  function tw_import_delimited_tw_form_sub
106        // Field names must be unique, generate variations if necessary        // Field names must be unique, generate variations if necessary
107        if (!isset($seen[$coltitle])) {        if (!isset($seen[$coltitle])) {
108          $seen[$coltitle] = 1;          $seen[$coltitle] = 1;
109        }        }
110        else {        else {
111          $seen[$coltitle]++;          $seen[$coltitle]++;
112        }        }
# Line 130  function tw_import_delimited_tw_form_sub Line 126  function tw_import_delimited_tw_form_sub
126      if (db_table_exists($tablename)) {      if (db_table_exists($tablename)) {
127        db_query('TRUNCATE TABLE {' . $tablename . '}');        db_query('TRUNCATE TABLE {' . $tablename . '}');
128        $prevexists = TRUE;        $prevexists = TRUE;
129      }      }
130      else {      else {
131        // Note that when using table prefixes, a prefix is prepended to the table name        // Note that when using table prefixes, a prefix is prepended to the table name
132        db_create_table($ret, $tablename, $schema);        db_create_table($ret, $tablename, $schema);
# Line 149  function tw_import_delimited_tw_form_sub Line 145  function tw_import_delimited_tw_form_sub
145        $srcinfo->success_msg = t('%filename has been imported into table %tablename, replacing        $srcinfo->success_msg = t('%filename has been imported into table %tablename, replacing
146          the original data in that table',          the original data in that table',
147          array('%filename' => $file->filename, '%tablename' => $tablename));          array('%filename' => $file->filename, '%tablename' => $tablename));
148      } else {      }
149        else {
150        $srcinfo->success_msg = t('%filename has been imported into new table %tablename. All fields        $srcinfo->success_msg = t('%filename has been imported into new table %tablename. All fields
151          have initially been defined as text, with no indexing - it is important to set appropriate          have initially been defined as text, with no indexing - it is important to set appropriate
152          data types for each column, and to define a single (unique, non-null) column as the          data types for each column, and to define a single (unique, non-null) column as the
# Line 171  function tw_import_delimited_tw_form_sub Line 168  function tw_import_delimited_tw_form_sub
168    
169      // Pass the table name back to Table Wizard      // Pass the table name back to Table Wizard
170      return array($tablename);      return array($tablename);
171    }    }
172    else {    else {
173      drupal_set_message(t('Failed to upload delimited file'));      drupal_set_message(t('Failed to upload delimited file'));
174    }    }
# Line 180  function tw_import_delimited_tw_form_sub Line 177  function tw_import_delimited_tw_form_sub
177  /**  /**
178   * Generate the Drupal-relative path for storing imported files (creating the tw_delimited   * Generate the Drupal-relative path for storing imported files (creating the tw_delimited
179   * directory if necessary).   * directory if necessary).
  *  
  * @param unknown_type $file  
  * @return unknown  
180   */   */
181  function _tw_delimited_file_name($file) {  function _tw_delimited_file_name($file) {
182    $dir = str_replace('\\', '/', getcwd()).'/'.file_directory_path() . '/tw_delimited';    $dir = str_replace('\\', '/', getcwd()) . '/' . file_directory_path() . '/tw_delimited';
183    if (file_check_directory($dir, TRUE)) {    if (file_check_directory($dir, TRUE)) {
184      return $dir . '/' . $file;      return $dir . '/' . $file;
185    }    }
186    else {    else {
187      return FALSE;      return FALSE;
188    }    }
# Line 213  function _tw_delimited_get_row($filepath Line 207  function _tw_delimited_get_row($filepath
207      $sample = fgets($handle);      $sample = fgets($handle);
208      if (strpos($sample, "\t") === FALSE) {      if (strpos($sample, "\t") === FALSE) {
209        $separator = ',';        $separator = ',';
210      } else {      }
211        else {
212        $separator = "\t";        $separator = "\t";
213      }      }
214      fseek($handle, $offset);      fseek($handle, $offset);
# Line 225  function _tw_delimited_get_row($filepath Line 220  function _tw_delimited_get_row($filepath
220      $handle = NULL;      $handle = NULL;
221      $currpath = NULL;      $currpath = NULL;
222      $separator = NULL;      $separator = NULL;
223    } else {    }
224      else {
225      $offset = ftell($handle);      $offset = ftell($handle);
226    }    }
227    
228    return $result;    return $result;
229  }  }
230    
# Line 252  function _tw_import_delimited_import_fil Line 248  function _tw_import_delimited_import_fil
248    
249    if ($context['sandbox']['total'] > 0) {    if ($context['sandbox']['total'] > 0) {
250      $context['finished'] = $context['sandbox']['done'] / $context['sandbox']['total'];      $context['finished'] = $context['sandbox']['done'] / $context['sandbox']['total'];
251    } else {    }
252      else {
253      $context['finished'] = 1;      $context['finished'] = 1;
254    }    }
255    $context['message'] = t('%done of %total bytes imported<br />%rows rows inserted into %tablename',    $context['message'] = t('%done of %total bytes imported<br />%rows rows inserted into %tablename',
256                            array('%done' => format_size($context['sandbox']['done']),                            array('%done' => format_size($context['sandbox']['done']),
257                                  '%total' => format_size($context['sandbox']['total']),                                  '%total' => format_size($context['sandbox']['total']),
258                                  '%rows' => $context['sandbox']['rows'],                                  '%rows' => $context['sandbox']['rows'],

Legend:
Removed from v.1.1.2.10  
changed lines
  Added in v.1.1.2.11

  ViewVC Help
Powered by ViewVC 1.1.2