Skip to content

Commit

Permalink
fix(SFT-686): fixing user photo upload
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavs-gutmanis committed Oct 17, 2023
1 parent d0dc9a6 commit f8141a8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
Event::on(SaveForm::class, SaveForm::EVENT_SAVE_FORM, [$this, 'prolongUnfinalizedAssets']);
}

public function prolongUnfinalizedAssets(SaveFormEvent $event)
public function prolongUnfinalizedAssets(SaveFormEvent $event): void
{
$saveTimeDays = (int) Freeform::getInstance()->settings->getSettingsModel()->saveFormTtl;
$newDate = new Carbon('now +'.$saveTimeDays.' days', 'UTC');
Expand All @@ -40,7 +40,7 @@ public function prolongUnfinalizedAssets(SaveFormEvent $event)
}
}

public function finalizeFiles(SubmitEvent $event)
public function finalizeFiles(SubmitEvent $event): void
{
$form = $event->getForm();

Expand All @@ -55,7 +55,7 @@ public function finalizeFiles(SubmitEvent $event)
}
}

public function handleDnDPost(TransformValueEvent $event)
public function handleDnDPost(TransformValueEvent $event): void
{
$field = $event->getField();
if (!$field instanceof FileDragAndDropField) {
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin/src/Fields/Interfaces/FileUploadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@

namespace Solspace\Freeform\Fields\Interfaces;

use craft\elements\db\AssetQuery;

interface FileUploadInterface
{
public const FLAG_GLOBAL_PROPERTY = 'global-property';

public function getAssets(): AssetQuery;

public function getAssetSourceId(): ?int;

public function getDefaultUploadLocation(): ?string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Solspace\Freeform\Integrations\Elements\User\EventListeners;

use Solspace\Freeform\Events\Integrations\ElementIntegrations\ProcessValueEvent;
use Solspace\Freeform\Fields\Interfaces\FileUploadInterface;
use Solspace\Freeform\Integrations\Elements\User\User;
use Solspace\Freeform\Library\Bundles\FeatureBundle;
use Solspace\Freeform\Library\Integrations\Types\Elements\ElementIntegrationInterface;
use yii\base\Event;

class PhotoTransform extends FeatureBundle
{
public function __construct()
{
Event::on(
ElementIntegrationInterface::class,
ElementIntegrationInterface::EVENT_PROCESS_VALUE,
[$this, 'processPhoto']
);
}

public function processPhoto(ProcessValueEvent $event): void
{
if (!$event->getIntegration() instanceof User) {
return;
}

if ('photo' !== $event->getHandle()) {
return;
}

$value = $event->getValue();
$field = $event->getFreeformField();

if (empty($value)) {
$event->setValue(null);

return;
}

if ($field instanceof FileUploadInterface) {
$asset = $field->getAssets()->one();
if ($asset) {
$event->setValue($asset);
}

return;
}

$assetId = reset($value);
if ($assetId && is_numeric($assetId)) {
$asset = \Craft::$app->getAssets()->getAssetById((int) $assetId);
if ($asset) {
$event->setValue($asset);
}
}
}
}

0 comments on commit f8141a8

Please sign in to comment.