Skip to content

Commit

Permalink
Event widget: allow multiple filter by event kind
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 15, 2023
1 parent 5171708 commit efc6fe1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
7 changes: 3 additions & 4 deletions app/Resources/views/event/_partial/widget.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
{% block content %}
{% if title %}
<p style="margin-top:0;">
liste des prochains événements
{% if eventKind %}<strong>{{ eventKind.name }}</strong>{% endif %}
{% if maxDate %}(jusqu'au {{ maxDate | date_short }}){% endif %}
Liste des prochains événements
{% if date_max %}(jusqu'au {{ date_max | date_short }}){% endif %}
</p>
{% endif %}
<ul style="margin:0;">
Expand All @@ -20,7 +19,7 @@
<span>[en cours]</span>
{% endif %}
{% endif %}
{# {% if not eventKind %}[{{ event.kind }}] {% endif %} #}
{# {% if not event_kind_id_list %}[{{ event.kind }}] {% endif %} #}
</li>
{% endfor %}
</ul>
Expand Down
11 changes: 8 additions & 3 deletions src/AppBundle/Controller/AdminEventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function widgetGeneratorAction(Request $request)
'label' => "Quel type d'événement ?",
'class' => 'AppBundle:EventKind',
'choice_label' => 'name',
'multiple' => false,
'multiple' => true,
'required' => false
))
->add('date_max', TextType::class, array(
Expand Down Expand Up @@ -461,8 +461,13 @@ public function widgetGeneratorAction(Request $request)

if ($form->handleRequest($request)->isValid()) {
$data = $form->getData();

$widgetQueryString = 'event_kind_id=' . ($data['kind'] ? $data['kind']->getId() : '') . '&date_max=' . ($data['date_max'] ? $data['date_max'] : '') . '&limit=' . ($data['limit'] ? $data['limit'] : '') . '&title=' . ($data['title'] ? 1 : 0) . '&links=' . ($data['links'] ? 1 : 0);
$widgetQueryString = '';
if ($data['kind']) {
foreach ($data['kind'] as $kind) {
$widgetQueryString .= 'event_kind_id[]=' . $kind->getId() . '&';
}
}
$widgetQueryString .= 'date_max=' . ($data['date_max'] ? $data['date_max'] : '') . '&limit=' . ($data['limit'] ? $data['limit'] : '') . '&title=' . ($data['title'] ? 1 : 0) . '&links=' . ($data['links'] ? 1 : 0);

return $this->render('admin/event/widget_generator.html.twig', array(
'form' => $form->createView(),
Expand Down
21 changes: 11 additions & 10 deletions src/AppBundle/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,32 @@ public function widgetAction(Request $request)
$em = $this->getDoctrine()->getManager();

$buckets = array();
$eventKind = null;
$eventDateMax = null;
$filter_event_kind_id_list = null;
$filter_date_max_cleaned = null;

$filter_title = $request->query->has('title') ? ($request->get('title') == 1) : false;
$filter_links = $request->query->has('links') ? ($request->get('links') == 1) : false;
$filter_date_max = $request->query->has('date_max') ? ($request->get('date_max') ? new DateTime($request->get('date_max')) : null) : null;
if ($filter_date_max) {
$eventDateMax = clone($filter_date_max);
$eventDateMax->modify('+1 day'); // also return events happening on max date
$filter_date_max_cleaned = clone($filter_date_max);
$filter_date_max_cleaned->modify('+1 day'); // also return events happening on max date
}
$filter_limit = $request->query->has('limit') ? ($request->get('limit') ? $request->get('limit') : null) : null;

$filter_event_kind_id = $request->get('event_kind_id');
if ($filter_event_kind_id) {
$eventKind = $em->getRepository('AppBundle:EventKind')->find($filter_event_kind_id);
$filter_event_kind_id_list = $request->get('event_kind_id'); // should be an array (or null)
if (is_string($filter_event_kind_id_list)) { // manage case if string (retro-compatibility)
$filter_event_kind_id_list = array($filter_event_kind_id_list);
}
$filter_event_kind_id_list = array_filter($filter_event_kind_id_list); // remove any empty values

$events = $em->getRepository('AppBundle:Event')->findFutureOrOngoing($eventKind, false, $eventDateMax, $filter_limit);
$events = $em->getRepository('AppBundle:Event')->findFutureOrOngoing($filter_event_kind_id_list, false, $filter_date_max_cleaned, $filter_limit);

return $this->render('event/_partial/widget.html.twig', [
'events' => $events,
'eventKind' => $eventKind,
'title' => $filter_title,
'links' => $filter_links,
'maxDate' => $filter_date_max,
'date_max' => $filter_date_max,
'event_kind_id_list' => $filter_event_kind_id_list
]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/AppBundle/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public function findAll()
return $this->findBy(array(), array('date' => 'DESC'));
}

public function findFutureOrOngoing(EventKind $eventKind = null, bool $dislayedHome = false, \DateTime $max = null, int $limit = null)
public function findFutureOrOngoing(array $eventKinds = null, bool $dislayedHome = false, \DateTime $max = null, int $limit = null)
{
$qb = $this->createQueryBuilder('e')
->leftJoin('e.kind', 'ek')
->addSelect('ek')
->where('e.date > :now OR e.date < :now AND e.end IS NOT NULL AND e.end > :now')
->setParameter('now', new \Datetime('now'));

if ($eventKind) {
if ($eventKinds) {
$qb
->andwhere('e.kind = :kind')
->setParameter('kind', $eventKind);
->andwhere('e.kind in (:kinds)')
->setParameter('kinds', $eventKinds);
}

if ($dislayedHome) {
Expand Down

0 comments on commit efc6fe1

Please sign in to comment.