1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Forms\GridField\GridFieldDetailForm; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CKANRegistry\Model\ResourceField; |
6
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; |
7
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate; |
8
|
|
|
|
9
|
|
|
class ResourceFieldItemRequest extends GridFieldDetailForm_ItemRequest |
10
|
|
|
{ |
11
|
|
|
protected function saveFormIntoRecord($data, $form) |
12
|
|
|
{ |
13
|
|
|
// Update the record so that we can investigate the changes |
14
|
|
|
$form->saveInto($this->record); |
15
|
|
|
$changes = $this->record->getChangedFields(); |
16
|
|
|
|
17
|
|
|
// Delegate to the parent now to do the save |
18
|
|
|
/** @var ResourceField $record */ |
19
|
|
|
$record = parent::saveFormIntoRecord($data, $form); |
20
|
|
|
|
21
|
|
|
// Continue out if the sort order has not changed |
22
|
|
|
if (!isset($changes['Position'])) { |
23
|
|
|
return $record; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$change = $changes['Position']; |
27
|
|
|
$oldPosition = (int) $change['before']; |
28
|
|
|
$newPosition = (int) $change['after']; |
29
|
|
|
|
30
|
|
|
if ($oldPosition === $newPosition) { |
31
|
|
|
return $record; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Otherwise we might need to update the position of other columns belonging to the resource |
35
|
|
|
$resource = $record->Resource; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
// If we've moved up the list that means other records need to move down |
39
|
|
|
$operator = $newPosition < $oldPosition ? '+' : '-'; |
40
|
|
|
|
41
|
|
|
// Now we just need to target all objects between the new and old positions |
42
|
|
|
if ($newPosition < $oldPosition) { |
43
|
|
|
$where = [ |
44
|
|
|
'"Position" >= ?' => $newPosition, |
45
|
|
|
'"Position" < ?' => $oldPosition, |
46
|
|
|
]; |
47
|
|
|
} else { |
48
|
|
|
$where = [ |
49
|
|
|
'"Position" > ?' => $oldPosition, |
50
|
|
|
'"Position" <= ?' => $newPosition, |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$update = SQLUpdate::create( |
55
|
|
|
$record->baseTable(), |
56
|
|
|
[], |
57
|
|
|
$where + ['"ID" != ?' => $record->ID, '"ResourceID"' => $resource->ID] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$update->assignSQL("\"Position\"", "\"Position\" $operator 1"); |
61
|
|
|
|
62
|
|
|
$update->execute(); |
63
|
|
|
|
64
|
|
|
return $record; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|