From 1942d18599c1dd1832b9bf7b4560eb0cfd6a33d3 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 21 May 2020 14:00:53 +1200 Subject: [PATCH] NEW DB column showing if file is userform upload --- _config/extensions.yml | 3 + code/Control/UserDefinedFormController.php | 2 + code/Extension/UserFormFileExtension.php | 72 +++++++++++++++++++ composer.json | 1 + tests/Extension/UserFormFileExtensionTest.php | 47 ++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 code/Extension/UserFormFileExtension.php create mode 100644 tests/Extension/UserFormFileExtensionTest.php diff --git a/_config/extensions.yml b/_config/extensions.yml index 3e243dccb..5ec2910c4 100644 --- a/_config/extensions.yml +++ b/_config/extensions.yml @@ -4,3 +4,6 @@ Name: userformsextensions SilverStripe\UserForms\Model\UserDefinedForm: extensions: UpgradePolymorphicExtension: SilverStripe\UserForms\Extension\UpgradePolymorphicExtension +SilverStripe\Assets\File: + extensions: + - SilverStripe\UserForms\Extension\UserFormFileExtension diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index 4f2e8049f..3ed0b753f 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -20,6 +20,7 @@ use SilverStripe\ORM\ValidationException; use SilverStripe\ORM\ValidationResult; use SilverStripe\Security\Security; +use SilverStripe\UserForms\Extension\UserFormFileExtension; use SilverStripe\UserForms\Form\UserForm; use SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\UserForms\Model\EditableFormField\EditableFileField; @@ -257,6 +258,7 @@ public function process($data, $form) $upload = Upload::create(); $file = File::create(); $file->ShowInSearch = 0; + $file->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE; try { $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername); } catch (ValidationException $e) { diff --git a/code/Extension/UserFormFileExtension.php b/code/Extension/UserFormFileExtension.php new file mode 100644 index 000000000..0e4f50f1e --- /dev/null +++ b/code/Extension/UserFormFileExtension.php @@ -0,0 +1,72 @@ + "Enum('f, t', null)", + ]; + + /** + * Check if the file is associated with a userform submission + * Save the result in the database as a tri-state for two reasons: + * a) performance - prevent the need for an extra DB query + * b) if in the future the userform submission is deleted and the uploaded file is not (file is orphaned), + * then it is still recorded that the file was originally uploaded from a userform submission + * + * @param bool $value + * @see File::isTrackedFormUpload(), UserDefinedFormController::process() + */ + public function updateIsTrackedFormUpload(&$value): void + { + /** @var File|Versioned $file */ + $file = $this->owner; + if ($file->UserFormUpload != self::USER_FORM_UPLOAD_UNKNOWN) { + $value = $file->UserFormUpload == self::USER_FORM_UPLOAD_TRUE; + return; + } + if ($file->ClassName == Folder::class) { + $value = false; + } else { + $value = SubmittedFileField::get()->find('UploadedFileID', $file->ID) ? true : false; + } + $this->updateDB($value); + } + + /** + * Update File.UserFormUpload draft table without altering File.LastEdited + * + * @param bool $value + */ + private function updateDB(bool $value): void + { + if (!$this->owner->isInDB()) { + return; + } + $table = DataObject::getSchema()->tableName(File::class); + $column = 'UserFormUpload'; + $enumVal = $value ? self::USER_FORM_UPLOAD_TRUE : self::USER_FORM_UPLOAD_FALSE; + SQLUpdate::create() + ->setTable($table) + ->addWhere(['"ID" = ?' => [$this->owner->ID]]) + ->addAssignments([sprintf('"%s"."%s"', $table, $column) => $enumVal]) + ->execute(); + } +} diff --git a/composer.json b/composer.json index b4344a974..5cee7392b 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ } ], "require": { + "php": ">=7.1", "silverstripe/cms": "^4.0", "symbiote/silverstripe-gridfieldextensions": "^3.1", "silverstripe/segment-field": "^2.0", diff --git a/tests/Extension/UserFormFileExtensionTest.php b/tests/Extension/UserFormFileExtensionTest.php new file mode 100644 index 000000000..9ef2e6f4f --- /dev/null +++ b/tests/Extension/UserFormFileExtensionTest.php @@ -0,0 +1,47 @@ +write(); + $this->assertTrue(is_null($file->UserFormUpload)); + + $value = true; + $file->invokeWithExtensions('updateIsUserFormUpload', $value); + $this->assertFalse($value); + + // refresh DataObject to get latest DB changes + $file = File::get()->byID($file->ID); + + $this->assertEquals(UserFormFileExtension::USER_FORM_UPLOAD_FALSE, $file->UserFormUpload); + } + + public function testUpdateIsUserFormUploadTrue() + { + $file = File::create(); + $file->write(); + $this->assertTrue(is_null($file->UserFormUpload)); + + $submittedFileField = SubmittedFileField::create(); + $submittedFileField->UploadedFileID = $file->ID; + $submittedFileField->write(); + + $value = false; + $file->invokeWithExtensions('updateIsUserFormUpload', $value); + $this->assertTrue($value); + + // refresh DataObject to get latest DB changes + $file = File::get()->byID($file->ID); + + $this->assertEquals(UserFormFileExtension::USER_FORM_UPLOAD_TRUE, $file->UserFormUpload); + } +}