diff --git a/CHANGELOG.md b/CHANGELOG.md index 285eab3e6..2b6b41c36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to ## Unreleased +* [main] Add mainLocation field to User entity and add it in user form type * rewrite page which allow to select activity ## Test releases diff --git a/src/Bundle/ChillMainBundle/Entity/User.php b/src/Bundle/ChillMainBundle/Entity/User.php index 0c3194fba..76fd7196d 100644 --- a/src/Bundle/ChillMainBundle/Entity/User.php +++ b/src/Bundle/ChillMainBundle/Entity/User.php @@ -97,6 +97,11 @@ class User implements AdvancedUserInterface */ private ?Center $mainCenter = null; + /** + * @ORM\ManyToOne(targetEntity=Location::class) + */ + private ?Location $mainLocation = null; + /** * @ORM\ManyToOne(targetEntity=Scope::class) */ @@ -228,6 +233,11 @@ class User implements AdvancedUserInterface return $this->mainCenter; } + public function getMainLocation(): ?Location + { + return $this->mainLocation; + } + public function getMainScope(): ?Scope { return $this->mainScope; @@ -405,6 +415,13 @@ class User implements AdvancedUserInterface return $this; } + public function setMainLocation(?Location $mainLocation): User + { + $this->mainLocation = $mainLocation; + + return $this; + } + public function setMainScope(?Scope $mainScope): User { $this->mainScope = $mainScope; diff --git a/src/Bundle/ChillMainBundle/Form/UserType.php b/src/Bundle/ChillMainBundle/Form/UserType.php index 58f053ac1..6662d0bab 100644 --- a/src/Bundle/ChillMainBundle/Form/UserType.php +++ b/src/Bundle/ChillMainBundle/Form/UserType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Templating\TranslatableStringHelper; @@ -75,6 +76,22 @@ class UserType extends AbstractType 'choice_label' => function (UserJob $c) { return $this->translatableStringHelper->localize($c->getLabel()); }, + ]) + ->add('mainLocation', EntityType::class, [ + 'label' => 'Main location', + 'required' => false, + 'placeholder' => 'choose a location', + 'class' => Location::class, + 'choice_label' => function (Location $l) { + return $this->translatableStringHelper->localize($l->getLocationType()->getTitle()) . ' - ' . $l->getName(); + }, + 'query_builder' => static function (EntityRepository $er) { + $qb = $er->createQueryBuilder('l'); + $qb->orderBy('l.locationType'); + $qb->where('l.availableForUsers = TRUE'); + + return $qb; + }, ]); if ($options['is_creation']) { diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php index 975a7c9f8..365433ef3 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php @@ -58,7 +58,7 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware ); $locationContext = array_merge( $context, - ['docgen:expects' => Location::class, 'groups' => 'dogen:read'] + ['docgen:expects' => Location::class, 'groups' => 'docgen:read'] ); if (null === $user && 'docgen' === $format) { @@ -67,6 +67,7 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware 'main_center' => $this->normalizer->normalize(null, $format, $centerContext), 'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext), 'current_location' => $this->normalizer->normalize(null, $format, $locationContext), + 'main_location' => $this->normalizer->normalize(null, $format, $locationContext), ]); } @@ -84,6 +85,7 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware if ('docgen' === $format) { $data['current_location'] = $this->normalizer->normalize($user->getCurrentLocation(), $format, $locationContext); + $data['main_location'] = $this->normalizer->normalize($user->getMainLocation(), $format, $locationContext); } return $data; diff --git a/src/Bundle/ChillMainBundle/migrations/Version20220112161136.php b/src/Bundle/ChillMainBundle/migrations/Version20220112161136.php new file mode 100644 index 000000000..89abc11d7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20220112161136.php @@ -0,0 +1,40 @@ +addSql('ALTER TABLE users DROP CONSTRAINT FK_1483A5E9DB622A42'); + $this->addSql('DROP INDEX IDX_1483A5E9DB622A42'); + $this->addSql('ALTER TABLE users DROP mainLocation_id'); + } + + public function getDescription(): string + { + return 'Add mainLocation to User'; + } + + public function up(Schema $schema): void + { + $this->addSql('ALTER TABLE users ADD mainLocation_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE users ADD CONSTRAINT FK_1483A5E9DB622A42 FOREIGN KEY (mainLocation_id) REFERENCES chill_main_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_1483A5E9DB622A42 ON users (mainLocation_id)'); + } +}