foldershare-8.x-1.2/src/Plugin/FolderShareCommand/ReleaseShare.php
src/Plugin/FolderShareCommand/ReleaseShare.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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | <?php namespace Drupal\foldershare\Plugin\FolderShareCommand; use Drupal\Core\Form\FormStateInterface; use Drupal\foldershare\Settings; use Drupal\foldershare\Utilities\FormatUtilities; use Drupal\foldershare\Entity\FolderShare; /** * Provides the base class for command plugins that delete files or folders. * * The command deletes all selected entities. Deletion recurses and * deletes all folder content as well. * * Configuration parameters: * - 'parentId': the parent folder, if any. * - 'selectionIds': selected entities to delete. * * @ingroup foldershare * * @FolderShareCommand( * id = "foldersharecommand_release_share", * label = @Translation("Release Share"), * menuNameDefault = @Translation("Release Share..."), * menuName = @Translation("Release Share..."), * description = @Translation("Release selected shared files and folders."), * category = "settings", * weight = 20, * userConstraints = { * "authenticated", * }, * parentConstraints = { * "kinds" = { * "rootlist", * }, * "access" = "update", * }, * selectionConstraints = { * "types" = { * "one", * "many", * }, * "kinds" = { * "any", * }, * "ownership" = { * "sharedwithusertoview", * "sharedwithusertoauthor", * }, * "access" = "view", * }, * ) */ class ReleaseShare extends FolderShareCommandBase { /*-------------------------------------------------------------------- * * Configuration form. * *--------------------------------------------------------------------*/ /** * {@inheritdoc} */ public function hasConfigurationForm() { return TRUE; } /** * {@inheritdoc} */ public function getDescription(bool $forPage ) { // The description varies for page vs. dialog: // // - Dialog: The description is longer and has the form "Release // the shared OPERAND? Once released, these items will no longer be // accessible." For a single item, OPERAND is the NAME of the file/folder. // // - Page: The description is as for a dialog, except that the single // item form is not included because it is already in the title. $selectionIds = $this ->getSelectionIds(); if ( count ( $selectionIds ) === 1) { // There is only one item. Load it. $item = FolderShare::load(reset( $selectionIds )); if ( $forPage === TRUE) { // Page description. The page title already gives the name of the // item to be changed. Don't include the item's name again here. return FormatUtilities::createFormattedMessage( t( 'Release this shared @operand?' , [ '@operand' => FolderShare::translateKind( $item ->getKind()), ]), t( 'Once released, this @operand will no longer be accessible by you.' , [ '@operand' => FolderShare::translateKind( $item ->getKind()), ])); } // Dialog description. Include the name of the item to be deleted. return FormatUtilities::createFormattedMessage( t( 'Release the shared @operand "@name"?' , [ '@operand' => FolderShare::translateKind( $item ->getKind()), '@name' => $item ->getName(), ]), t( 'Once released, this @operand will no longer be accessible by you.' , [ '@operand' => FolderShare::translateKind( $item ->getKind()), ])); } // Find the kinds for each of the selection IDs. Then choose an // operand based on the selection's single kind, or "items". $selectionKinds = FolderShare::findKindsForIds( $selectionIds ); if ( count ( $selectionIds ) === 1) { $operand = FolderShare::translateKind(key( $selectionKinds )); } elseif ( count ( $selectionKinds ) === 1) { $operand = FolderShare::translateKinds(key( $selectionKinds )); } else { $operand = FolderShare::translateKinds( 'items' ); } // Dialog and page description. // // Use the count and kind and end in a question mark. For folders, // include a reminder that all their contents are deleted too. return FormatUtilities::createFormattedMessage( t( 'Release these shared @operand?' , [ '@operand' => $operand , ]), t( 'Once released, these @operand will no longer be accessible by you.' , [ '@operand' => $operand , ])); } /** * {@inheritdoc} */ public function getTitle(bool $forPage ) { // The title varies for page vs. dialog: // // - Dialog: The title is short and has the form "Release shared OPERAND", // where OPERAND is the kind of item (e.g. "file"). By not putting // the item's name in the title, we keep the dialog title short and // avoid cropping or wrapping. // // - Page: The title is longer and has the form "Release shared OPERAND?", // where OPERAND can be the name of the item if one item is being deleted, // or the count and kinds if multiple items are being deleted. This // follows Drupal convention. $selectionIds = $this ->getSelectionIds(); if ( $forPage === TRUE && count ( $selectionIds ) === 1) { // Page title. There is only one item. Load it. $item = FolderShare::load( $selectionIds [0]); return t( 'Release shared "@name"?' , [ '@name' => $item ->getName(), ]); } // Find the kinds for each of the selection IDs. Then choose an // operand based on the selection's single kind, or "items". $selectionKinds = FolderShare::findKindsForIds( $selectionIds ); if ( count ( $selectionIds ) === 1) { $kind = key( $selectionKinds ); $operand = FolderShare::translateKind( $kind ); } elseif ( count ( $selectionKinds ) === 1) { $kind = key( $selectionKinds ); $operand = FolderShare::translateKinds( $kind ); } else { $operand = FolderShare::translateKinds( 'items' ); } if ( $forPage === TRUE) { // Page title. Include the count and operand kind. Question mark. return t( "Release @count shared @operand?" , [ '@count' => count ( $selectionIds ), '@operand' => $operand , ]); } return t( // Dialog title. Include the operand kind. No question mark. 'Release shared @operand' , [ '@operand' => $operand , ]); } /** * {@inheritdoc} */ public function getSubmitButtonName() { return t( 'Release' ); } /** * {@inheritdoc} */ public function buildConfigurationForm( array $form , FormStateInterface $formState ) { // The command wrapper provides form basics: // - Attached libraries. // - Page title (if not an AJAX dialog). // - Description (from ::getDescription()). // - Submit buttion (labeled with ::getSubmitButtonName()). // - Cancel button (if AJAX dialog). $form [ '#attributes' ][ 'class' ][] = 'confirmation' ; $form [ '#theme' ] = 'confirm_form' ; return $form ; } /** * {@inheritdoc} */ public function validateConfigurationForm( array & $form , FormStateInterface $formState ) { // Nothing to do. } /** * {@inheritdoc} */ public function submitConfigurationForm( array & $form , FormStateInterface $formState ) { $this ->execute(); } /*-------------------------------------------------------------------- * * Execute. * *--------------------------------------------------------------------*/ /** * {@inheritdoc} */ public function execute() { $ids = $this ->getSelectionIds(); try { FolderShare::unshareMultiple( $ids , \Drupal::currentUser()->id(), '' ); } catch (\Exception $e ) { \Drupal::messenger()->addMessage( $e ->getMessage(), 'error' ); } if (Settings::getCommandNormalCompletionReportEnable() === TRUE) { \Drupal::messenger()->addMessage( \Drupal::translation()->formatPlural( count ( $ids ), "Sharing for the item has been released." , "Sharing for @count items has been released." ), 'status' ); } } } |