Projet

Général

Profil

Paste
Télécharger (39,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / theme / theme.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Preprocessors and helper functions to make theming easier.
6
 */
7

    
8
/**
9
 * Provide a full array of possible themes to try for a given hook.
10
 *
11
 * @param string $hook
12
 *   The hook to use. This is the base theme/template name.
13
 * @param object $view
14
 *   The view being rendered.
15
 * @param object $display
16
 *   The display being rendered, if applicable.
17
 */
18
function _views_theme_functions($hook, $view, $display = NULL) {
19
  $themes = array();
20

    
21
  if ($display) {
22
    $themes[] = $hook . '__' . $view->name . '__' . $display->id;
23
    $themes[] = $hook . '__' . $display->id;
24
    $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($view->tag));
25

    
26
    // Add theme suggestions foreach single tag.
27
    foreach (drupal_explode_tags($view->tag) as $tag) {
28
      $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag));
29
    }
30

    
31
    if ($display->id != $display->display_plugin) {
32
      $themes[] = $hook . '__' . $view->name . '__' . $display->display_plugin;
33
      $themes[] = $hook . '__' . $display->display_plugin;
34
    }
35
  }
36
  $themes[] = $hook . '__' . $view->name;
37
  $themes[] = $hook;
38
  return $themes;
39
}
40

    
41
/**
42
 * Preprocess the primary theme implementation for a view.
43
 */
44
function template_preprocess_views_view(&$vars) {
45
  global $base_path;
46

    
47
  $view = $vars['view'];
48

    
49
  $vars['rows']       = (!empty($view->result) || $view->style_plugin->even_empty()) ? $view->style_plugin->render($view->result) : '';
50

    
51
  $vars['css_name']   = drupal_clean_css_identifier($view->name);
52
  $vars['name']       = $view->name;
53
  $vars['display_id'] = $view->current_display;
54

    
55
  // Basic classes.
56
  $vars['css_class'] = '';
57

    
58
  $vars['classes_array'] = array();
59
  $vars['classes_array'][] = 'view';
60
  $vars['classes_array'][] = 'view-' . drupal_clean_css_identifier($vars['name']);
61
  $vars['classes_array'][] = 'view-id-' . $vars['name'];
62
  $vars['classes_array'][] = 'view-display-id-' . $vars['display_id'];
63

    
64
  $css_class = $view->display_handler->get_option('css_class');
65
  if (!empty($css_class)) {
66
    $vars['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
67
    $vars['classes_array'][] = $vars['css_class'];
68
  }
69

    
70
  $empty = empty($vars['rows']);
71

    
72
  $vars['header'] = $view->display_handler->render_area('header', $empty);
73
  $vars['footer'] = $view->display_handler->render_area('footer', $empty);
74
  if ($empty) {
75
    $vars['empty'] = $view->display_handler->render_area('empty', $empty);
76
  }
77

    
78
  $vars['exposed']    = !empty($view->exposed_widgets) ? $view->exposed_widgets : '';
79
  $vars['more']       = $view->display_handler->render_more_link();
80
  $vars['feed_icon']  = !empty($view->feed_icon) ? $view->feed_icon : '';
81

    
82
  $vars['pager']      = '';
83

    
84
  // @todo Figure out whether this belongs into views_ui_preprocess_views_view.
85
  // Render title for the admin preview.
86
  $vars['title'] = !empty($view->views_ui_context) ? filter_xss_admin($view->get_title()) : '';
87

    
88
  if ($view->display_handler->render_pager()) {
89
    $exposed_input = $view->get_exposed_input();
90
    $vars['pager']      = $view->query->render_pager($exposed_input);
91
  }
92

    
93
  $vars['attachment_before'] = !empty($view->attachment_before) ? $view->attachment_before : '';
94
  $vars['attachment_after'] = !empty($view->attachment_after) ? $view->attachment_after : '';
95

    
96
  // Add contextual links to the view. We need to attach them to the dummy
97
  // $view_array variable, since contextual_preprocess() requires that they be
98
  // attached to an array (not an object) in order to process them. For our
99
  // purposes, it doesn't matter what we attach them to, since once they are
100
  // processed by contextual_preprocess() they will appear in the $title_suffix
101
  // variable (which we will then render in views-view.tpl.php).
102
  views_add_contextual_links($vars['view_array'], 'view', $view, $view->current_display);
103

    
104
  // Attachments are always updated with the outer view, never by themselves,
105
  // so they do not have dom ids.
106
  if (empty($view->is_attachment)) {
107
    // Our JavaScript needs to have some means to find the HTML belonging to
108
    // this view.
109
    //
110
    // It is true that the DIV wrapper has classes denoting the name of the view
111
    // and its display ID, but this is not enough to unequivocally match a view
112
    // with its HTML, because one view may appear several times on the page. So
113
    // we set up a hash with the current time, $dom_id, to issue a "unique"
114
    // identifier for each view. This identifier is written to both
115
    // Drupal.settings and the DIV wrapper.
116
    $vars['dom_id'] = $view->dom_id;
117
    $vars['classes_array'][] = 'view-dom-id-' . $vars['dom_id'];
118
  }
119

    
120
  // If using AJAX, send identifying data about this view.
121
  if ($view->use_ajax && empty($view->is_attachment) && empty($view->live_preview)) {
122
    $settings = array(
123
      'views' => array(
124
        'ajax_path' => url('views/ajax'),
125
        'ajaxViews' => array(
126
          'views_dom_id:' . $vars['dom_id'] => array(
127
            'view_name' => $view->name,
128
            'view_display_id' => $view->current_display,
129
            'view_args' => check_plain(implode('/', $view->args)),
130
            'view_path' => check_plain($_GET['q']),
131
            // Pass through URL to ensure we get e.g. language prefixes.
132
            // 'view_base_path' => isset($view->display['page']) ?
133
            //   substr(url($view->display['page']->display_options['path']),
134
            //   strlen($base_path)) : '',
135
            'view_base_path' => $view->get_path(),
136
            'view_dom_id' => $vars['dom_id'],
137
            // To fit multiple views on a page, the programmer may have
138
            // overridden the display's pager_element.
139
            'pager_element' => isset($view->query->pager) ? $view->query->pager->get_pager_id() : 0,
140
          ),
141
        ),
142
      ),
143
      // Support for AJAX path validation in core 7.39.
144
      'urlIsAjaxTrusted' => array(
145
        url('views/ajax') => TRUE,
146
      ),
147
    );
148

    
149
    drupal_add_js($settings, 'setting');
150
    views_add_js('ajax_view');
151
  }
152

    
153
  // If form fields were found in the View, reformat the View output as a form.
154
  if (views_view_has_form_elements($view)) {
155
    $output = !empty($vars['rows']) ? $vars['rows'] : $vars['empty'];
156
    $form = drupal_get_form(views_form_id($view), $view, $output);
157
    // The form is requesting that all non-essential views elements be hidden,
158
    // usually because the rendered step is not a view result.
159
    if ($form['show_view_elements']['#value'] == FALSE) {
160
      $vars['header'] = '';
161
      $vars['exposed'] = '';
162
      $vars['pager'] = '';
163
      $vars['footer'] = '';
164
      $vars['more'] = '';
165
      $vars['feed_icon'] = '';
166
    }
167
    $vars['rows'] = $form;
168
  }
169
}
170

    
171
/**
172
 * Process function to render certain elements into the view.
173
 */
174
function template_process_views_view(&$vars) {
175
  if (is_array($vars['rows'])) {
176
    $vars['rows'] = drupal_render($vars['rows']);
177
  }
178

    
179
  // Flatten the classes to a string for the template file.
180
  $vars['classes'] = implode(' ', $vars['classes_array']);
181
}
182

    
183
/**
184
 * Preprocess theme function to print a single record from a row, with fields.
185
 */
186
function template_preprocess_views_view_fields(&$vars) {
187
  $view = $vars['view'];
188

    
189
  // Loop through the fields for this view.
190
  $previous_inline = FALSE;
191
  $vars['fields'] = array();
192
  // Ensure it's at least an empty array.
193
  foreach ($view->field as $id => $field) {
194
    // Render this even if set to exclude so it can be used elsewhere.
195
    $field_output = $view->style_plugin->get_field($view->row_index, $id);
196
    $empty = $field->is_value_empty($field_output, $field->options['empty_zero']);
197
    if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($vars['options']['hide_empty'])))) {
198
      $object = new stdClass();
199
      $object->handler = &$view->field[$id];
200
      $object->inline = !empty($vars['options']['inline'][$id]);
201

    
202
      $object->element_type = $object->handler->element_type(TRUE, !$vars['options']['default_field_elements'], $object->inline);
203
      if ($object->element_type) {
204
        $class = '';
205
        if ($object->handler->options['element_default_classes']) {
206
          $class = 'field-content';
207
        }
208

    
209
        if ($classes = $object->handler->element_classes($view->row_index)) {
210
          if ($class) {
211
            $class .= ' ';
212
          }
213
          $class .= $classes;
214
        }
215

    
216
        $pre = '<' . $object->element_type;
217
        if ($class) {
218
          $pre .= ' class="' . $class . '"';
219
        }
220
        $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
221
      }
222

    
223
      // Protect ourself somewhat for backward compatibility. This will prevent
224
      // old templates from producing invalid HTML when no element type is
225
      // selected.
226
      if (empty($object->element_type)) {
227
        $object->element_type = 'span';
228
      }
229

    
230
      $object->content = $field_output;
231
      if (isset($view->field[$id]->field_alias) && isset($vars['row']->{$view->field[$id]->field_alias})) {
232
        $object->raw = $vars['row']->{$view->field[$id]->field_alias};
233
      }
234
      else {
235
        // Make sure it exists to reduce NOTICE.
236
        $object->raw = NULL;
237
      }
238

    
239
      if (!empty($vars['options']['separator']) && $previous_inline && $object->inline && $object->content) {
240
        $object->separator = filter_xss_admin($vars['options']['separator']);
241
      }
242

    
243
      $object->class = drupal_clean_css_identifier($id);
244

    
245
      $previous_inline = $object->inline;
246
      $object->inline_html = $object->handler->element_wrapper_type(TRUE, TRUE);
247
      if ($object->inline_html === '' && $vars['options']['default_field_elements']) {
248
        $object->inline_html = $object->inline ? 'span' : 'div';
249
      }
250

    
251
      // Set up the wrapper HTML.
252
      $object->wrapper_prefix = '';
253
      $object->wrapper_suffix = '';
254

    
255
      if ($object->inline_html) {
256
        $class = '';
257
        if ($object->handler->options['element_default_classes']) {
258
          $class = "views-field views-field-" . $object->class;
259
        }
260

    
261
        if ($classes = $object->handler->element_wrapper_classes($view->row_index)) {
262
          if ($class) {
263
            $class .= ' ';
264
          }
265
          $class .= $classes;
266
        }
267

    
268
        $object->wrapper_prefix = '<' . $object->inline_html;
269
        if ($class) {
270
          $object->wrapper_prefix .= ' class="' . $class . '"';
271
        }
272
        $object->wrapper_prefix .= '>';
273
        $object->wrapper_suffix = '</' . $object->inline_html . '>';
274
      }
275

    
276
      // Set up the label for the value and the HTML to make it easier on the
277
      // template.
278
      $object->label = check_plain($view->field[$id]->label());
279
      $object->label_html = '';
280
      if ($object->label) {
281
        $object->label_html .= $object->label;
282
        if ($object->handler->options['element_label_colon']) {
283
          $object->label_html .= ': ';
284
        }
285

    
286
        $object->element_label_type = $object->handler->element_label_type(TRUE, !$vars['options']['default_field_elements']);
287
        if ($object->element_label_type) {
288
          $class = '';
289
          if ($object->handler->options['element_default_classes']) {
290
            $class = 'views-label views-label-' . $object->class;
291
          }
292

    
293
          $element_label_class = $object->handler->element_label_classes($view->row_index);
294
          if ($element_label_class) {
295
            if ($class) {
296
              $class .= ' ';
297
            }
298

    
299
            $class .= $element_label_class;
300
          }
301

    
302
          $pre = '<' . $object->element_label_type;
303
          if ($class) {
304
            $pre .= ' class="' . $class . '"';
305
          }
306
          $pre .= '>';
307

    
308
          $object->label_html = $pre . $object->label_html . '</' . $object->element_label_type . '>';
309
        }
310
      }
311

    
312
      $vars['fields'][$id] = $object;
313
    }
314
  }
315
}
316

    
317
/**
318
 * Display a single views grouping.
319
 */
320
function theme_views_view_grouping($vars) {
321
  $view = $vars['view'];
322
  $title = $vars['title'];
323
  $content = $vars['content'];
324

    
325
  $output = '<div class="view-grouping">';
326
  $output .= '<div class="view-grouping-header">' . $title . '</div>';
327
  $output .= '<div class="view-grouping-content">' . $content . '</div>';
328
  $output .= '</div>';
329

    
330
  return $output;
331
}
332

    
333
/**
334
 * Process a single grouping within a view.
335
 */
336
function template_preprocess_views_view_grouping(&$vars) {
337
  $vars['content'] = $vars['view']->style_plugin->render_grouping_sets($vars['rows'], $vars['grouping_level']);
338
}
339

    
340
/**
341
 * Display a single views field.
342
 *
343
 * Interesting bits of info:
344
 * $field->field_alias says what the raw value in $row will be. Reach it like
345
 * this: @code { $row->{$field->field_alias} @endcode.
346
 */
347
function theme_views_view_field($vars) {
348
  $view = $vars['view'];
349
  $field = $vars['field'];
350
  $row = $vars['row'];
351
  return $vars['output'];
352
}
353

    
354
/**
355
 * Process a single field within a view.
356
 *
357
 * This preprocess function isn't normally run, as a function is used by
358
 * default, for performance. However, by creating a template, this
359
 * preprocess should get picked up.
360
 */
361
function template_preprocess_views_view_field(&$vars) {
362
  $vars['output'] = $vars['field']->advanced_render($vars['row']);
363
}
364

    
365
/**
366
 * Preprocess theme function to print a single record from a row, with fields.
367
 */
368
function template_preprocess_views_view_summary(&$vars) {
369
  $view     = $vars['view'];
370
  $argument = $view->argument[$view->build_info['summary_level']];
371
  $vars['row_classes'] = array();
372

    
373
  $url_options = array();
374

    
375
  if (!empty($view->exposed_raw_input)) {
376
    $url_options['query'] = $view->exposed_raw_input;
377
  }
378

    
379
  $active_urls = drupal_map_assoc(array(
380
    // Force system path.
381
    url($_GET['q'], array('alias' => TRUE)),
382
    url($_GET['q'], $url_options + array('alias' => TRUE)),
383
    // Could be an alias.
384
    url($_GET['q']),
385
    url($_GET['q'], $url_options),
386
  ));
387

    
388
  // Collect all arguments foreach row, to be able to alter them for example by
389
  // the validator. This is not done per single argument value, because this
390
  // could cause performance problems.
391
  $row_args = array();
392

    
393
  foreach ($vars['rows'] as $id => $row) {
394
    $row_args[$id] = $argument->summary_argument($row);
395
  }
396
  $argument->process_summary_arguments($row_args);
397

    
398
  foreach ($vars['rows'] as $id => $row) {
399
    $vars['row_classes'][$id] = '';
400

    
401
    $vars['rows'][$id]->link = $argument->summary_name($row);
402
    $args = $view->args;
403
    $args[$argument->position] = $row_args[$id];
404

    
405
    $base_path = NULL;
406
    if (!empty($argument->options['summary_options']['base_path'])) {
407
      $base_path = $argument->options['summary_options']['base_path'];
408
    }
409
    $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
410
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
411
    if (isset($active_urls[$vars['rows'][$id]->url])) {
412
      $vars['row_classes'][$id] = 'active';
413
    }
414
  }
415
}
416

    
417
/**
418
 * Template preprocess theme function to print summary basically unformatted.
419
 */
420
function template_preprocess_views_view_summary_unformatted(&$vars) {
421
  $view     = $vars['view'];
422
  $argument = $view->argument[$view->build_info['summary_level']];
423
  $vars['row_classes'] = array();
424

    
425
  $url_options = array();
426

    
427
  if (!empty($view->exposed_raw_input)) {
428
    $url_options['query'] = $view->exposed_raw_input;
429
  }
430

    
431
  $count = 0;
432
  $active_urls = drupal_map_assoc(array(
433
    // Force system path.
434
    url($_GET['q'], array('alias' => TRUE)),
435
    url($_GET['q'], $url_options + array('alias' => TRUE)),
436
    // Could be an alias.
437
    url($_GET['q']),
438
    url($_GET['q'], $url_options),
439
  ));
440

    
441
  // Collect all arguments foreach row, to be able to alter them for example by
442
  // the validator. This is not done per single argument value, because this
443
  // could cause performance problems.
444
  $row_args = array();
445
  foreach ($vars['rows'] as $id => $row) {
446
    $row_args[$id] = $argument->summary_argument($row);
447
  }
448
  $argument->process_summary_arguments($row_args);
449

    
450
  foreach ($vars['rows'] as $id => $row) {
451
    // Only false on first time.
452
    if ($count++) {
453
      $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
454
    }
455
    $vars['rows'][$id]->link = $argument->summary_name($row);
456
    $args = $view->args;
457
    $args[$argument->position] = $row_args[$id];
458

    
459
    $base_path = NULL;
460
    if (!empty($argument->options['summary_options']['base_path'])) {
461
      $base_path = $argument->options['summary_options']['base_path'];
462
    }
463
    $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
464
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
465
    if (isset($active_urls[$vars['rows'][$id]->url])) {
466
      $vars['row_classes'][$id] = 'active';
467
    }
468
  }
469
}
470

    
471
/**
472
 * Display a view as a table style.
473
 */
474
function template_preprocess_views_view_table(&$vars) {
475
  $view     = $vars['view'];
476

    
477
  // We need the raw data for this grouping, which is passed in as
478
  // $vars['rows']. However, the template also needs to use for the rendered
479
  // fields. We therefore swap the raw data out to a new variable and reset
480
  // $vars['rows'] so that it can get rebuilt. Store rows so that they may be
481
  // used by further preprocess functions.
482
  $result   = $vars['result'] = $vars['rows'];
483
  $vars['rows'] = array();
484
  $vars['field_classes'] = array();
485
  $vars['header'] = array();
486
  $vars['classes_array'] = array();
487

    
488
  $options  = $view->style_plugin->options;
489
  $handler  = $view->style_plugin;
490
  
491
  if (!empty($handler->options['class'])) {
492
    $classes = explode(' ', $handler->options['class']);
493
    $classes = array_map('views_clean_css_identifier', $classes);
494

    
495
    if (!empty($classes)) {
496
      // Trim empty class entries.
497
      foreach ($classes as $key => $class) {
498
        if (!empty($class)) {
499
          $vars['classes_array'][] = $class;
500
        }
501
      }
502
    }
503
  }
504

    
505
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
506
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
507

    
508
  $fields   = &$view->field;
509
  $columns  = $handler->sanitize_columns($options['columns'], $fields);
510

    
511
  $active   = !empty($handler->active) ? $handler->active : '';
512
  $order    = !empty($handler->order) ? $handler->order : 'asc';
513

    
514
  $query    = tablesort_get_query_parameters();
515
  if (isset($view->exposed_raw_input)) {
516
    $query += $view->exposed_raw_input;
517
  }
518

    
519
  // Fields must be rendered in order as of Views 2.3, so we will pre-render
520
  // everything.
521
  $renders = $handler->render_fields($result);
522

    
523
  foreach ($columns as $field => $column) {
524
    // Create a second variable so we can easily find what fields we have and
525
    // what the CSS classes should be.
526
    $vars['fields'][$field] = drupal_clean_css_identifier($field);
527
    if ($active == $field) {
528
      $vars['fields'][$field] .= ' active';
529
    }
530

    
531
    // Render the header labels.
532
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
533
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
534
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
535
        $vars['header'][$field] = $label;
536
      }
537
      else {
538
        $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
539

    
540
        if ($active == $field) {
541
          $initial = ($order == 'asc') ? 'desc' : 'asc';
542
        }
543

    
544
        $title = t('sort by @s', array('@s' => $label));
545
        if ($active == $field) {
546
          $label .= theme('tablesort_indicator', array('style' => $initial));
547
        }
548

    
549
        $query['order'] = $field;
550
        $query['sort'] = $initial;
551
        $link_options = array(
552
          'html' => TRUE,
553
          'attributes' => array('title' => $title),
554
          'query' => $query,
555
        );
556
        $vars['header'][$field] = l($label, $_GET['q'], $link_options);
557
      }
558

    
559
      $vars['header_classes'][$field] = '';
560
      // Set up the header label class.
561
      if ($fields[$field]->options['element_default_classes']) {
562
        $vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
563
      }
564
      $class = $fields[$field]->element_label_classes(0);
565
      if ($class) {
566
        if ($vars['header_classes'][$field]) {
567
          $vars['header_classes'][$field] .= ' ';
568
        }
569
        $vars['header_classes'][$field] .= $class;
570
      }
571
      // Add a CSS align class to each field if one was set.
572
      if (!empty($options['info'][$field]['align'])) {
573
        $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
574
      }
575

    
576
      // Add a header label wrapper if one was selected.
577
      if ($vars['header'][$field]) {
578
        $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
579
        if ($element_label_type) {
580
          $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
581
        }
582
      }
583
    }
584

    
585
    // Add a CSS align class to each field if one was set.
586
    if (!empty($options['info'][$field]['align'])) {
587
      $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
588
    }
589

    
590
    // Render each field into its appropriate column.
591
    foreach ($result as $num => $row) {
592
      // Add field classes.
593
      $vars['field_classes'][$field][$num] = '';
594
      if ($fields[$field]->options['element_default_classes']) {
595
        $vars['field_classes'][$field][$num] = "views-field views-field-" . $vars['fields'][$field];
596
      }
597
      if ($classes = $fields[$field]->element_classes($num)) {
598
        if ($vars['field_classes'][$field][$num]) {
599
          $vars['field_classes'][$field][$num] .= ' ';
600
        }
601

    
602
        $vars['field_classes'][$field][$num] .= $classes;
603
      }
604
      $vars['field_attributes'][$field][$num] = array();
605

    
606
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
607
        $field_output = $renders[$num][$field];
608
        $element_type = $fields[$field]->element_type(TRUE, TRUE);
609
        if ($element_type) {
610
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
611
        }
612

    
613
        // Don't bother with separators and stuff if the field does not show up.
614
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
615
          continue;
616
        }
617

    
618
        // Place the field into the column, along with an optional separator.
619
        if (!empty($vars['rows'][$num][$column])) {
620
          if (!empty($options['info'][$column]['separator'])) {
621
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
622
          }
623
        }
624
        else {
625
          $vars['rows'][$num][$column] = '';
626
        }
627

    
628
        $vars['rows'][$num][$column] .= $field_output;
629
      }
630
    }
631

    
632
    // Remove columns if the option is hide empty column is checked and the
633
    // field is not empty.
634
    if (!empty($options['info'][$field]['empty_column'])) {
635
      $empty = TRUE;
636
      foreach ($vars['rows'] as $num => $columns) {
637
        $empty &= empty($columns[$column]);
638
      }
639
      if ($empty) {
640
        foreach ($vars['rows'] as $num => &$column_items) {
641
          unset($column_items[$column]);
642
          unset($vars['header'][$column]);
643
        }
644
      }
645
    }
646
  }
647

    
648
  // Hide table header if all labels are empty.
649
  if (!array_filter($vars['header'])) {
650
    $vars['header'] = array();
651
  }
652

    
653
  $count = 0;
654
  foreach ($vars['rows'] as $num => $row) {
655
    $vars['row_classes'][$num] = array();
656
    if ($row_class_special) {
657
      $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
658
    }
659
    if ($row_class = $handler->get_row_class($num)) {
660
      $vars['row_classes'][$num][] = $row_class;
661
    }
662
  }
663

    
664
  if ($row_class_special) {
665
    $vars['row_classes'][0][] = 'views-row-first';
666
    $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
667
  }
668

    
669
  $vars['classes_array'][] = 'views-table';
670
  if (empty($vars['rows']) && !empty($options['empty_table'])) {
671
    $vars['rows'][0][0] = $view->display_handler->render_area('empty');
672
    // Calculate the amounts of rows with output.
673
    $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
674
    $vars['field_classes'][0][0] = 'views-empty';
675
  }
676

    
677
  if (!empty($options['sticky'])) {
678
    drupal_add_js('misc/tableheader.js');
679
    $vars['classes_array'][] = "sticky-enabled";
680
  }
681
  $vars['classes_array'][] = 'cols-' . count($vars['header']);
682

    
683
  // Add the summary to the list if set.
684
  if (!empty($handler->options['summary'])) {
685
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
686
  }
687

    
688
  // Add the caption to the list if set.
689
  if (!empty($handler->options['caption'])) {
690
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
691
  }
692
  else {
693
    $vars['caption'] = '';
694
  }
695
}
696

    
697
/**
698
 * Display a view as a grid style.
699
 */
700
function template_preprocess_views_view_grid(&$vars) {
701
  $view     = $vars['view'];
702
  $result   = $view->result;
703
  $options  = $view->style_plugin->options;
704
  $handler  = $view->style_plugin;
705
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
706
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
707

    
708
  $columns  = $options['columns'];
709

    
710
  $rows = array();
711
  $row_indexes = array();
712

    
713
  if ($options['alignment'] == 'horizontal') {
714
    $row = array();
715
    $col_count = 0;
716
    $row_count = 0;
717
    $count = 0;
718
    foreach ($vars['rows'] as $row_index => $item) {
719
      $count++;
720
      $row[] = $item;
721
      $row_indexes[$row_count][$col_count] = $row_index;
722
      $col_count++;
723
      if ($count % $columns == 0) {
724
        $rows[] = $row;
725
        $row = array();
726
        $col_count = 0;
727
        $row_count++;
728
      }
729
    }
730
    if ($row) {
731
      // Fill up the last line only if it's configured, but this is default.
732
      if (!empty($handler->options['fill_single_line'])) {
733
        for ($i = 0; $i < ($columns - $col_count); $i++) {
734
          $row[] = '';
735
        }
736
      }
737
      $rows[] = $row;
738
    }
739
  }
740
  else {
741
    $num_rows = floor(count($vars['rows']) / $columns);
742
    // The remainders are the 'odd' columns that are slightly longer.
743
    $remainders = count($vars['rows']) % $columns;
744
    $row = 0;
745
    $col = 0;
746
    foreach ($vars['rows'] as $count => $item) {
747
      $rows[$row][$col] = $item;
748
      $row_indexes[$row][$col] = $count;
749
      $row++;
750

    
751
      if (!$remainders && $row == $num_rows) {
752
        $row = 0;
753
        $col++;
754
      }
755
      elseif ($remainders && $row == $num_rows + 1) {
756
        $row = 0;
757
        $col++;
758
        $remainders--;
759
      }
760
    }
761

    
762
    // Fill out the row with empty values, if needed.
763
    if (!empty($handler->options['fill_single_line'])) {
764
      $column_fill = $columns;
765
    }
766
    else {
767
      $column_fill = count($rows[0]);
768
    }
769
    for ($i = 0; $i < $column_fill; $i++) {
770
      if (!isset($rows[count($rows) - 1][$i])) {
771
        $rows[count($rows) - 1][$i] = '';
772
      }
773
    }
774
  }
775

    
776
  // Apply the row classes.
777
  foreach ($rows as $row_number => $row) {
778
    $row_classes = array();
779
    if ($default_row_class) {
780
      $row_classes[] = 'row-' . ($row_number + 1);
781
    }
782
    if ($row_class_special) {
783
      if ($row_number == 0) {
784
        $row_classes[] = 'row-first';
785
      }
786
      if (count($rows) == ($row_number + 1)) {
787
        $row_classes[] = 'row-last';
788
      }
789
    }
790
    $vars['row_classes'][$row_number] = implode(' ', $row_classes);
791
    foreach ($rows[$row_number] as $column_number => $item) {
792
      $column_classes = array();
793
      if ($default_row_class) {
794
        $column_classes[] = 'col-' . ($column_number + 1);
795
      }
796
      if ($row_class_special) {
797
        if ($column_number == 0) {
798
          $column_classes[] = 'col-first';
799
        }
800
        elseif (count($rows[$row_number]) == ($column_number + 1)) {
801
          $column_classes[] = 'col-last';
802
        }
803
      }
804
      if (isset($row_indexes[$row_number][$column_number]) && $column_class = $view->style_plugin->get_row_class($row_indexes[$row_number][$column_number])) {
805
        $column_classes[] = $column_class;
806
      }
807
      $vars['column_classes'][$row_number][$column_number] = implode(' ', $column_classes);
808
    }
809
  }
810
  $vars['rows'] = $rows;
811
  $vars['class'] = 'views-view-grid cols-' . $columns;
812

    
813
  // Add the summary to the list if set.
814
  if (!empty($handler->options['summary'])) {
815
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
816
  }
817

    
818
  // Add the caption to the list if set.
819
  if (!empty($handler->options['caption'])) {
820
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
821
  }
822
  else {
823
    $vars['caption'] = '';
824
  }
825
}
826

    
827
/**
828
 * Display the simple view of rows one after another.
829
 */
830
function template_preprocess_views_view_unformatted(&$vars) {
831
  $view = $vars['view'];
832
  $rows = $vars['rows'];
833
  $style = $view->style_plugin;
834
  $options = $style->options;
835

    
836
  $vars['classes_array'] = array();
837
  $vars['classes'] = array();
838
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
839
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
840
  // Set up striping values.
841
  $count = 0;
842
  $max = count($rows);
843
  foreach ($rows as $id => $row) {
844
    $count++;
845
    if ($default_row_class) {
846
      $vars['classes'][$id][] = 'views-row';
847
      $vars['classes'][$id][] = 'views-row-' . $count;
848
    }
849
    if ($row_class_special) {
850
      $vars['classes'][$id][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
851
      if ($count == 1) {
852
        $vars['classes'][$id][] = 'views-row-first';
853
      }
854
      if ($count == $max) {
855
        $vars['classes'][$id][] = 'views-row-last';
856
      }
857
    }
858

    
859
    if ($row_class = $view->style_plugin->get_row_class($id)) {
860
      $vars['classes'][$id][] = $row_class;
861
    }
862

    
863
    // Flatten the classes to a string for each row for the template file.
864
    $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
865
  }
866
}
867

    
868
/**
869
 * Display the view as an HTML list element.
870
 */
871
function template_preprocess_views_view_list(&$vars) {
872
  $handler  = $vars['view']->style_plugin;
873

    
874
  $class = explode(' ', $handler->options['class']);
875
  $class = array_map('views_clean_css_identifier', $class);
876

    
877
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
878
  $wrapper_class = array_map('views_clean_css_identifier', $wrapper_class);
879

    
880
  $vars['class'] = implode(' ', $class);
881
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
882
  $vars['wrapper_prefix'] = '';
883
  $vars['wrapper_suffix'] = '';
884
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . '>';
885
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
886
  if ($vars['wrapper_class']) {
887
    $vars['wrapper_prefix'] = '<div class="' . $vars['wrapper_class'] . '">';
888
    $vars['wrapper_suffix'] = '</div>';
889
  }
890

    
891
  if ($vars['class']) {
892
    $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
893
  }
894
  template_preprocess_views_view_unformatted($vars);
895
}
896

    
897
/**
898
 * Preprocess an RSS feed.
899
 */
900
function template_preprocess_views_view_rss(&$vars) {
901
  global $base_url;
902
  global $language;
903

    
904
  $view     = &$vars['view'];
905
  $options  = &$vars['options'];
906
  $items    = &$vars['rows'];
907

    
908
  $style    = &$view->style_plugin;
909

    
910
  // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
911
  // We strip all HTML tags, but need to prevent double encoding from properly
912
  // escaped source data (such as &amp becoming &amp;amp;).
913
  $vars['description'] = check_plain(decode_entities(strip_tags($style->get_description())));
914

    
915
  if ($view->display_handler->get_option('sitename_title')) {
916
    $title = variable_get('site_name', 'Drupal');
917
    if ($slogan = variable_get('site_slogan', '')) {
918
      $title .= ' - ' . $slogan;
919
    }
920
  }
921
  else {
922
    $title = $view->get_title();
923
  }
924
  $vars['title'] = check_plain($title);
925

    
926
  // Figure out which display which has a path we're using for this feed. If
927
  // there isn't one, use the global $base_url.
928
  $link_display_id = $view->display_handler->get_link_display();
929
  if ($link_display_id && !empty($view->display[$link_display_id])) {
930
    $path = $view->display[$link_display_id]->handler->get_path();
931
  }
932

    
933
  if ($path) {
934
    $path = $view->get_url(NULL, $path);
935
    $url_options = array('absolute' => TRUE);
936
    if (!empty($view->exposed_raw_input)) {
937
      $url_options['query'] = $view->exposed_raw_input;
938
    }
939

    
940
    // Compare the link to the default home page; if it's the default home
941
    // page, just use $base_url.
942
    if ($path == variable_get('site_frontpage', 'node')) {
943
      $path = '';
944
    }
945

    
946
    $vars['link'] = check_url(url($path, $url_options));
947
  }
948

    
949
  $vars['langcode'] = check_plain($language->language);
950
  $vars['namespaces'] = drupal_attributes($style->namespaces);
951
  $vars['items'] = $items;
952
  $vars['channel_elements'] = format_xml_elements($style->channel_elements);
953

    
954
  // During live preview we don't want to output the header since the contents
955
  // of the feed are being displayed inside a normal HTML page.
956
  if (empty($vars['view']->live_preview)) {
957
    drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
958
  }
959
}
960

    
961
/**
962
 * Default theme function for all RSS rows.
963
 */
964
function template_preprocess_views_view_row_rss(&$vars) {
965
  $view     = &$vars['view'];
966
  $options  = &$vars['options'];
967
  $item     = &$vars['row'];
968

    
969
  $vars['title'] = check_plain($item->title);
970
  $vars['link'] = check_url($item->link);
971
  $vars['description'] = check_plain($item->description);
972
  $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
973
}
974

    
975
/**
976
 * Default theme function for all filter forms.
977
 */
978
function template_preprocess_views_exposed_form(&$vars) {
979
  $form = &$vars['form'];
980

    
981
  // Put all single checkboxes together in the last spot.
982
  $checkboxes = '';
983

    
984
  if (!empty($form['q'])) {
985
    $vars['q'] = drupal_render($form['q']);
986
  }
987

    
988
  $vars['widgets'] = array();
989
  foreach ($form['#info'] as $id => $info) {
990
    // Set aside checkboxes.
991
    if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
992
      $checkboxes .= drupal_render($form[$info['value']]);
993
      continue;
994
    }
995
    $widget = new stdClass();
996
    // Set up defaults so that there's always something there.
997
    $widget->label = $widget->operator = $widget->widget = $widget->description = NULL;
998

    
999
    $widget->id = isset($form[$info['value']]['#id']) ? $form[$info['value']]['#id'] : '';
1000

    
1001
    if (!empty($info['label'])) {
1002
      $widget->label = check_plain($info['label']);
1003
    }
1004
    if (!empty($info['operator'])) {
1005
      $widget->operator = drupal_render($form[$info['operator']]);
1006
    }
1007

    
1008
    $widget->widget = drupal_render($form[$info['value']]);
1009

    
1010
    if (!empty($info['description'])) {
1011
      $widget->description = check_plain($info['description']);
1012
    }
1013

    
1014
    $vars['widgets'][$id] = $widget;
1015
  }
1016

    
1017
  // Wrap up all the checkboxes we set aside into a widget.
1018
  if ($checkboxes) {
1019
    $widget = new stdClass();
1020
    // Set up defaults so that there's always something there.
1021
    $widget->label = $widget->operator = $widget->widget = NULL;
1022
    $widget->id = 'checkboxes';
1023
    $widget->widget = $checkboxes;
1024
    $vars['widgets']['checkboxes'] = $widget;
1025
  }
1026

    
1027
  if (isset($form['sort_by'])) {
1028
    $vars['sort_by'] = drupal_render($form['sort_by']);
1029
    $vars['sort_order'] = drupal_render($form['sort_order']);
1030
  }
1031
  if (isset($form['items_per_page'])) {
1032
    $vars['items_per_page'] = drupal_render($form['items_per_page']);
1033
  }
1034
  if (isset($form['offset'])) {
1035
    $vars['offset'] = drupal_render($form['offset']);
1036
  }
1037
  if (isset($form['reset'])) {
1038
    $vars['reset_button'] = drupal_render($form['reset']);
1039
  }
1040
  // This includes the submit button.
1041
  $vars['button'] = drupal_render_children($form);
1042
}
1043

    
1044
/**
1045
 * Theme function for a View with form elements: replace the placeholders.
1046
 */
1047
function theme_views_form_views_form($variables) {
1048
  $form = $variables['form'];
1049

    
1050
  // Placeholders and their substitutions (usually rendered form elements).
1051
  $search = array();
1052
  $replace = array();
1053

    
1054
  // Add in substitutions provided by the form.
1055
  foreach ($form['#substitutions']['#value'] as $substitution) {
1056
    $field_name = $substitution['field_name'];
1057
    $row_id = $substitution['row_id'];
1058

    
1059
    $search[] = $substitution['placeholder'];
1060
    $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
1061
  }
1062
  // Add in substitutions from hook_views_form_substitutions().
1063
  $substitutions = module_invoke_all('views_form_substitutions');
1064
  foreach ($substitutions as $placeholder => $substitution) {
1065
    $search[] = $placeholder;
1066
    $replace[] = $substitution;
1067
  }
1068

    
1069
  // Apply substitutions to the rendered output.
1070
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
1071

    
1072
  // Render and add remaining form fields.
1073
  return drupal_render_children($form);
1074
}
1075

    
1076
/**
1077
 * theme function for a mini pager.
1078
 */
1079
function theme_views_mini_pager($vars) {
1080
  global $pager_page_array, $pager_total;
1081

    
1082
  $tags = $vars['tags'];
1083
  $element = $vars['element'];
1084
  $parameters = $vars['parameters'];
1085

    
1086
  // Current is the page we are currently paged to.
1087
  $pager_current = $pager_page_array[$element] + 1;
1088
  // Max is the maximum page number.
1089
  $pager_max = $pager_total[$element];
1090
  // End of marker calculations.
1091
  if ($pager_total[$element] > 1) {
1092

    
1093
    $li_previous = theme('pager_previous',
1094
      array(
1095
        'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
1096
        'element' => $element,
1097
        'interval' => 1,
1098
        'parameters' => $parameters,
1099
      )
1100
    );
1101
    if (empty($li_previous)) {
1102
      $li_previous = "&nbsp;";
1103
    }
1104

    
1105
    $li_next = theme('pager_next',
1106
      array(
1107
        'text' => (isset($tags[3]) ? $tags[3] : t('››')),
1108
        'element' => $element,
1109
        'interval' => 1,
1110
        'parameters' => $parameters,
1111
      )
1112
    );
1113

    
1114
    if (empty($li_next)) {
1115
      $li_next = "&nbsp;";
1116
    }
1117

    
1118
    $items[] = array(
1119
      'class' => array('pager-previous'),
1120
      'data' => $li_previous,
1121
    );
1122

    
1123
    $items[] = array(
1124
      'class' => array('pager-current'),
1125
      'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
1126
    );
1127

    
1128
    $items[] = array(
1129
      'class' => array('pager-next'),
1130
      'data' => $li_next,
1131
    );
1132

    
1133
    return theme('item_list',
1134
      array(
1135
        'items' => $items,
1136
        'title' => NULL,
1137
        'type' => 'ul',
1138
        'attributes' => array('class' => array('pager')),
1139
      )
1140
    );
1141
  }
1142
}
1143

    
1144
/**
1145
 * Generic <div> container function.
1146
 */
1147
function theme_views_container($variables) {
1148
  $element = $variables['element'];
1149
  return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
1150
}
1151

    
1152
/**
1153
 * @defgroup views_templates Views template files
1154
 * @{
1155
 * All views templates can be overridden with a variety of names, using
1156
 * the view, the display ID of the view, the display type of the view,
1157
 * or some combination thereof.
1158
 *
1159
 * For each view, there will be a minimum of two templates used. The first
1160
 * is used for all views: views-view.tpl.php.
1161
 *
1162
 * The second template is determined by the style selected for the view. Note
1163
 * that certain aspects of the view can also change which style is used; for
1164
 * example, arguments which provide a summary view might change the style to
1165
 * one of the special summary styles.
1166
 *
1167
 * The default style for all views is views-view-unformatted.tpl.php
1168
 *
1169
 * Many styles will then farm out the actual display of each row to a row
1170
 * style; the default row style is views-view-fields.tpl.php.
1171
 *
1172
 * Here is an example of all the templates that will be tried in the following
1173
 * case:
1174
 *
1175
 * View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
1176
 *
1177
 * - views-view--foobar--page.tpl.php
1178
 * - views-view--page.tpl.php
1179
 * - views-view--foobar.tpl.php
1180
 * - views-view.tpl.php
1181
 *
1182
 * - views-view-unformatted--foobar--page.tpl.php
1183
 * - views-view-unformatted--page.tpl.php
1184
 * - views-view-unformatted--foobar.tpl.php
1185
 * - views-view-unformatted.tpl.php
1186
 *
1187
 * - views-view-fields--foobar--page.tpl.php
1188
 * - views-view-fields--page.tpl.php
1189
 * - views-view-fields--foobar.tpl.php
1190
 * - views-view-fields.tpl.php
1191
 *
1192
 * Important! When adding a new template to your theme, be sure to flush the
1193
 * theme registry cache!
1194
 *
1195
 * @see _views_theme_functions()
1196
 * @}
1197
 */