You are here

function tablefield_import_csv in TableField 7.2

Same name and namespace in other branches
  1. 6 tablefield.module \tablefield_import_csv()
  2. 7.3 tablefield.module \tablefield_import_csv()
  3. 7 tablefield.module \tablefield_import_csv()

Helper function to import data from a CSV file.

Parameters

array $form: The form structure.

array $form_state: The current state of the form.

string $langcode: The language associated with the form items.

string $file_form_field_name: The key of the upload form element in the form array.

array $tablefield_parents: The parents of the tablefield element.

1 call to tablefield_import_csv()
tablefield_field_widget_form in ./tablefield.module
Implements hook_widget_form().

File

./tablefield.module, line 1189
Provides a set of fields that can be used to store tabular data with a node.

Code

function tablefield_import_csv($form, &$form_state, $langcode, $file_form_field_name, $tablefield_parents) {

  // Extract the field and file name from the id of the clicked button.
  $file = file_save_upload($file_form_field_name, array(
    'file_validate_extensions' => array(
      'csv',
    ),
  ));
  if (is_object($file)) {

    // Assure Mac/Linux EOL markers work with fgetcsv().
    $auto_detect_line_endings = ini_get('auto_detect_line_endings');
    ini_set('auto_detect_line_endings', TRUE);
    if (($handle = fopen($file->uri, "r")) !== FALSE) {
      tablefield_delete_table_values(drupal_array_get_nested_value($form_state['values'], $tablefield_parents));
      tablefield_delete_table_values(drupal_array_get_nested_value($form_state['input'], $tablefield_parents));

      // Checking the encoding of the CSV file to be UTF-8.
      $encoding = 'UTF-8';
      if (function_exists('mb_detect_encoding')) {
        $file_contents = file_get_contents($file->uri);
        $encodings_list = implode(',', variable_get('tablefield_detect_encodings', array(
          'UTF-8',
          'ISO-8859-1',
          'WINDOWS-1251',
        )));
        $encoding = mb_detect_encoding($file_contents, $encodings_list);
      }

      // Populate CSV values.
      $max_col_count = 0;
      $row_count = 0;
      $imported_tablefield = array();
      while (($csv = fgetcsv($handle, 0, variable_get('tablefield_csv_separator', ','))) !== FALSE) {
        $col_count = count($csv);
        foreach ($csv as $col_id => $col) {
          $imported_tablefield['row_' . $row_count]['col_' . $col_id] = tablefield_convert_encoding($col, $encoding);
        }
        $max_col_count = $col_count > $max_col_count ? $col_count : $max_col_count;
        $row_count++;
      }
      fclose($handle);
      ini_set('auto_detect_line_endings', $auto_detect_line_endings);
      $imported_tablefield['rebuild'] = array(
        'count_cols' => $max_col_count,
        'count_rows' => $row_count,
      );
      drupal_array_set_nested_value($form_state['values'], $tablefield_parents, $imported_tablefield);
      drupal_array_set_nested_value($form_state['input'], $tablefield_parents, $imported_tablefield);
      drupal_set_message(t('Successfully imported @file', array(
        '@file' => $file->filename,
      )));
    }
    else {
      drupal_set_message(t('There was a problem importing @file.', array(
        '@file' => $file->filename,
      )));
    }
  }
}