mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
NEW DB column showing if file is userform upload
- UserFormUpload used by File::isTrackedFormUpload()
This commit is contained in:
parent
e54a825ea3
commit
ed53709631
@ -4,3 +4,6 @@ Name: userformsextensions
|
|||||||
SilverStripe\UserForms\Model\UserDefinedForm:
|
SilverStripe\UserForms\Model\UserDefinedForm:
|
||||||
extensions:
|
extensions:
|
||||||
UpgradePolymorphicExtension: SilverStripe\UserForms\Extension\UpgradePolymorphicExtension
|
UpgradePolymorphicExtension: SilverStripe\UserForms\Extension\UpgradePolymorphicExtension
|
||||||
|
SilverStripe\Assets\File:
|
||||||
|
extensions:
|
||||||
|
- SilverStripe\UserForms\Extension\UserFormFileExtension
|
||||||
|
@ -21,6 +21,7 @@ use SilverStripe\ORM\FieldType\DBField;
|
|||||||
use SilverStripe\ORM\ValidationException;
|
use SilverStripe\ORM\ValidationException;
|
||||||
use SilverStripe\ORM\ValidationResult;
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\Security\Security;
|
use SilverStripe\Security\Security;
|
||||||
|
use SilverStripe\UserForms\Extension\UserFormFileExtension;
|
||||||
use SilverStripe\UserForms\Form\UserForm;
|
use SilverStripe\UserForms\Form\UserForm;
|
||||||
use SilverStripe\UserForms\Model\EditableFormField;
|
use SilverStripe\UserForms\Model\EditableFormField;
|
||||||
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
|
||||||
@ -269,6 +270,7 @@ JS
|
|||||||
/** @var AssetContainer|File $file */
|
/** @var AssetContainer|File $file */
|
||||||
$file = $upload->getFile();
|
$file = $upload->getFile();
|
||||||
$file->ShowInSearch = 0;
|
$file->ShowInSearch = 0;
|
||||||
|
$file->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE;
|
||||||
$file->write();
|
$file->write();
|
||||||
|
|
||||||
// generate image thumbnail to show in asset-admin
|
// generate image thumbnail to show in asset-admin
|
||||||
|
80
code/Extension/UserFormFileExtension.php
Normal file
80
code/Extension/UserFormFileExtension.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\UserForms\Extension;
|
||||||
|
|
||||||
|
use SilverStripe\Assets\File;
|
||||||
|
use SilverStripe\Assets\Folder;
|
||||||
|
use SilverStripe\Core\Convert;
|
||||||
|
use SilverStripe\ORM\DataExtension;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\ORM\Queries\SQLUpdate;
|
||||||
|
use SilverStripe\UserForms\Control\UserDefinedFormController;
|
||||||
|
use SilverStripe\UserForms\Model\Submission\SubmittedFileField;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @method File SubmittedFileField
|
||||||
|
*/
|
||||||
|
class UserFormFileExtension extends DataExtension
|
||||||
|
{
|
||||||
|
|
||||||
|
public const USER_FORM_UPLOAD_UNKNOWN = null;
|
||||||
|
|
||||||
|
public const USER_FORM_UPLOAD_FALSE = 'f';
|
||||||
|
|
||||||
|
public const USER_FORM_UPLOAD_TRUE = 't';
|
||||||
|
|
||||||
|
private static $db = [
|
||||||
|
'UserFormUpload' => "Enum('f, t', null)",
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $belongs_to = [
|
||||||
|
'SubmittedFileField' => SubmittedFileField::class
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 updateTrackedFormUpload(&$value): void
|
||||||
|
{
|
||||||
|
/** @var File|Versioned|UserFormFileExtension $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 = $file->SubmittedFileField()->exists();
|
||||||
|
}
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
$tableName = Convert::raw2sql(DataObject::getSchema()->tableName(File::class));
|
||||||
|
$column = 'UserFormUpload';
|
||||||
|
$enumVal = $value ? self::USER_FORM_UPLOAD_TRUE : self::USER_FORM_UPLOAD_FALSE;
|
||||||
|
SQLUpdate::create()
|
||||||
|
->setTable(sprintf('"%s"', $tableName))
|
||||||
|
->addWhere(['"ID" = ?' => [$this->owner->ID]])
|
||||||
|
->addAssignments([sprintf('"%s"', $column) => $enumVal])
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
|
"php": ">=7.1",
|
||||||
"silverstripe/cms": "^4.0",
|
"silverstripe/cms": "^4.0",
|
||||||
"symbiote/silverstripe-gridfieldextensions": "^3.1",
|
"symbiote/silverstripe-gridfieldextensions": "^3.1",
|
||||||
"silverstripe/segment-field": "^2.0",
|
"silverstripe/segment-field": "^2.0",
|
||||||
|
49
tests/Extension/UserFormFileExtensionTest.php
Normal file
49
tests/Extension/UserFormFileExtensionTest.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\UserForms\Tests\Extension;
|
||||||
|
|
||||||
|
use SilverStripe\Assets\File;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\UserForms\Model\Submission\SubmittedFileField;
|
||||||
|
use SilverStripe\UserForms\Extension\UserFormFileExtension;
|
||||||
|
|
||||||
|
class UserFormFileExtensionTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected $usesDatabase = true;
|
||||||
|
|
||||||
|
public function testUpdateIsUserFormUploadFalse()
|
||||||
|
{
|
||||||
|
$file = File::create();
|
||||||
|
$file->write();
|
||||||
|
$this->assertNull($file->UserFormUpload);
|
||||||
|
|
||||||
|
$value = true;
|
||||||
|
$file->invokeWithExtensions('updateTrackedFormUpload', $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->assertNull($file->UserFormUpload);
|
||||||
|
|
||||||
|
$submittedFileField = SubmittedFileField::create();
|
||||||
|
$submittedFileField->UploadedFileID = $file->ID;
|
||||||
|
$submittedFileField->write();
|
||||||
|
|
||||||
|
$value = false;
|
||||||
|
$file->invokeWithExtensions('updateTrackedFormUpload', $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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user