mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-18 20:54:59 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,23 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Templating\Listing;
|
||||
|
||||
use Chill\MainBundle\Form\Type\Listing\FilterOrderType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use function array_merge;
|
||||
|
||||
class FilterOrderHelper
|
||||
{
|
||||
private FormFactoryInterface $formFactory;
|
||||
private RequestStack $requestStack;
|
||||
private ?array $searchBoxFields = null;
|
||||
private array $checkboxes = [];
|
||||
private ?array $submitted = null;
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private ?string $formName = 'f';
|
||||
private string $formType = FilterOrderType::class;
|
||||
|
||||
private array $formOptions = [];
|
||||
|
||||
private string $formType = FilterOrderType::class;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
private ?array $searchBoxFields = null;
|
||||
|
||||
private ?array $submitted = null;
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
@@ -27,29 +41,32 @@ class FilterOrderHelper
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function setSearchBox($searchBoxFields = null): self
|
||||
{
|
||||
$this->searchBoxFields = $searchBoxFields;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self
|
||||
{
|
||||
$missing = count($choices) - count($trans) - 1;
|
||||
$this->checkboxes[$name] = [
|
||||
'choices' => $choices, 'default' => $default,
|
||||
'trans' =>
|
||||
\array_merge(
|
||||
'trans' => array_merge(
|
||||
$trans,
|
||||
0 < $missing ?
|
||||
array_fill(0, $missing, null) : []
|
||||
)
|
||||
),
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildForm(): FormInterface
|
||||
{
|
||||
return $this->formFactory
|
||||
->createNamed($this->formName, $this->formType, $this->getDefaultData(), array_merge([
|
||||
'helper' => $this,
|
||||
'method' => 'GET',
|
||||
'csrf_protection' => false,
|
||||
], $this->formOptions))
|
||||
->handleRequest($this->requestStack->getCurrentRequest());
|
||||
}
|
||||
|
||||
public function getCheckboxData(string $name): array
|
||||
{
|
||||
return $this->getFormData()['checkboxes'][$name];
|
||||
@@ -60,28 +77,31 @@ class FilterOrderHelper
|
||||
return $this->checkboxes;
|
||||
}
|
||||
|
||||
public function hasSearchBox(): bool
|
||||
public function getQueryString(): ?string
|
||||
{
|
||||
return $this->searchBoxFields !== null;
|
||||
return $this->getFormData()['q'];
|
||||
}
|
||||
|
||||
private function getFormData(): array
|
||||
public function hasSearchBox(): bool
|
||||
{
|
||||
if (NULL === $this->submitted) {
|
||||
$this->submitted = $this->buildForm()
|
||||
->getData();
|
||||
}
|
||||
return null !== $this->searchBoxFields;
|
||||
}
|
||||
|
||||
return $this->submitted;
|
||||
public function setSearchBox($searchBoxFields = null): self
|
||||
{
|
||||
$this->searchBoxFields = $searchBoxFields;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function getDefaultData(): array
|
||||
{
|
||||
$r = [];
|
||||
$r = [];
|
||||
|
||||
if ($this->hasSearchBox()) {
|
||||
$r['q'] = '';
|
||||
}
|
||||
|
||||
if ($this->hasSearchBox()) {
|
||||
$r['q'] = '';
|
||||
}
|
||||
foreach ($this->checkboxes as $name => $c) {
|
||||
$r['checkboxes'][$name] = $c['default'];
|
||||
}
|
||||
@@ -89,19 +109,13 @@ class FilterOrderHelper
|
||||
return $r;
|
||||
}
|
||||
|
||||
public function getQueryString(): ?string
|
||||
private function getFormData(): array
|
||||
{
|
||||
return $this->getFormData()['q'];
|
||||
}
|
||||
if (null === $this->submitted) {
|
||||
$this->submitted = $this->buildForm()
|
||||
->getData();
|
||||
}
|
||||
|
||||
public function buildForm(): FormInterface
|
||||
{
|
||||
return $this->formFactory
|
||||
->createNamed($this->formName, $this->formType, $this->getDefaultData(), \array_merge([
|
||||
'helper' => $this,
|
||||
'method' => 'GET',
|
||||
'csrf_protection' => false,
|
||||
], $this->formOptions))
|
||||
->handleRequest($this->requestStack->getCurrentRequest());
|
||||
return $this->submitted;
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Templating\Listing;
|
||||
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
@@ -7,11 +14,14 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
class FilterOrderHelperBuilder
|
||||
{
|
||||
private ?array $searchBoxFields = null;
|
||||
private array $checkboxes = [];
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
private ?array $searchBoxFields = null;
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
@@ -20,16 +30,16 @@ class FilterOrderHelperBuilder
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function addSearchBox(?array $fields = [], ?array $options = []): self
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self
|
||||
{
|
||||
$this->searchBoxFields = $fields;
|
||||
$this->checkboxes[$name] = ['choices' => $choices, 'default' => $default, 'trans' => $trans];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self
|
||||
public function addSearchBox(?array $fields = [], ?array $options = []): self
|
||||
{
|
||||
$this->checkboxes[$name] = [ 'choices' => $choices, 'default' => $default, 'trans' => $trans];
|
||||
$this->searchBoxFields = $fields;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -42,12 +52,13 @@ class FilterOrderHelperBuilder
|
||||
);
|
||||
|
||||
$helper->setSearchBox($this->searchBoxFields);
|
||||
|
||||
foreach ($this->checkboxes as $name => [
|
||||
'choices' => $choices,
|
||||
'default' => $default,
|
||||
'trans' => $trans
|
||||
'trans' => $trans,
|
||||
]) {
|
||||
$helper->addCheckbox($name, $choices, $default, $trans);
|
||||
$helper->addCheckbox($name, $choices, $default, $trans);
|
||||
}
|
||||
|
||||
return $helper;
|
||||
|
@@ -1,14 +1,21 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Templating\Listing;
|
||||
|
||||
use Symfony\Component\Form\FormFactoryBuilderInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
||||
{
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
public function __construct(
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Templating\Listing;
|
||||
|
||||
interface FilterOrderHelperFactoryInterface
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Templating\Listing;
|
||||
|
||||
use Twig\Environment;
|
||||
@@ -8,27 +15,25 @@ use Twig\TwigFilter;
|
||||
|
||||
class Templating extends AbstractExtension
|
||||
{
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_render_filter_order_helper', [$this, 'renderFilterOrderHelper'], [
|
||||
'needs_environment' => true, 'is_safe' => ['html'],
|
||||
])
|
||||
];
|
||||
}
|
||||
|
||||
public function renderFilterOrderHelper(
|
||||
Environment $environment,
|
||||
FilterOrderHelper $helper,
|
||||
?string $template = '@ChillMain/FilterOrder/base.html.twig',
|
||||
?array $options = []
|
||||
) {
|
||||
return $environment->render($template, [
|
||||
'helper' => $helper,
|
||||
'form' => $helper->buildForm()->createView(),
|
||||
'options' => $options
|
||||
]);
|
||||
}
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function renderFilterOrderHelper(
|
||||
Environment $environment,
|
||||
FilterOrderHelper $helper,
|
||||
?string $template = '@ChillMain/FilterOrder/base.html.twig',
|
||||
?array $options = []
|
||||
) {
|
||||
return $environment->render($template, [
|
||||
'helper' => $helper,
|
||||
'form' => $helper->buildForm()->createView(),
|
||||
'options' => $options,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user