1 | <?php |
||
2 | |||
3 | namespace SilverStripe\UserForms\Extension; |
||
4 | |||
5 | use SilverStripe\Assets\File; |
||
6 | use SilverStripe\Assets\Folder; |
||
7 | use SilverStripe\Core\Convert; |
||
8 | use SilverStripe\ORM\DataExtension; |
||
9 | use SilverStripe\ORM\DataObject; |
||
10 | use SilverStripe\ORM\Queries\SQLUpdate; |
||
11 | use SilverStripe\UserForms\Control\UserDefinedFormController; |
||
12 | use SilverStripe\UserForms\Model\Submission\SubmittedFileField; |
||
13 | use SilverStripe\Versioned\Versioned; |
||
14 | |||
15 | /** |
||
16 | * @method File SubmittedFileField |
||
17 | */ |
||
18 | class UserFormFileExtension extends DataExtension |
||
19 | { |
||
20 | |||
21 | public const USER_FORM_UPLOAD_UNKNOWN = null; |
||
22 | |||
23 | public const USER_FORM_UPLOAD_FALSE = 'f'; |
||
24 | |||
25 | public const USER_FORM_UPLOAD_TRUE = 't'; |
||
26 | |||
27 | private static $db = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
28 | 'UserFormUpload' => "Enum('f, t', null)", |
||
29 | ]; |
||
30 | |||
31 | private static $belongs_to = [ |
||
0 ignored issues
–
show
|
|||
32 | 'SubmittedFileField' => SubmittedFileField::class |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Check if the file is associated with a userform submission |
||
37 | * Save the result in the database as a tri-state for two reasons: |
||
38 | * a) performance - prevent the need for an extra DB query |
||
39 | * b) if in the future the UserForm submission is deleted and the uploaded file is not (file is orphaned), |
||
40 | * then it is still recorded that the file was originally uploaded from a userform submission |
||
41 | * |
||
42 | * @param bool $value |
||
43 | * @see File::isTrackedFormUpload(), UserDefinedFormController::process() |
||
44 | */ |
||
45 | public function updateTrackedFormUpload(&$value): void |
||
46 | { |
||
47 | /** @var File|Versioned|UserFormFileExtension $file */ |
||
48 | $file = $this->owner; |
||
49 | if ($file->UserFormUpload != self::USER_FORM_UPLOAD_UNKNOWN) { |
||
50 | $value = $file->UserFormUpload == self::USER_FORM_UPLOAD_TRUE; |
||
51 | return; |
||
52 | } |
||
53 | if ($file->ClassName == Folder::class) { |
||
54 | $value = false; |
||
55 | } else { |
||
56 | $value = $file->SubmittedFileField()->exists(); |
||
57 | } |
||
58 | $this->updateDB($value); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Update File.UserFormUpload draft table without altering File.LastEdited |
||
63 | * |
||
64 | * @param bool $value |
||
65 | */ |
||
66 | private function updateDB(bool $value): void |
||
67 | { |
||
68 | if (!$this->owner->isInDB()) { |
||
69 | return; |
||
70 | } |
||
71 | $tableName = Convert::raw2sql(DataObject::getSchema()->tableName(File::class)); |
||
72 | $column = 'UserFormUpload'; |
||
73 | $enumVal = $value ? self::USER_FORM_UPLOAD_TRUE : self::USER_FORM_UPLOAD_FALSE; |
||
74 | SQLUpdate::create() |
||
75 | ->setTable(sprintf('"%s"', $tableName)) |
||
76 | ->addWhere(['"ID" = ?' => [$this->owner->ID]]) |
||
77 | ->addAssignments([sprintf('"%s"', $column) => $enumVal]) |
||
78 | ->execute(); |
||
79 | } |
||
80 | } |
||
81 |