FusionDirectory
class_Column.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2017-2018 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
24 class Column
25 {
29  protected $attributes;
31  protected $templateAttributes = NULL;
32  protected $label;
33  protected $type = 'string';
34 
35 
36  /* management class instance */
37  protected $parent = NULL;
38 
46  static function build (managementListing $parent, string $type, array $data): Column
47  {
48  $attributes = NULL;
49  $label = NULL;
50  if (isset($data['attributes'])) {
51  $attributes = $data['attributes'];
52  if (!is_array($attributes)) {
53  $attributes = array_map('trim', explode(',', $attributes));
54  }
55  }
56  if (isset($data['label'])) {
57  $label = $data['label'];
58  }
59 
60  return new $type($parent, $attributes, $label);
61  }
62 
63  function __construct (managementListing $parent, array $attributes = NULL, string $label = NULL)
64  {
65  $this->parent = $parent;
66  $this->label = $label;
67  $this->setAttributesVar('attributes', $attributes);
68  }
69 
70  protected function setAttributesVar (string $var, array $attributes = NULL)
71  {
72  $this->$var = $attributes;
73  }
74 
75  function setTemplateAttributes (array $attributes = NULL)
76  {
77  $this->setAttributesVar('templateAttributes', $attributes);
78  }
79 
80  function isSortable (): bool
81  {
82  return TRUE;
83  }
84 
85  function isExportable (): bool
86  {
87  return !empty($this->attributes);
88  }
89 
90  function getHtmlProps (): string
91  {
92  return '';
93  }
94 
95  function getHtmlCellProps (): string
96  {
97  return '';
98  }
99 
100  function getLabel (): string
101  {
102  if (isset($this->label)) {
103  return _($this->label);
104  } else {
105  return ' ';
106  }
107  }
108 
109  function fillNeededAttributes (array &$attrs)
110  {
111  if (isset($this->attributes)) {
112  foreach ($this->attributes as $attr) {
113  if (($attr == 'mainAttr') || ($attr == 'nameAttr')) {
114  /* nameAttr and mainAttr as always set as needed in managementFilter */
115  continue;
116  } elseif ($attr == 'dn') {
117  /* Handle special case of dn */
118  $attrs[$attr] = 'raw';
119  } else {
120  /* Get all values from other attributes */
121  $attrs[$attr] = '*';
122  }
123  }
124  }
125  }
126 
127  function fillSearchedAttributes (array &$attrs)
128  {
129  if (isset($this->attributes)) {
130  foreach ($this->attributes as $attr) {
131  if (($attr == 'mainAttr') || ($attr == 'nameAttr')) {
132  /* nameAttr and mainAttr as always searched for */
133  continue;
134  }
135  if ($attr != 'dn') {
136  $attrs[] = $attr;
137  }
138  }
139  }
140  }
141 
142  function fillRowClasses (array &$classes, ListingEntry $entry)
143  {
144  }
145 
146  protected function getAttributeValues (ListingEntry $entry): array
147  {
148  $attrs = $this->attributes;
149  if (isset($this->templateAttributes) && $entry->isTemplate()) {
150  $attrs = $this->templateAttributes;
151  }
152  if (isset($attrs)) {
153  foreach ($attrs as $attr) {
154  if (($attr == 'mainAttr') || ($attr == 'nameAttr')) {
155  $infos = objects::infos($entry->getTemplatedType());
156  $attr = $infos[$attr];
157  }
158  if (isset($entry[$attr])) {
159  if (is_array($entry[$attr])) {
160  return $entry[$attr];
161  } else {
162  /* Should only happen for dn */
163  return [$entry[$attr]];
164  }
165  }
166  }
167  }
168  return [];
169  }
170 
171  function renderCell (ListingEntry $entry): string
172  {
173  $values = $this->getAttributeValues($entry);
174  if (empty($values)) {
175  return '&nbsp;';
176  } else {
177  return implode("<br/>\n",
178  array_map(
179  function ($value) use ($entry)
180  {
181  return $this->renderSingleValue($entry, $value);
182  },
183  $values
184  )
185  );
186  }
187  }
188 
189  protected function renderSingleValue (ListingEntry $entry, string $value): string
190  {
191  return htmlescape($value);
192  }
193 
194  function getRawExportValues (ListingEntry $entry): array
195  {
196  return $this->getAttributeValues($entry);
197  }
198 
199  function compare (ListingEntry $ao, ListingEntry $bo): int
200  {
201  $a = $this->getAttributeValues($ao)[0] ?? '';
202  $b = $this->getAttributeValues($bo)[0] ?? '';
203 
204  // Take a look at the several types
205  switch ($this->type) {
206  case 'department':
207  return strnatcmp($a, $b);
208 
209  case 'integer':
210  return $b - $a;
211 
212  case 'date':
213  if ($a == '') {
214  $a = '31.12.0000';
215  }
216  if ($b == '') {
217  $b = '31.12.0000';
218  }
219  list($d, $m, $y) = explode('.', $a);
220  $a = (int)sprintf('%04d%02d%02d', $y, $m, $d);
221  list($d, $m, $y) = explode('.', $b);
222  $b = (int)sprintf('%04d%02d%02d', $y, $m, $d);
223  return $b - $a;
224 
225  case 'ip':
226  $parts_a = explode('.', $a, 4);
227  $parts_b = explode('.', $b, 4);
228  for ($i = 0; $i < 4; $i++) {
229  if ((int)($parts_a[$i]) != (int)($parts_b[$i])) {
230  return (int)($parts_a[$i]) - (int)($parts_b[$i]);
231  }
232  }
233  return 0;
234 
235  // Sort for string by default
236  case 'string':
237  default:
238  return strcoll($a, $b);
239  }
240  }
241 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
$attributes
Array of attributes to look for, ordered by priority The first non-empty attribute will be displayed...
static build(managementListing $parent, string $type, array $data)
Builds a column object from given data.
This class handles the entries list for a management instance.
Column base class.
$templateAttributes
Same thing for templates, if it differs.