You are here

public function Presentation::listView in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Presentation.php \Drupal\filebrowser\Presentation::listView()

File

src/Presentation.php, line 74

Class

Presentation

Namespace

Drupal\filebrowser

Code

public function listView() {

  /** @var MetadataInfo $event
   * @var FilebrowserMetadataEntity $metadata */
  foreach ($this->dbFileList['files'] as $key => $display_file_obj) {
    $fid = $display_file_obj->fid;

    // Collect the metadata now
    $metadatas = $this
      ->retrieveMetadata($fid);
    foreach ($metadatas as $metadata) {
      $contents = $metadata
        ->getContent();
      foreach ($contents as $content) {
      }
    }
  }
  $default_sort = $this->filebrowser->defaultSort;
  $sort_order = $this->filebrowser->defaultSortOrder;
  $directory_empty = true;
  $column_names = [];
  $dispatcher = \Drupal::service('event_dispatcher');

  // Visible columns
  $visible_columns = [];
  $e = new MetadataInfo($column_names);
  $event = $dispatcher
    ->dispatch('filebrowser.metadata_info', $e);
  $column_names = $event
    ->getMetaDataInfo();

  // columns provided by Filebrowser:
  // icon, name, created, size, modified, mime_type, description
  // Create the $unsorted_rows array
  $this
    ->createUnsortedRowsAndColumns($unsorted_rows, $visible_columns, $column_names, $directory_empty);
  $header = [];

  // Builder header and clean up unused columns
  $this
    ->buildHeader($header, $unsorted_rows, $visible_columns, $column_names, $default_sort, $sort_order);

  // Split files in two heaps to preserve folders and files
  $request = Request::createFromGlobals();
  $result = $this
    ->splitFiles($this->dbFileList['files']);
  $just_files = isset($result['files']) ? $result['files'] : null;
  $just_folders = isset($result['folders']) ? $result['folders'] : null;
  $table_sort = TableSort::getContextFromRequest($header, $request);

  // Sort files according to correct column.
  if (isset($table_sort['sql'])) {
    $field = $table_sort['sql'];
    if (isset($column_names[$field]) && isset($column_names[$field]['sortable']) && $column_names[$field]['sortable']) {
      $sorter = null;
      switch ($column_names[$field]['type']) {
        case 'integer':
          $sorter = function ($a, $b) use ($field) {
            $a = isset($a->{$field}) ? $a->{$field} : 0;
            $b = isset($b->{$field}) ? $b->{$field} : 0;
            return $a - $b;
          };
          break;
        case 'string':
          $sorter = function ($a, $b) use ($field) {
            $a = isset($a->{$field}) ? $a->{$field} : '';
            $b = isset($b->{$field}) ? $b->{$field} : '';
            return -strcmp(strtolower($a), strtolower($b));
          };
          break;
      }
      if (!empty($just_folders)) {
        usort($just_folders, $sorter);
        if ($table_sort['sort'] == 'asc') {
          $just_folders = array_reverse($just_folders, true);
        }
      }
      if (!empty($just_files)) {
        usort($just_files, $sorter);
        if ($table_sort['sort'] == 'asc') {
          $just_files = array_reverse($just_files, true);
        }
      }
    }
  }
  $rows = [];
  if (!is_null($just_folders)) {
    foreach ($just_folders as $data) {
      $rows[$data->fid] = $unsorted_rows[$data->displayName];
    }
  }
  if (!is_null($just_files)) {
    foreach ($just_files as $data) {
      $rows[$data->fid] = $unsorted_rows[$data->displayName];
    }
  }

  // $this->dbFileList['.']['full_path'] contains the sub folder (relative path of this listing.
  // maybe we need a separate objects to bundle the per-listing data
  if (count($this->formActions) > 0) {
    $params = [
      'header' => $header,
      'rows' => $rows,
      'actions' => $this->formActions,
      'node' => $this->node,
      'dbFileList' => $this->dbFileList,
    ];
    return \Drupal::formBuilder()
      ->getForm('Drupal\\filebrowser\\Form\\ActionForm', $params);
  }

  // align the rows to the proper column in the header
  $rows_aligned = [];
  foreach ($rows as $row) {
    $rows_aligned[] = array_replace(array_flip(array_keys($header)), $row);
  }
  \Drupal::service('page_cache_kill_switch')
    ->trigger();
  return [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows_aligned,
    '#sticky' => true,
    '#empty' => $this
      ->t('This directory is empty'),
    '#attributes' => [
      'id' => 'filebrowser-table',
    ],
  ];
}