Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / theme / theme.inc @ 13755f8d

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 = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
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
    );
142

    
143
    drupal_add_js($settings, 'setting');
144
    views_add_js('ajax_view');
145
  }
146

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

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

    
173
  // Flatten the classes to a string for the template file.
174
  $vars['classes'] = implode(' ', $vars['classes_array']);
175
}
176

    
177
/**
178
 * Preprocess theme function to print a single record from a row, with fields
179
 */
180
function template_preprocess_views_view_fields(&$vars) {
181
  $view = $vars['view'];
182

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

    
195
      $object->element_type = $object->handler->element_type(TRUE, !$vars['options']['default_field_elements'], $object->inline);
196
      if ($object->element_type) {
197
        $class = '';
198
        if ($object->handler->options['element_default_classes']) {
199
          $class = 'field-content';
200
        }
201

    
202
        if ($classes = $object->handler->element_classes($view->row_index)) {
203
          if ($class) {
204
            $class .= ' ';
205
          }
206
          $class .=  $classes;
207
        }
208

    
209
        $pre = '<' . $object->element_type;
210
        if ($class) {
211
          $pre .= ' class="' . $class . '"';
212
        }
213
        $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
214
      }
215

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

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

    
230
      if (!empty($vars['options']['separator']) && $previous_inline && $object->inline && $object->content) {
231
        $object->separator = filter_xss_admin($vars['options']['separator']);
232
      }
233

    
234
      $object->class = drupal_clean_css_identifier($id);
235

    
236
      $previous_inline = $object->inline;
237
      $object->inline_html = $object->handler->element_wrapper_type(TRUE, TRUE);
238
      if ($object->inline_html === '' && $vars['options']['default_field_elements']) {
239
        $object->inline_html = $object->inline ? 'span' : 'div';
240
      }
241

    
242
      // Set up the wrapper HTML.
243
      $object->wrapper_prefix = '';
244
      $object->wrapper_suffix = '';
245

    
246
      if ($object->inline_html) {
247
        $class = '';
248
        if ($object->handler->options['element_default_classes']) {
249
          $class = "views-field views-field-" . $object->class;
250
        }
251

    
252
        if ($classes = $object->handler->element_wrapper_classes($view->row_index)) {
253
          if ($class) {
254
            $class .= ' ';
255
          }
256
          $class .= $classes;
257
        }
258

    
259
        $object->wrapper_prefix = '<' . $object->inline_html;
260
        if ($class) {
261
          $object->wrapper_prefix .= ' class="' . $class . '"';
262
        }
263
        $object->wrapper_prefix .= '>';
264
        $object->wrapper_suffix = '</' . $object->inline_html . '>';
265
      }
266

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

    
277
        $object->element_label_type = $object->handler->element_label_type(TRUE, !$vars['options']['default_field_elements']);
278
        if ($object->element_label_type) {
279
          $class = '';
280
          if ($object->handler->options['element_default_classes']) {
281
            $class = 'views-label views-label-' . $object->class;
282
          }
283

    
284
          $element_label_class = $object->handler->element_label_classes($view->row_index);
285
          if ($element_label_class) {
286
            if ($class) {
287
              $class .= ' ';
288
            }
289

    
290
            $class .= $element_label_class;
291
          }
292

    
293
          $pre = '<' . $object->element_label_type;
294
          if ($class) {
295
            $pre .= ' class="' . $class . '"';
296
          }
297
          $pre .= '>';
298

    
299
          $object->label_html = $pre . $object->label_html . '</' . $object->element_label_type . '>';
300
        }
301
      }
302

    
303
      $vars['fields'][$id] = $object;
304
    }
305
  }
306

    
307
}
308

    
309
/**
310
 * Display a single views grouping.
311
 */
312
function theme_views_view_grouping($vars) {
313
  $view = $vars['view'];
314
  $title = $vars['title'];
315
  $content = $vars['content'];
316

    
317
  $output = '<div class="view-grouping">';
318
  $output .= '<div class="view-grouping-header">' . $title . '</div>';
319
  $output .= '<div class="view-grouping-content">' . $content . '</div>' ;
320
  $output .= '</div>';
321

    
322
  return $output;
323
}
324

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

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

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

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

    
365
  $url_options = array();
366

    
367
  if (!empty($view->exposed_raw_input)) {
368
    $url_options['query'] = $view->exposed_raw_input;
369
  }
370

    
371
  $active_urls = drupal_map_assoc(array(
372
    url($_GET['q'], array('alias' => TRUE)), // force system path
373
    url($_GET['q']), // could be an alias
374
  ));
375

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

    
380
  foreach ($vars['rows'] as $id => $row) {
381
    $row_args[$id] = $argument->summary_argument($row);
382
  }
383
  $argument->process_summary_arguments($row_args);
384

    
385
  foreach ($vars['rows'] as $id => $row) {
386
    $vars['row_classes'][$id] = '';
387

    
388
    $vars['rows'][$id]->link = $argument->summary_name($row);
389
    $args = $view->args;
390
    $args[$argument->position] = $row_args[$id];
391

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

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

    
413
  $url_options = array();
414

    
415
  if (!empty($view->exposed_raw_input)) {
416
    $url_options['query'] = $view->exposed_raw_input;
417
  }
418

    
419
  $count = 0;
420
  $active_urls = drupal_map_assoc(array(
421
    url($_GET['q'], array('alias' => TRUE)), // force system path
422
    url($_GET['q']), // could be an alias
423
  ));
424

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

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

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

    
454
/**
455
 * Display a view as a table style.
456
 */
457
function template_preprocess_views_view_table(&$vars) {
458
  $view     = $vars['view'];
459

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

    
470
  $options  = $view->style_plugin->options;
471
  $handler  = $view->style_plugin;
472

    
473
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
474
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
475

    
476
  $fields   = &$view->field;
477
  $columns  = $handler->sanitize_columns($options['columns'], $fields);
478

    
479
  $active   = !empty($handler->active) ? $handler->active : '';
480
  $order    = !empty($handler->order) ? $handler->order : 'asc';
481

    
482
  $query    = tablesort_get_query_parameters();
483
  if (isset($view->exposed_raw_input)) {
484
    $query += $view->exposed_raw_input;
485
  }
486

    
487
  // Fields must be rendered in order as of Views 2.3, so we will pre-render
488
  // everything.
489
  $renders = $handler->render_fields($result);
490

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

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

    
508
        if ($active == $field) {
509
          $initial = ($order == 'asc') ? 'desc' : 'asc';
510
        }
511

    
512
        $title = t('sort by @s', array('@s' => $label));
513
        if ($active == $field) {
514
          $label .= theme('tablesort_indicator', array('style' => $initial));
515
        }
516

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

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

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

    
552
    }
553

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

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

    
571
        $vars['field_classes'][$field][$num] .= $classes;
572
      }
573
      $vars['field_attributes'][$field][$num] = array();
574

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

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

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

    
597
        $vars['rows'][$num][$column] .= $field_output;
598
      }
599
    }
600

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

    
616
  // Hide table header if all labels are empty.
617
  if (!array_filter($vars['header'])) {
618
    $vars['header'] = array();
619
  }
620

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

    
632
  if ($row_class_special) {
633
    $vars['row_classes'][0][] = 'views-row-first';
634
    $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
635
  }
636

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

    
645

    
646
  if (!empty($options['sticky'])) {
647
    drupal_add_js('misc/tableheader.js');
648
    $vars['classes_array'][] = "sticky-enabled";
649
  }
650
  $vars['classes_array'][] = 'cols-'. count($vars['header']);
651

    
652
  // Add the summary to the list if set.
653
  if (!empty($handler->options['summary'])) {
654
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
655
  }
656

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

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

    
677
  $columns  = $options['columns'];
678

    
679
  $rows = array();
680
  $row_indexes = array();
681

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

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

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

    
775
  // Add the summary to the list if set.
776
  if (!empty($handler->options['summary'])) {
777
    $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
778
  }
779

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

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

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

    
821
    if ($row_class = $view->style_plugin->get_row_class($id)) {
822
      $vars['classes'][$id][] = $row_class;
823
    }
824

    
825
    // Flatten the classes to a string for each row for the template file.
826
    $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
827
  }
828
}
829

    
830
/**
831
 * Display the view as an HTML list element
832
 */
833
function template_preprocess_views_view_list(&$vars) {
834
  $handler  = $vars['view']->style_plugin;
835

    
836
  $class = explode(' ', $handler->options['class']);
837
  $class = array_map('views_clean_css_identifier', $class);
838

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

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

    
853
  if ($vars['class']) {
854
    $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
855
  }
856
  template_preprocess_views_view_unformatted($vars);
857
}
858

    
859
/**
860
 * Preprocess an RSS feed
861
 */
862
function template_preprocess_views_view_rss(&$vars) {
863
  global $base_url;
864
  global $language;
865

    
866
  $view     = &$vars['view'];
867
  $options  = &$vars['options'];
868
  $items    = &$vars['rows'];
869

    
870
  $style    = &$view->style_plugin;
871

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

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

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

    
895
  if ($path) {
896
    $path = $view->get_url(NULL, $path);
897
    $url_options = array('absolute' => TRUE);
898
    if (!empty($view->exposed_raw_input)) {
899
      $url_options['query'] = $view->exposed_raw_input;
900
    }
901

    
902
    // Compare the link to the default home page; if it's the default home page, just use $base_url.
903
    if ($path == variable_get('site_frontpage', 'node')) {
904
      $path = '';
905
    }
906

    
907
    $vars['link'] = check_url(url($path, $url_options));
908
  }
909

    
910
  $vars['langcode'] = check_plain($language->language);
911
  $vars['namespaces'] = drupal_attributes($style->namespaces);
912
  $vars['items'] = $items;
913
  $vars['channel_elements'] = format_xml_elements($style->channel_elements);
914

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

    
922
/**
923
 * Default theme function for all RSS rows.
924
 */
925
function template_preprocess_views_view_row_rss(&$vars) {
926
  $view     = &$vars['view'];
927
  $options  = &$vars['options'];
928
  $item     = &$vars['row'];
929

    
930
  $vars['title'] = check_plain($item->title);
931
  $vars['link'] = check_url($item->link);
932
  $vars['description'] = check_plain($item->description);
933
  $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
934
}
935

    
936
/**
937
 * Default theme function for all filter forms.
938
 */
939
function template_preprocess_views_exposed_form(&$vars) {
940
  $form = &$vars['form'];
941

    
942
  // Put all single checkboxes together in the last spot.
943
  $checkboxes = '';
944

    
945
  if (!empty($form['q'])) {
946
    $vars['q'] = drupal_render($form['q']);
947
  }
948

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

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

    
962
    if (!empty($info['label'])) {
963
      $widget->label = check_plain($info['label']);
964
    }
965
    if (!empty($info['operator'])) {
966
      $widget->operator = drupal_render($form[$info['operator']]);
967
    }
968

    
969
    $widget->widget = drupal_render($form[$info['value']]);
970

    
971
    if (!empty($info['description'])) {
972
      $widget->description = check_plain($info['description']);
973
    }
974

    
975
    $vars['widgets'][$id] = $widget;
976
  }
977

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

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

    
1005
/**
1006
 * Theme function for a View with form elements: replace the placeholders.
1007
 */
1008
function theme_views_form_views_form($variables) {
1009
  $form = $variables['form'];
1010

    
1011
  // Placeholders and their substitutions (usually rendered form elements).
1012
  $search = array();
1013
  $replace = array();
1014

    
1015
  // Add in substitutions provided by the form.
1016
  foreach ($form['#substitutions']['#value'] as $substitution) {
1017
    $field_name = $substitution['field_name'];
1018
    $row_id = $substitution['row_id'];
1019

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

    
1030
  // Apply substitutions to the rendered output.
1031
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
1032

    
1033
  // Render and add remaining form fields.
1034
  return drupal_render_children($form);
1035
}
1036

    
1037
function theme_views_mini_pager($vars) {
1038
  global $pager_page_array, $pager_total;
1039

    
1040
  $tags = $vars['tags'];
1041
  $element = $vars['element'];
1042
  $parameters = $vars['parameters'];
1043

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

    
1050
  if ($pager_total[$element] > 1) {
1051

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

    
1064
    $li_next = theme('pager_next',
1065
      array(
1066
        'text' => (isset($tags[3]) ? $tags[3] : t('››')),
1067
        'element' => $element,
1068
        'interval' => 1,
1069
        'parameters' => $parameters,
1070
      )
1071
    );
1072

    
1073
    if (empty($li_next)) {
1074
      $li_next = "&nbsp;";
1075
    }
1076

    
1077
    $items[] = array(
1078
      'class' => array('pager-previous'),
1079
      'data' => $li_previous,
1080
    );
1081

    
1082
    $items[] = array(
1083
      'class' => array('pager-current'),
1084
      'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
1085
    );
1086

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

    
1102
/**
1103
 * Generic <div> container function.
1104
 */
1105
function theme_views_container($variables) {
1106
  $element = $variables['element'];
1107
  return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
1108
}
1109

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