mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
add more filtering possibilities with order helper
This commit is contained in:
parent
aea5e9b1d7
commit
97dbc4bc16
@ -32,6 +32,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
$checkboxesBuilder = $builder->create('checkboxes', null, [ 'compound' => true ]);
|
||||
foreach ($helper->getCheckboxes() as $name => $c) {
|
||||
|
||||
$choices = \array_combine(
|
||||
@ -42,17 +43,21 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
|
||||
$c['choices']
|
||||
);
|
||||
|
||||
$builder->add('c_'.$name, ChoiceType::class, [
|
||||
$checkboxesBuilder->add($name, ChoiceType::class, [
|
||||
'choices' => $choices,
|
||||
'expanded' => true,
|
||||
'multiple' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
if (0 < count($helper->getCheckboxes())) {
|
||||
$builder->add($checkboxesBuilder);
|
||||
}
|
||||
|
||||
foreach ($this->requestStack->getCurrentRequest()->query->getIterator() as $key => $value) {
|
||||
switch($key) {
|
||||
case 'q':
|
||||
case 'c_'.$key:
|
||||
case 'checkboxes'.$key:
|
||||
break;
|
||||
case 'page':
|
||||
$builder->add($key, HiddenType::class, [
|
||||
@ -75,7 +80,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
|
||||
$view->vars['has_search_box'] = $helper->hasSearchBox();
|
||||
$view->vars['checkboxes'] = [];
|
||||
foreach ($helper->getCheckboxes() as $name => $c) {
|
||||
$view->vars['checkboxes']['c_'.$name] = [];
|
||||
$view->vars['checkboxes'][$name] = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,17 +10,30 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% for checkbox_name, options in form.vars.checkboxes %}
|
||||
{% if form.checkboxes|length > 0 %}
|
||||
{% for checkbox_name, options in form.checkboxes %}
|
||||
<div class="row gx-0">
|
||||
<div class="col-md-12">
|
||||
<div class="col-md-11">
|
||||
{{ form_widget(form[checkbox_name]) }}
|
||||
</div>
|
||||
{% if loop.last %}
|
||||
<div class="col-md-1">
|
||||
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% for c in form['checkboxes'][checkbox_name].children %}
|
||||
<div class="form-check form-check-inline">
|
||||
{{ form_widget(c) }}
|
||||
{{ form_label(c) }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% if loop.last %}
|
||||
<div class="row gx-0">
|
||||
<div class="col-md-12">
|
||||
<ul class="record_actions">
|
||||
<li>
|
||||
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{{ form_end(form) }}
|
||||
|
@ -13,6 +13,11 @@ class FilterOrderHelper
|
||||
private RequestStack $requestStack;
|
||||
private ?array $searchBoxFields = null;
|
||||
private array $checkboxes = [];
|
||||
private ?array $submitted = null;
|
||||
private ?string $formName = 'filter';
|
||||
private string $formType = FilterOrderType::class;
|
||||
private array $formOptions = [];
|
||||
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
@ -45,10 +50,9 @@ class FilterOrderHelper
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCheckbox(string $name): array
|
||||
public function getCheckboxData(string $name): array
|
||||
{
|
||||
return $this->requestStack->getCurrentRequest()
|
||||
->query->get('c_'.$name, $this->checkboxes[$name]['default']);
|
||||
return $this->getFormData()['checkboxes'][$name];
|
||||
}
|
||||
|
||||
public function getCheckboxes(): array
|
||||
@ -63,12 +67,23 @@ class FilterOrderHelper
|
||||
|
||||
private function getFormData(): array
|
||||
{
|
||||
$r = [
|
||||
'q' => $this->getQueryString(),
|
||||
];
|
||||
if (NULL === $this->submitted) {
|
||||
$this->submitted = $this->buildForm()
|
||||
->getData();
|
||||
}
|
||||
|
||||
return $this->submitted;
|
||||
}
|
||||
|
||||
private function getDefaultData(): array
|
||||
{
|
||||
$r = [];
|
||||
|
||||
if ($this->hasSearchBox()) {
|
||||
$r['q'] = '';
|
||||
}
|
||||
foreach ($this->checkboxes as $name => $c) {
|
||||
$r[$name] = $this->getCheckbox($name);
|
||||
$r['checkboxes'][$name] = $c['default'];
|
||||
}
|
||||
|
||||
return $r;
|
||||
@ -76,21 +91,17 @@ class FilterOrderHelper
|
||||
|
||||
public function getQueryString(): ?string
|
||||
{
|
||||
$q = $this->requestStack->getCurrentRequest()
|
||||
->query->get('q', null);
|
||||
|
||||
return empty($q) ? NULL : $q;
|
||||
return $this->getFormData()['q'];
|
||||
}
|
||||
|
||||
public function buildForm($name = null, string $type = FilterOrderType::class, array $options = []): FormInterface
|
||||
public function buildForm(): FormInterface
|
||||
{
|
||||
$form = $this->formFactory
|
||||
->createNamed($name, $type, $this->getFormData(), \array_merge([
|
||||
return $this->formFactory
|
||||
->createNamed($this->formName, $this->formType, $this->getDefaultData(), \array_merge([
|
||||
'helper' => $this,
|
||||
'method' => 'GET',
|
||||
'csrf_protection' => false,
|
||||
], $options));
|
||||
|
||||
return $form;
|
||||
], $this->formOptions))
|
||||
->handleRequest($this->requestStack->getCurrentRequest());
|
||||
}
|
||||
}
|
||||
|
@ -532,14 +532,18 @@ final class SingleTaskController extends AbstractController
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
|
||||
$filterOrder = $this->buildFilterOrder();
|
||||
$flags = \array_merge(
|
||||
$filterOrder->getCheckboxData('status'),
|
||||
\array_map(fn ($i) => 'state_'.$i, $filterOrder->getCheckboxData('states'))
|
||||
);
|
||||
$nb = $this->singleTaskAclAwareRepository->countByCurrentUsersTasks(
|
||||
$filterOrder->getQueryString(),
|
||||
$filterOrder->getCheckbox('status')
|
||||
$flags
|
||||
);
|
||||
$paginator = $this->paginatorFactory->create($nb);
|
||||
$tasks = $this->singleTaskAclAwareRepository->findByCurrentUsersTasks(
|
||||
$filterOrder->getQueryString(),
|
||||
$filterOrder->getCheckbox('status'),
|
||||
$flags,
|
||||
$paginator->getCurrentPageFirstItemNumber(),
|
||||
$paginator->getItemsPerPage()
|
||||
);
|
||||
@ -555,14 +559,19 @@ final class SingleTaskController extends AbstractController
|
||||
{
|
||||
$statuses = ['no-alert', 'warning', 'alert'];
|
||||
$statusTrans = [
|
||||
'Tasks without alert',
|
||||
'Tasks near deadline',
|
||||
'Tasks over deadline',
|
||||
'Tasks without alert',
|
||||
];
|
||||
$states = [
|
||||
// todo: get a list of possible states dynamically
|
||||
'new', 'in_progress', 'closed', 'canceled'
|
||||
];
|
||||
return $this->filterOrderHelperFactory
|
||||
->create(self::class)
|
||||
->addSearchBox()
|
||||
->addCheckbox('status', $statuses, $statuses, $statusTrans)
|
||||
->addCheckbox('states', $states, ['new', 'in_progress'])
|
||||
->build()
|
||||
;
|
||||
}
|
||||
|
@ -63,16 +63,19 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
|
||||
if (!empty($pattern)) {
|
||||
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
|
||||
->setParameter('pattern', $pattern)
|
||||
->setParameter('pattern', '%'.$pattern.'%')
|
||||
;
|
||||
}
|
||||
|
||||
if (count($flags) > 0) {
|
||||
$orX = $qb->expr()->orX();
|
||||
$orXDate = $qb->expr()->orX();
|
||||
$orXState = $qb->expr()->orX();
|
||||
$now = new \DateTime();
|
||||
|
||||
if (\in_array('no-alert', $flags)) {
|
||||
$orX
|
||||
foreach ($flags as $key => $flag) {
|
||||
switch ($flag) {
|
||||
case 'no-alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNull('t.endDate'),
|
||||
@ -81,45 +84,52 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
);
|
||||
$qb
|
||||
->setParameter('intervalBlank', new \DateInterval('P0D'))
|
||||
->setParameter('now', $now)
|
||||
;
|
||||
}
|
||||
|
||||
if (\in_array('warning', $flags)) {
|
||||
$orX
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'warning':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->eq('t.closed', "'FALSE'"),
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->not($qb->expr()->isNull('t.warningInterval')),
|
||||
$qb->expr()->lte('t.endDate - t.warningInterval', ':now')
|
||||
$qb->expr()->gte('t.endDate - t.warningInterval', ':now'),
|
||||
$qb->expr()->lt('t.endDate', ':now')
|
||||
)
|
||||
)
|
||||
;
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now)
|
||||
;
|
||||
}
|
||||
|
||||
if (\in_array('alert', $flags)) {
|
||||
$orX
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->eq('t.closed', "'FALSE'"),
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->lte('t.endDate', ':now')
|
||||
)
|
||||
)
|
||||
;
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now)
|
||||
;
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case \substr($flag, 0, 6) === 'state_':
|
||||
$state = \substr($flag, 6);
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_EXISTS_IN_ARRAY(t.currentStates, :state_$key) = 'TRUE'"
|
||||
);
|
||||
$qb->setParameter("state_$key", $state);
|
||||
break;
|
||||
default:
|
||||
throw new \LogicException("this flag is not supported: $flag");
|
||||
}
|
||||
}
|
||||
|
||||
$qb->andWhere($orX);
|
||||
if ($orXDate->count() > 0) {
|
||||
$qb->andWhere($orXDate);
|
||||
}
|
||||
if ($orXState->count() > 0) {
|
||||
$qb->andWhere($orXState);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ Are you sure you want to start this task ?: Êtes-vous sûrs de vouloir démarre
|
||||
|
||||
Tasks near deadline: Tâches à échéance proche
|
||||
Tasks over deadline: Tâches à échéance dépassée
|
||||
Tasks without alert: Tâches sans alerte
|
||||
Tasks without alert: Tâches à échéance future ou sans échéance
|
||||
|
||||
#title
|
||||
My tasks near deadline: Mes tâches à échéance proche
|
||||
|
Loading…
x
Reference in New Issue
Block a user