list.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * View a list of items
  4. *
  5. * @package Elgg
  6. *
  7. * @uses $vars['items'] Array of ElggEntity, ElggAnnotation or ElggRiverItem objects
  8. * @uses $vars['offset'] Index of the first list item in complete list
  9. * @uses $vars['limit'] Number of items per page. Only used as input to pagination.
  10. * @uses $vars['count'] Number of items in the complete list
  11. * @uses $vars['base_url'] Base URL of list (optional)
  12. * @uses $vars['url_fragment'] URL fragment to add to links if not present in base_url (optional)
  13. * @uses $vars['pagination'] Show pagination? (default: true)
  14. * @uses $vars['position'] Position of the pagination: before, after, or both
  15. * @uses $vars['full_view'] Show the full view of the items (default: false)
  16. * @uses $vars['list_class'] Additional CSS class for the <ul> element
  17. * @uses $vars['item_class'] Additional CSS class for the <li> elements
  18. * @uses $vars['item_view'] Alternative view to render list items
  19. * @uses $vars['no_results'] Message to display if no results (string|Closure)
  20. */
  21. $items = $vars['items'];
  22. $count = elgg_extract('count', $vars);
  23. $pagination = elgg_extract('pagination', $vars, true);
  24. $position = elgg_extract('position', $vars, 'after');
  25. $no_results = elgg_extract('no_results', $vars, '');
  26. if (!$items && $no_results) {
  27. if ($no_results instanceof Closure) {
  28. echo $no_results();
  29. return;
  30. }
  31. echo "<p class='elgg-no-results'>$no_results</p>";
  32. return;
  33. }
  34. if (!is_array($items) || count($items) == 0) {
  35. return;
  36. }
  37. $list_classes = ['elgg-list'];
  38. if (isset($vars['list_class'])) {
  39. $list_classes[] = $vars['list_class'];
  40. }
  41. $item_classes = ['elgg-item'];
  42. if (isset($vars['item_class'])) {
  43. $item_classes[] = $vars['item_class'];
  44. }
  45. $nav = ($pagination) ? elgg_view('navigation/pagination', $vars) : '';
  46. $list_items = '';
  47. foreach ($items as $item) {
  48. $item_view = elgg_view_list_item($item, $vars);
  49. if (!$item_view) {
  50. continue;
  51. }
  52. $li_attrs = ['class' => $item_classes];
  53. if ($item instanceof \ElggEntity) {
  54. $guid = $item->getGUID();
  55. $type = $item->getType();
  56. $subtype = $item->getSubtype();
  57. $li_attrs['id'] = "elgg-$type-$guid";
  58. $li_attrs['class'][] = "elgg-item-$type";
  59. if ($subtype) {
  60. $li_attrs['class'][] = "elgg-item-$type-$subtype";
  61. }
  62. } else if (is_callable(array($item, 'getType'))) {
  63. $li_attrs['id'] = "item-{$item->getType()}-{$item->id}";
  64. }
  65. $list_items .= elgg_format_element('li', $li_attrs, $item_view);
  66. }
  67. if ($position == 'before' || $position == 'both') {
  68. echo $nav;
  69. }
  70. echo elgg_format_element('ul', ['class' => $list_classes], $list_items);
  71. if ($position == 'after' || $position == 'both') {
  72. echo $nav;
  73. }