Elgg  Version 6.1
table.php
Go to the documentation of this file.
1 <?php
24 
26 $pagination = elgg_extract('pagination', $vars, true);
27 $position = elgg_extract('position', $vars, 'after');
28 
29 /* @var TableColumn[] $columns */
30 $columns = elgg_extract('columns', $vars);
31 if (empty($columns) || !is_array($columns)) {
32  return;
33 }
34 
35 if (!is_array($items) || count($items) == 0) {
36  echo elgg_view('page/components/no_results', $vars);
37  return;
38 }
39 
40 // render THEAD
41 $headings = '';
42 foreach ($columns as $column) {
43  if (!$column instanceof TableColumn) {
44  elgg_log('$vars["columns"] must be an array of ' . TableColumn::class, \Psr\Log\LogLevel::NOTICE);
45  return;
46  }
47 
48  $cell = trim($column->renderHeading());
49  if (!preg_match('~^<t[dh]~i', $cell)) {
50  $cell = elgg_format_element('th', [], $cell);
51  }
52 
53  $headings .= $cell;
54 }
55 
56 $headings = "<thead><tr>$headings</tr></thead>";
57 
58 $table_classes = elgg_extract_class($vars, ['elgg-list', 'elgg-table'], 'list_class');
59 
60 $nav = ($pagination) ? elgg_view('navigation/pagination', $vars) : '';
61 
62 $rows = '';
63 foreach ($items as $item) {
64  $row_attrs = [
65  'class' => elgg_extract_class($vars, 'elgg-item', 'item_class'),
66  ];
67 
68  $type = '';
69  if ($item instanceof \ElggEntity) {
70  $guid = $item->guid;
71  $type = $item->type;
72  $subtype = $item->getSubtype();
73 
74  $row_attrs['id'] = "elgg-$type-$guid";
75  $row_attrs['class'][] = "elgg-item-$type";
76  $row_attrs['data-elgg-guid'] = $guid;
77  $row_attrs['data-elgg-type-subtype'] = "$type:$subtype";
78  if ($subtype) {
79  $row_attrs['class'][] = "elgg-item-$type-$subtype";
80  }
81  } elseif (is_callable([$item, 'getType'])) {
82  $type = $item->getType();
83 
84  $row_attrs['id'] = "elgg-$type-{$item->id}";
85  $row_attrs['data-elgg-id'] = $item->id;
86  $row_attrs['data-elgg-type'] = $type;
87  }
88 
89  $row = '';
90 
91  foreach ($columns as $column) {
92  $cell = trim($column->renderCell($item, $type, $vars));
93  if (!preg_match('~^<t[dh]~i', $cell)) {
94  $cell = elgg_format_element('td', [], $cell);
95  }
96 
97  $row .= $cell;
98  }
99 
100  $rows .= elgg_format_element('tr', $row_attrs, $row);
101 }
102 
103 $body = "$headings<tbody>$rows</tbody>";
104 
105 if ($position == 'before' || $position == 'both') {
106  echo $nav;
107 }
108 
109 echo elgg_format_element('table', ['class' => $table_classes], $body);
110 
111 if ($position == 'after' || $position == 'both') {
112  echo $nav;
113 }
114 
116 if (!$pagination && $limit !== false && !empty($items) && count($items) >= $limit) {
117  echo elgg_view('page/components/list/widget_more', $vars);
118 }
if(!$items) $item
Definition: delete.php:13
$table_classes
Definition: table.php:58
$rows
Definition: table.php:62
foreach($items as $item) $body
Definition: table.php:103
$pagination
Definition: table.php:26
$nav
Definition: table.php:60
$column
Definition: add.php:10
if(empty($columns)||!is_array($columns)) if(!is_array($items)||count($items)==0) $headings
Definition: table.php:41
$type
Definition: delete.php:21
if($position== 'after'||$position== 'both') $limit
Definition: table.php:115
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
$vars['pagination']
Definition: table.php:3
$columns
Definition: table.php:30
$position
Definition: table.php:27
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
$items
Definition: table.php:25
$subtype
Definition: delete.php:22
A renderer for a column of table cells and a header.
Definition: TableColumn.php:7
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:279
$guid
Reset an ElggUpgrade.
Definition: reset.php:6