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