#281555: Add row classes (first, last, even, odd, row number) to the unformatted...
[project/views.git] / theme / theme.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file theme.inc
6 *
7 * An array of preprocessors to fill variables for templates and helper
8 * functions to make theming easier.
9 */
10
11 /**
12 * Provide a full array of possible themes to try for a given hook.
13 *
14 * @param $hook
15 * The hook to use. This is the base theme/template name.
16 * @param $view
17 * The view being rendered.
18 * @param $display
19 * The display being rendered, if applicable.
20 */
21 function _views_theme_functions($hook, $view, $display = NULL) {
22 $themes = array();
23
24 if ($display) {
25 $themes[] = $hook . '__' . $view->name . '__' . $display->id;
26 $themes[] = $hook . '__' . $display->id;
27 if ($display->id != $display->display_plugin) {
28 $themes[] = $hook . '__' . $view->name . '__' . $display->display_plugin;
29 $themes[] = $hook . '__' . $display->display_plugin;
30 }
31 }
32 $themes[] = $hook . '__' . $view->name;
33 $themes[] = $hook;
34
35 return $themes;
36 }
37
38 /**
39 * Preprocess the primary theme implementation for a view.
40 */
41 function template_preprocess_views_view(&$vars) {
42 $view = $vars['view'];
43
44 $vars['rows'] = !empty($view->result) || !empty($view->style_plugin->definition['even empty']) ? $view->style_plugin->render($view->result) : '';
45
46 $vars['css_name'] = views_css_safe($view->name);
47 $vars['name'] = $view->name;
48 $vars['display_id'] = $view->current_display;
49
50 if (!$vars['rows']) {
51 $vars['empty'] = $view->display_handler->render_empty();
52 if (!$view->display_handler->get_option('header_empty')) {
53 $vars['header'] = '';
54 }
55 if (!$view->display_handler->get_option('footer_empty')) {
56 $vars['footer'] = '';
57 }
58 }
59 else {
60 $vars['empty'] = '';
61 $header = TRUE;
62 }
63
64 $vars['exposed'] = !empty($view->exposed_widgets) ? $view->exposed_widgets : '';
65 if (!isset($vars['header'])) {
66 $vars['header'] = $view->display_handler->render_header();
67 }
68 if (!isset($vars['footer'])) {
69 $vars['footer'] = $view->display_handler->render_footer();
70 }
71 $vars['more'] = $view->display_handler->render_more_link();
72 $vars['feed_icon'] = !empty($view->feed_icon) ? $view->feed_icon : '';
73
74 $vars['attachment_before'] = !empty($view->attachment_before) ? $view->attachment_before : '';
75 $vars['attachment_after'] = !empty($view->attachment_after) ? $view->attachment_after : '';
76
77 $vars['pager'] = '';
78
79 $exposed_input = isset($view->exposed_data_raw) ? $view->exposed_data_raw : NULL;
80 if (!empty($view->pager['use_pager'])) {
81 $pager_type = ($view->pager['use_pager'] === 'mini' ? 'views_mini_pager' : 'pager');
82 $pager_theme = views_theme_functions($pager_type, $view, $view->display_handler->display);
83 $vars['pager'] = theme($pager_theme, $exposed_input, $view->pager['items_per_page'], $view->pager['element']);
84 }
85
86 // if administrator, add some links. These used to be tabs, but this is better.
87 if (user_access('administer views') && module_exists('views_ui') && empty($view->hide_admin_links) && !variable_get('views_no_hover_links', FALSE)) {
88 $vars['admin_links_raw'] = array(
89 array(
90 'title' => t('Edit'),
91 'alt' => t("Edit this view"),
92 'href' => "admin/build/views/edit/$view->name",
93 'fragment' => 'views-tab-' . $view->current_display,
94 'query' => drupal_get_destination(),
95 ),
96 array(
97 'title' => t('Export'),
98 'alt' => t("Export this view"),
99 'href' => "admin/build/views/export/$view->name",
100 ),
101 array(
102 'title' => t('Clone'),
103 'alt' => t("Create a copy of this view"),
104 'href' => "admin/build/views/clone/$view->name",
105 ),
106 );
107
108 drupal_alter('views_admin_links', $vars['admin_links_raw']);
109 $vars['admin_links'] = theme('links', $vars['admin_links_raw']);
110 views_add_css('views');
111 }
112 else {
113 $vars['admin_links'] = '';
114 $vars['admin_links_raw'] = array();
115 }
116
117 // Our JavaScript needs to have some means to find the HTML belonging to this
118 // view.
119 //
120 // It is true that the DIV wrapper has classes denoting the name of the view
121 // and its display ID, but this is not enough to unequivocally match a view
122 // with its HTML, because one view may appear several times on the page. So
123 // we set up a running counter, $dom_id, to issue a "unique" identifier for
124 // each view. This identifier is written to both Drupal.settings and the DIV
125 // wrapper.
126 static $dom_id = 1;
127 $vars['dom_id'] = !empty($view->dom_id) ? $view->dom_id : $dom_id++;
128
129 // If using AJAX, send identifying data about this view.
130 if ($view->use_ajax) {
131 $settings = array(
132 'views' => array(
133 'ajax_path' => url('views/ajax'),
134 'ajaxViews' => array(
135 array(
136 'view_name' => $view->name,
137 'view_display_id' => $view->current_display,
138 'view_args' => implode('/', $view->args),
139 'view_path' => $_GET['q'],
140 'view_dom_id' => $vars['dom_id'],
141 // To fit multiple views on a page, the programmer may have
142 // overridden the display's pager_element.
143 'pager_element' => $view->pager['element'],
144 ),
145 ),
146 ),
147 );
148
149 drupal_add_js($settings, 'setting');
150 views_add_js('ajax_view');
151 }
152 }
153
154 /**
155 * Preprocess theme function to print a single record from a row, with fields
156 */
157 function template_preprocess_views_view_fields(&$vars) {
158 $view = $vars['view'];
159
160 // Loop through the fields for this view.
161 $inline = FALSE;
162 $vars['fields'] = array(); // ensure it's at least an empty array.
163 foreach ($view->field as $id => $field) {
164 if (empty($field->options['exclude'])) {
165 $object = new stdClass();
166
167 $object->content = $view->field[$id]->theme($vars['row']);
168 if (isset($view->field[$id]->field_alias) && isset($vars['row']->{$view->field[$id]->field_alias})) {
169 $object->raw = $vars['row']->{$view->field[$id]->field_alias};
170 }
171 else {
172 $object->raw = NULL; // make sure it exists to reduce NOTICE
173 }
174 $object->inline = !empty($vars['options']['inline'][$id]);
175 $object->inline_html = $object->inline ? 'span' : 'div';
176 if (!empty($vars['options']['separator']) && $inline && $object->inline && $object->content) {
177 $object->separator = filter_xss_admin($vars['options']['separator']);
178 }
179
180 $inline = $object->inline;
181
182 $object->handler = &$view->field[$id];
183 $object->element_type = $object->handler->element_type();
184
185 $object->class = views_css_safe($id);
186 $object->label = check_plain($view->field[$id]->label());
187 $vars['fields'][$id] = $object;
188 }
189 }
190
191 }
192
193 /**
194 * Display a single views field.
195 *
196 * Interesting bits of info:
197 * $field->field_alias says what the raw value in $row will be. Reach it like
198 * this: @code { $row->{$field->field_alias} @endcode
199 */
200 function theme_views_view_field($view, $field, $row) {
201 return $field->render($row);
202 }
203
204 /**
205 * Process a single field within a view.
206 *
207 * This preprocess function isn't normally run, as a function is used by
208 * default, for performance. However, by creating a template, this
209 * preprocess should get pickedup.
210 */
211 function template_preprocess_views_view_field(&$vars) {
212 $vars['output'] = $vars['field']->render($vars['row']);
213 }
214
215 /**
216 * Preprocess theme function to print a single record from a row, with fields
217 */
218 function template_preprocess_views_view_summary(&$vars) {
219 $view = $vars['view'];
220 $argument = $view->argument[$view->build_info['summary_level']];
221
222 $url_options = array();
223
224 if (!empty($view->exposed_raw_input)) {
225 $url_options['query'] = $view->exposed_raw_input;
226 }
227 foreach ($vars['rows'] as $id => $row) {
228 $vars['rows'][$id]->link = $argument->summary_name($row);
229 $args = $view->args;
230 $args[$argument->position] = $argument->summary_argument($row);
231
232 $vars['rows'][$id]->url = url($view->get_url($args), $url_options);
233 $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
234 }
235 }
236
237 /**
238 * Template preprocess theme function to print summary basically
239 * unformatted.
240 */
241 function template_preprocess_views_view_summary_unformatted(&$vars) {
242 $view = $vars['view'];
243 $argument = $view->argument[$view->build_info['summary_level']];
244
245 $url_options = array();
246
247 if (!empty($view->exposed_raw_input)) {
248 $url_options['query'] = $view->exposed_raw_input;
249 }
250
251 $count = 0;
252 foreach ($vars['rows'] as $id => $row) {
253 // only false on first time:
254 if ($count++) {
255 $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
256 }
257 $vars['rows'][$id]->link = $argument->summary_name($row);
258 $args = $view->args;
259 $args[$argument->position] = $argument->summary_argument($row);
260
261 $vars['rows'][$id]->url = url($view->get_url($args), $url_options);
262 $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
263 }
264 }
265
266 /**
267 * Display a view as a table style.
268 */
269 function template_preprocess_views_view_table(&$vars) {
270 $view = $vars['view'];
271
272 // We need the raw data for this grouping, which is passed in as $vars['rows'].
273 // However, the template also needs to use for the rendered fields. We
274 // therefore swap the raw data out to a new variable and reset $vars['rows']
275 // so that it can get rebuilt.
276 $result = $vars['rows'];
277 $vars['rows'] = array();
278
279 $options = $view->style_plugin->options;
280 $handler = $view->style_plugin;
281
282 $fields = &$view->field;
283 $columns = $handler->sanitize_columns($options['columns'], $fields);
284
285 $active = !empty($handler->active) ? $handler->active : '';
286 $order = !empty($handler->order) ? $handler->order : 'asc';
287
288 $query = tablesort_get_querystring();
289 if ($query) {
290 $query = '&' . $query;
291 }
292
293 foreach ($columns as $field => $column) {
294 // render the header labels
295 if ($field == $column && empty($fields[$field]->options['exclude'])) {
296 $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
297 if (empty($options['info'][$field]['sortable'])) {
298 $vars['header'][$field] = $label;
299 }
300 else {
301 // @todo -- make this a setting
302 $initial = 'asc';
303
304 if ($active == $field && $order == 'asc') {
305 $initial = 'desc';
306 }
307
308 $image = theme('tablesort_indicator', $initial);
309 $title = t('sort by @s', array('@s' => $label));
310 $link_options = array(
311 'html' => true,
312 'attributes' => array('title' => $title),
313 'query' => 'order=' . urlencode($field) . '&sort=' . $initial . $query,
314 );
315 $vars['header'][$field] = l($label . $image, $_GET['q'], $link_options);
316 }
317 }
318
319 // Create a second variable so we can easily find what fields we have and what the
320 // CSS classes should be.
321 $vars['fields'][$field] = views_css_safe($field);
322 if ($active == $field) {
323 $vars['fields'][$field] .= ' active';
324 }
325
326 // Render each field into its appropriate column.
327 foreach ($result as $num => $row) {
328 if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
329 $field_output = $fields[$field]->theme($row);
330
331 // Don't bother with separators and stuff if the field does not show up.
332 if (!isset($field_output) && isset($vars['rows'][$num][$column])) {
333 continue;
334 }
335
336 // Place the field into the column, along with an optional separator.
337 if (isset($vars['rows'][$num][$column])) {
338 if (!empty($options['info'][$column]['separator'])) {
339 $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
340 }
341 }
342 else {
343 $vars['rows'][$num][$column] = '';
344 }
345
346 $vars['rows'][$num][$column] .= $field_output;
347 }
348 }
349 }
350
351 $vars['class'] = 'views-table';
352 if (!empty($options['sticky'])) {
353 drupal_add_js('misc/tableheader.js');
354 $vars['class'] .= " sticky-enabled";
355 }
356 }
357
358 /**
359 * Display a view as a grid style.
360 */
361 function template_preprocess_views_view_grid(&$vars) {
362 $view = $vars['view'];
363 $result = $view->result;
364 $options = $view->style_plugin->options;
365 $handler = $view->style_plugin;
366
367 $columns = $options['columns'];
368
369 $rows = array();
370
371 if ($options['alignment'] == 'horizontal') {
372 $row = array();
373 foreach ($vars['rows'] as $count => $item) {
374 $row[] = $item;
375 if (($count + 1) % $columns == 0) {
376 $rows[] = $row;
377 $row = array();
378 }
379 }
380 if ($row) {
381 $rows[] = $row;
382 }
383 }
384 else {
385 $num_rows = floor(count($vars['rows']) / $columns);
386 // The remainders are the 'odd' columns that are slightly longer.
387 $remainders = count($vars['rows']) % $columns;
388 $row = 0;
389 $col = 0;
390 foreach ($vars['rows'] as $count => $item) {
391 $rows[$row][$col] = $item;
392 $row++;
393
394 if (!$remainders && $row == $num_rows) {
395 $row = 0;
396 $col++;
397 }
398 else if ($remainders && $row == $num_rows + 1) {
399 $row = 0;
400 $col++;
401 $remainders--;
402 }
403 }
404 }
405 $vars['rows'] = $rows;
406 }
407
408 /**
409 * Display the simple view of rows one after another
410 */
411 function template_preprocess_views_view_unformatted(&$vars) {
412 $view = $vars['view'];
413 $rows = $vars['rows'];
414
415 $vars['classes'] = array();
416 // Set up striping values.
417 foreach ($rows as $id => $row) {
418 $vars['classes'][$id] = 'views-row-' . ($id + 1);
419 $vars['classes'][$id] .= ' views-row-' . ($id % 2 ? 'even' : 'odd');
420 if ($id == 0) {
421 $vars['classes'][$id] .= ' views-row-first';
422 }
423 }
424 $vars['classes'][$id] .= ' views-row-last';
425 }
426
427 /**
428 * Preprocess an RSS feed
429 */
430 function template_preprocess_views_view_rss(&$vars) {
431 global $base_url;
432 global $language;
433
434 $view = &$vars['view'];
435 $options = &$vars['options'];
436 $items = &$vars['rows'];
437
438 $style = &$view->style_plugin;
439
440 if (!empty($options['mission_description'])) {
441 $description = variable_get('site_mission', '');
442 }
443 else {
444 $description = $options['description'];
445 }
446
447 if ($view->display_handler->get_option('sitename_title')) {
448 $title = variable_get('site_name', 'Drupal');
449 if ($slogan = variable_get('site_slogan', '')) {
450 $title .= ' - ' . $slogan;
451 }
452 }
453 else {
454 $title = $view->get_title();
455 }
456
457 // Figure out which display which has a path we're using for this feed. If there isn't
458 // one, use the global $base_url
459 $link_display_id = $view->display_handler->get_link_display();
460 if ($link_display_id && !empty($view->display[$link_display_id])) {
461 $path = $view->display[$link_display_id]->handler->get_path();
462 }
463
464 if ($path) {
465 $path = $view->get_url(NULL, $path);
466 $url_options = array('absolute' => TRUE);
467 if (!empty($view->exposed_raw_input)) {
468 $url_options['query'] = $view->exposed_raw_input;
469 }
470
471 // Compare the link to the default home page; if it's the default home page, just use $base_url.
472 if ($path == variable_get('site_frontpage', 'node')) {
473 $path = '';
474 }
475
476 $vars['link'] = check_url(url($path, $url_options));
477 }
478
479 $vars['namespaces'] = drupal_attributes($style->namespaces);
480 $vars['channel'] = format_rss_channel($title, $vars['link'], $description, $items, $language->language);
481
482 drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
483 }
484
485 /**
486 * Default theme function for all filter forms.
487 */
488 function template_preprocess_views_exposed_form(&$vars) {
489 views_add_css('views');
490 $form = &$vars['form'];
491
492 // Put all single checkboxes together in the last spot.
493 $checkboxes = '';
494
495 if (!empty($form['q'])) {
496 $vars['q'] = drupal_render($form['q']);
497 }
498
499 $vars['widgets'] = array();
500 foreach ($form['#info'] as $id => $info) {
501 // Set aside checkboxes.
502 if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
503 $checkboxes .= drupal_render($form[$info['value']]);
504 continue;
505 }
506 $widget = new stdClass;
507 // set up defaults so that there's always something there.
508 $widget->label = $widget->operator = $widget->widget = NULL;
509
510 if (!empty($info['label'])) {
511 $widget->label = $info['label'];
512 }
513 if (!empty($info['operator'])) {
514 $widget->operator = drupal_render($form[$info['operator']]);
515 }
516 $widget->widget = drupal_render($form[$info['value']]);
517 $vars['widgets'][$id] = $widget;
518 }
519
520 // Wrap up all the checkboxes we set aside into a widget.
521 if ($checkboxes) {
522 $widget = new stdClass;
523 // set up defaults so that there's always something there.
524 $widget->label = $widget->operator = $widget->widget = NULL;
525 $widget->widget = $checkboxes;
526 $vars['widgets']['checkboxes'] = $widget;
527 }
528
529 // Don't render these:
530 unset($form['form_id']);
531 unset($form['form_build_id']);
532 unset($form['form_token']);
533
534 // This includes the submit button.
535 $vars['button'] = drupal_render($form);
536 }
537
538 function theme_views_mini_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
539 global $pager_page_array, $pager_total;
540
541 // Calculate various markers within this pager piece:
542 // Middle is used to "center" pages around the current page.
543 $pager_middle = ceil($quantity / 2);
544 // current is the page we are currently paged to
545 $pager_current = $pager_page_array[$element] + 1;
546 // max is the maximum page number
547 $pager_max = $pager_total[$element];
548 // End of marker calculations.
549
550
551 $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹‹')), $limit, $element, 1, $parameters);
552 $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('››')), $limit, $element, 1, $parameters);
553
554 if ($pager_total[$element] > 1) {
555 $items[] = array(
556 'class' => 'pager-previous',
557 'data' => $li_previous,
558 );
559
560 $items[] = array(
561 'class' => 'pager-current',
562 'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
563 );
564
565 $items[] = array(
566 'class' => 'pager-next',
567 'data' => $li_next,
568 );
569 return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
570 }
571 }
572
573 /**
574 * @defgroup views_templates Views' template files
575 * @{
576 * All views templates can be overridden with a variety of names, using
577 * the view, the display ID of the view, the display type of the view,
578 * or some combination thereof.
579 *
580 * For each view, there will be a minimum of two templates used. The first
581 * is used for all views: views-view.tpl.php.
582 *
583 * The second template is determined by the style selected for the view. Note
584 * that certain aspects of the view can also change which style is used; for
585 * example, arguments which provide a summary view might change the style to
586 * one of the special summary styles.
587 *
588 * The default style for all views is views-view-unformatted.tpl.php
589 *
590 * Many styles will then farm out the actual display of each row to a row
591 * style; the default row style is views-view-fields.tpl.php.
592 *
593 * Here is an example of all the templates that will be tried in the following
594 * case:
595 *
596 * View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
597 *
598 * - views-view--foobar--page.tpl.php
599 * - views-view--page.tpl.php
600 * - views-view--foobar.tpl.php
601 * - views-view.tpl.php
602 *
603 * - views-view-unformatted--foobar--page.tpl.php
604 * - views-view-unformatted--page.tpl.php
605 * - views-view-unformatted--foobar.tpl.php
606 * - views-view-unformatted.tpl.php
607 *
608 * - views-view-fields--foobar--page.tpl.php
609 * - views-view-fields--page.tpl.php
610 * - views-view-fields--foobar.tpl.php
611 * - views-view-fields.tpl.php
612 *
613 * Important! When adding a new template to your theme, be sure to flush the
614 * theme registry cache!
615 *
616 * @see _views_theme_functions
617 * @}
618 */