Projet

Général

Profil

Paste
Télécharger (7,76 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / plugins / FeedsCSVParser.inc @ 41cc1b08

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the FeedsCSVParser class.
6
 */
7

    
8
/**
9
 * Parses a given file as a CSV file.
10
 */
11
class FeedsCSVParser extends FeedsParser {
12

    
13
  /**
14
   * Implements FeedsParser::parse().
15
   */
16
  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
17
    $source_config = $source->getConfigFor($this);
18
    $state = $source->state(FEEDS_PARSE);
19

    
20
    // Load and configure parser.
21
    feeds_include_library('ParserCSV.inc', 'ParserCSV');
22
    $parser = new ParserCSV();
23
    $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
24
    $parser->setDelimiter($delimiter);
25

    
26
    $iterator = new ParserCSVIterator($fetcher_result->getFilePath());
27
    if (empty($source_config['no_headers'])) {
28
      // Get first line and use it for column names, convert them to lower case.
29
      $header = $this->parseHeader($parser, $iterator);
30
      if (!$header) {
31
        drupal_set_message(t('The CSV file is empty.'), 'warning', FALSE);
32
        return new FeedsParserResult();
33
      }
34
      $parser->setColumnNames($header);
35
    }
36

    
37
    // Determine section to parse, parse.
38
    $start = $state->pointer ? $state->pointer : $parser->lastLinePos();
39
    $limit = $source->importer->getLimit();
40
    $rows = $this->parseItems($parser, $iterator, $start, $limit);
41

    
42
    // Report progress.
43
    $state->total = filesize($fetcher_result->getFilePath());
44
    $state->pointer = $parser->lastLinePos();
45
    $progress = $parser->lastLinePos() ? $parser->lastLinePos() : $state->total;
46
    $state->progress($state->total, $progress);
47

    
48
    // Create a result object and return it.
49
    return new FeedsParserResult($rows, $source->feed_nid);
50
  }
51

    
52
  /**
53
   * Get first line and use it for column names, convert them to lower case.
54
   * Be aware that the $parser and iterator objects can be modified in this
55
   * function since they are passed in by reference
56
   *
57
   * @param ParserCSV $parser
58
   * @param ParserCSVIterator $iterator
59
   * @return
60
   *   An array of lower-cased column names to use as keys for the parsed items.
61
   */
62
  protected function parseHeader(ParserCSV $parser, ParserCSVIterator $iterator) {
63
    $parser->setLineLimit(1);
64
    $rows = $parser->parse($iterator);
65
    if (!count($rows)) {
66
      return FALSE;
67
    }
68
    $header = array_shift($rows);
69
    foreach ($header as $i => $title) {
70
      $header[$i] = trim(drupal_strtolower($title));
71
    }
72
    return $header;
73
  }
74

    
75
  /**
76
   * Parse all of the items from the CSV.
77
   *
78
   * @param ParserCSV $parser
79
   * @param ParserCSVIterator $iterator
80
   * @return
81
   *   An array of rows of the CSV keyed by the column names previously set
82
   */
83
  protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator, $start = 0, $limit = 0) {
84
    $parser->setLineLimit($limit);
85
    $parser->setStartByte($start);
86
    $rows = $parser->parse($iterator);
87
    return $rows;
88
  }
89

    
90
  /**
91
   * Override parent::getMappingSources().
92
   */
93
  public function getMappingSources() {
94
    return FALSE;
95
  }
96

    
97
  /**
98
   * Override parent::getSourceElement() to use only lower keys.
99
   */
100
  public function getSourceElement(FeedsSource $source, FeedsParserResult $result, $element_key) {
101
    return parent::getSourceElement($source, $result, drupal_strtolower($element_key));
102
  }
103

    
104
  /**
105
   * Define defaults.
106
   */
107
  public function sourceDefaults() {
108
    return array(
109
      'delimiter' => $this->config['delimiter'],
110
      'no_headers' => $this->config['no_headers'],
111
    );
112
  }
113

    
114
  /**
115
   * Source form.
116
   *
117
   * Show mapping configuration as a guidance for import form users.
118
   */
119
  public function sourceForm($source_config) {
120
    $form = array();
121
    $form['#weight'] = -10;
122

    
123
    $mappings = feeds_importer($this->id)->processor->config['mappings'];
124
    $sources = $uniques = array();
125
    foreach ($mappings as $mapping) {
126
      $sources[] = check_plain($mapping['source']);
127
      if (!empty($mapping['unique'])) {
128
        $uniques[] = check_plain($mapping['source']);
129
      }
130
    }
131
    $sources = array_unique($sources);
132

    
133
    $output = t('Import !csv_files with one or more of these columns: !columns.', array('!csv_files' => l(t('CSV files'), 'http://en.wikipedia.org/wiki/Comma-separated_values'), '!columns' => implode(', ', $sources)));
134
    $items = array();
135
    $items[] = format_plural(count($uniques), 'Column <strong>!columns</strong> is mandatory and considered unique: only one item per !columns value will be created.', 'Columns <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array('!columns' => implode(', ', $uniques)));
136
    $items[] = l(t('Download a template'), 'import/' . $this->id . '/template');
137
    $form['help'] = array(
138
      '#prefix' => '<div class="help">',
139
      '#suffix' => '</div>',
140
      'description' => array(
141
        '#prefix' => '<p>',
142
        '#markup' => $output,
143
        '#suffix' => '</p>',
144
      ),
145
      'list' => array(
146
        '#theme' => 'item_list',
147
        '#items' => $items,
148
      ),
149
    );
150
    $form['delimiter'] = array(
151
      '#type' => 'select',
152
      '#title' => t('Delimiter'),
153
      '#description' => t('The character that delimits fields in the CSV file.'),
154
      '#options'  => array(
155
        ',' => ',',
156
        ';' => ';',
157
        'TAB' => 'TAB',
158
        '|' => '|',
159
        '+' => '+',
160
      ),
161
      '#default_value' => isset($source_config['delimiter']) ? $source_config['delimiter'] : ',',
162
    );
163
    $form['no_headers'] = array(
164
      '#type' => 'checkbox',
165
      '#title' => t('No Headers'),
166
      '#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
167
      '#default_value' => isset($source_config['no_headers']) ? $source_config['no_headers'] : 0,
168
    );
169
    return $form;
170
  }
171

    
172
  /**
173
   * Define default configuration.
174
   */
175
  public function configDefaults() {
176
    return array(
177
      'delimiter' => ',',
178
      'no_headers' => 0,
179
    );
180
  }
181

    
182
  /**
183
   * Build configuration form.
184
   */
185
  public function configForm(&$form_state) {
186
    $form = array();
187
    $form['delimiter'] = array(
188
      '#type' => 'select',
189
      '#title' => t('Default delimiter'),
190
      '#description' => t('Default field delimiter.'),
191
      '#options' => array(
192
        ',' => ',',
193
        ';' => ';',
194
        'TAB' => 'TAB',
195
        '|' => '|',
196
        '+' => '+',
197
      ),
198
      '#default_value' => $this->config['delimiter'],
199
    );
200
    $form['no_headers'] = array(
201
      '#type' => 'checkbox',
202
      '#title' => t('No headers'),
203
      '#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
204
      '#default_value' => $this->config['no_headers'],
205
    );
206
    return $form;
207
  }
208

    
209
  public function getTemplate() {
210
    $mappings = feeds_importer($this->id)->processor->config['mappings'];
211
    $sources = $uniques = array();
212
    foreach ($mappings as $mapping) {
213
      if (in_array(check_plain($mapping['source']), $uniques) || in_array(check_plain($mapping['source']), $sources)) {
214
        // Skip columns we've already seen.
215
        continue;
216
      }
217

    
218
      if (!empty($mapping['unique'])) {
219
        $uniques[] = check_plain($mapping['source']);
220
      }
221
      else {
222
        $sources[] = check_plain($mapping['source']);
223
      }
224
    }
225
    $sep = $this->config['delimiter'];
226
    $columns = array();
227
    foreach (array_merge($uniques, $sources) as $col) {
228
      if (strpos($col, $sep) !== FALSE) {
229
        $col = '"' . str_replace('"', '""', $col) . '"';
230
      }
231
      $columns[] = $col;
232
    }
233
    drupal_add_http_header('Cache-Control', 'max-age=60, must-revalidate');
234
    drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $this->id . '_template.csv"');
235
    drupal_add_http_header('Content-type', 'text/csv; charset=utf-8');
236
    print implode($sep, $columns);
237
    return;
238
  }
239
}