Skip to content

Commit

Permalink
Merge branch 'release/1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Culka committed Oct 25, 2024
2 parents 1c16ba9 + f06e86c commit 6361436
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trexima/cv",
"description": "Bundle provides form for creating CV in european format.",
"type": "symfony-bundle",
"version": "1.7.2",
"version": "1.8.0",
"license": "MIT",
"keywords": ["cv"],
"authors": [
Expand Down
69 changes: 17 additions & 52 deletions src/Form/Parts/EuropeanCVPartBasicInfoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Trexima\EuropeanCvBundle\Form\Parts;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\DataAccessor\CallbackAccessor;
Expand All @@ -16,9 +15,6 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -42,7 +38,7 @@
/**
* Basic user info
*/
class EuropeanCVPartBasicInfoType extends AbstractType implements DataMapperInterface, EventSubscriberInterface
class EuropeanCVPartBasicInfoType extends AbstractType implements DataMapperInterface
{
private readonly DataMapper $dataMapper;

Expand Down Expand Up @@ -81,6 +77,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => t('trexima_european_cv.form_label.sex_label', [], 'trexima_european_cv'),
'placeholder' => false,
])
->add('photo', PhotoType::class, array_merge([
'required' => false,
'mapped' => false,
'label' => false,
'aspect_ratio' => 1,
'max_size' => 16 << 20,
'max_size_message' => t(
'job.max_file_size',
['limit' => 16, 'suffix' => 'MB'],
'validators',
),
], ($options['field_options']['photo'] ?? [])))
->add('firstName', TextType::class, [
'label' => t('trexima_european_cv.form_label.first_name_label', [], 'trexima_european_cv'),
'attr' => [
Expand Down Expand Up @@ -215,32 +223,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'placeholder' => t('trexima_european_cv.form_label.description_placeholder', [], 'trexima_european_cv')
]
]);

$builder->addEventSubscriber($this);
}

public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SET_DATA => 'onPreSetData',
FormEvents::SUBMIT => 'onSubmit',
];
}

public function onPreSetData(FormEvent $formEvent): void
{
$this->addPhotoField($formEvent->getForm());
}

public function onSubmit(FormEvent $formEvent): void
{
$form = $formEvent->getForm();

if ($onSubmitCallback = $form->getConfig()->getOption('callback_options')['photo']['on_submit'] ?? null) {
$onSubmitCallback($form, $formEvent->getData());
}

$this->addPhotoField($form);
}

/**
Expand All @@ -252,7 +234,6 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults([
'data_class' => EuropeanCV::class,
'field_options' => [],
'callback_options' => [],
]);

$resolver->setRequired([
Expand All @@ -279,24 +260,9 @@ public function mapDataToForms(mixed $viewData, \Traversable $forms): void
$this->mapPhoto($viewData, $forms);
}

private function addPhotoField(FormInterface $form): void
public function makePhotoUrl(string $photo): string
{
$options = $form->getConfig()->getOptions();

$form
->add('photo', PhotoType::class, array_merge([
'required' => false,
'mapped' => false,
'label' => false,
'aspect_ratio' => 1,
'max_size' => 16 << 20,
'max_size_message' => t(
'job.max_file_size',
['limit' => 16, 'suffix' => 'MB'],
'validators',
),
], ($options['field_options']['photo'] ?? [])))
;
return $this->uploadUrl . '/images/' . $photo;
}

private function mapPhoto(EuropeanCV|null $viewData, \Traversable $forms): void
Expand All @@ -307,16 +273,15 @@ private function mapPhoto(EuropeanCV|null $viewData, \Traversable $forms): void
}

$photo = $viewData?->getPhoto();

if (null === $photo) {
$forms['photo']->setData(null);
return;
}

$url = $this->uploadUrl . '/images/' . $photo;

$photoType = (new Photo())
->setExistingFileId('123')
->setExistingFileUrl($url)
->setExistingFileUrl($this->makePhotoUrl($photo))
->setFile(null);

$forms['photo']->setData($photoType);
Expand Down
10 changes: 10 additions & 0 deletions src/Form/Type/PhotoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('options', HiddenType::class, [
'required' => false,
'error_bubbling' => true,
'constraints' => [
new Assert\Type('array'),
new Assert\Collection([
'x' => new Assert\GreaterThanOrEqual(0, message: 'trexima_european_cv.photo.constraint.options.x'),
'y' => new Assert\GreaterThanOrEqual(0, message: 'trexima_european_cv.photo.constraint.options.y'),
'width' => new Assert\GreaterThan(0, message: 'trexima_european_cv.photo.constraint.options.width'),
'height' => new Assert\GreaterThan(0, message: 'trexima_european_cv.photo.constraint.options.height'),
'rotate' => new Assert\Range(min: -360, max: 360, notInRangeMessage: 'trexima_european_cv.photo.constraint.options.rotate'),
], missingFieldsMessage: 'trexima_european_cv.photo.constraint.options'),
],
])
->addEventListener(FormEvents::POST_SET_DATA, $this->postSetData(...));

Expand Down
36 changes: 36 additions & 0 deletions translations/validators+intl-icu.sk-SK.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,41 @@
<target>Prosím, vyplň dátum narodenia v správnom tvare.</target>
</segment>
</unit>
<unit id="hkrJKVB" name="trexima_european_cv.photo.constraint.options">
<segment>
<source>trexima_european_cv.photo.constraint.options</source>
<target>Pole {field} chýba.</target>
</segment>
</unit>
<unit id="fVXjjmH" name="trexima_european_cv.photo.constraint.options.x">
<segment>
<source>trexima_european_cv.photo.constraint.options.x</source>
<target>Hodnota pre "X-ovú súradnicu" musí byť väčšia alebo rovná 0</target>
</segment>
</unit>
<unit id="69nWrlF" name="trexima_european_cv.photo.constraint.options.y">
<segment>
<source>trexima_european_cv.photo.constraint.options.y</source>
<target>Hodnota pre "Y-ovú súradnicu" musí byť väčšia alebo rovná 0</target>
</segment>
</unit>
<unit id="9q16aMK" name="trexima_european_cv.photo.constraint.options.width">
<segment>
<source>trexima_european_cv.photo.constraint.options.width</source>
<target>Šírka musí byť väčšia ako 0</target>
</segment>
</unit>
<unit id="1_2SANY" name="trexima_european_cv.photo.constraint.options.height">
<segment>
<source>trexima_european_cv.photo.constraint.options.height</source>
<target>Výška musí byť väčšia ako 0</target>
</segment>
</unit>
<unit id="1z1w_wr" name="trexima_european_cv.photo.constraint.options.rotate">
<segment>
<source>trexima_european_cv.photo.constraint.options.rotate</source>
<target>Uhol otočenia musí byť v rozmedzí -360° až 360°</target>
</segment>
</unit>
</file>
</xliff>

0 comments on commit 6361436

Please sign in to comment.