Project

General

Profile

Paste
Download (16.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views_data_export / theme / views_data_export.theme.inc @ 56aebcb7

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
/**
263
 * Implements hook_preprocess_views_data_export_doc_body().
264
 */
265
function template_preprocess_views_data_export_doc_body(&$vars) {
266
  // Pass through the generic MS Office preprocess.
267
  template_preprocess_views_data_export_msoffice_body($vars);
268
}
269

    
270
/**
271
 * Implements hook_preprocess_views_data_export_xls_body().
272
 */
273
function template_preprocess_views_data_export_xls_body(&$vars) {
274
  // Pass through the generic MS Office preprocess.
275
  template_preprocess_views_data_export_msoffice_body($vars);
276
}
277

    
278
function template_preprocess_views_data_export_msoffice_body(&$vars) {
279
  _views_data_export_header_shared_preprocess($vars);
280
  _views_data_export_body_shared_preprocess($vars);
281
}
282

    
283
/**
284
 * Implements hook_process_views_data_export_doc_body().
285
 */
286
function template_process_views_data_export_doc_body(&$vars) {
287
  // Pass through the generic MS Office process.
288
  template_process_views_data_export_msoffice_body($vars);
289
}
290

    
291
/**
292
 * Implements hook_process_views_data_export_xls_body().
293
 */
294
function template_process_views_data_export_xls_body(&$vars) {
295
  // Pass through the generic MS Office process.
296
  template_process_views_data_export_msoffice_body($vars);
297
}
298

    
299
function template_process_views_data_export_msoffice_body(&$vars) {
300

    
301
  $output = '';
302

    
303
  // Construct the tbody of a table, see theme_table().
304

    
305
  $ts = tablesort_init($vars['header']);
306

    
307
  $flip = array(
308
    'even' => 'odd',
309
    'odd' => 'even',
310
  );
311
  $class = 'even';
312
  foreach ($vars['themed_rows'] as $number => $row) {
313
    $attributes = array();
314

    
315
    // Check if we're dealing with a simple or complex row
316
    if (isset($row['data'])) {
317
      foreach ($row as $key => $value) {
318
        if ($key == 'data') {
319
          $cells = $value;
320
        }
321
        else {
322
          $attributes[$key] = $value;
323
        }
324
      }
325
    }
326
    else {
327
      $cells = $row;
328
    }
329
    if (count($cells)) {
330
      // Add odd/even class
331
      $class = $flip[$class];
332
      if (isset($attributes['class'])) {
333
        $attributes['class'] .= ' ' . $class;
334
      }
335
      else {
336
        $attributes['class'] = $class;
337
      }
338

    
339
      // Build row
340
      $output .= ' <tr' . drupal_attributes($attributes) . '>';
341
      $i = 0;
342
      foreach ($cells as $cell) {
343
        $cell = tablesort_cell($cell, $vars['header'], $ts, $i++);
344
        $output .= _theme_table_cell($cell);
345
      }
346
      $output .= " </tr>\n";
347
    }
348
  }
349

    
350

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

    
353
}
354

    
355
function template_preprocess_views_data_export_doc_header(&$vars) {
356
  // Pass through the generic MS Office preprocess.
357
  template_preprocess_views_data_export_msoffice_header($vars);
358
}
359

    
360
function template_preprocess_views_data_export_xls_header(&$vars) {
361
  // Pass through the generic MS Office preprocess.
362
  template_preprocess_views_data_export_msoffice_header($vars);
363
}
364

    
365
function template_preprocess_views_data_export_msoffice_header(&$vars) {
366
  _views_data_export_header_shared_preprocess($vars);
367

    
368
  // Need to do a little work to construct the table header, see theme_table().
369
  $vars['header_row'] = '';
370
  $vars['header_row'] .= '<thead><tr>';
371

    
372
  $ts = tablesort_init($vars['header']);
373

    
374
  foreach ($vars['header'] as $cell) {
375
    $cell = tablesort_header($cell, $vars['header'], $ts);
376
    $vars['header_row'] .= _theme_table_cell($cell, TRUE);
377
  }
378

    
379
  $vars['header_row'] .= '</tr></thead>';
380

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

    
384
/**
385
 * Preprocess xml output template.
386
 */
387
function template_preprocess_views_data_export_xml_header(&$vars) {
388
  $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']);
389
}
390

    
391
/**
392
 * Preprocess xml output template.
393
 */
394
function template_preprocess_views_data_export_xml_footer(&$vars) {
395
  $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']);
396
}
397

    
398
/**
399
 * Preprocess xml output template.
400
 */
401
function template_preprocess_views_data_export_xml_body(&$vars) {
402
  _views_data_export_header_shared_preprocess($vars);
403
  // We support not outputting fields when they are empty, so indicate so.
404
  $vars['hide_empty_support'] = TRUE;
405
  _views_data_export_body_shared_preprocess($vars);
406

    
407
  $view = $vars['view'];
408
  $style_options = $view->display_handler->get_option('style_options');
409

    
410
  $no_encode = isset($style_options['no_entity_encode']) ? $style_options['no_entity_encode'] : array();
411

    
412
  $cdata_wrapper = isset($style_options['cdata_wrapper']) ? $style_options['cdata_wrapper'] : array();
413

    
414
  $vars['item_node'] = _views_data_export_xml_tag_clean($vars['options']['item_node']);
415

    
416
  foreach ($vars['themed_rows'] as $num => $row) {
417
    foreach ($row as $field => $content) {
418

    
419
      // Perform xml entity encoding unless excluded by style options.
420
      if (empty($no_encode[$field]) && empty($cdata_wrapper[$field])) {
421

    
422
        // Prevent double encoding of the ampersand. Look for the entities produced by check_plain().
423
        $content = preg_replace('/&(?!(amp|quot|#039|lt|gt);)/', '&amp;', $content);
424
        // Convert < and > to HTML entities.
425
        $content = str_replace(
426
          array('<', '>'),
427
          array('&lt;', '&gt;'),
428
          $content);
429
      }
430

    
431
      // Perform wrapping the field data using the CDATA tag
432
      // unless excluded by style options.
433
      if (!empty($cdata_wrapper[$field])) {
434
        // Escape CDATA end sequence only.
435
        $content = '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $content) . ']]>';
436
      }
437

    
438
      $vars['themed_rows'][$num][$field] = $content;
439
    }
440
  }
441

    
442
  foreach ($vars['header'] as $field => $header) {
443
    // If there is no field label, use 'no name'.
444
    if (empty($header)) {
445
      $header = 'no name';
446
    }
447
    if ($vars['options']['transform']) {
448
      switch ($vars['options']['transform_type']) {
449
        case 'dash':
450
          $vars['xml_tag'][$field] = str_replace(' ', '-', $header);
451
          break;
452
        case 'underline':
453
          $vars['xml_tag'][$field] = str_replace(' ', '_', $header);
454
          break;
455
        case 'camel':
456
          $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header)));
457
          // Convert the very first character of the string to lowercase.
458
          $vars['xml_tag'][$field][0] = strtolower($vars['xml_tag'][$field][0]);
459
          break;
460
        case 'pascal':
461
          $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header)));
462
          break;
463
      }
464
    }
465
    // We should always try to output valid XML.
466
    $vars['xml_tag'][$field] = _views_data_export_xml_tag_clean($vars['xml_tag'][$field]);
467
  }
468
}
469

    
470
/**
471
 * Returns a valid XML tag formed from the given input.
472
 *
473
 * @param $tag The string that should be made into a valid XML tag.
474
 * @return The valid XML tag or an empty string if the string contained no valid
475
 * XML tag characters.
476
 */
477
function _views_data_export_xml_tag_clean($tag) {
478

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

    
484
  // These characters are not valid at the start of an XML tag:
485
  static $invalid_start_chars = '-.0123456789';
486

    
487
  // Convert invalid chars to '-':
488
  $tag = preg_replace($invalid_tag_chars_regex, '-', $tag);
489

    
490
  // Need to trim invalid characters from the start of the string:
491
  $tag = ltrim($tag, $invalid_start_chars);
492

    
493
  // As a last line of defense, if we've stripped out everything, set it to
494
  // something.
495
  if (empty($tag)) {
496
    $tag = 'invalid-tag-name';
497
  }
498

    
499
  return $tag;
500
}
501

    
502
/**
503
 * Shared helper function for export preprocess functions.
504
 */
505
function _views_data_export_header_shared_preprocess(&$vars) {
506
  $view     = $vars['view'];
507
  $fields   = &$view->field;
508
  $fields_info = $view->display_handler->get_option('fields');
509
  $vars['header'] = array();
510
  foreach ($fields as $key => $field) {
511
    if (empty($field->options['exclude'])) {
512
      if (isset($fields_info) && isset($fields_info[$key]['label'])) {
513
        $vars['header'][$key] = check_plain($fields_info[$key]['label']);
514
      }
515
      else {
516
        $vars['header'][$key] = check_plain($field->label());
517
      }
518
    }
519
  }
520
}
521

    
522
/**
523
 * Shared helper function for export preprocess functions.
524
 */
525
function _views_data_export_body_shared_preprocess(&$vars) {
526
  $view     = $vars['view'];
527
  $fields   = &$view->field;
528
  $hide_empty_support = !empty($vars['hide_empty_support']);
529

    
530
  $rows = $vars['rows'];
531

    
532
  $vars['themed_rows'] = array();
533
  $keys = array_keys($fields);
534
  foreach ($rows as $num => $row) {
535
    $vars['themed_rows'][$num] = array();
536

    
537
    foreach ($keys as $id) {
538
      if (empty($fields[$id]->options['exclude'])) {
539
        $content = $view->style_plugin->rendered_fields[$num][$id];
540

    
541
        if ($hide_empty_support && !empty($fields[$id]->options['hide_empty'])) {
542
          if ($fields[$id]->is_value_empty($content, $fields[$id]->options['empty_zero'])) {
543
            continue;
544
          }
545
        }
546

    
547
        $vars['themed_rows'][$num][$id] = $content;
548
      }
549
    }
550
  }
551
}