You are here

class Delete in IMCE 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/ImcePlugin/Delete.php \Drupal\imce\Plugin\ImcePlugin\Delete

Defines Imce Delete plugin.

Plugin annotation


@ImcePlugin(
  id = "delete",
  label = "Delete",
  weight = -5,
  operations = {
    "delete" = "opDelete"
  }
)

Hierarchy

Expanded class hierarchy of Delete

1 file declares its use of Delete
DeleteTest.php in tests/src/Kernel/Plugin/ImcePlugin/DeleteTest.php
2 string references to 'Delete'
ImceProfileDeleteForm::getConfirmText in src/Form/ImceProfileDeleteForm.php
Returns a caption for the button that confirms the action.
ImceProfileDeleteFormTest::testConfirmText in tests/src/Kernel/Form/ImceProfileDeleteFormTest.php
Test the method getConfirmText().

File

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

Namespace

Drupal\imce\Plugin\ImcePlugin
View source
class Delete extends ImcePluginBase {

  /**
   * {@inheritdoc}
   */
  public function permissionInfo() {
    return [
      'delete_files' => $this
        ->t('Delete files'),
      'delete_subfolders' => $this
        ->t('Delete subfolders'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildPage(array &$page, ImceFM $fm) {

    // Check if delete permission exists.
    if ($fm
      ->hasPermission('delete_files') || $fm
      ->hasPermission('delete_subfolders')) {
      $page['#attached']['library'][] = 'imce/drupal.imce.delete';
    }
  }

  /**
   * Operation handler: delete.
   */
  public function opDelete(ImceFM $fm) {
    $items = $fm
      ->getSelection();
    if ($this
      ->validateDelete($fm, $items)) {
      $this
        ->deleteItems($fm, $items);
    }
  }

  /**
   * Validates the deletion of the given items.
   */
  public function validateDelete(ImceFM $fm, array $items) {
    return $items && $fm
      ->validatePermissions($items, 'delete_files', 'delete_subfolders') && $fm
      ->validatePredefinedPath($items);
  }

  /**
   * Deletes a list of imce items and returns succeeded ones.
   */
  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;
  }

  /**
   * Deletes a file by uri.
   */
  public static function deleteFileUri($uri, $ignore_usage = FALSE) {

    // Managed file.
    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;
    }

    // Unmanaged file.
    return \Drupal::service('file_system')
      ->delete($uri);
  }

  /**
   * Deletes a folder by uri.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Delete::buildPage public function Builds imce page by adding required libraries and elements. Overrides ImcePluginBase::buildPage
Delete::deleteFileUri public static function Deletes a file by uri.
Delete::deleteFolderUri public static function Deletes a folder by uri.
Delete::deleteItems public function Deletes a list of imce items and returns succeeded ones.
Delete::opDelete public function Operation handler: delete.
Delete::permissionInfo public function Returns folder permission definitions. Overrides ImcePluginBase::permissionInfo
Delete::validateDelete public function Validates the deletion of the given items.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ImcePluginBase::alterProfileForm public function Alters entity form of an Imce Profile. Overrides ImcePluginInterface::alterProfileForm
ImcePluginBase::processUserConf public function Processes profile configuration for a user. Overrides ImcePluginInterface::processUserConf
ImcePluginBase::validateProfileForm public function Validates entity form of an Imce Profile. Overrides ImcePluginInterface::validateProfileForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.