You are here

public static function Delete::deleteFolderUri in IMCE 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/ImcePlugin/Delete.php \Drupal\imce\Plugin\ImcePlugin\Delete::deleteFolderUri()

Deletes a folder by uri.

1 call to Delete::deleteFolderUri()
Delete::deleteItems in src/Plugin/ImcePlugin/Delete.php
Deletes a list of imce items and returns succeeded ones.

File

src/Plugin/ImcePlugin/Delete.php, line 103

Class

Delete
Defines Imce Delete plugin.

Namespace

Drupal\imce\Plugin\ImcePlugin

Code

public static function deleteFolderUri($uri, $ignore_usage = FALSE, $check_files = FALSE) {

  // Get folder content without any filtering.
  $content = Imce::scanDir($uri);
  if (!empty($content['error'])) {
    return FALSE;
  }
  if ($check_files && !empty($content['files'])) {
    \Drupal::messenger()
      ->addMessage(t('%folder contains files and can not be deleted.', [
      '%folder' => \Drupal::service('file_system')
        ->basename($uri),
    ]), 'error');
    return FALSE;
  }

  // Delete subfolders first.
  foreach ($content['subfolders'] as $path) {
    if (!static::deleteFolderUri($path, $ignore_usage, $check_files)) {
      return FALSE;
    }
  }

  // Delete files.
  foreach ($content['files'] as $path) {
    if (!static::deleteFileUri($path, $ignore_usage)) {
      return FALSE;
    }
  }

  // Recently emptied folders need some refreshing
  // before the removal on windows.
  if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
    @closedir(@opendir($uri));
  }

  // Remove the folder.
  return rmdir($uri);
}