|
1
|
|
|
<?php |
|
2
|
|
|
namespace DNADesign\Elemental\GraphQL; |
|
3
|
|
|
|
|
4
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
|
|
|
|
|
6
|
|
|
use GraphQL\Type\Definition\Type; |
|
|
|
|
|
|
7
|
|
|
use SilverStripe\GraphQL\MutationCreator; |
|
|
|
|
|
|
8
|
|
|
use SilverStripe\GraphQL\OperationResolver; |
|
|
|
|
|
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\ORM\DB; |
|
11
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate; |
|
12
|
|
|
|
|
13
|
|
|
class SortBlockMutationCreator extends MutationCreator implements OperationResolver |
|
14
|
|
|
{ |
|
15
|
|
|
public function attributes() |
|
16
|
|
|
{ |
|
17
|
|
|
return [ |
|
18
|
|
|
'name' => 'sortBlock', |
|
19
|
|
|
'description' => 'Changes the sort position of an element' |
|
20
|
|
|
]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function type() |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->manager->getType('Block'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function args() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
'ID' => ['type' => Type::nonNull(Type::id())], |
|
32
|
|
|
'AfterBlockID' => ['type' => Type::nonNull(Type::id())], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function resolve($object, array $args, $context, ResolveInfo $info) |
|
37
|
|
|
{ |
|
38
|
|
|
$block = BaseElement::get_by_id($args['ID']); |
|
39
|
|
|
|
|
40
|
|
|
if (!$block) { |
|
|
|
|
|
|
41
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
42
|
|
|
'%s#%s not found', |
|
43
|
|
|
BaseElement::class, |
|
44
|
|
|
$args['ID'] |
|
45
|
|
|
)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$block->canEdit($context['currentUser'])) { |
|
49
|
|
|
throw new \InvalidArgumentException( |
|
50
|
|
|
'Changing the sort order of blocks is not allowed' |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$parentId = $block->ParentID; |
|
55
|
|
|
$blockPosition = $block->Sort; |
|
56
|
|
|
|
|
57
|
|
|
$sortAfterPosition = 0; |
|
58
|
|
|
$afterBlockID = $args['AfterBlockID']; |
|
59
|
|
|
if ($afterBlockID) { |
|
60
|
|
|
$afterBlock = BaseElement::get_by_id($afterBlockID); |
|
61
|
|
|
|
|
62
|
|
|
if (!$afterBlock) { |
|
|
|
|
|
|
63
|
|
|
throw new \InvalidArgumentException( |
|
64
|
|
|
'Trying to sort block after a block in a different elemental area' |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
if ($afterBlock->ParentID !== $parentId) { |
|
68
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
69
|
|
|
'%s#%s not found', |
|
70
|
|
|
BaseElement::class, |
|
71
|
|
|
$parentId |
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$sortAfterPosition = $afterBlock->Sort; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($sortAfterPosition < $blockPosition) { |
|
79
|
|
|
$operator = '+'; |
|
80
|
|
|
$filter = "Sort > $sortAfterPosition && Sort < $blockPosition"; |
|
81
|
|
|
$newBlockPosition = $sortAfterPosition + 1; |
|
82
|
|
|
} else { |
|
83
|
|
|
$operator = '-'; |
|
84
|
|
|
$filter = "Sort <= $sortAfterPosition && Sort > $blockPosition"; |
|
85
|
|
|
$newBlockPosition = $sortAfterPosition; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$table = DataObject::getSchema()->tableName(BaseElement::class); |
|
89
|
|
|
|
|
90
|
|
|
$query = SQLUpdate::create() |
|
91
|
|
|
->setTable($table) |
|
92
|
|
|
->assignSQL('"' . $table . '"."Sort"', "\"$table\".\"Sort\" $operator 1") |
|
93
|
|
|
->addWhere([$filter, '"ParentId"' => $parentId]); |
|
94
|
|
|
|
|
95
|
|
|
$query->execute(); |
|
96
|
|
|
|
|
97
|
|
|
$block->Sort = $newBlockPosition; |
|
98
|
|
|
$block->write(); |
|
99
|
|
|
|
|
100
|
|
|
return $block; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths