Source of file UserFormFileExtension.php
Size: 2,665 Bytes - Last Modified: 2021-12-23T10:35:40+00:00
/var/www/docs.ssmods.com/process/src/code/Extension/UserFormFileExtension.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | <?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; /** * @property string $UserFormUpload * @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(); } } |