From fb08e472c63194a34b59b983c750c39e6240ed95 Mon Sep 17 00:00:00 2001 From: Florian Thoma Date: Tue, 30 Aug 2016 12:04:08 +1000 Subject: [PATCH] fix edit after upload (from @benmanu), fixes #53 --- code/cms/DMSUploadField.php | 114 ++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/code/cms/DMSUploadField.php b/code/cms/DMSUploadField.php index 1b91ee1..6ca3f7e 100644 --- a/code/cms/DMSUploadField.php +++ b/code/cms/DMSUploadField.php @@ -214,12 +214,126 @@ class DMSUploadField extends UploadField { return DMSUploadField_ItemHandler::create($this, $itemID); } + + + /** + * FieldList $fields for the EditForm + * @example 'getCMSFields' + * + * @param File $file File context to generate fields for + * @return FieldList List of form fields + */ + public function getDMSFileEditFields($file) + { + + // Empty actions, generate default + if(empty($this->fileEditFields)) { + $fields = $file->getCMSFields(); + // Only display main tab, to avoid overly complex interface + if($fields->hasTabSet() && ($mainTab = $fields->findOrMakeTab('Root.Main'))) { + $fields = $mainTab->Fields(); + } + return $fields; + } + + // Fields instance + if ($this->fileEditFields instanceof FieldList) return $this->fileEditFields; + + // Method to call on the given file + if($file->hasMethod($this->fileEditFields)) { + return $file->{$this->fileEditFields}(); + } + + user_error("Invalid value for UploadField::fileEditFields", E_USER_ERROR); + } + + /** + * FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm + * @example 'getCMSActions' + * + * @param File $file File context to generate form actions for + * @return FieldList Field list containing FormAction + */ + public function getDMSFileEditActions($file) + { + + // Empty actions, generate default + if(empty($this->fileEditActions)) { + $actions = new FieldList($saveAction = new FormAction('doEdit', _t('UploadField.DOEDIT', 'Save'))); + $saveAction->addExtraClass('ss-ui-action-constructive icon-accept'); + return $actions; + } + + // Actions instance + if ($this->fileEditActions instanceof FieldList) return $this->fileEditActions; + + // Method to call on the given file + if($file->hasMethod($this->fileEditActions)) { + return $file->{$this->fileEditActions}(); + } + + user_error("Invalid value for UploadField::fileEditActions", E_USER_ERROR); + } + + /** + * Determines the validator to use for the edit form + * @example 'getCMSValidator' + * + * @param File $file File context to generate validator from + * @return Validator Validator object + */ + public function getDMSFileEditValidator($file) + { + // Empty validator + if(empty($this->fileEditValidator)) return null; + + // Validator instance + if($this->fileEditValidator instanceof Validator) return $this->fileEditValidator; + + // Method to call on the given file + if($file->hasMethod($this->fileEditValidator)) { + return $file->{$this->fileEditValidator}(); + } + + user_error("Invalid value for UploadField::fileEditValidator", E_USER_ERROR); + } } class DMSUploadField_ItemHandler extends UploadField_ItemHandler { + + private static $allowed_actions = array( + 'delete', + 'edit', + 'EditForm', + ); + public function getItem() { return DataObject::get_by_id('DMSDocument', $this->itemID); } + + /** + * @return Form + */ + public function EditForm() { + $file = $this->getItem(); + + // Get form components + $fields = $this->parent->getDMSFileEditFields($file); + $actions = $this->parent->getDMSFileEditActions($file); + $validator = $this->parent->getDMSFileEditValidator($file); + $form = new Form( + $this, + __FUNCTION__, + $fields, + $actions, + $validator + ); + $form->loadDataFrom($file); + $form->addExtraClass('small'); + + return $form; + } + }