1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\AssetAdmin\GraphQL; |
3
|
|
|
|
4
|
|
|
use SilverStripe\Assets\File; |
5
|
|
|
use SilverStripe\Assets\Folder; |
6
|
|
|
use SilverStripe\ORM\ArrayList; |
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use GraphQL\Type\Definition\UnionType; |
9
|
|
|
use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator; |
10
|
|
|
use SilverStripe\GraphQL\Pagination\Connection; |
11
|
|
|
use SilverStripe\ORM\DataList; |
12
|
|
|
use SilverStripe\ORM\Versioning\Versioned; |
13
|
|
|
|
14
|
|
|
class ReadFileQueryCreator extends PaginatedQueryCreator |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function attributes() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
'name' => 'readFiles' |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function connection() |
25
|
|
|
{ |
26
|
|
|
return Connection::create('readFiles') |
27
|
|
|
->setConnectionType(function () { |
28
|
|
|
$unionType = new UnionType([ |
29
|
|
|
'name' => 'Result', |
30
|
|
|
'types' => [ |
31
|
|
|
$this->manager->getType('File'), |
32
|
|
|
$this->manager->getType('Folder') |
33
|
|
|
], |
34
|
|
View Code Duplication |
'resolveType' => function ($object) { |
|
|
|
|
35
|
|
|
if ($object instanceof Folder) { |
36
|
|
|
return $this->manager->getType('Folder'); |
37
|
|
|
} |
38
|
|
|
if ($object instanceof File) { |
39
|
|
|
return $this->manager->getType('File'); |
40
|
|
|
} |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
]); |
44
|
|
|
return $unionType; |
45
|
|
|
}) |
46
|
|
|
->setArgs([ |
47
|
|
|
'id' => [ |
48
|
|
|
'type' => Type::id(), |
49
|
|
|
], |
50
|
|
|
'parentId' => [ |
51
|
|
|
'type' => Type::id(), |
52
|
|
|
], |
53
|
|
|
]) |
54
|
|
|
->setSortableFields(['ID', 'Title', 'Created', 'LastEdited']) |
55
|
|
|
->setConnectionResolver(array($this, 'resolveConnection')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function resolveConnection($object, array $args, $context, $info) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
// Permission checks |
61
|
|
|
$parent = Folder::singleton(); |
62
|
|
|
if (isset($args['parentId'])) { |
63
|
|
|
$parent = Folder::get()->byID($args['parentId']); |
64
|
|
|
} |
65
|
|
|
if (!$parent->canView($context['currentUser'])) { |
66
|
|
|
throw new \InvalidArgumentException(sprintf( |
67
|
|
|
'%s#%s view access not permitted', |
68
|
|
|
Folder::class, |
69
|
|
|
$parent->ID |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$list = Versioned::get_by_stage(File::class, Versioned::DRAFT); |
74
|
|
|
|
75
|
|
|
if (isset($args['parentId'])) { |
76
|
|
|
$list = $list->filter('ParentID', $args['parentId']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (isset($args['id']) && (int)$args['id'] > 0) { |
80
|
|
|
$list = $list->filter('ID', $args['id']); |
81
|
|
|
} elseif (isset($args['id']) && (int)$args['id'] === 0) { |
82
|
|
|
// Special case for root folder |
83
|
|
|
$list = new ArrayList([new Folder([ |
84
|
|
|
'ID' => 0, |
85
|
|
|
])]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($list instanceof DataList) { |
89
|
|
|
$list = $list->filterByCallback(function ($file) use ($context) { |
90
|
|
|
return $file->canView($context['currentUser']); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// TODO Sorting |
95
|
|
|
|
96
|
|
|
return $list; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.