fixes for settign a localisation

This commit is contained in:
Julien Fastré 2021-11-22 13:43:33 +01:00
parent 69384a84d1
commit 764bb6a21f
6 changed files with 52 additions and 36 deletions

View File

@ -3,6 +3,7 @@
namespace Chill\MainBundle\Form; namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Repository\LocationRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -11,15 +12,16 @@ use Symfony\Component\Form\FormBuilderInterface;
class UserCurrentLocationType extends AbstractType class UserCurrentLocationType extends AbstractType
{ {
private LocationRepository $locationRepository;
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
/** /**
* @param TranslatableStringHelper $translatableStringHelper * @param TranslatableStringHelper $translatableStringHelper
*/ */
public function __construct(TranslatableStringHelper $translatableStringHelper) public function __construct(TranslatableStringHelper $translatableStringHelper, LocationRepository $locationRepository)
{ {
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->locationRepository = $locationRepository;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
@ -27,11 +29,14 @@ class UserCurrentLocationType extends AbstractType
$builder $builder
->add('currentLocation', EntityType::class, [ ->add('currentLocation', EntityType::class, [
'class' => Location::class, 'class' => Location::class,
'choices' => $this->locationRepository->findByPublicLocations(),
'choice_label' => function (Location $entity) { 'choice_label' => function (Location $entity) {
return $entity->getName() ? return $entity->getName() ?
$this->translatableStringHelper->localize($entity->getLocationType()->getTitle()) . ' ' . $entity->getName() : $entity->getName().' ('.$this->translatableStringHelper->localize($entity->getLocationType()->getTitle()).')' :
$this->translatableStringHelper->localize($entity->getLocationType()->getTitle()); $this->translatableStringHelper->localize($entity->getLocationType()->getTitle());
}, },
'placeholder' => 'Pick a location',
'required' => false,
]); ]);
} }
} }

View File

@ -18,4 +18,14 @@ class LocationRepository extends ServiceEntityRepository
{ {
parent::__construct($registry, Location::class); parent::__construct($registry, Location::class);
} }
/**
* fetch locations which are selectable for users
*
* @return Location[]
*/
public function findByPublicLocations()
{
return $this->findBy(['active' => true, 'availableForUsers' => true]);
}
} }

View File

@ -3,21 +3,19 @@
{% block title %}{{ 'Edit my current location'|trans }}{% endblock %} {% block title %}{{ 'Edit my current location'|trans }}{% endblock %}
{% block content -%} {% block content -%}
<h1>{{ 'Edit my current location'|trans }}</h1> <div class="col-md-10 col-xxl">
<h1>{{ 'Edit my current location'|trans }}</h1>
{{ form_start(edit_form) }} {{ form_start(edit_form) }}
{{ form_row(edit_form.currentLocation) }} {{ form_row(edit_form.currentLocation) }}
<ul class="record_actions"> <ul class="record_actions sticky-form-buttons">
<li class="cancel"> <li>
<a href="{{ chill_return_path_or('chill_main_homepage') }}" class="btn btn-cancel"> {{ form_widget(edit_form.submit, { 'attr': { 'class': 'btn btn-edit' } } ) }}
{{ 'Cancel'|trans }} </li>
</a> </ul>
</li>
<li>
{{ form_widget(edit_form.submit, { 'attr': { 'class': 'btn btn-edit' } } ) }}
</li>
</ul>
{{ form_end(edit_form) }} {{ form_end(edit_form) }}
</div>
{% endblock %} {% endblock %}

View File

@ -20,35 +20,37 @@ namespace Chill\MainBundle\Routing\MenuBuilder;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface; use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class UserMenuBuilder implements LocalMenuBuilderInterface class UserMenuBuilder implements LocalMenuBuilderInterface
{ {
/** private Security $security;
*
* @var TokenStorageInterface
*/
protected $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage) public function __construct(Security $security)
{ {
$this->tokenStorage = $tokenStorage; $this->security = $security;
} }
public function buildMenu($menuId, \Knp\Menu\MenuItem $menu, array $parameters) public function buildMenu($menuId, \Knp\Menu\MenuItem $menu, array $parameters)
{ {
if ($this->tokenStorage->getToken()->getUser() instanceof User) { $user = $this->security->getUser();
if ($user instanceof User) {
if (null !== $user->getCurrentLocation()) {
$locationTextMenu = $user->getCurrentLocation()->getName();
} else {
$locationTextMenu = 'Set a location';
}
$menu $menu
->addChild( ->addChild(
'Change location', $locationTextMenu,
['route' => 'chill_main_user_currentlocation_edit'] ['route' => 'chill_main_user_currentlocation_edit']
) )
->setExtras([ ->setExtras([
'order' => 99999999997 'order' => -9999999,
'icon' => 'map-marker'
]); ]);
$menu $menu
->addChild( ->addChild(

View File

@ -7,8 +7,8 @@ services:
resource: '../../Routing/MenuBuilder' resource: '../../Routing/MenuBuilder'
Chill\MainBundle\Routing\MenuBuilder\UserMenuBuilder: Chill\MainBundle\Routing\MenuBuilder\UserMenuBuilder:
arguments: autowire: true
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' autoconfigure: true
tags: tags:
- { name: 'chill.menu_builder' } - { name: 'chill.menu_builder' }

View File

@ -184,8 +184,9 @@ User jobs: Métiers
Current location: Localisation actuelle Current location: Localisation actuelle
Edit my current location: Éditer ma localisation actuelle Edit my current location: Éditer ma localisation actuelle
Change current location: Changer ma localisation actuelle Change current location: Changer ma localisation actuelle
Change location: Changer ma localisation Set a location: Indiquer une localisation
Current location successfully updated: Localisation actuelle mise à jour Current location successfully updated: Localisation actuelle mise à jour
Pick a location: Choisir un lieu
#admin section for circles (old: scopes) #admin section for circles (old: scopes)
List circles: Cercles List circles: Cercles