class_Column.inc 6.14 KiB
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2017-2018  FusionDirectory
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
/*!
 * \brief Column base class
class Column
  /*! \brief Array of attributes to look for, ordered by priority
   * The first non-empty attribute will be displayed
   * */
  protected $attributes;
  /*! \brief Same thing for templates, if it differs */
  protected $templateAttributes = NULL;
  protected $label;
  protected $type = 'string';
  /* management class instance */
  protected $parent = NULL;
  /*!
   * \brief Builds a column object from given data
   * \param managementListing $parent the managementListing instance
   * \param string $type a column class
   * \param array $data an associative array with "attributes" and "label"
   * */
  static function build (managementListing $parent, string $type, array $data): Column
    $attributes = NULL;
    $label      = NULL;
    if (isset($data['attributes'])) {
      $attributes = $data['attributes'];
      if (!is_array($attributes)) {
        $attributes = array_map('trim', explode(',', $attributes));
    if (isset($data['label'])) {
      $label = $data['label'];
    return new $type($parent, $attributes, $label);
  function __construct (managementListing $parent, array $attributes = NULL, string $label = NULL)
    $this->parent = $parent;
    $this->label  = $label;
    $this->setAttributesVar('attributes', $attributes);
  protected function setAttributesVar (string $var, array $attributes = NULL)
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
{ $this->$var = $attributes; } function setTemplateAttributes (array $attributes = NULL) { $this->setAttributesVar('templateAttributes', $attributes); } function isSortable (): bool { return TRUE; } function isExportable (): bool { return !empty($this->attributes); } function getHtmlProps (): string { return ''; } function getHtmlCellProps (): string { return ''; } function getLabel (): string { if (isset($this->label)) { return _($this->label); } else { return ' '; } } function fillNeededAttributes (array &$attrs) { if (isset($this->attributes)) { foreach ($this->attributes as $attr) { if (($attr == 'mainAttr') || ($attr == 'nameAttr')) { /* nameAttr and mainAttr as always set as needed in managementFilter */ continue; } elseif ($attr == 'dn') { /* Handle special case of dn */ $attrs[$attr] = 'raw'; } else { /* Get all values from other attributes */ $attrs[$attr] = '*'; } } } } function fillSearchedAttributes (array &$attrs) { if (isset($this->attributes)) { foreach ($this->attributes as $attr) { if (($attr == 'mainAttr') || ($attr == 'nameAttr')) { /* nameAttr and mainAttr as always searched for */ continue; } if ($attr != 'dn') { $attrs[] = $attr; } } } }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
function fillRowClasses (array &$classes, ListingEntry $entry) { } protected function getAttributeValues (ListingEntry $entry): array { $attrs = $this->attributes; if (isset($this->templateAttributes) && $entry->isTemplate()) { $attrs = $this->templateAttributes; } if (isset($attrs)) { foreach ($attrs as $attr) { if (($attr == 'mainAttr') || ($attr == 'nameAttr')) { $infos = objects::infos($entry->getTemplatedType()); $attr = $infos[$attr]; } if (isset($entry[$attr])) { if (is_array($entry[$attr])) { return $entry[$attr]; } else { /* Should only happen for dn */ return [$entry[$attr]]; } } } } return []; } function renderCell (ListingEntry $entry): string { $values = $this->getAttributeValues($entry); if (empty($values)) { return '&nbsp;'; } else { return implode("<br/>\n", array_map( function ($value) use ($entry) { return $this->renderSingleValue($entry, $value); }, $values ) ); } } protected function renderSingleValue (ListingEntry $entry, string $value): string { return htmlescape($value); } function getRawExportValues (ListingEntry $entry): array { return $this->getAttributeValues($entry); } function compare (ListingEntry $ao, ListingEntry $bo): int { $a = $this->getAttributeValues($ao)[0] ?? ''; $b = $this->getAttributeValues($bo)[0] ?? ''; // Take a look at the several types switch ($this->type) { case 'department': return strnatcmp($a, $b); case 'integer': return $b - $a;