Source of file DataObjectOneFieldOneRecordUpdateController.php
Size: 4,000 Bytes - Last Modified: 2021-12-23T10:39:22+00:00
/var/www/docs.ssmods.com/process/src/src/DataObjectOneFieldOneRecordUpdateController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | <?php namespace Sunnysideup\DataObjectSorter; use SilverStripe\Control\HTTPResponse; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HiddenField; use Sunnysideup\DataObjectSorter\Api\DataObjectSorterRequirements; /** *@author nicolaas [at] sunnysideup.co.nz *@package: dataobjectsorter *@description: allows you to edit one field in one record */ class DataObjectOneFieldOneRecordUpdateController extends DataObjectSortBaseClass { private static $allowed_actions = [ 'onefieldform' => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', 'show' => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', 'save' => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', ]; /** * make sure to also change in routes if you change this link. * * @var string */ private static $url_segment = 'dataobjectonefieldonerecordupdate'; /** * get a link. */ public static function popup_link_only(string $className, string $fieldName, int $recordID): string { return self::link_only_maker( DataObjectOneFieldOneRecordUpdateController::class, 'show/' . self::classNameToString($className) . '/' . $fieldName, ['id' => $recordID] ); } /** * get a link. */ public static function popup_link( string $className, string $fieldName, int $recordID, ?string $linkText = 'click here to edit' ): string { $link = self::popup_link_only($className, $fieldName, $recordID); return self::link_html_maker( $link, 'modalPopUp modal-popup', 'oneFieldOneRecord' . self::classNameToString($className) . $fieldName . $recordID, $linkText ); } /** * create a nice button. */ public static function button_link( string $className, string $fieldName, int $recordID, ?string $linkText = 'click here to edit' ): string { $link = self::popup_link_only($className, $fieldName, $recordID); return self::button_maker( $link, 'modalPopUp modal-popup', 'oneFieldOneRecord' . self::classNameToString($className) . $fieldName . $recordID, $linkText ); } public function onefieldform() { $obj = $this->getRecordAndCheckPermissions(); if ($obj instanceof HTTPResponse) { return $obj; } $field = $this->SecureFieldToBeUpdated(); $FormField = $this->getFormField($obj, $field); $FormField->setValue($obj->{$field}); return new Form( $controller = $this, $name = 'OneFieldForm', $fields = new FieldList( $FormField, new HiddenField('Table', 'Table', self::classNameToString($this->SecureClassNameToBeUpdatedAsString())), new HiddenField('Field', 'Field', $field), new HiddenField('Record', 'Record', ($this->SecureRecordIdToBeUpdated())) ), $actions = new FieldList(new FormAction('save', 'save and close')) ); } public function save($data, $form) { $obj = $this->getRecordAndCheckPermissions(); if ($obj instanceof HTTPResponse) { return $obj; } $field = $this->SecureFieldToBeUpdated(); $obj->{$field} = $data[$field]; $obj->write(); return 'DONE'; } protected function init() { //must set this first ... DataObjectSorterRequirements::theme_fix(); // Only administrators can run this method parent::init(); DataObjectSorterRequirements::popup_requirements('onefieldonerecord'); DataObjectSorterRequirements::url_variable( DataObjectOneFieldOneRecordUpdateController::class, 'DataObjectOneFieldOneRecordUpdateURL' ); } } |