| 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) { |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 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(); |
||||||
|
0 ignored issues
–
show
The method
write() does not exist on LeKoala\CommonExtensions\LockableExtension.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 68 | |||||||
| 69 | return 'Record locked'; |
||||||
| 70 | } |
||||||
| 71 | } |
||||||
| 72 |