views_handler_area_result.inc 3.1 KB

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