foldershare-8.x-1.2/src/Plugin/FolderShareCommand/Copy.php

src/Plugin/FolderShareCommand/Copy.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
 
namespace Drupal\foldershare\Plugin\FolderShareCommand;
 
use Symfony\Component\HttpFoundation\Request;
 
use Drupal\foldershare\Constants;
use Drupal\foldershare\Settings;
use Drupal\foldershare\Utilities\FormatUtilities;
use Drupal\foldershare\FolderShareInterface;
use Drupal\foldershare\Entity\FolderShare;
use Drupal\foldershare\Entity\Exception\RuntimeExceptionWithMarkup;
use Drupal\foldershare\Entity\Exception\ValidationException;
 
/**
 * Defines a command plugin to copy files and folders.
 *
 * The command copies all selected files and folders to a chosen
 * destination folder or the root list.
 *
 * Configuration parameters:
 * - 'parentId': the parent folder, if any.
 * - 'selectionIds': selected entities to duplicate.
 * - 'destinationId': the destination folder, if any.
 *
 * @ingroup foldershare
 *
 * @FolderShareCommand(
 *  id              = "foldersharecommand_copy",
 *  label           = @Translation("Copy"),
 *  menuNameDefault = @Translation("Copy..."),
 *  menuName        = @Translation("Copy..."),
 *  description     = @Translation("Copy selected files and folders to a new location."),
 *  category        = "copy & move",
 *  weight          = 10,
 *  userConstraints = {
 *    "authorpermission",
 *  },
 *  parentConstraints = {
 *    "kinds"   = {
 *      "rootlist",
 *      "folder",
 *    },
 *    "access"  = "view",
 *  },
 *  destinationConstraints = {
 *    "kinds"   = {
 *      "rootlist",
 *      "folder",
 *    },
 *    "access"  = "create",
 *  },
 *  selectionConstraints = {
 *    "types"   = {
 *      "one",
 *      "many",
 *    },
 *    "kinds"   = {
 *      "any",
 *    },
 *    "access"  = "view",
 *  },
 * )
 */
class Copy extends CopyMoveBase {
 
  /*--------------------------------------------------------------------
   *
   * Configuration.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function validateDestinationConstraints() {
    if ($this->destinationValidated === TRUE) {
      return;
    }
 
    if ($this->selectionValidated === FALSE) {
      $this->validateSelectionConstraints();
    }
 
    // Handle special cases for destination.
    $destinationId = $this->getDestinationId();
    if ($destinationId === FolderShareInterface::ALL_ROOT_LIST) {
      $routeProvider = \Drupal::service('router.route_provider');
      $titleResolver = \Drupal::service('title_resolver');
      $dummyRequest = new Request();
 
      $route = $routeProvider->getRouteByName(
        Constants::ROUTE_ROOT_ITEMS_ALL);
      $title = $titleResolver->getTitle($dummyRequest, $route);
 
      throw new ValidationException(FormatUtilities::createFormattedMessage(
        t(
          'Items cannot be copied to the administrator\'s "@title" list.',
          [
            '@title' => $title,
          ]),
        t('Please select a subfolder instead.')));
    }
 
    if ($destinationId === FolderShareInterface::PUBLIC_ROOT_LIST) {
      $routeProvider = \Drupal::service('router.route_provider');
      $titleResolver = \Drupal::service('title_resolver');
      $dummyRequest = new Request();
 
      $route = $routeProvider->getRouteByName(
        Constants::ROUTE_ROOT_ITEMS_PUBLIC);
      $title = $titleResolver->getTitle($dummyRequest, $route);
 
      throw new ValidationException(FormatUtilities::createFormattedMessage(
        t(
          'Items cannot be copied to the "@title" list.',
          [
            '@title' => $title,
          ]),
        t('Please select a subfolder instead.')));
    }
 
    parent::validateDestinationConstraints();
  }
 
  /**
   * {@inheritdoc}
   */
  public function validateParameters() {
    if ($this->parametersValidated === TRUE) {
      // Already validated.
      return;
    }
 
    //
    // Validate destination.
    // ---------------------
    // There must be a destination ID. It must be a valid ID. It must
    // not be one of the selected items. And it must not be a descendant
    // of the selected items.
    //
    // A positive destination ID is for a folder to receive the selected
    // items.
    //
    // A negative destination ID is for the user's root list.
    $destinationId = $this->getDestinationId();
 
    if ($destinationId < 0) {
      // Destination is the user's root list. Nothing further to validate.
      $this->parametersValidated = TRUE;
      return;
    }
 
    // Destination is a specific folder.
    $destination = FolderShare::load($destinationId);
 
    if ($destination === NULL) {
      // Destination ID is not valid. This should have been caught
      // well before this validation stage.
      throw new ValidationException(FormatUtilities::createFormattedMessage(
        t(
          '@method was called with an invalid entity ID "@id".',
          [
            '@method' => __METHOD__,
            '@id'     => $destinationId,
          ])));
    }
 
    // Verify that the destination is not in the selection. That would
    // be a copy to self, which is not valid.
    $selectionIds = $this->getSelectionIds();
    if (in_array($destinationId, $selectionIds) === TRUE) {
      throw new ValidationException(FormatUtilities::createFormattedMessage(
        t('Items cannot be copied into themselves.')));
    }
 
    // Verify that the destination is not a descendant of the selection.
    // That would be a recursive tree copy into itself.
    foreach ($selectionIds as $id) {
      $item = FolderShare::load($id);
      if ($item === NULL) {
        // The item does not exist.
        continue;
      }
 
      if ($destination->isDescendantOfFolderId($item->id()) === TRUE) {
        throw new ValidationException(FormatUtilities::createFormattedMessage(
          t('Items cannot be copied into their own subfolders.')));
      }
 
      unset($item);
    }
 
    unset($destination);
 
    $this->parametersValidated = TRUE;
 
    // Garbage collect.
    gc_collect_cycles();
  }
 
  /*--------------------------------------------------------------------
   *
   * Configuration form.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function getDescription(bool $forPage) {
    // The description varies for page vs. dialog:
    //
    // - Dialog: The description is longer and has the form "Copy OPERAND
    //   to a new location, including all of its contents?" 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 deleted. Don't include the item's name again here.
        if ($item->isFolder() === FALSE) {
          return [
            t(
              'Copy this @operand to a new location.',
              [
                '@operand' => FolderShare::translateKind($item->getKind()),
              ]),
          ];
        }
 
        return [
          t('Copy this folder to a new location, including all of its contents.'),
        ];
      }
 
      // Dialog description. Include the name of the item to be deleted.
      if ($item->isFolder() === FALSE) {
        return [
          t(
            'Copy "@name" to a new location.',
            [
              '@name' => $item->getName(),
            ]),
        ];
      }
 
      return [
        t(
          'Copy "@name" to a new location, including all of its contents.',
          [
            '@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($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.
    if (isset($selectionKinds[FolderShare::FOLDER_KIND]) === FALSE) {
      return [
        t(
          'Copy these @operand to a new location.',
          [
            '@operand' => $operand,
          ]),
      ];
    }
 
    return [
      t(
        'Copy these @operand to a new location, including all of their contents?',
        [
          '@operand' => $operand,
        ]),
    ];
  }
 
  /**
   * {@inheritdoc}
   */
  public function getTitle(bool $forPage) {
    // The title varies for page vs. dialog:
    //
    // - Dialog: "Copy".
    //
    // - Page: The title is longer and has the form "Copy 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.
    if ($forPage === FALSE) {
      return t('Copy');
    }
 
    $selectionIds = $this->getSelectionIds();
 
    if (count($selectionIds) === 1) {
      // Page title. There is only one item. Load it.
      $item = FolderShare::load($selectionIds[0]);
      return t(
        'Copy "@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');
    }
 
    // Include the count and operand kind.
    return t(
      "Copy @count @operand?",
      [
        '@count' => count($selectionIds),
        '@operand' => $operand,
      ]);
  }
 
  /**
   * {@inheritdoc}
   */
  public function getSubmitButtonName() {
    return t('Copy');
  }
 
  /*--------------------------------------------------------------------
   *
   * Execute.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function execute() {
    $ids = $this->getSelectionIds();
    $destination = $this->getDestination();
 
    try {
      if ($destination === NULL) {
        FolderShare::copyToRootMultiple($ids);
      }
      else {
        FolderShare::copyToFolderMultiple($ids, $destination);
      }
    }
    catch (RuntimeExceptionWithMarkup $e) {
      \Drupal::messenger()->addMessage($e->getMarkup(), 'error', TRUE);
    }
    catch (\Exception $e) {
      \Drupal::messenger()->addMessage($e->getMessage(), 'error');
    }
 
    if (Settings::getCommandNormalCompletionReportEnable() === TRUE) {
      \Drupal::messenger()->addMessage(
        \Drupal::translation()->formatPlural(
          count($ids),
          "The item has been copied.",
          "@count items have been copied."),
        'status');
    }
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc