Projet

Général

Profil

Paste
Télécharger (16,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_data_export / theme / views_data_export.theme.inc @ 0ccfec7f

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
        if (!empty($vars['options']['newline_token'])) {
175
          $output = str_replace( array("\r\n", "\r", "\n"), $vars['options']['newline_replacement'], $output);
176
        }
177
        else {
178
          $output = str_replace("\n", $vars['options']['newline_replacement'], $output);
179
        }
180
      }
181
      $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
182
    }
183
  }
184
}
185

    
186
/**
187
 * Preprocess csv output template.
188
 */
189
function template_preprocess_views_data_export_csv(&$vars) {
190
  // TODO Replace items with themed_rows.
191
  _views_data_export_shared_preprocess($vars);
192

    
193
  // Make sure we catch saved options that are misspelled. LEGACY
194
  if (isset($vars['options']['separator'])) {
195
    $vars['options']['separator'] = $vars['options']['seperator'];
196
  }
197
  // Support old misspelled templates. LEGACY
198
  $vars['seperator'] =
199
    $vars['separator'] = $vars['options']['separator'];
200

    
201
  // Special handling when quoted values are involved.
202
  if ($vars['options']['quote']) {
203
    $wrap = '"';
204
    $replace_value = '""';
205
  }
206
  else {
207
    $wrap = '';
208
    $replace_value = '';
209
  }
210

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

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

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

    
262
function template_preprocess_views_data_export_doc_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_xls_body(&$vars) {
268
  // Pass through the generic MS Office preprocess.
269
  template_preprocess_views_data_export_msoffice_body($vars);
270
}
271

    
272
function template_preprocess_views_data_export_msoffice_body(&$vars) {
273
  _views_data_export_header_shared_preprocess($vars);
274
  _views_data_export_body_shared_preprocess($vars);
275

    
276
  $output = '';
277

    
278
  // Construct the tbody of a table, see theme_table().
279

    
280
  $ts = tablesort_init($vars['header']);
281

    
282
  $flip = array(
283
    'even' => 'odd',
284
    'odd' => 'even',
285
  );
286
  $class = 'even';
287
  foreach ($vars['themed_rows'] as $number => $row) {
288
    $attributes = array();
289

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

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

    
325

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

    
328
}
329

    
330
function template_preprocess_views_data_export_doc_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_xls_header(&$vars) {
336
  // Pass through the generic MS Office preprocess.
337
  template_preprocess_views_data_export_msoffice_header($vars);
338
}
339

    
340
function template_preprocess_views_data_export_msoffice_header(&$vars) {
341
  _views_data_export_header_shared_preprocess($vars);
342

    
343
  // Need to do a little work to construct the table header, see theme_table().
344
  $vars['header_row'] = '';
345
  $vars['header_row'] .= '<thead><tr>';
346

    
347
  $ts = tablesort_init($vars['header']);
348

    
349
  foreach ($vars['header'] as $cell) {
350
    $cell = tablesort_header($cell, $vars['header'], $ts);
351
    $vars['header_row'] .= _theme_table_cell($cell, TRUE);
352
  }
353

    
354
  $vars['header_row'] .= '</tr></thead>';
355

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

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

    
366
/**
367
 * Preprocess xml output template.
368
 */
369
function template_preprocess_views_data_export_xml_footer(&$vars) {
370
  $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']);
371
}
372

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

    
382
  $view = $vars['view'];
383
  $style_options = $view->display_handler->get_option('style_options');
384

    
385
  $no_encode = isset($style_options['no_entity_encode']) ? $style_options['no_entity_encode'] : array();
386

    
387
  $cdata_wrapper = isset($style_options['cdata_wrapper']) ? $style_options['cdata_wrapper'] : array();
388

    
389
  $vars['item_node'] = _views_data_export_xml_tag_clean($vars['options']['item_node']);
390

    
391
  foreach ($vars['themed_rows'] as $num => $row) {
392
    foreach ($row as $field => $content) {
393

    
394
      // Perform xml entity encoding unless excluded by style options.
395
      if (empty($no_encode[$field]) && empty($cdata_wrapper[$field])) {
396

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

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

    
413
      $vars['themed_rows'][$num][$field] = $content;
414
    }
415
  }
416

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

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

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

    
459
  // These characters are not valid at the start of an XML tag:
460
  static $invalid_start_chars = '-.0123456789';
461

    
462
  // Convert invalid chars to '-':
463
  $tag = preg_replace($invalid_tag_chars_regex, '-', $tag);
464

    
465
  // Need to trim invalid characters from the start of the string:
466
  $tag = ltrim($tag, $invalid_start_chars);
467

    
468
  // As a last line of defense, if we've stripped out everything, set it to
469
  // something.
470
  if (empty($tag)) {
471
    $tag = 'invalid-tag-name';
472
  }
473

    
474
  return $tag;
475
}
476

    
477
/**
478
 * Shared helper function for export preprocess functions.
479
 */
480
function _views_data_export_header_shared_preprocess(&$vars) {
481
  $view     = $vars['view'];
482
  $fields   = &$view->field;
483
  $fields_info = $view->display_handler->get_option('fields');
484
  $vars['header'] = array();
485
  foreach ($fields as $key => $field) {
486
    if (empty($field->options['exclude'])) {
487
      if (isset($fields_info) && isset($fields_info[$key]['label'])) {
488
        $vars['header'][$key] = check_plain($fields_info[$key]['label']);
489
      }
490
      else {
491
        $vars['header'][$key] = check_plain($field->label());
492
      }
493
    }
494
  }
495
}
496

    
497
/**
498
 * Shared helper function for export preprocess functions.
499
 */
500
function _views_data_export_body_shared_preprocess(&$vars) {
501
  $view     = $vars['view'];
502
  $fields   = &$view->field;
503
  $hide_empty_support = !empty($vars['hide_empty_support']);
504

    
505
  $rows = $vars['rows'];
506

    
507
  $vars['themed_rows'] = array();
508
  $keys = array_keys($fields);
509
  foreach ($rows as $num => $row) {
510
    $vars['themed_rows'][$num] = array();
511

    
512
    foreach ($keys as $id) {
513
      if (empty($fields[$id]->options['exclude'])) {
514
        $content = $view->style_plugin->rendered_fields[$num][$id];
515

    
516
        if ($hide_empty_support && !empty($fields[$id]->options['hide_empty'])) {
517
          if ($fields[$id]->is_value_empty($content, $fields[$id]->options['empty_zero'])) {
518
            continue;
519
          }
520
        }
521

    
522
        $vars['themed_rows'][$num][$id] = $content;
523
      }
524
    }
525
  }
526
}