Projet

Général

Profil

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

root / drupal7 / sites / all / modules / feeds / plugins / FeedsCSVParser.inc @ 2c8c2b87

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
        return;
32
      }
33
      $parser->setColumnNames($header);
34
    }
35

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

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

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

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

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

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

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

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

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

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

    
131
    $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)));
132
    $items = array();
133
    $items[] = format_plural(count($uniques), t('Column <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array('!column' => implode(', ', $uniques))), t('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))));
134
    $items[] = l(t('Download a template'), 'import/' . $this->id . '/template');
135
    $form['help'] = array(
136
      '#prefix' => '<div class="help">',
137
      '#suffix' => '</div>',
138
      'description' => array(
139
        '#prefix' => '<p>',
140
        '#markup' => $output,
141
        '#suffix' => '</p>',
142
      ),
143
      'list' => array(
144
        '#theme' => 'item_list',
145
        '#items' => $items,
146
      ),
147
    );
148
    $form['delimiter'] = array(
149
      '#type' => 'select',
150
      '#title' => t('Delimiter'),
151
      '#description' => t('The character that delimits fields in the CSV file.'),
152
      '#options'  => array(
153
        ',' => ',',
154
        ';' => ';',
155
        'TAB' => 'TAB',
156
        '|' => '|',
157
        '+' => '+',
158
      ),
159
      '#default_value' => isset($source_config['delimiter']) ? $source_config['delimiter'] : ',',
160
    );
161
    $form['no_headers'] = array(
162
      '#type' => 'checkbox',
163
      '#title' => t('No Headers'),
164
      '#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.'),
165
      '#default_value' => isset($source_config['no_headers']) ? $source_config['no_headers'] : 0,
166
    );
167
    return $form;
168
  }
169

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

    
180
  /**
181
   * Build configuration form.
182
   */
183
  public function configForm(&$form_state) {
184
    $form = array();
185
    $form['delimiter'] = array(
186
      '#type' => 'select',
187
      '#title' => t('Default delimiter'),
188
      '#description' => t('Default field delimiter.'),
189
      '#options' => array(
190
        ',' => ',',
191
        ';' => ';',
192
        'TAB' => 'TAB',
193
        '|' => '|',
194
        '+' => '+',
195
      ),
196
      '#default_value' => $this->config['delimiter'],
197
    );
198
    $form['no_headers'] = array(
199
      '#type' => 'checkbox',
200
      '#title' => t('No headers'),
201
      '#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.'),
202
      '#default_value' => $this->config['no_headers'],
203
    );
204
    return $form;
205
  }
206

    
207
  public function getTemplate() {
208
    $mappings = feeds_importer($this->id)->processor->config['mappings'];
209
    $sources = $uniques = array();
210
    foreach ($mappings as $mapping) {
211
      if (!empty($mapping['unique'])) {
212
        $uniques[] = check_plain($mapping['source']);
213
      }
214
      else {
215
        $sources[] = check_plain($mapping['source']);
216
      }
217
    }
218
    $sep = ',';
219
    $columns = array();
220
    foreach (array_merge($uniques, $sources) as $col) {
221
      if (strpos($col, $sep) !== FALSE) {
222
        $col = '"' . str_replace('"', '""', $col) . '"';
223
      }
224
      $columns[] = $col;
225
    }
226
    drupal_add_http_header('Cache-Control', 'max-age=60, must-revalidate');
227
    drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $this->id . '_template.csv"');
228
    drupal_add_http_header('Content-type', 'text/csv; charset=utf-8');
229
    print implode($sep, $columns);
230
    return;
231
  }
232
}