views_handler_area_result.inc

Definition of views_handler_area_result.

File

handlers/views_handler_area_result.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_area_result.
  5. */
  6. /**
  7. * Views area handler to display some configurable result summary.
  8. *
  9. * @ingroup views_area_handlers
  10. */
  11. class views_handler_area_result extends views_handler_area {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['content'] = array(
  15. 'default' => 'Displaying @start - @end of @total',
  16. 'translatable' => TRUE,
  17. );
  18. return $options;
  19. }
  20. function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. $variables = array(
  23. 'items' => array(
  24. '@start -- the initial record number in the set',
  25. '@end -- the last record number in the set',
  26. '@total -- the total records in the set',
  27. '@name -- the human-readable name of the view',
  28. '@per_page -- the number of items per page',
  29. '@current_page -- the current page number',
  30. '@current_record_count -- the current page record count',
  31. '@page_count -- the total page count',
  32. ),
  33. );
  34. $list = theme('item_list', $variables);
  35. $form['content'] = array(
  36. '#title' => t('Display'),
  37. '#type' => 'textarea',
  38. '#rows' => 3,
  39. '#default_value' => $this->options['content'],
  40. '#description' => t('You may use HTML code in this field. The following tokens are supported:') . $list,
  41. );
  42. }
  43. /**
  44. * Find out the information to render.
  45. */
  46. function render($empty = FALSE) {
  47. // Must have options and does not work on summaries.
  48. if (!isset($this->options['content']) || $this->view->plugin_name == 'default_summary') {
  49. return;
  50. }
  51. $output = '';
  52. $format = $this->options['content'];
  53. // Calculate the page totals.
  54. $current_page = (int) $this->view->get_current_page() + 1;
  55. $per_page = (int) $this->view->get_items_per_page();
  56. $count = count($this->view->result);
  57. // @TODO: Maybe use a possible is views empty functionality.
  58. // Not every view has total_rows set, use view->result instead.
  59. $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result);
  60. $name = check_plain($this->view->human_name);
  61. if ($per_page === 0) {
  62. $page_count = 1;
  63. $start = 1;
  64. $end = $total;
  65. }
  66. else {
  67. $page_count = (int) ceil($total / $per_page);
  68. $total_count = $current_page * $per_page;
  69. if ($total_count > $total) {
  70. $total_count = $total;
  71. }
  72. $start = ($current_page - 1) * $per_page + 1;
  73. $end = $total_count;
  74. }
  75. $current_record_count = ($end - $start) + 1;
  76. // Get the search information.
  77. $items = array('start', 'end', 'total', 'name', 'per_page', 'current_page', 'current_record_count', 'page_count');
  78. $replacements = array();
  79. foreach ($items as $item) {
  80. $replacements["@$item"] = ${$item};
  81. }
  82. // Send the output.
  83. if (!empty($total)) {
  84. $output .= filter_xss_admin(str_replace(array_keys($replacements), array_values($replacements), $format));
  85. }
  86. return $output;
  87. }
  88. }