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

src/Plugin/FolderShareCommand/MoveBase.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?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 move files and folders.
 *
 * The command moves 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
 */
class MoveBase 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 moved 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 moved 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 moved 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 moved 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: "Move this OPERAND to a new location".
    //
    // - 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();
 
    //
    // Handle single item.
    // -------------------
    // Load the item and determine if it is shared.
    $nItems = count($selectionIds);
    if ($nItems === 1) {
      $item = FolderShare::load(reset($selectionIds));
      $isShared = $item->getRootItem()->isAccessShared();
      $isRoot = $item->isRootItem();
      $kind = $item->getKind();
      $name = $item->getName();
 
      unset($item);
 
      if ($forPage === TRUE) {
        if ($isShared === TRUE) {
          return [
            t(
              'Move this shared @operand to a new location.',
              [
                '@operand' => FolderShare::translateKind($kind),
              ]),
            ($isRoot === TRUE ?
              t('This item is shared. Moving it will end shared access and may affect other users.') :
              t('This item is shared. Moving it may affect other users.')),
          ];
        }
 
        return [
          t(
            'Move this @operand to a new location.',
            [
              '@operand' => FolderShare::translateKind($kind),
            ]),
        ];
      }
 
      if ($isShared === TRUE) {
        return [
          t(
            'Move shared "@name" to a new location.',
            [
              '@name' => $name,
            ]),
          ($isRoot === TRUE ?
            t('This item is shared. Moving it will end shared access and may affect other users.') :
            t('This item is shared. Moving it may affect other users.')),
        ];
      }
 
      return [
        t(
          'Move "@name" to a new location.',
          [
            '@name' => $name,
          ]),
      ];
    }
 
    // 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');
    }
 
    // If there is no parent, then the selection are all root items.
    // Load them all and check if any of them are shared.
    //
    // If there is a parent, then just check that parent to get the root
    // and see if it is shared. Don't load the selection.
    $someShared = FALSE;
    $allShared = FALSE;
    $isRoot = FALSE;
    if ($this->getParent() === NULL) {
      $isRoot = TRUE;
      $nShared = 0;
 
      foreach ($selectionIds as $id) {
        $item = FolderShare::load($id);
        if ($item === NULL) {
          // The item does not exist.
          continue;
        }
 
        if ($item->isAccessShared() === TRUE) {
          $nShared++;
        }
 
        unset($item);
      }
 
      if ($nShared !== 0) {
        if (count($selectionIds) === $nShared) {
          $allShared = TRUE;
        }
        else {
          $someShared = TRUE;
        }
      }
    }
    else {
      $parent    = $this->getParent();
      $root      = $parent->getRootItem();
      $allShared = $root->isAccessShared();
      unset($root);
    }
 
    if ($allShared === TRUE) {
      return [
        t(
          'Move @count shared @operand to a new location.',
          [
            '@count'   => $nItems,
            '@operand' => $operand,
          ]),
        ($isRoot === TRUE ?
          t('These items shared. Moving them will end shared access and may affect other users.') :
          t('These items are shared. Moving them may affect other users.')),
      ];
    }
 
    if ($someShared === TRUE) {
      return [
        t(
          'Move @count @operand to a new location.',
          [
            '@count'   => $nItems,
            '@operand' => $operand,
          ]),
        ($isRoot === TRUE ?
          t('Some of these items shared. Moving them will end shared access and may affect other users.') :
          t('Some of these items are shared. Moving them may affect other users.')),
      ];
    }
 
    return [
      t(
        'Move @count @operand to a new location.',
        [
          '@count'   => $nItems,
          '@operand' => $operand,
        ]),
    ];
  }
 
  /**
   * {@inheritdoc}
   */
  public function getTitle(bool $forPage) {
    // The title varies for page vs. dialog:
    //
    // - Dialog: "Move".
    //
    // - Page: The title is longer and has the form "Move 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('Move');
    }
 
    $selectionIds = $this->getSelectionIds();
 
    if (count($selectionIds) === 1) {
      // Page title. There is only one item. Load it.
      $item = FolderShare::load($selectionIds[0]);
      return t(
        'Move "@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(
      "Move @count @operand?",
      [
        '@count' => count($selectionIds),
        '@operand' => $operand,
      ]);
  }
 
  /**
   * {@inheritdoc}
   */
  public function getSubmitButtonName() {
    return t('Move');
  }
 
  /*--------------------------------------------------------------------
   *
   * Execute.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function execute() {
    $ids = $this->getSelectionIds();
    $destination = $this->getDestination();
 
    try {
      if ($destination === NULL) {
        FolderShare::moveToRootMultiple($ids);
      }
      else {
        FolderShare::moveToFolderMultiple($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 moved.",
          "@count items have been moved."),
        'status');
    }
  }
 
}

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

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