View source
<?php
namespace Drupal\imce\Plugin\ImcePlugin;
use Drupal\imce\Imce;
use Drupal\imce\ImcePluginBase;
use Drupal\imce\ImceFM;
class Delete extends ImcePluginBase {
public function permissionInfo() {
return [
'delete_files' => $this
->t('Delete files'),
'delete_subfolders' => $this
->t('Delete subfolders'),
];
}
public function buildPage(array &$page, ImceFM $fm) {
if ($fm
->hasPermission('delete_files') || $fm
->hasPermission('delete_subfolders')) {
$page['#attached']['library'][] = 'imce/drupal.imce.delete';
}
}
public function opDelete(ImceFM $fm) {
$items = $fm
->getSelection();
if ($this
->validateDelete($fm, $items)) {
$this
->deleteItems($fm, $items);
}
}
public function validateDelete(ImceFM $fm, array $items) {
return $items && $fm
->validatePermissions($items, 'delete_files', 'delete_subfolders') && $fm
->validatePredefinedPath($items);
}
public function deleteItems(ImceFM $fm, array $items) {
$success = [];
$ignore_usage = $fm
->getConf('ignore_usage', FALSE);
foreach ($items as $item) {
if ($uri = $item
->getUri()) {
$result = $item->type === 'folder' ? $this
->deleteFolderUri($uri, $ignore_usage, !$item
->getPermission('delete_files')) : $this
->deleteFileUri($uri, $ignore_usage);
if ($result) {
$item
->removeFromJs();
$item
->remove();
$success[] = $item;
}
}
}
return $success;
}
public static function deleteFileUri($uri, $ignore_usage = FALSE) {
if ($file = Imce::getFileEntity($uri)) {
if (!$ignore_usage && ($usage = \Drupal::service('file.usage')
->listUsage($file))) {
unset($usage['imce']);
if ($usage) {
\Drupal::messenger()
->addMessage(t('%filename is in use by another application.', [
'%filename' => $file
->getFilename(),
]), 'error');
return FALSE;
}
}
$file
->delete();
return TRUE;
}
return \Drupal::service('file_system')
->delete($uri);
}
public static function deleteFolderUri($uri, $ignore_usage = FALSE, $check_files = FALSE) {
$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;
}
foreach ($content['subfolders'] as $path) {
if (!static::deleteFolderUri($path, $ignore_usage, $check_files)) {
return FALSE;
}
}
foreach ($content['files'] as $path) {
if (!static::deleteFileUri($path, $ignore_usage)) {
return FALSE;
}
}
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
@closedir(@opendir($uri));
}
return rmdir($uri);
}
}