Merge pull request #88 from xini/fix-edit-after-upload

fix edit after upload (from @benmanu), fixes #53
This commit is contained in:
Daniel Hensby 2016-09-06 12:08:16 +01:00 committed by GitHub
commit 25aab70810

View File

@ -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;
}
}