You are here

protected function DisplayFileList::processServerFileList in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x src/File/DisplayFileList.php \Drupal\filebrowser\File\DisplayFileList::processServerFileList()
1 call to DisplayFileList::processServerFileList()
DisplayFileList::createFileDisplayList in src/File/DisplayFileList.php
Creates the data required to build the Filebrowser listing in following steps

File

src/File/DisplayFileList.php, line 190

Class

DisplayFileList
Class FileDisplayList @package Drupal\filebrowser This class holds the list of files to be displayed on the filebrowser node. These files are retrieved from the filesystem and filtered for user and node access. The array produced by this class…

Namespace

Drupal\filebrowser\File

Code

protected function processServerFileList() {

  /** @var /Drupal/filebrowser/File/DisplayFile $result_file */
  $stats = [
    'folders_count' => 0,
    'files_count' => 0,
    'total_size' => 0,
  ];
  $encoding = $this->node->filebrowser->encoding;

  // get the DB_contents for this nid
  // first time after node creation $db_content = NULL; there is nothing in the DB
  // If there is content it will return a filename as key, with next indexes:
  // array ('nid' => '1', 'fid' => '3', 'root' => '/', 'path' => '/',)
  $db_content = $this->storage
    ->loadRecordsFromRoot($this->node
    ->id(), $this->relativePath);

  // debug($db_content, 'DB CONTENT');
  // Iterate over file list from the server
  if (!is_null($this->serverFileList)) {
    foreach ($this->serverFileList as $key => $fs_file) {

      // Build file relative path
      $file_relative_path = $this
        ->buildFileRelativePath($fs_file->filename, $encoding);

      // Build database file record if it doesn't exists
      if (!isset($db_content[$file_relative_path])) {
        $db_content[$file_relative_path] = [
          'exists' => true,
          'nid' => $this->node
            ->id(),
          'root' => $this->relativePath,
          'path' => $file_relative_path,
          'description' => $this
            ->t('No description.'),
          'file_data' => $fs_file,
        ];
      }
      $db_content[$file_relative_path]['exists'] = true;
      $db_content[$file_relative_path]['display_name'] = $fs_file->filename;
      $result_file = new DisplayFile($this->node
        ->id());
      $result_file
        ->fileSetData($file_relative_path, $fs_file, $stats, $db_content[$file_relative_path], $this->fsRoot);
      $result_list[$fs_file->filename] = $result_file;
    }
  }

  // The abstracted filesystem does not provide . and .. files. Therefore
  // we will create them manually
  if ($this->isSubDir) {
    $subDir = new DisplayFile($this->node
      ->id());

    // Create the .. file data
    $result_list['..'] = $subDir
      ->createSubdir($this->relativePath);

    // Create the . file data
    $file = new DisplayFile($this->node
      ->id());
    $result_list['.'] = $file
      ->createUpDir($this->relativePath);

    // Set DB content for Up-directory. In this case the '/' folder
    $this
      ->createUpDirContent($db_content['/']);

    //set DB record for current directory (. file)
    if (!isset($db_content[$this->relativePath])) {
      $db_content[$this->relativePath] = [
        'exists' => TRUE,
        'nid' => $this->node
          ->id(),
        'root' => $this->relativePath,
        'path' => $this->relativePath,
      ];
    }
    $db_content[$this->relativePath]['exists'] = true;
    $db_content[$this->relativePath]['display_name'] = '.';
  }
  else {

    // not a sub dir so we only set the . file and / DB data
    if (!isset($db_content['/'])) {
      $db_content['/'] = [
        'nid' => $this->node
          ->id(),
        'root' => '/',
        'path' => '/',
      ];
    }
    $db_content['/']['exists'] = true;
    $db_content['/']['display_name'] = '.';

    // changes to the File System Array
    $result_file = new DisplayFile($this->node
      ->id());
    $result_list['.'] = $result_file
      ->createUpDir($this->relativePath);
  }

  //debug($db_content, 'END DB CONTENT');

  // Set global folder properties
  $this->data['stats'] = $this
    ->buildStatistics($result_list);
  $this->data['relative_path'] = $this->relativePath;
  $this
    ->dbSync($db_content, $result_list, $this->data);
  return $result_list;
}