views_handler_area_result.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // @TODO: Maybe use a possible is views empty functionality.
  57. // Not every view has total_rows set, use view->result instead.
  58. $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result);
  59. $name = check_plain($this->view->human_name);
  60. if ($per_page === 0) {
  61. $page_count = 1;
  62. $start = 1;
  63. $end = $total;
  64. }
  65. else {
  66. $page_count = (int) ceil($total / $per_page);
  67. $total_count = $current_page * $per_page;
  68. if ($total_count > $total) {
  69. $total_count = $total;
  70. }
  71. $start = ($current_page - 1) * $per_page + 1;
  72. $end = $total_count;
  73. }
  74. $current_record_count = ($end - $start) + 1;
  75. // Get the search information.
  76. $items = array('start', 'end', 'total', 'name', 'per_page', 'current_page', 'current_record_count', 'page_count');
  77. $replacements = array();
  78. foreach ($items as $item) {
  79. $replacements["@$item"] = ${$item};
  80. }
  81. // Send the output.
  82. if (!empty($total)) {
  83. $output .= filter_xss_admin(str_replace(array_keys($replacements), array_values($replacements), $format));
  84. }
  85. return $output;
  86. }
  87. }