load postal codes dynamically

This commit is contained in:
Julien Fastré 2018-07-05 13:02:21 +02:00
parent 86a814cfb5
commit 26a4d80ce6
16 changed files with 368 additions and 10 deletions

View File

@ -0,0 +1,89 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2018, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Chill\MainBundle\Entity\PostalCode;
use Symfony\Component\HttpFoundation\JsonResponse;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PostalCodeController extends Controller
{
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
{
$this->translatableStringHelper = $translatableStringHelper;
}
/**
*
* @Route(
* "{_locale}/postalcode/search"
* )
* @param Request $request
* @return JsonResponse
*/
public function searchAction(Request $request)
{
$pattern = $request->query->getAlnum('q', '');
if (empty($pattern)) {
return new JsonResponse(["results" => [], "pagination" => [ "more" => false]]);
}
$query = $this->getDoctrine()->getManager()
->createQuery(sprintf(
"SELECT p.id AS id, p.name AS name, p.code AS code, "
. "country.name AS country_name, "
. "country.countryCode AS country_code "
. "FROM %s p "
. "JOIN p.country country "
. "WHERE LOWER(p.name) LIKE LOWER(:pattern) OR LOWER(p.code) LIKE LOWER(:pattern) "
. "ORDER BY code"
, PostalCode::class)
)
->setParameter('pattern', '%'.$pattern.'%')
->setMaxResults(30)
;
$results = \array_map(function($row) {
$row['country_name'] = $this->translatableStringHelper->localize($row['country_name']);
$row['text'] = $row['code']." ".$row["name"]." (".$row['country_name'].")";
return $row;
}, $query->getResult(Query::HYDRATE_ARRAY));
return new JsonResponse([ 'results' => $results, "pagination" => [ "more" => false ] ]);
}
}

View File

@ -0,0 +1,85 @@
<?php
/*
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Form\ChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Chill\MainBundle\Repository\PostalCodeRepository;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Chill\MainBundle\Entity\PostalCode;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PostalCodeChoiceLoader implements ChoiceLoaderInterface
{
/**
*
* @var PostalCodeRepository
*/
protected $postalCodeRepository;
protected $lazyLoadedPostalCodes = [];
public function __construct(PostalCodeRepository $postalCodeRepository)
{
$this->postalCodeRepository = $postalCodeRepository;
}
public function loadChoiceList($value = null): ChoiceListInterface
{
$list = new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
$this->lazyLoadedPostalCodes,
function(PostalCode $pc) use ($value) {
return \call_user_func($value, $pc);
});
return $list;
}
public function loadChoicesForValues(array $values, $value = null)
{
$choices = [];
foreach($values as $value) {
$choices[] = $this->postalCodeRepository->find($value);
}
return $choices;
}
public function loadValuesForChoices(array $choices, $value = null)
{
$values = [];
foreach ($choices as $choice) {
if (NULL === $choice) {
$values[] = null;
continue;
}
$id = \call_user_func($value, $choice);
$values[] = $id;
$this->lazyLoadedPostalCodes[$id] = $choice;
}
return $values;
}
}

View File

@ -21,10 +21,14 @@ namespace Chill\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Entity\PostalCode;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Chill\MainBundle\Form\ChoiceLoader\PostalCodeChoiceLoader;
use Symfony\Component\Translation\TranslatorInterface;
/**
* A form to pick between PostalCode
@ -39,10 +43,35 @@ class PostalCodeType extends AbstractType
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
/**
*
* @var PostalCodeChoiceLoader
*/
protected $choiceLoader;
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct(TranslatableStringHelper $helper)
{
public function __construct(
TranslatableStringHelper $helper,
UrlGeneratorInterface $urlGenerator,
PostalCodeChoiceLoader $choiceLoader,
TranslatorInterface $translator
) {
$this->translatableStringHelper = $helper;
$this->urlGenerator = $urlGenerator;
$this->choiceLoader = $choiceLoader;
$this->translator = $translator;
}
@ -55,11 +84,26 @@ class PostalCodeType extends AbstractType
{
// create a local copy for usage in Closure
$helper = $this->translatableStringHelper;
$resolver->setDefault('class', PostalCode::class)
$resolver
->setDefault('class', PostalCode::class)
->setDefault('choice_label', function(PostalCode $code) use ($helper) {
return $code->getCode().' '.$code->getName().' ['.
$helper->localize($code->getCountry()->getName()).']';
}
);
})
->setDefault('choice_loader', $this->choiceLoader)
->setDefault('placeholder', 'Select a postal code')
;
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['attr']['data-postal-code'] = 'data-postal-code';
$view->vars['attr']['data-search-url'] = $this->urlGenerator
->generate('chill_main_postal_code_search');
$view->vars['attr']['data-placeholder'] = $this->translator->trans($options['placeholder']);
$view->vars['attr']['data-no-results-label'] = $this->translator->trans('select2.no_results');
$view->vars['attr']['data-error-load-label'] = $this->translator->trans('select2.error_loading');
$view->vars['attr']['data-searching-label'] = $this->translator->trans('select2.searching');
}
}

View File

@ -0,0 +1,28 @@
<?php
/*
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Repository;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PostalCodeRepository extends \Doctrine\ORM\EntityRepository
{
}

View File

@ -1,6 +1,9 @@
Chill\MainBundle\Entity\PostalCode:
type: entity
table: chill_main_postal_code
repositoryClass: Chill\MainBundle\Repository\PostalCodeRepository
indexes:
- { name: search_name_code, columns: [ "code", "label" ] }
id:
id:
type: integer

View File

@ -17,6 +17,10 @@ chill_main_admin:
chill_main_exports:
resource: "@ChillMainBundle/Resources/config/routing/exports.yml"
prefix: "{_locale}/exports"
chill_postal_code:
resource: "@ChillMainBundle/Resources/config/routing/postal-code.yml"
prefix: "{_locale}/postal-code"
root:
path: /

View File

@ -0,0 +1,4 @@
chill_main_postal_code_search:
path: /search
defaults: { _controller: ChillMainBundle:PostalCode:search }

View File

@ -1,4 +1,6 @@
services:
Chill\MainBundle\Controller\:
autowire: true
resource: '../../../Controller'
tags: ['controller.service_arguments']

View File

@ -54,9 +54,17 @@ services:
class: Chill\MainBundle\Form\Type\PostalCodeType
arguments:
- "@chill.main.helper.translatable_string"
- '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
- '@chill.main.form.choice_loader.postal_code'
- '@Symfony\Component\Translation\TranslatorInterface'
tags:
- { name: form.type }
chill.main.form.choice_loader.postal_code:
class: Chill\MainBundle\Form\ChoiceLoader\PostalCodeChoiceLoader
arguments:
- '@Chill\MainBundle\Repository\PostalCodeRepository'
chill.main.form.type.export:
class: Chill\MainBundle\Form\Type\Export\ExportType
arguments:

View File

@ -15,4 +15,13 @@ services:
class: Doctrine\ORM\EntityRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- "Chill\\MainBundle\\Entity\\Scope"
- "Chill\\MainBundle\\Entity\\Scope"
chill.main.postalcode_repository:
class: Doctrine\ORM\EntityRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- "Chill\\MainBundle\\Entity\\PostalCode"
Chill\MainBundle\Repository\PostalCodeRepository: '@chill.main.postalcode_repository'

View File

@ -0,0 +1,33 @@
<?php declare(strict_types=1);
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Add index to postal code
*/
final class Version20180703191509 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
try {
$this->addSql('CREATE EXTENSION IF NOT EXISTS pg_trgm');
$this->addSql('CREATE INDEX search_name_code ON chill_main_postal_code USING GIN (LOWER(code) gin_trgm_ops, LOWER(label) gin_trgm_ops)');
} catch (\Exception $e) {
$this->skipIf(true, "Could not create extension pg_trgm");
}
}
public function down(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('DROP INDEX search_name_code');
}
}

View File

@ -0,0 +1,36 @@
window.addEventListener('load', function (e) {
var
postalCodes = document.querySelectorAll('[data-postal-code]')
;
for (let i = 0; i < postalCodes.length; i++) {
let
searchUrl = postalCodes[i].dataset.searchUrl,
noResultsLabel = postalCodes[i].dataset.noResultsLabel,
errorLoadLabel = postalCodes[i].dataset.errorLoadLabel,
searchingLabel = postalCodes[i].dataset.searchingLabel
;
$(postalCodes[i]).select2({
allowClear: true,
language: {
errorLoading: function () {
return errorLoadLabel;
},
noResults: function () {
return noResultsLabel;
},
searching: function () {
return searchingLabel;
}
},
ajax: {
url: searchUrl,
dataType: 'json',
delay: 250
}
});
}
});

View File

@ -18,4 +18,11 @@ span.force-inline-label label {
align-self: center;
margin-left: 1em;
}
}
.chill-form__errors {
.chill-form_errors__entry.chill-form__errors__entry--warning {
color: var(--chill-green-dark);
}
}

View File

@ -182,4 +182,9 @@ column: colonne
Comma separated values (CSV): Valeurs séparées par des virgules (CSV - tableur)
# spreadsheet formatter
Choose the format: Choisir le format
Choose the format: Choisir le format
# select2
'select2.no_results': Aucun résultat
'select2.error_loading': Erreur de chargement des résultats
'select2.searching': Recherche en cours...

View File

@ -141,9 +141,9 @@
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul class="errors">
<ul class="errors chill-form__errors">
{% for error in errors %}
<li>{{ error.message }}</li>
<li class="chill-form_errors__entry {% if 'severity' in error.cause.constraint.payload|keys %}chill-form__errors__entry--{{ error.cause.constraint.payload.severity }}{% endif %}">{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}

View File

@ -27,5 +27,6 @@ require('./Resources/public/modules/download-report/index.js');
//require('./Resources/public/css/scratch.css');
//require('./Resources/public/css/select2/select2.css');
require('select2/dist/css/select2.css');
require('./Resources/public/modules/postal-code/index.js');
// img