Notification dropdown symfony

265 views Asked by At

I have a notification icon. When user clicks on it, a dropdown appears and shows a list. I want to filter list to only show what's related to the connected user.

{% if app.user.roles[0] == "ROLE_DEVELOPPER"%}
<!-- dropdown notification message -->
<li class="dropdown">
    <a title="Notifications" href="#fakelink" class="dropdown-toggle" data-toggle="dropdown">
        <strong> <i class="fa fa-bell"></i></strong>

    </a>
    {% if notif_conges|length > 0 %}
    <ul class="dropdown-menu animated half flipInX">
        //this section
        {% for notif_c in notif_conges %}

        {% if  notif_c.etat == "Acceptée" %}
        <li><a>Votre demande : "{{notif_c.motif}}" Envoyée le "{{notif_c.CreatedAt|date('Y-m-d H:i:s')}}" a était traitée </a></li>
        {% endif %}

        {% endfor %}

    </ul>
    {% endif %}

</li>
{% endif %}
2

There are 2 answers

2
Alexandre Tranchant On BEST ANSWER

You want to filter notifications. Only notifications concerning connected users have to be displayed. You have two solutions:

The (very) bad way: Add a test in view

    <ul class="dropdown-menu animated half flipInX">
        //this section
        {% for notif_c in notif_conges %}

        {% if  notif_c.etat == "Acceptée" and notif_c.user = app.user %}
        <li><a>Votre demande : "{{notif_c.motif}}" Envoyée le "{{notif_c.CreatedAt|date('Y-m-d H:i:s')}}" a était traitée </a></li>
        {% endif %}

        {% endfor %}

    </ul>

This is a bad way because you will forward all notifications to the view

The bad way: Add a filter in your controller

class YourController
{
    public yourAction()
    {
       /** ... your code to handle notifications **/
       foreach ($notifications as $notification) {
          if ($notification->getUser()->getId() == $this->getUser()->getId)) {
             $filteredNotification[] = $notification;
          }
       }
       return $this->render('your_template_file', [
           'notif_c' => $filteredNotifications
       ]);
    }
}

This is still a bad way because you are retrieving all notifications from database.

The good way: Ask only the necessaries notification to your repository Assuming, your notifications are retrieving with Doctrine, repository can filter data to only retrieve pertinent data.

class YourController
{
    public yourAction()
    {
       /** ... your code is certainly something lke this: **/
         $notificationRepository = $this->entityManager->getRepository('AppBundle:Notification');
         $notifications = $notificationRepository->findAll();
         render 

       return $this->render('your_template_file', [
           'notif_c' => $notifications
       ]);
    }
}

Replace findAll() by findBy(array $criteria) and add a criteria as first parameter:

class YourController
{
    public yourAction()
    {
         $notificationRepository = $this->entityManager->getRepository('AppBundle:Notification');
         $notifications = $notificationRepository->findBy([
           'etat' => 'Acceptée',
           'user' => $this->getUser(), //this could be a little different, I do not remember how to handle user in Sf2.8
         ]);
         render 

       return $this->render('your_template_file', [
           'notif_c' => $notifications
       ]);
    }
}
0
Pablo Lozano On

Yo must use is_granted twig filter.

You can specify the user role that you one but IS_AUTHENTICATED_REMEMBERED role is given to all users who are logged in.

{% is_granted('IS_AUTHENTICATED_REMEMBERED') %}
<!-- dropdown notification message -->

...

{% endif %}