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); |
||||||||
0 ignored issues
–
show
|
|||||||||
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) |
||||||||
0 ignored issues
–
show
The parameter
$controller is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$data is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$form is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||||
65 | { |
||||||||
66 | $this->owner->IsLocked = true; |
||||||||
67 | $this->owner->write(); |
||||||||
68 | |||||||||
69 | return 'Record locked'; |
||||||||
70 | } |
||||||||
71 | } |
||||||||
72 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.