Projet

Général

Profil

Paste
Télécharger (38,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / theme / theme.inc @ 5d12d676

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
    // Could be an alias.
383
    url($_GET['q']),
384
  ));
385

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

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

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

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

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

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

    
423
  $url_options = array();
424

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

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

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

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

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

    
467
/**
468
 * Display a view as a table style.
469
 */
470
function template_preprocess_views_view_table(&$vars) {
471
  $view     = $vars['view'];
472

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

    
483
  $options  = $view->style_plugin->options;
484
  $handler  = $view->style_plugin;
485

    
486
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
487
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
488

    
489
  $fields   = &$view->field;
490
  $columns  = $handler->sanitize_columns($options['columns'], $fields);
491

    
492
  $active   = !empty($handler->active) ? $handler->active : '';
493
  $order    = !empty($handler->order) ? $handler->order : 'asc';
494

    
495
  $query    = tablesort_get_query_parameters();
496
  if (isset($view->exposed_raw_input)) {
497
    $query += $view->exposed_raw_input;
498
  }
499

    
500
  // Fields must be rendered in order as of Views 2.3, so we will pre-render
501
  // everything.
502
  $renders = $handler->render_fields($result);
503

    
504
  foreach ($columns as $field => $column) {
505
    // Create a second variable so we can easily find what fields we have and
506
    // what the CSS classes should be.
507
    $vars['fields'][$field] = drupal_clean_css_identifier($field);
508
    if ($active == $field) {
509
      $vars['fields'][$field] .= ' active';
510
    }
511

    
512
    // Render the header labels.
513
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
514
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
515
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
516
        $vars['header'][$field] = $label;
517
      }
518
      else {
519
        $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
520

    
521
        if ($active == $field) {
522
          $initial = ($order == 'asc') ? 'desc' : 'asc';
523
        }
524

    
525
        $title = t('sort by @s', array('@s' => $label));
526
        if ($active == $field) {
527
          $label .= theme('tablesort_indicator', array('style' => $initial));
528
        }
529

    
530
        $query['order'] = $field;
531
        $query['sort'] = $initial;
532
        $link_options = array(
533
          'html' => TRUE,
534
          'attributes' => array('title' => $title),
535
          'query' => $query,
536
        );
537
        $vars['header'][$field] = l($label, $_GET['q'], $link_options);
538
      }
539

    
540
      $vars['header_classes'][$field] = '';
541
      // Set up the header label class.
542
      if ($fields[$field]->options['element_default_classes']) {
543
        $vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
544
      }
545
      $class = $fields[$field]->element_label_classes(0);
546
      if ($class) {
547
        if ($vars['header_classes'][$field]) {
548
          $vars['header_classes'][$field] .= ' ';
549
        }
550
        $vars['header_classes'][$field] .= $class;
551
      }
552
      // Add a CSS align class to each field if one was set.
553
      if (!empty($options['info'][$field]['align'])) {
554
        $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
555
      }
556

    
557
      // Add a header label wrapper if one was selected.
558
      if ($vars['header'][$field]) {
559
        $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
560
        if ($element_label_type) {
561
          $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
562
        }
563
      }
564
    }
565

    
566
    // Add a CSS align class to each field if one was set.
567
    if (!empty($options['info'][$field]['align'])) {
568
      $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
569
    }
570

    
571
    // Render each field into its appropriate column.
572
    foreach ($result as $num => $row) {
573
      // Add field classes.
574
      $vars['field_classes'][$field][$num] = '';
575
      if ($fields[$field]->options['element_default_classes']) {
576
        $vars['field_classes'][$field][$num] = "views-field views-field-" . $vars['fields'][$field];
577
      }
578
      if ($classes = $fields[$field]->element_classes($num)) {
579
        if ($vars['field_classes'][$field][$num]) {
580
          $vars['field_classes'][$field][$num] .= ' ';
581
        }
582

    
583
        $vars['field_classes'][$field][$num] .= $classes;
584
      }
585
      $vars['field_attributes'][$field][$num] = array();
586

    
587
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
588
        $field_output = $renders[$num][$field];
589
        $element_type = $fields[$field]->element_type(TRUE, TRUE);
590
        if ($element_type) {
591
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
592
        }
593

    
594
        // Don't bother with separators and stuff if the field does not show up.
595
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
596
          continue;
597
        }
598

    
599
        // Place the field into the column, along with an optional separator.
600
        if (!empty($vars['rows'][$num][$column])) {
601
          if (!empty($options['info'][$column]['separator'])) {
602
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
603
          }
604
        }
605
        else {
606
          $vars['rows'][$num][$column] = '';
607
        }
608

    
609
        $vars['rows'][$num][$column] .= $field_output;
610
      }
611
    }
612

    
613
    // Remove columns if the option is hide empty column is checked and the
614
    // field is not empty.
615
    if (!empty($options['info'][$field]['empty_column'])) {
616
      $empty = TRUE;
617
      foreach ($vars['rows'] as $num => $columns) {
618
        $empty &= empty($columns[$column]);
619
      }
620
      if ($empty) {
621
        foreach ($vars['rows'] as $num => &$column_items) {
622
          unset($column_items[$column]);
623
          unset($vars['header'][$column]);
624
        }
625
      }
626
    }
627
  }
628

    
629
  // Hide table header if all labels are empty.
630
  if (!array_filter($vars['header'])) {
631
    $vars['header'] = array();
632
  }
633

    
634
  $count = 0;
635
  foreach ($vars['rows'] as $num => $row) {
636
    $vars['row_classes'][$num] = array();
637
    if ($row_class_special) {
638
      $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
639
    }
640
    if ($row_class = $handler->get_row_class($num)) {
641
      $vars['row_classes'][$num][] = $row_class;
642
    }
643
  }
644

    
645
  if ($row_class_special) {
646
    $vars['row_classes'][0][] = 'views-row-first';
647
    $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
648
  }
649

    
650
  $vars['classes_array'] = array('views-table');
651
  if (empty($vars['rows']) && !empty($options['empty_table'])) {
652
    $vars['rows'][0][0] = $view->display_handler->render_area('empty');
653
    // Calculate the amounts of rows with output.
654
    $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
655
    $vars['field_classes'][0][0] = 'views-empty';
656
  }
657

    
658
  if (!empty($options['sticky'])) {
659
    drupal_add_js('misc/tableheader.js');
660
    $vars['classes_array'][] = "sticky-enabled";
661
  }
662
  $vars['classes_array'][] = 'cols-' . count($vars['header']);
663

    
664
  // Add the summary to the list if set.
665
  if (!empty($handler->options['summary'])) {
666
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
667
  }
668

    
669
  // Add the caption to the list if set.
670
  if (!empty($handler->options['caption'])) {
671
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
672
  }
673
  else {
674
    $vars['caption'] = '';
675
  }
676
}
677

    
678
/**
679
 * Display a view as a grid style.
680
 */
681
function template_preprocess_views_view_grid(&$vars) {
682
  $view     = $vars['view'];
683
  $result   = $view->result;
684
  $options  = $view->style_plugin->options;
685
  $handler  = $view->style_plugin;
686
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
687
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
688

    
689
  $columns  = $options['columns'];
690

    
691
  $rows = array();
692
  $row_indexes = array();
693

    
694
  if ($options['alignment'] == 'horizontal') {
695
    $row = array();
696
    $col_count = 0;
697
    $row_count = 0;
698
    $count = 0;
699
    foreach ($vars['rows'] as $row_index => $item) {
700
      $count++;
701
      $row[] = $item;
702
      $row_indexes[$row_count][$col_count] = $row_index;
703
      $col_count++;
704
      if ($count % $columns == 0) {
705
        $rows[] = $row;
706
        $row = array();
707
        $col_count = 0;
708
        $row_count++;
709
      }
710
    }
711
    if ($row) {
712
      // Fill up the last line only if it's configured, but this is default.
713
      if (!empty($handler->options['fill_single_line']) && count($rows)) {
714
        for ($i = 0; $i < ($columns - $col_count); $i++) {
715
          $row[] = '';
716
        }
717
      }
718
      $rows[] = $row;
719
    }
720
  }
721
  else {
722
    $num_rows = floor(count($vars['rows']) / $columns);
723
    // The remainders are the 'odd' columns that are slightly longer.
724
    $remainders = count($vars['rows']) % $columns;
725
    $row = 0;
726
    $col = 0;
727
    foreach ($vars['rows'] as $count => $item) {
728
      $rows[$row][$col] = $item;
729
      $row_indexes[$row][$col] = $count;
730
      $row++;
731

    
732
      if (!$remainders && $row == $num_rows) {
733
        $row = 0;
734
        $col++;
735
      }
736
      elseif ($remainders && $row == $num_rows + 1) {
737
        $row = 0;
738
        $col++;
739
        $remainders--;
740
      }
741
    }
742
    for ($i = 0; $i < count($rows[0]); $i++) {
743
      // This should be string so that's okay :)
744
      if (!isset($rows[count($rows) - 1][$i])) {
745
        $rows[count($rows) - 1][$i] = '';
746
      }
747
    }
748
  }
749

    
750
  // Apply the row classes.
751
  foreach ($rows as $row_number => $row) {
752
    $row_classes = array();
753
    if ($default_row_class) {
754
      $row_classes[] = 'row-' . ($row_number + 1);
755
    }
756
    if ($row_class_special) {
757
      if ($row_number == 0) {
758
        $row_classes[] = 'row-first';
759
      }
760
      if (count($rows) == ($row_number + 1)) {
761
        $row_classes[] = 'row-last';
762
      }
763
    }
764
    $vars['row_classes'][$row_number] = implode(' ', $row_classes);
765
    foreach ($rows[$row_number] as $column_number => $item) {
766
      $column_classes = array();
767
      if ($default_row_class) {
768
        $column_classes[] = 'col-' . ($column_number + 1);
769
      }
770
      if ($row_class_special) {
771
        if ($column_number == 0) {
772
          $column_classes[] = 'col-first';
773
        }
774
        elseif (count($rows[$row_number]) == ($column_number + 1)) {
775
          $column_classes[] = 'col-last';
776
        }
777
      }
778
      if (isset($row_indexes[$row_number][$column_number]) && $column_class = $view->style_plugin->get_row_class($row_indexes[$row_number][$column_number])) {
779
        $column_classes[] = $column_class;
780
      }
781
      $vars['column_classes'][$row_number][$column_number] = implode(' ', $column_classes);
782
    }
783
  }
784
  $vars['rows'] = $rows;
785
  $vars['class'] = 'views-view-grid cols-' . $columns;
786

    
787
  // Add the summary to the list if set.
788
  if (!empty($handler->options['summary'])) {
789
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
790
  }
791

    
792
  // Add the caption to the list if set.
793
  if (!empty($handler->options['caption'])) {
794
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
795
  }
796
  else {
797
    $vars['caption'] = '';
798
  }
799
}
800

    
801
/**
802
 * Display the simple view of rows one after another.
803
 */
804
function template_preprocess_views_view_unformatted(&$vars) {
805
  $view = $vars['view'];
806
  $rows = $vars['rows'];
807
  $style = $view->style_plugin;
808
  $options = $style->options;
809

    
810
  $vars['classes_array'] = array();
811
  $vars['classes'] = array();
812
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
813
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
814
  // Set up striping values.
815
  $count = 0;
816
  $max = count($rows);
817
  foreach ($rows as $id => $row) {
818
    $count++;
819
    if ($default_row_class) {
820
      $vars['classes'][$id][] = 'views-row';
821
      $vars['classes'][$id][] = 'views-row-' . $count;
822
    }
823
    if ($row_class_special) {
824
      $vars['classes'][$id][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
825
      if ($count == 1) {
826
        $vars['classes'][$id][] = 'views-row-first';
827
      }
828
      if ($count == $max) {
829
        $vars['classes'][$id][] = 'views-row-last';
830
      }
831
    }
832

    
833
    if ($row_class = $view->style_plugin->get_row_class($id)) {
834
      $vars['classes'][$id][] = $row_class;
835
    }
836

    
837
    // Flatten the classes to a string for each row for the template file.
838
    $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
839
  }
840
}
841

    
842
/**
843
 * Display the view as an HTML list element.
844
 */
845
function template_preprocess_views_view_list(&$vars) {
846
  $handler  = $vars['view']->style_plugin;
847

    
848
  $class = explode(' ', $handler->options['class']);
849
  $class = array_map('views_clean_css_identifier', $class);
850

    
851
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
852
  $wrapper_class = array_map('views_clean_css_identifier', $wrapper_class);
853

    
854
  $vars['class'] = implode(' ', $class);
855
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
856
  $vars['wrapper_prefix'] = '';
857
  $vars['wrapper_suffix'] = '';
858
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . '>';
859
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
860
  if ($vars['wrapper_class']) {
861
    $vars['wrapper_prefix'] = '<div class="' . $vars['wrapper_class'] . '">';
862
    $vars['wrapper_suffix'] = '</div>';
863
  }
864

    
865
  if ($vars['class']) {
866
    $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
867
  }
868
  template_preprocess_views_view_unformatted($vars);
869
}
870

    
871
/**
872
 * Preprocess an RSS feed.
873
 */
874
function template_preprocess_views_view_rss(&$vars) {
875
  global $base_url;
876
  global $language;
877

    
878
  $view     = &$vars['view'];
879
  $options  = &$vars['options'];
880
  $items    = &$vars['rows'];
881

    
882
  $style    = &$view->style_plugin;
883

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

    
889
  if ($view->display_handler->get_option('sitename_title')) {
890
    $title = variable_get('site_name', 'Drupal');
891
    if ($slogan = variable_get('site_slogan', '')) {
892
      $title .= ' - ' . $slogan;
893
    }
894
  }
895
  else {
896
    $title = $view->get_title();
897
  }
898
  $vars['title'] = check_plain($title);
899

    
900
  // Figure out which display which has a path we're using for this feed. If
901
  // there isn't one, use the global $base_url.
902
  $link_display_id = $view->display_handler->get_link_display();
903
  if ($link_display_id && !empty($view->display[$link_display_id])) {
904
    $path = $view->display[$link_display_id]->handler->get_path();
905
  }
906

    
907
  if ($path) {
908
    $path = $view->get_url(NULL, $path);
909
    $url_options = array('absolute' => TRUE);
910
    if (!empty($view->exposed_raw_input)) {
911
      $url_options['query'] = $view->exposed_raw_input;
912
    }
913

    
914
    // Compare the link to the default home page; if it's the default home
915
    // page, just use $base_url.
916
    if ($path == variable_get('site_frontpage', 'node')) {
917
      $path = '';
918
    }
919

    
920
    $vars['link'] = check_url(url($path, $url_options));
921
  }
922

    
923
  $vars['langcode'] = check_plain($language->language);
924
  $vars['namespaces'] = drupal_attributes($style->namespaces);
925
  $vars['items'] = $items;
926
  $vars['channel_elements'] = format_xml_elements($style->channel_elements);
927

    
928
  // During live preview we don't want to output the header since the contents
929
  // of the feed are being displayed inside a normal HTML page.
930
  if (empty($vars['view']->live_preview)) {
931
    drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
932
  }
933
}
934

    
935
/**
936
 * Default theme function for all RSS rows.
937
 */
938
function template_preprocess_views_view_row_rss(&$vars) {
939
  $view     = &$vars['view'];
940
  $options  = &$vars['options'];
941
  $item     = &$vars['row'];
942

    
943
  $vars['title'] = check_plain($item->title);
944
  $vars['link'] = check_url($item->link);
945
  $vars['description'] = check_plain($item->description);
946
  $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
947
}
948

    
949
/**
950
 * Default theme function for all filter forms.
951
 */
952
function template_preprocess_views_exposed_form(&$vars) {
953
  $form = &$vars['form'];
954

    
955
  // Put all single checkboxes together in the last spot.
956
  $checkboxes = '';
957

    
958
  if (!empty($form['q'])) {
959
    $vars['q'] = drupal_render($form['q']);
960
  }
961

    
962
  $vars['widgets'] = array();
963
  foreach ($form['#info'] as $id => $info) {
964
    // Set aside checkboxes.
965
    if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
966
      $checkboxes .= drupal_render($form[$info['value']]);
967
      continue;
968
    }
969
    $widget = new stdClass();
970
    // Set up defaults so that there's always something there.
971
    $widget->label = $widget->operator = $widget->widget = $widget->description = NULL;
972

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

    
975
    if (!empty($info['label'])) {
976
      $widget->label = check_plain($info['label']);
977
    }
978
    if (!empty($info['operator'])) {
979
      $widget->operator = drupal_render($form[$info['operator']]);
980
    }
981

    
982
    $widget->widget = drupal_render($form[$info['value']]);
983

    
984
    if (!empty($info['description'])) {
985
      $widget->description = check_plain($info['description']);
986
    }
987

    
988
    $vars['widgets'][$id] = $widget;
989
  }
990

    
991
  // Wrap up all the checkboxes we set aside into a widget.
992
  if ($checkboxes) {
993
    $widget = new stdClass();
994
    // Set up defaults so that there's always something there.
995
    $widget->label = $widget->operator = $widget->widget = NULL;
996
    $widget->id = 'checkboxes';
997
    $widget->widget = $checkboxes;
998
    $vars['widgets']['checkboxes'] = $widget;
999
  }
1000

    
1001
  if (isset($form['sort_by'])) {
1002
    $vars['sort_by'] = drupal_render($form['sort_by']);
1003
    $vars['sort_order'] = drupal_render($form['sort_order']);
1004
  }
1005
  if (isset($form['items_per_page'])) {
1006
    $vars['items_per_page'] = drupal_render($form['items_per_page']);
1007
  }
1008
  if (isset($form['offset'])) {
1009
    $vars['offset'] = drupal_render($form['offset']);
1010
  }
1011
  if (isset($form['reset'])) {
1012
    $vars['reset_button'] = drupal_render($form['reset']);
1013
  }
1014
  // This includes the submit button.
1015
  $vars['button'] = drupal_render_children($form);
1016
}
1017

    
1018
/**
1019
 * Theme function for a View with form elements: replace the placeholders.
1020
 */
1021
function theme_views_form_views_form($variables) {
1022
  $form = $variables['form'];
1023

    
1024
  // Placeholders and their substitutions (usually rendered form elements).
1025
  $search = array();
1026
  $replace = array();
1027

    
1028
  // Add in substitutions provided by the form.
1029
  foreach ($form['#substitutions']['#value'] as $substitution) {
1030
    $field_name = $substitution['field_name'];
1031
    $row_id = $substitution['row_id'];
1032

    
1033
    $search[] = $substitution['placeholder'];
1034
    $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
1035
  }
1036
  // Add in substitutions from hook_views_form_substitutions().
1037
  $substitutions = module_invoke_all('views_form_substitutions');
1038
  foreach ($substitutions as $placeholder => $substitution) {
1039
    $search[] = $placeholder;
1040
    $replace[] = $substitution;
1041
  }
1042

    
1043
  // Apply substitutions to the rendered output.
1044
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
1045

    
1046
  // Render and add remaining form fields.
1047
  return drupal_render_children($form);
1048
}
1049

    
1050
/**
1051
 * theme function for a mini pager.
1052
 */
1053
function theme_views_mini_pager($vars) {
1054
  global $pager_page_array, $pager_total;
1055

    
1056
  $tags = $vars['tags'];
1057
  $element = $vars['element'];
1058
  $parameters = $vars['parameters'];
1059

    
1060
  // Current is the page we are currently paged to.
1061
  $pager_current = $pager_page_array[$element] + 1;
1062
  // Max is the maximum page number.
1063
  $pager_max = $pager_total[$element];
1064
  // End of marker calculations.
1065
  if ($pager_total[$element] > 1) {
1066

    
1067
    $li_previous = theme('pager_previous',
1068
      array(
1069
        'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
1070
        'element' => $element,
1071
        'interval' => 1,
1072
        'parameters' => $parameters,
1073
      )
1074
    );
1075
    if (empty($li_previous)) {
1076
      $li_previous = "&nbsp;";
1077
    }
1078

    
1079
    $li_next = theme('pager_next',
1080
      array(
1081
        'text' => (isset($tags[3]) ? $tags[3] : t('››')),
1082
        'element' => $element,
1083
        'interval' => 1,
1084
        'parameters' => $parameters,
1085
      )
1086
    );
1087

    
1088
    if (empty($li_next)) {
1089
      $li_next = "&nbsp;";
1090
    }
1091

    
1092
    $items[] = array(
1093
      'class' => array('pager-previous'),
1094
      'data' => $li_previous,
1095
    );
1096

    
1097
    $items[] = array(
1098
      'class' => array('pager-current'),
1099
      'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
1100
    );
1101

    
1102
    $items[] = array(
1103
      'class' => array('pager-next'),
1104
      'data' => $li_next,
1105
    );
1106

    
1107
    return theme('item_list',
1108
      array(
1109
        'items' => $items,
1110
        'title' => NULL,
1111
        'type' => 'ul',
1112
        'attributes' => array('class' => array('pager')),
1113
      )
1114
    );
1115
  }
1116
}
1117

    
1118
/**
1119
 * Generic <div> container function.
1120
 */
1121
function theme_views_container($variables) {
1122
  $element = $variables['element'];
1123
  return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
1124
}
1125

    
1126
/**
1127
 * @defgroup views_templates Views template files
1128
 * @{
1129
 * All views templates can be overridden with a variety of names, using
1130
 * the view, the display ID of the view, the display type of the view,
1131
 * or some combination thereof.
1132
 *
1133
 * For each view, there will be a minimum of two templates used. The first
1134
 * is used for all views: views-view.tpl.php.
1135
 *
1136
 * The second template is determined by the style selected for the view. Note
1137
 * that certain aspects of the view can also change which style is used; for
1138
 * example, arguments which provide a summary view might change the style to
1139
 * one of the special summary styles.
1140
 *
1141
 * The default style for all views is views-view-unformatted.tpl.php
1142
 *
1143
 * Many styles will then farm out the actual display of each row to a row
1144
 * style; the default row style is views-view-fields.tpl.php.
1145
 *
1146
 * Here is an example of all the templates that will be tried in the following
1147
 * case:
1148
 *
1149
 * View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
1150
 *
1151
 * - views-view--foobar--page.tpl.php
1152
 * - views-view--page.tpl.php
1153
 * - views-view--foobar.tpl.php
1154
 * - views-view.tpl.php
1155
 *
1156
 * - views-view-unformatted--foobar--page.tpl.php
1157
 * - views-view-unformatted--page.tpl.php
1158
 * - views-view-unformatted--foobar.tpl.php
1159
 * - views-view-unformatted.tpl.php
1160
 *
1161
 * - views-view-fields--foobar--page.tpl.php
1162
 * - views-view-fields--page.tpl.php
1163
 * - views-view-fields--foobar.tpl.php
1164
 * - views-view-fields.tpl.php
1165
 *
1166
 * Important! When adding a new template to your theme, be sure to flush the
1167
 * theme registry cache!
1168
 *
1169
 * @see _views_theme_functions()
1170
 * @}
1171
 */