Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_data_export / theme / views_data_export.theme.inc @ a6e869e4

1
<?php
2

    
3
/**
4
 * @file
5
 * Theme related functions for processing our output style plugins.
6
 *
7
 * Views bug: http://drupal.org/node/593336
8
 */
9

    
10

    
11
/**
12
 * Theme a status message
13
 */
14
function theme_views_data_export_message($var) {
15
  $output = '';
16
  $output .= '<div class="messages status ' . $var['type'] . '">';
17
  $output .= $var['message'];
18
  $output .= '</div>';
19
  return $output;
20
}
21

    
22
/**
23
 * Theme a feed link.
24
 *
25
 * This theme function uses the theme pattern system to allow it to be
26
 * overidden in a more specific manner. The options for overiding this include
27
 * providing per display id; per type; per display id and per type.
28
 *
29
 * e.g.
30
 * For the view "export_test" with the display "page_1" and the type "csv" you
31
 * would have the following options.
32
 *   views_data_export_feed_icon__export_test__page_1__csv
33
 *   views_data_export_feed_icon__export_test__page_1
34
 *   views_data_export_feed_icon__export_test__csv
35
 *   views_data_export_feed_icon__page_1__csv
36
 *   views_data_export_feed_icon__page_1
37
 *   views_data_export_feed_icon__csv
38
 *   views_data_export_feed_icon
39
 *
40
 * @ingroup themeable
41
 */
42
function theme_views_data_export_feed_icon($variables) {
43
  extract($variables, EXTR_SKIP);
44
  $url_options = array('html' => TRUE);
45
  if ($query) {
46
    $url_options['query'] = $query;
47
  }
48
  $image = theme('image', array('path' => $image_path, 'alt' => $text, 'title' => $text));
49
  return l($image, $url, $url_options);
50
}
51

    
52
/**
53
 * Theme callback for the export complete page.
54
 *
55
 * @param $file
56
 *  Link to output file
57
 */
58
function theme_views_data_export_complete_page($variables) {
59
  extract($variables, EXTR_SKIP);
60
  drupal_set_title(t('Data export successful'));
61
  drupal_add_html_head(array('#tag' => 'meta', '#attributes' => array('http-equiv' =>"Refresh", 'content' => '3;url='. $file)), 'views_data_export_download');
62
  $output = '';
63
  $output .= '<p>';
64
  $output .= t('Your export has been created. View/download the file <a href="@link">here</a> (will automatically download in 3 seconds.)', array('@link' => $file));
65
  $output .= '</p>';
66

    
67
  if (!empty($return_url)) {
68
    $output .= '<p>';
69
    $output .= l(t('Return to previous page'), $return_url);
70
    $output .= '</p>';
71
  }
72
  return $output;
73
}
74

    
75

    
76
function template_preprocess_views_data_export(&$vars) {
77
  $vars['header'] = $vars['rows']['header'];
78
  $vars['body'] = $vars['rows']['body'];
79
  $vars['footer'] = $vars['rows']['footer'];
80

    
81
  $view     = $vars['view'];
82
  $fields   = &$view->field;
83
}
84

    
85
function template_preprocess_views_data_export_csv_header(&$vars) {
86
  _views_data_export_header_shared_preprocess($vars);
87

    
88
  // Make sure we catch saved options that are misspelled. LEGACY
89
  if (isset($vars['options']['seperator'])) {
90
    $vars['options']['separator'] = $vars['options']['seperator'];
91
  }
92
  // Support old misspelled templates. LEGACY
93
  $vars['seperator'] =
94
    $vars['separator'] = $vars['options']['separator'];
95

    
96
  // Special handling when quoted values are involved.
97
  if ($vars['options']['quote']) {
98
    $wrap = '"';
99
    $replace_value = '""';
100
  }
101
  else {
102
    $wrap = '';
103
    $replace_value = '';
104
  }
105

    
106
  // Format header values.
107
  foreach ($vars['header'] as $key => $value) {
108
    $output = decode_entities($value);
109
    $output = (empty($vars['options']['keep_html'])) ? strip_tags($output) : $output;
110
    if (!empty($vars['options']['trim'])) {
111
      $output = trim($output);
112
    }
113
    if (!empty($vars['options']['encoding']) && function_exists('iconv')) {
114
      switch($vars['options']['encoding']) {
115
        case 'utf8_decode':
116
          $converted = utf8_decode($output);
117
          break;
118
        default:
119
          $converted = iconv("UTF-8", $vars['options']['encoding'], $output);
120
          break;
121
      }
122
      if ($converted !== FALSE) {
123
        $output = $converted;
124
      }
125
    }
126
    $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
127
  }
128
}
129

    
130
function template_preprocess_views_data_export_csv_body(&$vars) {
131
  _views_data_export_body_shared_preprocess($vars);
132

    
133
  // Make sure we catch saved options that are misspelled. LEGACY
134
  if (isset($vars['options']['seperator'])) {
135
    $vars['options']['separator'] = $vars['options']['seperator'];
136
  }
137
  // Support old misspelled templates. LEGACY
138
  $vars['seperator'] =
139
    $vars['separator'] = $vars['options']['separator'];
140

    
141
  // Special handling when quoted values are involved.
142
  if ($vars['options']['quote']) {
143
    $wrap = '"';
144
    $replace_value = '""';
145
  }
146
  else {
147
    $wrap = '';
148
    $replace_value = '';
149
  }
150

    
151
  // Format row values.
152
  foreach ($vars['themed_rows'] as $i => $values) {
153
    foreach ($values as $j => $value) {
154
      $output = decode_entities($value);
155
      $output = (empty($vars['options']['keep_html'])) ? strip_tags($output) : $output;
156
      if (!empty($vars['options']['trim'])) {
157
        $output = trim($output);
158
      }
159

    
160
      if (!empty($vars['options']['encoding']) && function_exists('iconv')) {
161
        switch($vars['options']['encoding']) {
162
          case 'utf8_decode':
163
            $converted = utf8_decode($output);
164
            break;
165
          default:
166
            $converted = iconv("UTF-8", $vars['options']['encoding'], $output);
167
            break;
168
        }
169
        if ($converted !== FALSE) {
170
          $output = $converted;
171
        }
172
      }
173
      if (!empty($vars['options']['replace_newlines'])) {
174
        $output = str_replace("\n", $vars['options']['newline_replacement'], $output);
175
      }
176
      $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
177
    }
178
  }
179
}
180

    
181
/**
182
 * Preprocess csv output template.
183
 */
184
function template_preprocess_views_data_export_csv(&$vars) {
185
  // TODO Replace items with themed_rows.
186
  _views_data_export_shared_preprocess($vars);
187

    
188
  // Make sure we catch saved options that are misspelled. LEGACY
189
  if (isset($vars['options']['separator'])) {
190
    $vars['options']['separator'] = $vars['options']['seperator'];
191
  }
192
  // Support old misspelled templates. LEGACY
193
  $vars['seperator'] =
194
    $vars['separator'] = $vars['options']['separator'];
195

    
196
  // Special handling when quoted values are involved.
197
  if ($vars['options']['quote']) {
198
    $wrap = '"';
199
    $replace_value = '""';
200
  }
201
  else {
202
    $wrap = '';
203
    $replace_value = '';
204
  }
205

    
206
  // Format header values.
207
  foreach ($vars['header'] as $key => $value) {
208
    $output = decode_entities(strip_tags($value));
209
    if ($vars['options']['trim']) {
210
      $output = trim($output);
211
    }
212
    if (!empty($vars['options']['encoding']) && function_exists('iconv')) {
213
      switch($vars['options']['encoding']) {
214
        case 'ASCII':
215
          $converted = iconv("UTF-8", "ASCII//TRANSLIT", $output);
216
          if ($converted !== FALSE) {
217
            $output = $converted;
218
          }
219
          break;
220
      }
221
    }
222
    $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
223
  }
224

    
225
  // Format row values.
226
  foreach ($vars['themed_rows'] as $i => $values) {
227
    foreach ($values as $j => $value) {
228
      $output = decode_entities(strip_tags($value));
229
      if ($vars['options']['trim']) {
230
        $output = trim($output);
231
      }
232
      if (!empty($vars['options']['encoding']) && function_exists('iconv')) {
233
        switch($vars['options']['encoding']) {
234
          case 'ASCII':
235
            $converted = iconv("UTF-8", "ASCII//TRANSLIT", $output);
236
            if ($converted !== FALSE) {
237
              $output = $converted;
238
            }
239
            break;
240
        }
241
      }
242
      $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
243
    }
244
  }
245
}
246

    
247
/**
248
 * Preprocess txt output template.
249
 */
250
function template_preprocess_views_data_export_txt_body(&$vars) {
251
  _views_data_export_header_shared_preprocess($vars);
252
  // We support not outputting fields when they are empty, so indicate so.
253
  $vars['hide_empty_support'] = TRUE;
254
  _views_data_export_body_shared_preprocess($vars);
255
}
256

    
257
function template_preprocess_views_data_export_doc_body(&$vars) {
258
  // Pass through the generic MS Office preprocess.
259
  template_preprocess_views_data_export_msoffice_body($vars);
260
}
261

    
262
function template_preprocess_views_data_export_xls_body(&$vars) {
263
  // Pass through the generic MS Office preprocess.
264
  template_preprocess_views_data_export_msoffice_body($vars);
265
}
266

    
267
function template_preprocess_views_data_export_msoffice_body(&$vars) {
268
  _views_data_export_header_shared_preprocess($vars);
269
  _views_data_export_body_shared_preprocess($vars);
270

    
271
  $output = '';
272

    
273
  // Construct the tbody of a table, see theme_table().
274

    
275
  $ts = tablesort_init($vars['header']);
276

    
277
  $flip = array(
278
    'even' => 'odd',
279
    'odd' => 'even',
280
  );
281
  $class = 'even';
282
  foreach ($vars['themed_rows'] as $number => $row) {
283
    $attributes = array();
284

    
285
    // Check if we're dealing with a simple or complex row
286
    if (isset($row['data'])) {
287
      foreach ($row as $key => $value) {
288
        if ($key == 'data') {
289
          $cells = $value;
290
        }
291
        else {
292
          $attributes[$key] = $value;
293
        }
294
      }
295
    }
296
    else {
297
      $cells = $row;
298
    }
299
    if (count($cells)) {
300
      // Add odd/even class
301
      $class = $flip[$class];
302
      if (isset($attributes['class'])) {
303
        $attributes['class'] .= ' ' . $class;
304
      }
305
      else {
306
        $attributes['class'] = $class;
307
      }
308

    
309
      // Build row
310
      $output .= ' <tr' . drupal_attributes($attributes) . '>';
311
      $i = 0;
312
      foreach ($cells as $cell) {
313
        $cell = tablesort_cell($cell, $vars['header'], $ts, $i++);
314
        $output .= _theme_table_cell($cell);
315
      }
316
      $output .= " </tr>\n";
317
    }
318
  }
319

    
320

    
321
  $vars['tbody'] = preg_replace('/<\/?(a|span) ?.*?>/', '', $output); // strip 'a' and 'span' tags
322

    
323
}
324

    
325
function template_preprocess_views_data_export_doc_header(&$vars) {
326
  // Pass through the generic MS Office preprocess.
327
  template_preprocess_views_data_export_msoffice_header($vars);
328
}
329

    
330
function template_preprocess_views_data_export_xls_header(&$vars) {
331
  // Pass through the generic MS Office preprocess.
332
  template_preprocess_views_data_export_msoffice_header($vars);
333
}
334

    
335
function template_preprocess_views_data_export_msoffice_header(&$vars) {
336
  _views_data_export_header_shared_preprocess($vars);
337

    
338
  // Need to do a little work to construct the table header, see theme_table().
339
  $vars['header_row'] = '';
340
  $vars['header_row'] .= '<thead><tr>';
341

    
342
  $ts = tablesort_init($vars['header']);
343

    
344
  foreach ($vars['header'] as $cell) {
345
    $cell = tablesort_header($cell, $vars['header'], $ts);
346
    $vars['header_row'] .= _theme_table_cell($cell, TRUE);
347
  }
348

    
349
  $vars['header_row'] .= '</tr></thead>';
350

    
351
  $vars['header_row'] = preg_replace('/<\/?(a|span) ?.*?>/', '', $vars['header_row']); // strip 'a' and 'span' tags
352
}
353

    
354
/**
355
 * Preprocess xml output template.
356
 */
357
function template_preprocess_views_data_export_xml_header(&$vars) {
358
  $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']);
359
}
360

    
361
/**
362
 * Preprocess xml output template.
363
 */
364
function template_preprocess_views_data_export_xml_footer(&$vars) {
365
  $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']);
366
}
367

    
368
/**
369
 * Preprocess xml output template.
370
 */
371
function template_preprocess_views_data_export_xml_body(&$vars) {
372
  _views_data_export_header_shared_preprocess($vars);
373
  // We support not outputting fields when they are empty, so indicate so.
374
  $vars['hide_empty_support'] = TRUE;
375
  _views_data_export_body_shared_preprocess($vars);
376

    
377
  $view = $vars['view'];
378
  $style_options = $view->display_handler->get_option('style_options');
379

    
380
  $no_encode = isset($style_options['no_entity_encode']) ? $style_options['no_entity_encode'] : array();
381

    
382
  $cdata_wrapper = isset($style_options['cdata_wrapper']) ? $style_options['cdata_wrapper'] : array();
383

    
384
  $vars['item_node'] = _views_data_export_xml_tag_clean($vars['options']['item_node']);
385

    
386
  foreach ($vars['themed_rows'] as $num => $row) {
387
    foreach ($row as $field => $content) {
388

    
389
      // Perform xml entity encoding unless excluded by style options.
390
      if (empty($no_encode[$field]) && empty($cdata_wrapper[$field])) {
391

    
392
        // Prevent double encoding of the ampersand. Look for the entities produced by check_plain().
393
        $content = preg_replace('/&(?!(amp|quot|#039|lt|gt);)/', '&amp;', $content);
394
        // Convert < and > to HTML entities.
395
        $content = str_replace(
396
          array('<', '>'),
397
          array('&lt;', '&gt;'),
398
          $content);
399
      }
400

    
401
      // Perform wrapping the field data using the CDATA tag
402
      // unless excluded by style options.
403
      if (!empty($cdata_wrapper[$field])) {
404
        // Escape CDATA end sequence only.
405
        $content = '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $content) . ']]>';
406
      }
407

    
408
      $vars['themed_rows'][$num][$field] = $content;
409
    }
410
  }
411

    
412
  foreach ($vars['header'] as $field => $header) {
413
    // If there is no field label, use 'no name'.
414
    if (empty($header)) {
415
      $header = 'no name';
416
    }
417
    if ($vars['options']['transform']) {
418
      switch ($vars['options']['transform_type']) {
419
        case 'dash':
420
          $vars['xml_tag'][$field] = str_replace(' ', '-', $header);
421
          break;
422
        case 'underline':
423
          $vars['xml_tag'][$field] = str_replace(' ', '_', $header);
424
          break;
425
        case 'camel':
426
          $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header)));
427
          // Convert the very first character of the string to lowercase.
428
          $vars['xml_tag'][$field][0] = strtolower($vars['xml_tag'][$field][0]);
429
          break;
430
        case 'pascal':
431
          $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header)));
432
          break;
433
      }
434
    }
435
    // We should always try to output valid XML.
436
    $vars['xml_tag'][$field] = _views_data_export_xml_tag_clean($vars['xml_tag'][$field]);
437
  }
438
}
439

    
440
/**
441
 * Returns a valid XML tag formed from the given input.
442
 *
443
 * @param $tag The string that should be made into a valid XML tag.
444
 * @return The valid XML tag or an empty string if the string contained no valid
445
 * XML tag characters.
446
 */
447
function _views_data_export_xml_tag_clean($tag) {
448

    
449
  // This regex matches characters that are not valid in XML tags, and the
450
  // unicode ones that are. We don't bother with unicode, because it would so
451
  // the preg_replace down a lot.
452
  static $invalid_tag_chars_regex = '#[^\:A-Za-z_\-.0-9]+#';
453

    
454
  // These characters are not valid at the start of an XML tag:
455
  static $invalid_start_chars = '-.0123456789';
456

    
457
  // Convert invalid chars to '-':
458
  $tag = preg_replace($invalid_tag_chars_regex, '-', $tag);
459

    
460
  // Need to trim invalid characters from the start of the string:
461
  $tag = ltrim($tag, $invalid_start_chars);
462

    
463
  // As a last line of defense, if we've stripped out everything, set it to
464
  // something.
465
  if (empty($tag)) {
466
    $tag = 'invalid-tag-name';
467
  }
468

    
469
  return $tag;
470
}
471

    
472
/**
473
 * Shared helper function for export preprocess functions.
474
 */
475
function _views_data_export_header_shared_preprocess(&$vars) {
476
  $view     = $vars['view'];
477
  $fields   = &$view->field;
478

    
479
  $vars['header'] = array();
480
  foreach ($fields as $key => $field) {
481
    if (empty($field->options['exclude'])) {
482
      $vars['header'][$key] = check_plain($field->label());
483
    }
484
  }
485

    
486
}
487

    
488
/**
489
 * Shared helper function for export preprocess functions.
490
 */
491
function _views_data_export_body_shared_preprocess(&$vars) {
492
  $view     = $vars['view'];
493
  $fields   = &$view->field;
494
  $hide_empty_support = !empty($vars['hide_empty_support']);
495

    
496
  $rows = $vars['rows'];
497

    
498
  $vars['themed_rows'] = array();
499
  $keys = array_keys($fields);
500
  foreach ($rows as $num => $row) {
501
    $vars['themed_rows'][$num] = array();
502

    
503
    foreach ($keys as $id) {
504
      if (empty($fields[$id]->options['exclude'])) {
505
        $content = $view->style_plugin->rendered_fields[$num][$id];
506

    
507
        if ($hide_empty_support && !empty($fields[$id]->options['hide_empty'])) {
508
          if ($fields[$id]->is_value_empty($content, $fields[$id]->options['empty_zero'])) {
509
            continue;
510
          }
511
        }
512

    
513
        $vars['themed_rows'][$num][$id] = $content;
514
      }
515
    }
516
  }
517
}