Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overwrite existing uploads #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/controllers/PartialSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,29 +165,36 @@ protected function savePartialFile($formData, array $filter, EditableFormField\E
$partialFileSubmission = PartialFileFieldSubmission::create($partialData);
$partialFileSubmission->write();
}
// Don't overwrite existing uploads
if (!$partialFileSubmission->UploadedFileID && is_array($formData['Value'])) {
$file = $this->uploadFile($formData, $editableField);

if (is_array($formData['Value'])) {
$file = $this->uploadFile($formData, $editableField, $partialFileSubmission);
$partialFileSubmission->UploadedFileID = $file->ID ?? 0;
$partialFileSubmission->write();
}
$partialFileSubmission->write();
}

/**
* @param array $formData
* @param EditableFormField\EditableFileField $field
* @param PartialFileFieldSubmission $partialFileSubmission
* @return bool|File
* @throws \Exception
* @throws Exception
*/
protected function uploadFile($formData, $field)
protected function uploadFile($formData, $field, $partialFileSubmission)
{
if (!empty($formData['Value']['name'])) {
$foldername = $field->getFormField()->getFolderName();

// create the file from post data
if (!$partialFileSubmission->UploadedFileID) {
$file = File::create();
$file->ShowInSearch = 0;
} else {
// Allow overwrite existing uploads
$file = $partialFileSubmission->UploadedFile();
}

// Upload the file from post data
$upload = Upload::create();
$file = File::create();
$file->ShowInSearch = 0;
if ($upload->loadIntoFile($formData['Value'], $file, $foldername)) {
return $file;
}
Expand Down
53 changes: 41 additions & 12 deletions src/controllers/PartialUserFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
Expand Down Expand Up @@ -67,39 +68,67 @@ public function partial(HTTPRequest $request)
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID);
}

// TODO: Recognize visitor with the password
// Set data record and load the form
$this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID);
$this->setFailover($this->dataRecord);
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID);
$controller = parent::create($record);
$controller->doInit();

$form = $this->Form();
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray();
$form->loadDataFrom($fields);
$form = $controller->Form();
$form->loadDataFrom($partial->getFields());
$this->populateFileData($form, $partial);

// Copied from {@link UserDefinedFormController}
if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) {
$hasLocation = stristr($this->Content, '$UserDefinedForm');
if ($controller->Content && $form && !$controller->config()->disable_form_content_shortcode) {
$hasLocation = stristr($controller->Content, '$UserDefinedForm');
if ($hasLocation) {
/** @see Requirements_Backend::escapeReplacement */
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$');
$content = preg_replace(
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i',
$formEscapedForRegex,
$this->Content
$controller->Content
);

return $this->customise([
return $controller->customise([
'Content' => DBField::create_field('HTMLText', $content),
'Form' => '',
'PartialLink' => $partial->getPartialLink()
])->renderWith([static::class, Page::class]);
}
}

return $this->customise([
'Content' => DBField::create_field('HTMLText', $this->Content),
return $controller->customise([
'Content' => DBField::create_field('HTMLText', $controller->Content),
'Form' => $form,
'PartialLink' => $partial->getPartialLink()
])->renderWith([static::class, Page::class]);
}

/**
* Set the uploaded filenames as right title of the file fields
*
* @param Form $form
* @param PartialFormSubmission $partialSubmission
*/
protected function populateFileData($form, $partialSubmission)
{
$uploads = $partialSubmission->PartialUploads()->filter([
'UploadedFileID:not'=> null
]);

if (!$uploads->exists()) {
return;
}

$fields = $form->Fields();
foreach ($uploads as $upload) {
$fields->dataFieldByName($upload->Name)
->setRightTitle(
sprintf(
'Uploaded: %s (Attach a new file to replace the uploaded file)',
$upload->UploadedFile()->Name
)
);
}
}
}
8 changes: 8 additions & 0 deletions src/models/PartialFileFieldSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public function canDelete($member = null)

return parent::canDelete($member);
}

/**
* @return string
*/
public function getFilename()
{
return $this->UploadedFileID ? $this->UploadedFile()->Name : '';
lhalaa marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 13 additions & 0 deletions src/models/PartialFormSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,17 @@ public function generateKey($token)
{
return hash_pbkdf2('sha256', $token, $this->TokenSalt, 1000, 16);
}

/**
* Get all partial fields for loading data into the form
*
* @return array
*/
public function getFields()
{
$formFields = $this->PartialFields()->map('Name', 'Value')->toArray();
$fileFields = $this->PartialUploads()->map('Name', 'Filename')->toArray();

return $formFields + $fileFields;
lhalaa marked this conversation as resolved.
Show resolved Hide resolved
}
}