foldershare-8.x-1.2/src/Plugin/FolderShareCommand/Compress.php
src/Plugin/FolderShareCommand/Compress.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | <?php namespace Drupal\foldershare\Plugin\FolderShareCommand; use Drupal\foldershare\Settings; use Drupal\foldershare\Entity\FolderShare; use Drupal\foldershare\Entity\Exception\RuntimeExceptionWithMarkup; /** * Defines a command plugin to archive (compress) files and folders. * * The command creates a new ZIP archive containing all of the selected * files and folders and adds the archive to current folder. * * Configuration parameters: * - 'parentId': the parent folder, if any. * - 'selectionIds': selected entities to Archive. * * @ingroup foldershare * * @FolderShareCommand( * id = "foldersharecommand_archive", * label = @Translation("Compress"), * menuNameDefault = @Translation("Compress"), * menuName = @Translation("Compress"), * description = @Translation("Compress selected files and folders into a new ZIP archive. (This command is in development and not recommended for production sites.)"), * category = "archive", * weight = 20, * parentConstraints = { * "kinds" = { * "rootlist", * "folder", * }, * "access" = "create", * }, * selectionConstraints = { * "types" = { * "one", * "many", * }, * "kinds" = { * "any", * }, * "access" = "view", * }, * ) */ class Compress extends FolderShareCommandBase { /*-------------------------------------------------------------------- * * Execute. * *--------------------------------------------------------------------*/ /** * {@inheritdoc} */ public function execute() { // Consolidate the selection into a single entity list. $children = []; $selectionIds = $this ->getSelectionIds(); $firstItem = NULL; foreach ( $selectionIds as $id ) { $item = FolderShare::load( $id ); if ( $item === NULL) { // The item does not exist. continue ; } $children [] = $item ; if ( $firstItem === NULL) { $firstItem = $item ; } } $nItems = count ( $children ); // Create an archive. try { $parent = $this ->getParent(); if ( $parent === NULL) { $archive = FolderShare::archiveToRoot( $children ); } else { $archive = $parent ->archiveToFolder( $children ); unset( $parent ); } } catch (RuntimeExceptionWithMarkup $e ) { \Drupal::messenger()->addMessage( $e ->getMarkup(), 'error' , TRUE); } catch (\Exception $e ) { \Drupal::messenger()->addMessage( $e ->getMessage(), 'error' ); } if (Settings::getCommandNormalCompletionReportEnable() === TRUE) { if ( $nItems === 1) { // One item. Refer to it by name. \Drupal::messenger()->addMessage( t( "The @kind '@name' has been compressed and saved into the new '@archive' file." , [ '@kind' => FolderShare::translateKind( $firstItem ->getKind()), '@name' => $firstItem ->getName(), '@archive' => $archive ->getName(), ]), 'status' ); } elseif ( count ( $children ) === 1) { // One kind of items. Refer to them by kind. \Drupal::messenger()->addMessage( t( "@number @kinds have been compressed and saved into the new '@archive' file." , [ '@number' => $nItems , '@kinds' => FolderShare::translateKinds( $firstItem ->getKind()), '@archive' => $archive ->getName(), ]), 'status' ); } else { // Multiple kinds of items. \Drupal::messenger()->addMessage( t( "@number @kinds have been compressed and saved into the new '@archive' file." , [ '@number' => $nItems , '@kinds' => FolderShare::translateKinds( 'items' ), '@archive' => $archive ->getName(), ]), 'status' ); } } } } |