1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CommonExtensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
use LeKoala\CmsActions\CustomAction; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Makes record locked |
12
|
|
|
* |
13
|
|
|
* Unlocking require special action |
14
|
|
|
* |
15
|
|
|
* @property \LeKoala\CommonExtensions\LockableExtension|DataObject $owner |
16
|
|
|
* @property boolean $IsLocked |
17
|
|
|
*/ |
18
|
|
|
class LockableExtension extends DataExtension |
19
|
|
|
{ |
20
|
|
|
private static $db = [ |
|
|
|
|
21
|
|
|
"IsLocked" => "Boolean" |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function updateCMSFields(FieldList $fields) |
25
|
|
|
{ |
26
|
|
|
$fields->removeByName('IsLocked'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Use this in your model. It has to run last and cannot be done automatically |
31
|
|
|
* |
32
|
|
|
* if($this->hasExtension(LockableExtension::class)) { |
33
|
|
|
* $this->lockFields($fields); |
34
|
|
|
* } |
35
|
|
|
* |
36
|
|
|
* @param FieldList $fields |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function lockFields(FieldList $fields) |
40
|
|
|
{ |
41
|
|
|
if (!$this->owner->IsLocked) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
$fields->makeReadonly(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function canEdit($member) |
48
|
|
|
{ |
49
|
|
|
if ($this->owner->IsLocked) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
return parent::canEdit($member); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function updateCMSActions(FieldList $actions) |
56
|
|
|
{ |
57
|
|
|
if ($this->owner->ID && !$this->owner->IsLocked) { |
|
|
|
|
58
|
|
|
$lockRecord = new CustomAction("LockRecord", "Lock"); |
59
|
|
|
$lockRecord->setConfirmation("Are you sure to lock this record?"); |
60
|
|
|
$actions->push($lockRecord); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function LockRecord($data, $form, $controller) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->owner->IsLocked = true; |
67
|
|
|
$this->owner->write(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return 'Record locked'; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|