Merge branch '149-150-events-improve' into 'master'

Modernisation fonctionnement module Evénement

Closes #149, #150, and #225

See merge request Chill-Projet/chill-bundles!621
This commit is contained in:
2024-02-08 20:19:30 +00:00
47 changed files with 1483 additions and 444 deletions

View File

@@ -42,6 +42,8 @@ class PickUserDynamicType extends AbstractType
$view->vars['types'] = ['user'];
$view->vars['uniqid'] = uniqid('pick_user_dyn');
$view->vars['suggested'] = [];
$view->vars['as_id'] = true === $options['as_id'] ? '1' : '0';
$view->vars['submit_on_adding_new_entity'] = true === $options['submit_on_adding_new_entity'] ? '1' : '0';
foreach ($options['suggested'] as $user) {
$view->vars['suggested'][] = $this->normalizer->normalize($user, 'json', ['groups' => 'read']);
@@ -54,7 +56,12 @@ class PickUserDynamicType extends AbstractType
->setDefault('multiple', false)
->setAllowedTypes('multiple', ['bool'])
->setDefault('compound', false)
->setDefault('suggested', []);
->setDefault('suggested', [])
// if set to true, only the id will be set inside the content. The denormalization will not work.
->setDefault('as_id', false)
->setAllowedTypes('as_id', ['bool'])
->setDefault('submit_on_adding_new_entity', false)
->setAllowedTypes('submit_on_adding_new_entity', ['bool']);
}
public function getBlockPrefix()

View File

@@ -17,7 +17,7 @@ use Symfony\Component\Routing\RouterInterface;
/**
* Create paginator instances.
*/
class PaginatorFactory
final readonly class PaginatorFactory implements PaginatorFactoryInterface
{
final public const DEFAULT_CURRENT_PAGE_KEY = 'page';
@@ -25,23 +25,20 @@ class PaginatorFactory
final public const DEFAULT_PAGE_NUMBER = 1;
/**
* @param int $itemPerPage
*/
public function __construct(
/**
* the request stack.
*/
private readonly RequestStack $requestStack,
private RequestStack $requestStack,
/**
* the router and generator for url.
*/
private readonly RouterInterface $router,
private RouterInterface $router,
/**
* the default item per page. This may be overriden by
* the request or inside the paginator.
*/
private $itemPerPage = 20
private int $itemPerPage = 20
) {
}
@@ -51,17 +48,14 @@ class PaginatorFactory
* The default route and route parameters are the current ones. If set,
* thos route are overriden.
*
* @param int $totalItems
* @param string|null $route the specific route to use in pages
* @param array|null $routeParameters the specific route parameters to use in pages
*
* @return PaginatorInterface
*/
public function create(
$totalItems,
int $totalItems,
?string $route = null,
?array $routeParameters = null
) {
): PaginatorInterface {
return new Paginator(
$totalItems,
$this->getCurrentItemsPerPage(),
@@ -74,7 +68,7 @@ class PaginatorFactory
);
}
public function getCurrentItemsPerPage()
public function getCurrentItemsPerPage(): int
{
return $this->requestStack
->getCurrentRequest()
@@ -82,16 +76,13 @@ class PaginatorFactory
->getInt(self::DEFAULT_ITEM_PER_NUMBER_KEY, $this->itemPerPage);
}
public function getCurrentPageFirstItemNumber()
public function getCurrentPageFirstItemNumber(): int
{
return ($this->getCurrentPageNumber() - 1) *
$this->getCurrentItemsPerPage();
}
/**
* @return int
*/
public function getCurrentPageNumber()
public function getCurrentPageNumber(): int
{
return $this->requestStack
->getCurrentRequest()
@@ -99,14 +90,14 @@ class PaginatorFactory
->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER);
}
protected function getCurrentRoute()
private function getCurrentRoute()
{
$request = $this->requestStack->getCurrentRequest();
return $request->get('_route');
}
protected function getCurrentRouteParameters()
private function getCurrentRouteParameters()
{
return array_merge(
$this->router->getContext()->getParameters(),

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Pagination;
/**
* Create paginator instances.
*/
interface PaginatorFactoryInterface
{
/**
* create a paginator instance.
*
* The default route and route parameters are the current ones. If set,
* thos route are overriden.
*
* @param string|null $route the specific route to use in pages
* @param array|null $routeParameters the specific route parameters to use in pages
*/
public function create(int $totalItems, ?string $route = null, ?array $routeParameters = null): PaginatorInterface;
public function getCurrentItemsPerPage(): int;
public function getCurrentPageFirstItemNumber(): int;
public function getCurrentPageNumber(): int;
}

View File

@@ -24,7 +24,10 @@ function loadDynamicPicker(element) {
(input.value === '[]' || input.value === '') ?
null : [ JSON.parse(input.value) ]
)
suggested = JSON.parse(el.dataset.suggested)
suggested = JSON.parse(el.dataset.suggested),
as_id = parseInt(el.dataset.asId) === 1,
submit_on_adding_new_entity = parseInt(el.dataset.submitOnAddingNewEntity) === 1
label = el.dataset.label;
if (!isMultiple) {
if (input.value === '[]'){
@@ -39,6 +42,7 @@ function loadDynamicPicker(element) {
':picked="picked" ' +
':uniqid="uniqid" ' +
':suggested="notPickedSuggested" ' +
':label="label" ' +
'@addNewEntity="addNewEntity" ' +
'@removeEntity="removeEntity"></pick-entity>',
components: {
@@ -50,7 +54,10 @@ function loadDynamicPicker(element) {
types: JSON.parse(el.dataset.types),
picked: picked === null ? [] : picked,
uniqid: el.dataset.uniqid,
suggested: suggested
suggested,
as_id,
submit_on_adding_new_entity,
label,
}
},
computed: {
@@ -69,7 +76,12 @@ function loadDynamicPicker(element) {
return el.type === entity.type && el.id === entity.id;
})) {
this.picked.push(entity);
input.value = JSON.stringify(this.picked);
if (!as_id) {
input.value = JSON.stringify(this.picked);
} else {
const ids = this.picked.map(el => el.id);
input.value = ids.join(',');
}
console.log(entity)
}
} else {
@@ -78,9 +90,17 @@ function loadDynamicPicker(element) {
})) {
this.picked.splice(0, this.picked.length);
this.picked.push(entity);
input.value = JSON.stringify(this.picked[0]);
if (!as_id) {
input.value = JSON.stringify(this.picked[0]);
} else {
input.value = this.picked.map(el => el.id);
}
}
}
if (this.submit_on_adding_new_entity) {
input.form.submit();
}
},
removeEntity({entity}) {
if (-1 === this.suggested.findIndex(e => e.type === entity.type && e.id === entity.id)) {

View File

@@ -56,6 +56,10 @@ export default {
suggested: {
type: Array,
default: []
},
label: {
type: String,
required: false,
}
},
emits: ['addNewEntity', 'removeEntity'],
@@ -80,6 +84,10 @@ export default {
};
},
translatedListOfTypes() {
if (this.label !== '') {
return this.label;
}
let trans = [];
this.types.forEach(t => {
if (this.$props.multiple) {

View File

@@ -256,7 +256,10 @@
data-types="{{ form.vars['types']|json_encode }}"
data-multiple="{{ form.vars['multiple'] }}"
data-uniqid="{{ form.vars['uniqid'] }}"
data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}"></div>
data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}"
data-as-id="{{ form.vars['as_id'] }}"
data-submit-on-adding-new-entity="{{ form.vars['submit_on_adding_new_entity'] }}"
data-label="{{ form.vars['label']|trans|escape('html_attr') }}"></div>
{% endblock %}
{% block pick_postal_code_widget %}

View File

@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test;
use Chill\MainBundle\Pagination\Paginator;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class DummyPaginator implements PaginatorFactoryInterface
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly string $route,
private readonly array $routeParameters = []
) {
}
public function create(int $totalItems, ?string $route = null, ?array $routeParameters = null): PaginatorInterface
{
return new Paginator(
$totalItems,
$totalItems,
1,
$this->route,
$this->routeParameters,
$this->urlGenerator,
PaginatorFactory::DEFAULT_CURRENT_PAGE_KEY,
PaginatorFactory::DEFAULT_ITEM_PER_NUMBER_KEY
);
}
public function getCurrentItemsPerPage(): int
{
return 20;
}
public function getCurrentPageFirstItemNumber(): int
{
return 1;
}
public function getCurrentPageNumber(): int
{
return 1;
}
}

View File

@@ -12,6 +12,7 @@ services:
- "%chill_main.pagination.item_per_page%"
Chill\MainBundle\Pagination\PaginatorFactory: '@chill_main.paginator_factory'
Chill\MainBundle\Pagination\PaginatorFactoryInterface: '@chill_main.paginator_factory'
chill_main.paginator.twig_extensions:
class: Chill\MainBundle\Pagination\ChillPaginationTwig