Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / theme / theme.inc @ 651307cd

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 $hook
12
 *   The hook to use. This is the base theme/template name.
13
 * @param $view
14
 *   The view being rendered.
15
 * @param $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 this
108
    // 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" identifier for
114
    // each view. This identifier is written to both Drupal.settings and the DIV
115
    // 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']) ? substr(url($view->display['page']->display_options['path']), strlen($base_path)) : '',
133
            'view_base_path' => $view->get_path(),
134
            'view_dom_id' => $vars['dom_id'],
135
            // To fit multiple views on a page, the programmer may have
136
            // overridden the display's pager_element.
137
            'pager_element' => isset($view->query->pager) ? $view->query->pager->get_pager_id() : 0,
138
          ),
139
        ),
140
      ),
141
      // Support for AJAX path validation in core 7.39.
142
      'urlIsAjaxTrusted' => array(
143
        url('views/ajax') => TRUE,
144
      ),
145
    );
146

    
147
    drupal_add_js($settings, 'setting');
148
    views_add_js('ajax_view');
149
  }
150

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

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

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

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

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

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

    
206
        if ($classes = $object->handler->element_classes($view->row_index)) {
207
          if ($class) {
208
            $class .= ' ';
209
          }
210
          $class .=  $classes;
211
        }
212

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

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

    
226
      $object->content = $field_output;
227
      if (isset($view->field[$id]->field_alias) && isset($vars['row']->{$view->field[$id]->field_alias})) {
228
        $object->raw = $vars['row']->{$view->field[$id]->field_alias};
229
      }
230
      else {
231
        $object->raw = NULL; // make sure it exists to reduce NOTICE
232
      }
233

    
234
      if (!empty($vars['options']['separator']) && $previous_inline && $object->inline && $object->content) {
235
        $object->separator = filter_xss_admin($vars['options']['separator']);
236
      }
237

    
238
      $object->class = drupal_clean_css_identifier($id);
239

    
240
      $previous_inline = $object->inline;
241
      $object->inline_html = $object->handler->element_wrapper_type(TRUE, TRUE);
242
      if ($object->inline_html === '' && $vars['options']['default_field_elements']) {
243
        $object->inline_html = $object->inline ? 'span' : 'div';
244
      }
245

    
246
      // Set up the wrapper HTML.
247
      $object->wrapper_prefix = '';
248
      $object->wrapper_suffix = '';
249

    
250
      if ($object->inline_html) {
251
        $class = '';
252
        if ($object->handler->options['element_default_classes']) {
253
          $class = "views-field views-field-" . $object->class;
254
        }
255

    
256
        if ($classes = $object->handler->element_wrapper_classes($view->row_index)) {
257
          if ($class) {
258
            $class .= ' ';
259
          }
260
          $class .= $classes;
261
        }
262

    
263
        $object->wrapper_prefix = '<' . $object->inline_html;
264
        if ($class) {
265
          $object->wrapper_prefix .= ' class="' . $class . '"';
266
        }
267
        $object->wrapper_prefix .= '>';
268
        $object->wrapper_suffix = '</' . $object->inline_html . '>';
269
      }
270

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

    
281
        $object->element_label_type = $object->handler->element_label_type(TRUE, !$vars['options']['default_field_elements']);
282
        if ($object->element_label_type) {
283
          $class = '';
284
          if ($object->handler->options['element_default_classes']) {
285
            $class = 'views-label views-label-' . $object->class;
286
          }
287

    
288
          $element_label_class = $object->handler->element_label_classes($view->row_index);
289
          if ($element_label_class) {
290
            if ($class) {
291
              $class .= ' ';
292
            }
293

    
294
            $class .= $element_label_class;
295
          }
296

    
297
          $pre = '<' . $object->element_label_type;
298
          if ($class) {
299
            $pre .= ' class="' . $class . '"';
300
          }
301
          $pre .= '>';
302

    
303
          $object->label_html = $pre . $object->label_html . '</' . $object->element_label_type . '>';
304
        }
305
      }
306

    
307
      $vars['fields'][$id] = $object;
308
    }
309
  }
310

    
311
}
312

    
313
/**
314
 * Display a single views grouping.
315
 */
316
function theme_views_view_grouping($vars) {
317
  $view = $vars['view'];
318
  $title = $vars['title'];
319
  $content = $vars['content'];
320

    
321
  $output = '<div class="view-grouping">';
322
  $output .= '<div class="view-grouping-header">' . $title . '</div>';
323
  $output .= '<div class="view-grouping-content">' . $content . '</div>' ;
324
  $output .= '</div>';
325

    
326
  return $output;
327
}
328

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

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

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

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

    
369
  $url_options = array();
370

    
371
  if (!empty($view->exposed_raw_input)) {
372
    $url_options['query'] = $view->exposed_raw_input;
373
  }
374

    
375
  $active_urls = drupal_map_assoc(array(
376
    url($_GET['q'], array('alias' => TRUE)), // force system path
377
    url($_GET['q']), // could be an alias
378
  ));
379

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

    
384
  foreach ($vars['rows'] as $id => $row) {
385
    $row_args[$id] = $argument->summary_argument($row);
386
  }
387
  $argument->process_summary_arguments($row_args);
388

    
389
  foreach ($vars['rows'] as $id => $row) {
390
    $vars['row_classes'][$id] = '';
391

    
392
    $vars['rows'][$id]->link = $argument->summary_name($row);
393
    $args = $view->args;
394
    $args[$argument->position] = $row_args[$id];
395

    
396
    $base_path = NULL;
397
    if (!empty($argument->options['summary_options']['base_path'])) {
398
      $base_path = $argument->options['summary_options']['base_path'];
399
    }
400
    $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
401
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
402
    if (isset($active_urls[$vars['rows'][$id]->url])) {
403
      $vars['row_classes'][$id] = 'active';
404
    }
405
  }
406
}
407

    
408
/**
409
 * Template preprocess theme function to print summary basically
410
 * unformatted.
411
 */
412
function template_preprocess_views_view_summary_unformatted(&$vars) {
413
  $view     = $vars['view'];
414
  $argument = $view->argument[$view->build_info['summary_level']];
415
  $vars['row_classes'] = array();
416

    
417
  $url_options = array();
418

    
419
  if (!empty($view->exposed_raw_input)) {
420
    $url_options['query'] = $view->exposed_raw_input;
421
  }
422

    
423
  $count = 0;
424
  $active_urls = drupal_map_assoc(array(
425
    url($_GET['q'], array('alias' => TRUE)), // force system path
426
    url($_GET['q']), // could be an alias
427
  ));
428

    
429
  // Collect all arguments foreach row, to be able to alter them for example by the validator.
430
  // This is not done per single argument value, because this could cause performance problems.
431
  $row_args = array();
432
  foreach ($vars['rows'] as $id => $row) {
433
    $row_args[$id] = $argument->summary_argument($row);
434
  }
435
  $argument->process_summary_arguments($row_args);
436

    
437
  foreach ($vars['rows'] as $id => $row) {
438
    // only false on first time:
439
    if ($count++) {
440
      $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
441
    }
442
    $vars['rows'][$id]->link = $argument->summary_name($row);
443
    $args = $view->args;
444
    $args[$argument->position] = $row_args[$id];
445

    
446
    $base_path = NULL;
447
    if (!empty($argument->options['summary_options']['base_path'])) {
448
      $base_path = $argument->options['summary_options']['base_path'];
449
    }
450
    $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
451
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
452
    if (isset($active_urls[$vars['rows'][$id]->url])) {
453
      $vars['row_classes'][$id] = 'active';
454
    }
455
  }
456
}
457

    
458
/**
459
 * Display a view as a table style.
460
 */
461
function template_preprocess_views_view_table(&$vars) {
462
  $view     = $vars['view'];
463

    
464
  // We need the raw data for this grouping, which is passed in as $vars['rows'].
465
  // However, the template also needs to use for the rendered fields.  We
466
  // therefore swap the raw data out to a new variable and reset $vars['rows']
467
  // so that it can get rebuilt.
468
  // Store rows so that they may be used by further preprocess functions.
469
  $result   = $vars['result'] = $vars['rows'];
470
  $vars['rows'] = array();
471
  $vars['field_classes'] = array();
472
  $vars['header'] = array();
473

    
474
  $options  = $view->style_plugin->options;
475
  $handler  = $view->style_plugin;
476

    
477
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
478
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
479

    
480
  $fields   = &$view->field;
481
  $columns  = $handler->sanitize_columns($options['columns'], $fields);
482

    
483
  $active   = !empty($handler->active) ? $handler->active : '';
484
  $order    = !empty($handler->order) ? $handler->order : 'asc';
485

    
486
  $query    = tablesort_get_query_parameters();
487
  if (isset($view->exposed_raw_input)) {
488
    $query += $view->exposed_raw_input;
489
  }
490

    
491
  // Fields must be rendered in order as of Views 2.3, so we will pre-render
492
  // everything.
493
  $renders = $handler->render_fields($result);
494

    
495
  foreach ($columns as $field => $column) {
496
    // Create a second variable so we can easily find what fields we have and what the
497
    // CSS classes should be.
498
    $vars['fields'][$field] = drupal_clean_css_identifier($field);
499
    if ($active == $field) {
500
      $vars['fields'][$field] .= ' active';
501
    }
502

    
503
    // render the header labels
504
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
505
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
506
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
507
        $vars['header'][$field] = $label;
508
      }
509
      else {
510
        $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
511

    
512
        if ($active == $field) {
513
          $initial = ($order == 'asc') ? 'desc' : 'asc';
514
        }
515

    
516
        $title = t('sort by @s', array('@s' => $label));
517
        if ($active == $field) {
518
          $label .= theme('tablesort_indicator', array('style' => $initial));
519
        }
520

    
521
        $query['order'] = $field;
522
        $query['sort'] = $initial;
523
        $link_options = array(
524
          'html' => TRUE,
525
          'attributes' => array('title' => $title),
526
          'query' => $query,
527
        );
528
        $vars['header'][$field] = l($label, $_GET['q'], $link_options);
529
      }
530

    
531
      $vars['header_classes'][$field] = '';
532
      // Set up the header label class.
533
      if ($fields[$field]->options['element_default_classes']) {
534
        $vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
535
      }
536
      $class = $fields[$field]->element_label_classes(0);
537
      if ($class) {
538
        if ($vars['header_classes'][$field]) {
539
          $vars['header_classes'][$field] .= ' ';
540
        }
541
        $vars['header_classes'][$field] .= $class;
542
      }
543
      // Add a CSS align class to each field if one was set
544
      if (!empty($options['info'][$field]['align'])) {
545
        $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
546
      }
547

    
548
      // Add a header label wrapper if one was selected.
549
      if ($vars['header'][$field]) {
550
        $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
551
        if ($element_label_type) {
552
          $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
553
        }
554
      }
555

    
556
    }
557

    
558
    // Add a CSS align class to each field if one was set
559
    if (!empty($options['info'][$field]['align'])) {
560
      $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
561
    }
562

    
563
    // Render each field into its appropriate column.
564
    foreach ($result as $num => $row) {
565
      // Add field classes
566
      $vars['field_classes'][$field][$num] = '';
567
      if ($fields[$field]->options['element_default_classes']) {
568
        $vars['field_classes'][$field][$num] = "views-field views-field-" . $vars['fields'][$field];
569
      }
570
      if ($classes = $fields[$field]->element_classes($num)) {
571
        if ($vars['field_classes'][$field][$num]) {
572
          $vars['field_classes'][$field][$num] .= ' ';
573
        }
574

    
575
        $vars['field_classes'][$field][$num] .= $classes;
576
      }
577
      $vars['field_attributes'][$field][$num] = array();
578

    
579
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
580
        $field_output = $renders[$num][$field];
581
        $element_type = $fields[$field]->element_type(TRUE, TRUE);
582
        if ($element_type) {
583
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
584
        }
585

    
586
        // Don't bother with separators and stuff if the field does not show up.
587
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
588
          continue;
589
        }
590

    
591
        // Place the field into the column, along with an optional separator.
592
        if (!empty($vars['rows'][$num][$column])) {
593
          if (!empty($options['info'][$column]['separator'])) {
594
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
595
          }
596
        }
597
        else {
598
          $vars['rows'][$num][$column] = '';
599
        }
600

    
601
        $vars['rows'][$num][$column] .= $field_output;
602
      }
603
    }
604

    
605
    // Remove columns if the option is hide empty column is checked and the field is not empty.
606
    if (!empty($options['info'][$field]['empty_column'])) {
607
      $empty = TRUE;
608
      foreach ($vars['rows'] as $num => $columns) {
609
        $empty &= empty($columns[$column]);
610
      }
611
      if ($empty) {
612
        foreach ($vars['rows'] as $num => &$column_items) {
613
          unset($column_items[$column]);
614
          unset($vars['header'][$column]);
615
        }
616
      }
617
    }
618
  }
619

    
620
  // Hide table header if all labels are empty.
621
  if (!array_filter($vars['header'])) {
622
    $vars['header'] = array();
623
  }
624

    
625
  $count = 0;
626
  foreach ($vars['rows'] as $num => $row) {
627
    $vars['row_classes'][$num] = array();
628
    if ($row_class_special) {
629
      $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
630
    }
631
    if ($row_class = $handler->get_row_class($num)) {
632
      $vars['row_classes'][$num][] = $row_class;
633
    }
634
  }
635

    
636
  if ($row_class_special) {
637
    $vars['row_classes'][0][] = 'views-row-first';
638
    $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
639
  }
640

    
641
  $vars['classes_array'] = array('views-table');
642
  if (empty($vars['rows']) && !empty($options['empty_table'])) {
643
    $vars['rows'][0][0] = $view->display_handler->render_area('empty');
644
    // Calculate the amounts of rows with output.
645
    $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
646
    $vars['field_classes'][0][0] = 'views-empty';
647
  }
648

    
649

    
650
  if (!empty($options['sticky'])) {
651
    drupal_add_js('misc/tableheader.js');
652
    $vars['classes_array'][] = "sticky-enabled";
653
  }
654
  $vars['classes_array'][] = 'cols-'. count($vars['header']);
655

    
656
  // Add the summary to the list if set.
657
  if (!empty($handler->options['summary'])) {
658
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
659
  }
660

    
661
  // Add the caption to the list if set.
662
  if (!empty($handler->options['caption'])) {
663
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
664
  }
665
  else {
666
    $vars['caption'] = '';
667
  }
668
}
669

    
670
/**
671
 * Display a view as a grid style.
672
 */
673
function template_preprocess_views_view_grid(&$vars) {
674
  $view     = $vars['view'];
675
  $result   = $view->result;
676
  $options  = $view->style_plugin->options;
677
  $handler  = $view->style_plugin;
678
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
679
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
680

    
681
  $columns  = $options['columns'];
682

    
683
  $rows = array();
684
  $row_indexes = array();
685

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

    
724
      if (!$remainders && $row == $num_rows) {
725
        $row = 0;
726
        $col++;
727
      }
728
      elseif ($remainders && $row == $num_rows + 1) {
729
        $row = 0;
730
        $col++;
731
        $remainders--;
732
      }
733
    }
734
    for ($i = 0; $i < count($rows[0]); $i++) {
735
      // This should be string so that's okay :)
736
      if (!isset($rows[count($rows) - 1][$i])) {
737
        $rows[count($rows) - 1][$i] = '';
738
      }
739
    }
740
  }
741

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

    
779
  // Add the summary to the list if set.
780
  if (!empty($handler->options['summary'])) {
781
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
782
  }
783

    
784
  // Add the caption to the list if set.
785
  if (!empty($handler->options['caption'])) {
786
    $vars['caption'] = filter_xss_admin($handler->options['caption']);
787
  }
788
  else {
789
    $vars['caption'] = '';
790
  }
791
}
792

    
793
/**
794
 * Display the simple view of rows one after another
795
 */
796
function template_preprocess_views_view_unformatted(&$vars) {
797
  $view = $vars['view'];
798
  $rows = $vars['rows'];
799
  $style = $view->style_plugin;
800
  $options = $style->options;
801

    
802
  $vars['classes_array'] = array();
803
  $vars['classes'] = array();
804
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
805
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
806
  // Set up striping values.
807
  $count = 0;
808
  $max = count($rows);
809
  foreach ($rows as $id => $row) {
810
    $count++;
811
    if ($default_row_class) {
812
      $vars['classes'][$id][] = 'views-row';
813
      $vars['classes'][$id][] = 'views-row-' . $count;
814
    }
815
    if ($row_class_special) {
816
      $vars['classes'][$id][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
817
      if ($count == 1) {
818
        $vars['classes'][$id][] = 'views-row-first';
819
      }
820
      if ($count == $max) {
821
        $vars['classes'][$id][] = 'views-row-last';
822
      }
823
    }
824

    
825
    if ($row_class = $view->style_plugin->get_row_class($id)) {
826
      $vars['classes'][$id][] = $row_class;
827
    }
828

    
829
    // Flatten the classes to a string for each row for the template file.
830
    $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
831
  }
832
}
833

    
834
/**
835
 * Display the view as an HTML list element
836
 */
837
function template_preprocess_views_view_list(&$vars) {
838
  $handler  = $vars['view']->style_plugin;
839

    
840
  $class = explode(' ', $handler->options['class']);
841
  $class = array_map('views_clean_css_identifier', $class);
842

    
843
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
844
  $wrapper_class = array_map('views_clean_css_identifier', $wrapper_class);
845

    
846
  $vars['class'] = implode(' ', $class);
847
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
848
  $vars['wrapper_prefix'] = '';
849
  $vars['wrapper_suffix'] = '';
850
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . '>';
851
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
852
  if ($vars['wrapper_class']) {
853
    $vars['wrapper_prefix'] = '<div class="' . $vars['wrapper_class'] . '">';
854
    $vars['wrapper_suffix'] = '</div>';
855
  }
856

    
857
  if ($vars['class']) {
858
    $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
859
  }
860
  template_preprocess_views_view_unformatted($vars);
861
}
862

    
863
/**
864
 * Preprocess an RSS feed
865
 */
866
function template_preprocess_views_view_rss(&$vars) {
867
  global $base_url;
868
  global $language;
869

    
870
  $view     = &$vars['view'];
871
  $options  = &$vars['options'];
872
  $items    = &$vars['rows'];
873

    
874
  $style    = &$view->style_plugin;
875

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

    
881
  if ($view->display_handler->get_option('sitename_title')) {
882
    $title = variable_get('site_name', 'Drupal');
883
    if ($slogan = variable_get('site_slogan', '')) {
884
      $title .= ' - ' . $slogan;
885
    }
886
  }
887
  else {
888
    $title = $view->get_title();
889
  }
890
  $vars['title'] = check_plain($title);
891

    
892
  // Figure out which display which has a path we're using for this feed. If there isn't
893
  // one, use the global $base_url
894
  $link_display_id = $view->display_handler->get_link_display();
895
  if ($link_display_id && !empty($view->display[$link_display_id])) {
896
    $path = $view->display[$link_display_id]->handler->get_path();
897
  }
898

    
899
  if ($path) {
900
    $path = $view->get_url(NULL, $path);
901
    $url_options = array('absolute' => TRUE);
902
    if (!empty($view->exposed_raw_input)) {
903
      $url_options['query'] = $view->exposed_raw_input;
904
    }
905

    
906
    // Compare the link to the default home page; if it's the default home page, just use $base_url.
907
    if ($path == variable_get('site_frontpage', 'node')) {
908
      $path = '';
909
    }
910

    
911
    $vars['link'] = check_url(url($path, $url_options));
912
  }
913

    
914
  $vars['langcode'] = check_plain($language->language);
915
  $vars['namespaces'] = drupal_attributes($style->namespaces);
916
  $vars['items'] = $items;
917
  $vars['channel_elements'] = format_xml_elements($style->channel_elements);
918

    
919
  // During live preview we don't want to output the header since the contents
920
  // of the feed are being displayed inside a normal HTML page.
921
  if (empty($vars['view']->live_preview)) {
922
    drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
923
  }
924
}
925

    
926
/**
927
 * Default theme function for all RSS rows.
928
 */
929
function template_preprocess_views_view_row_rss(&$vars) {
930
  $view     = &$vars['view'];
931
  $options  = &$vars['options'];
932
  $item     = &$vars['row'];
933

    
934
  $vars['title'] = check_plain($item->title);
935
  $vars['link'] = check_url($item->link);
936
  $vars['description'] = check_plain($item->description);
937
  $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
938
}
939

    
940
/**
941
 * Default theme function for all filter forms.
942
 */
943
function template_preprocess_views_exposed_form(&$vars) {
944
  $form = &$vars['form'];
945

    
946
  // Put all single checkboxes together in the last spot.
947
  $checkboxes = '';
948

    
949
  if (!empty($form['q'])) {
950
    $vars['q'] = drupal_render($form['q']);
951
  }
952

    
953
  $vars['widgets'] = array();
954
  foreach ($form['#info'] as $id => $info) {
955
    // Set aside checkboxes.
956
    if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
957
      $checkboxes .= drupal_render($form[$info['value']]);
958
      continue;
959
    }
960
    $widget = new stdClass;
961
    // set up defaults so that there's always something there.
962
    $widget->label = $widget->operator = $widget->widget = $widget->description = NULL;
963

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

    
966
    if (!empty($info['label'])) {
967
      $widget->label = check_plain($info['label']);
968
    }
969
    if (!empty($info['operator'])) {
970
      $widget->operator = drupal_render($form[$info['operator']]);
971
    }
972

    
973
    $widget->widget = drupal_render($form[$info['value']]);
974

    
975
    if (!empty($info['description'])) {
976
      $widget->description = check_plain($info['description']);
977
    }
978

    
979
    $vars['widgets'][$id] = $widget;
980
  }
981

    
982
  // Wrap up all the checkboxes we set aside into a widget.
983
  if ($checkboxes) {
984
    $widget = new stdClass;
985
    // set up defaults so that there's always something there.
986
    $widget->label = $widget->operator = $widget->widget = NULL;
987
    $widget->id = 'checkboxes';
988
    $widget->widget = $checkboxes;
989
    $vars['widgets']['checkboxes'] = $widget;
990
  }
991

    
992
  if (isset($form['sort_by'])) {
993
    $vars['sort_by'] = drupal_render($form['sort_by']);
994
    $vars['sort_order'] = drupal_render($form['sort_order']);
995
  }
996
  if (isset($form['items_per_page'])) {
997
    $vars['items_per_page'] = drupal_render($form['items_per_page']);
998
  }
999
  if (isset($form['offset'])) {
1000
    $vars['offset'] = drupal_render($form['offset']);
1001
  }
1002
  if (isset($form['reset'])) {
1003
    $vars['reset_button'] = drupal_render($form['reset']);
1004
  }
1005
  // This includes the submit button.
1006
  $vars['button'] = drupal_render_children($form);
1007
}
1008

    
1009
/**
1010
 * Theme function for a View with form elements: replace the placeholders.
1011
 */
1012
function theme_views_form_views_form($variables) {
1013
  $form = $variables['form'];
1014

    
1015
  // Placeholders and their substitutions (usually rendered form elements).
1016
  $search = array();
1017
  $replace = array();
1018

    
1019
  // Add in substitutions provided by the form.
1020
  foreach ($form['#substitutions']['#value'] as $substitution) {
1021
    $field_name = $substitution['field_name'];
1022
    $row_id = $substitution['row_id'];
1023

    
1024
    $search[] = $substitution['placeholder'];
1025
    $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
1026
  }
1027
  // Add in substitutions from hook_views_form_substitutions().
1028
  $substitutions = module_invoke_all('views_form_substitutions');
1029
  foreach ($substitutions as $placeholder => $substitution) {
1030
    $search[] = $placeholder;
1031
    $replace[] = $substitution;
1032
  }
1033

    
1034
  // Apply substitutions to the rendered output.
1035
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
1036

    
1037
  // Render and add remaining form fields.
1038
  return drupal_render_children($form);
1039
}
1040

    
1041
function theme_views_mini_pager($vars) {
1042
  global $pager_page_array, $pager_total;
1043

    
1044
  $tags = $vars['tags'];
1045
  $element = $vars['element'];
1046
  $parameters = $vars['parameters'];
1047

    
1048
  // current is the page we are currently paged to
1049
  $pager_current = $pager_page_array[$element] + 1;
1050
  // max is the maximum page number
1051
  $pager_max = $pager_total[$element];
1052
  // End of marker calculations.
1053

    
1054
  if ($pager_total[$element] > 1) {
1055

    
1056
    $li_previous = theme('pager_previous',
1057
      array(
1058
        'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
1059
        'element' => $element,
1060
        'interval' => 1,
1061
        'parameters' => $parameters,
1062
      )
1063
    );
1064
    if (empty($li_previous)) {
1065
      $li_previous = "&nbsp;";
1066
    }
1067

    
1068
    $li_next = theme('pager_next',
1069
      array(
1070
        'text' => (isset($tags[3]) ? $tags[3] : t('››')),
1071
        'element' => $element,
1072
        'interval' => 1,
1073
        'parameters' => $parameters,
1074
      )
1075
    );
1076

    
1077
    if (empty($li_next)) {
1078
      $li_next = "&nbsp;";
1079
    }
1080

    
1081
    $items[] = array(
1082
      'class' => array('pager-previous'),
1083
      'data' => $li_previous,
1084
    );
1085

    
1086
    $items[] = array(
1087
      'class' => array('pager-current'),
1088
      'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
1089
    );
1090

    
1091
    $items[] = array(
1092
      'class' => array('pager-next'),
1093
      'data' => $li_next,
1094
    );
1095
    return theme('item_list',
1096
      array(
1097
        'items' => $items,
1098
        'title' => NULL,
1099
        'type' => 'ul',
1100
        'attributes' => array('class' => array('pager')),
1101
      )
1102
    );
1103
  }
1104
}
1105

    
1106
/**
1107
 * Generic <div> container function.
1108
 */
1109
function theme_views_container($variables) {
1110
  $element = $variables['element'];
1111
  return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
1112
}
1113

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