Elgg  Version 2.3
table.php
Go to the documentation of this file.
1 <?php
2 
4 
26 $items = $vars['items'];
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  elgg_log('$vars["columns"] must be an array of ' . TableColumn::class, 'ERROR');
38  return;
39 }
40 
41 if (!is_array($items) || count($items) == 0) {
42  if ($no_results) {
43  if ($no_results instanceof Closure) {
44  echo $no_results();
45  return;
46  }
47  echo "<p class='elgg-no-results'>$no_results</p>";
48  }
49  return;
50 }
51 
52 // render THEAD
53 $headings = '';
54 foreach ($columns as $column) {
55  if (!$column instanceof TableColumn) {
56  elgg_log('$vars["columns"] must be an array of ' . TableColumn::class, 'ERROR');
57  return;
58  }
59 
60  $cell = trim($column->renderHeading());
61  if (!preg_match('~^<t[dh]~i', $cell)) {
62  $cell = "<th>$cell</th>";
63  }
64  $headings .= $cell;
65 }
66 $headings = "<thead><tr>$headings</tr></thead>";
67 
68 $table_classes = ['elgg-list', 'elgg-table'];
69 if (isset($vars['list_class'])) {
70  $table_classes[] = $vars['list_class'];
71 }
72 
73 $nav = ($pagination) ? elgg_view('navigation/pagination', $vars) : '';
74 
75 $rows = '';
76 foreach ($items as $item) {
77  $row_attrs = [
78  'class' => ['elgg-item']
79  ];
80 
81  if (!empty($vars['item_class'])) {
82  $row_attrs['class'][] = $vars['item_class'];
83  }
84 
85  $type = '';
86  $entity = null;
87 
88  if ($item instanceof \ElggEntity) {
89  $entity = $item;
90  $guid = $item->guid;
91  $type = $item->type;
92  $subtype = $item->getSubtype();
93 
94  $row_attrs['id'] = "elgg-$type-$guid";
95  $row_attrs['class'][] = "elgg-item-$type";
96  $row_attrs['data-elgg-guid'] = $guid;
97  $row_attrs['data-elgg-type-subtype'] = "$type:$subtype";
98  if ($subtype) {
99  $row_attrs['class'][] = "elgg-item-$type-$subtype";
100  }
101 
102  } elseif (is_callable(array($item, 'getType'))) {
103  $type = $item->getType();
104 
105  $row_attrs['id'] = "elgg-$type-{$item->id}";
106  $row_attrs['data-elgg-id'] = $item->id;
107  $row_attrs['data-elgg-type'] = $type;
108  }
109 
110  $row = '';
111 
112  foreach ($columns as $column) {
113  $cell = trim($column->renderCell($item, $type, $vars));
114  if (!preg_match('~^<t[dh]~i', $cell)) {
115  $cell = "<td>$cell</td>";
116  }
117  $row .= $cell;
118  }
119 
120  $rows .= elgg_format_element('tr', $row_attrs, $row);
121 }
122 
123 $body = "$headings<tbody>$rows</tbody>";
124 
125 if ($position == 'before' || $position == 'both') {
126  echo $nav;
127 }
128 
129 echo elgg_format_element('table', ['class' => $table_classes], $body);
130 
131 if ($position == 'after' || $position == 'both') {
132  echo $nav;
133 }
$table_classes
Definition: table.php:68
if(!$items) $item
Definition: delete.php:17
$rows
Definition: table.php:75
foreach($items as $item) $body
Definition: table.php:123
$pagination
Definition: table.php:28
$count
Definition: table.php:27
$column
Definition: add.php:13
$subtype
Definition: delete.php:28
$no_results
Definition: table.php:30
if(empty($columns)||!is_array($columns)) if(!is_array($items)||count($items)==0) $headings
Definition: table.php:53
$cell_views
Definition: table.php:31
$guid
Removes an admin notice.
elgg_format_element($tag_name, array $attributes=array(), $text= '', array $options=array())
Format an HTML element.
Definition: output.php:208
$vars['pagination']
Definition: table.php:3
$columns
Definition: table.php:33
$position
Definition: table.php:29
elgg echo
Translates a string.
Definition: languages.js:48
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:336
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:1375
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1028
$items
View a table of items.
Definition: table.php:26
$entity
Definition: delete.php:7
class
Definition: placeholder.php:21
if(isset($vars['list_class'])) $nav
Definition: table.php:73
A renderer for a column of table cells and a header.
Definition: TableColumn.php:7
$row
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
if(!$display_name) $type
Definition: delete.php:27