1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\AssetAdmin\GraphQL; |
3
|
|
|
|
4
|
|
|
use GraphQL\Type\Definition\Type; |
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Assets\Folder; |
8
|
|
|
use SilverStripe\GraphQL\MutationCreator; |
9
|
|
|
use SilverStripe\GraphQL\OperationResolver; |
10
|
|
|
use SilverStripe\ORM\Versioning\Versioned; |
11
|
|
|
use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor; |
12
|
|
|
use SilverStripe\GraphQL\Manager; |
13
|
|
|
|
14
|
|
|
class MoveFilesMutationCreator extends MutationCreator implements OperationResolver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CaseInsensitiveFieldAccessor |
18
|
|
|
*/ |
19
|
|
|
protected $accessor; |
20
|
|
|
|
21
|
|
|
public function __construct(Manager $manager = null) |
22
|
|
|
{ |
23
|
|
|
$this->accessor = new CaseInsensitiveFieldAccessor(); |
24
|
|
|
|
25
|
|
|
parent::__construct($manager); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function attributes() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
'name' => 'moveFiles', |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function type() |
36
|
|
|
{ |
37
|
|
|
return function () { |
38
|
|
|
return $this->manager->getType('FileInterface'); |
39
|
|
|
}; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function args() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
'folderId' => [ |
46
|
|
|
'type' => Type::nonNull(Type::id()) |
47
|
|
|
], |
48
|
|
|
'fileIds' => [ |
49
|
|
|
'type' => Type::listOf(Type::id()) |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function resolve($object, array $args, $context, ResolveInfo $info) |
55
|
|
|
{ |
56
|
|
|
$folderId = (isset($args['folderId'])) ? $args['folderId'] : 0; |
57
|
|
|
|
58
|
|
|
if ($folderId) { |
59
|
|
|
/** @var Folder $folder */ |
60
|
|
|
$folder = Versioned::get_by_stage(Folder::class, Versioned::DRAFT) |
61
|
|
|
->byID($folderId); |
62
|
|
|
if (!$folder) { |
63
|
|
|
throw new \InvalidArgumentException(sprintf( |
64
|
|
|
'%s#%s not found', |
65
|
|
|
Folder::class, |
66
|
|
|
$folderId |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Check permission |
71
|
|
|
if (!$folder->canEdit($context['currentUser'])) { |
72
|
|
|
throw new \InvalidArgumentException(sprintf( |
73
|
|
|
'%s edit not allowed', |
74
|
|
|
Folder::class |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$files = Versioned::get_by_stage(File::class, Versioned::DRAFT) |
79
|
|
|
->byIDs($args['fileIds']); |
80
|
|
|
$errorFiles = []; |
81
|
|
|
|
82
|
|
|
foreach($files as $file) { |
83
|
|
|
if ($file->canEdit($context['currentUser'])) { |
84
|
|
|
$this->accessor->setValue($file, 'ParentID', $folderId); |
85
|
|
|
$file->writeToStage(Versioned::DRAFT); |
86
|
|
|
} else { |
87
|
|
|
$errorFiles[] = $file->ID; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($errorFiles) { |
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException(sprintf( |
93
|
|
|
'%s (%s) edit not allowed', |
94
|
|
|
File::class, |
95
|
|
|
implode(', ', $errorFiles) |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!isset($folder)) { |
100
|
|
|
return Folder::singleton(); |
101
|
|
|
} |
102
|
|
|
return $folder; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.