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