Browse Source

Système de notifications

tags/0.1
Guillaume 3 years ago
parent
commit
4239edaf6d
12 changed files with 152 additions and 37 deletions
  1. +19
    -8
      Controller/Admin/AbstractCrudController.php
  2. +3
    -2
      Controller/Admin/DashboardController.php
  3. +50
    -0
      EventSubscriber/EasyAdminEventSubscriber.php
  4. +1
    -0
      Resources/assets/app/adminlte/common/app.common.js
  5. +3
    -0
      Resources/assets/app/adminlte/common/common.js
  6. +24
    -13
      Resources/assets/app/adminlte/common/js/notification.js
  7. +30
    -12
      Resources/assets/app/adminlte/common/scss/_toastr.scss
  8. +2
    -2
      Resources/assets/app/adminlte/index/index.js
  9. +5
    -0
      Resources/translations/admin.fr.yaml
  10. +1
    -0
      Resources/views/adminlte/block/flash_messages.html.twig
  11. +1
    -0
      Resources/views/adminlte/crud/index.html.twig
  12. +13
    -0
      Resources/views/adminlte/layout.html.twig

+ 19
- 8
Controller/Admin/AbstractCrudController.php View File

Action::NEW => [ Action::NEW => [
'icon' => 'plus', 'icon' => 'plus',
'label' => 'Créer', 'label' => 'Créer',
'add_class'=>'btn-sm'
'add_class' => 'btn-sm'
], ],
Action::EDIT => [ Action::EDIT => [
'class' => 'btn btn-sm btn-primary', 'class' => 'btn btn-sm btn-primary',
'icon' => 'edit', 'icon' => 'edit',
'label' => false, 'label' => false,
'html_attributes'=> array('data-toggle'=> 'tooltip', 'title'=> 'Éditer')
'html_attributes' => array('data-toggle' => 'tooltip', 'title' => 'Éditer')
], ],
Action::DELETE => [ Action::DELETE => [
'icon' => 'trash', 'icon' => 'trash',
$indexChildAction = Action::new('index_children', 'Afficher les enfants', 'fa fa-list') $indexChildAction = Action::new('index_children', 'Afficher les enfants', 'fa fa-list')
->linkToCrudAction(Action::INDEX) ->linkToCrudAction(Action::INDEX)
->setLabel('') ->setLabel('')
->setHtmlAttributes(array('data-toggle'=> 'tooltip', 'title'=> 'Afficher les enfants'))
->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig') ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
->setCssClass('btn btn-sm btn-success'); ->setCssClass('btn btn-sm btn-success');


} }


if (isset($button['icon'])) { if (isset($button['icon'])) {
$action->setIcon('fa fa-'.$button['icon']);
$action->setIcon('fa fa-' . $button['icon']);
} }


if (isset($button['label'])) { if (isset($button['label'])) {
} }


if (isset($button['html_attributes']) && $button['html_attributes']) { if (isset($button['html_attributes']) && $button['html_attributes']) {
$action->setHtmlAttributes( $button['html_attributes']);
$action->setHtmlAttributes($button['html_attributes']);
} }


return $action; return $action;
$crud = parent::configureCrud($crud); $crud = parent::configureCrud($crud);


$this->setMaxResults($crud); $this->setMaxResults($crud);
if($this->isInstanceOf(SortableInterface::class)){
if ($this->isInstanceOf(SortableInterface::class)) {
$crud->setDefaultSort(['position' => 'ASC']); $crud->setDefaultSort(['position' => 'ASC']);
} }
return $crud; return $crud;
{ {
$entityClass = $this->getEntityFqcn(); $entityClass = $this->getEntityFqcn();
$paramListMaxResults = 'listMaxResults'; $paramListMaxResults = 'listMaxResults';
$paramSessionListMaxResults = $entityClass.'-'.$paramListMaxResults;
$paramSessionListMaxResults = $entityClass . '-' . $paramListMaxResults;
$requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults); $requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults);


if ($requestListMaxResults) { if ($requestListMaxResults) {
$filters $filters
); );


dump(get_defined_vars());
if ($this->isInstanceOf(TreeInterface::class)) { if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId'); $entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) { if ($entityId !== null) {
return $queryBuilder; return $queryBuilder;
} }


public function edit(AdminContext $context)
{
$response = parent::edit($context); ;

// on vide le flash bag si édition en ajax (notification déjà affichée en Javascript)
if ($context->getRequest()->isXmlHttpRequest()) {
$this->session->getFlashBag()->clear() ;
}

return $response ;
}

public function isInstanceOf(string $interfaceName): bool public function isInstanceOf(string $interfaceName): bool
{ {
return in_array($interfaceName, class_implements($this->getEntityFqcn())); return in_array($interfaceName, class_implements($this->getEntityFqcn()));

+ 3
- 2
Controller/Admin/DashboardController.php View File

'crud/paginator' => '@LcSov/adminlte/crud/paginator.html.twig', 'crud/paginator' => '@LcSov/adminlte/crud/paginator.html.twig',
'crud/edit' => '@LcSov/adminlte/crud/edit.html.twig', 'crud/edit' => '@LcSov/adminlte/crud/edit.html.twig',
'crud/new' => '@LcSov/adminlte/crud/new.html.twig', 'crud/new' => '@LcSov/adminlte/crud/new.html.twig',
//'crud/action' => '@LcSov/adminlte/crud/action/action.html.twig',
'flash_messages' => '@LcSov/adminlte/block/flash_messages.html.twig',
] ]
) )
->setFormThemes( ->setFormThemes(
'@LcSov/adminlte/crud/form_theme.html.twig', '@LcSov/adminlte/crud/form_theme.html.twig',
'@FOSCKEditor/Form/ckeditor_widget.html.twig' '@FOSCKEditor/Form/ckeditor_widget.html.twig'
] ]
);
)
;
} }


} }

+ 50
- 0
EventSubscriber/EasyAdminEventSubscriber.php View File

<?php

namespace Lc\SovBundle\EventSubscriber;

use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatableMessage;

class EasyAdminEventSubscriber implements EventSubscriberInterface
{
protected $session ;

public function __construct(SessionInterface $session)
{
$this->session = $session ;
}

public static function getSubscribedEvents(): array
{
return [
AfterEntityPersistedEvent::class => ['flashMessageAfterPersist'],
AfterEntityUpdatedEvent::class => ['flashMessageAfterUpdate'],
AfterEntityDeletedEvent::class => ['flashMessageAfterDelete'],
];
}

public function flashMessageAfterPersist(AfterEntityPersistedEvent $event): void
{
$this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.create', [
'%name%' => (string) $event->getEntityInstance(),
], 'admin'));
}

public function flashMessageAfterUpdate(AfterEntityUpdatedEvent $event): void
{
$this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.update', [
'%name%' => (string) $event->getEntityInstance(),
], 'admin'));
}

public function flashMessageAfterDelete(AfterEntityDeletedEvent $event): void
{
$this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.delete', [
'%name%' => (string) $event->getEntityInstance(),
], 'admin'));
}
}

+ 1
- 0
Resources/assets/app/adminlte/common/app.common.js View File



import { Notification } from './js/notification.js'; import { Notification } from './js/notification.js';
global.Notification = Notification; global.Notification = Notification;
Notification.init() ;


// Common // Common
import './common.scss'; import './common.scss';

+ 3
- 0
Resources/assets/app/adminlte/common/common.js View File



/* Notifications */
//Notification.init() ;

/* Tooltip */ /* Tooltip */
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();



+ 24
- 13
Resources/assets/app/adminlte/common/js/notification.js View File



export class Notification { export class Notification {


static setNotifications(notifications) {
static init() {
toastr.options.timeOut = 3000;
toastr.options.positionClass = 'toast-bottom-right';
toastr.options.onHidden = function () {
if ($('#toast-container .toast').length == 1) {
$('#toast-close-all').remove();
}
};
}

static set(notifications) {
var currentNotifications = new Array(); var currentNotifications = new Array();
for (var type in notifications) { for (var type in notifications) {
for (var key in notifications[type]) { for (var key in notifications[type]) {
if (!currentNotifications.includes(notifications[type][key])) { if (!currentNotifications.includes(notifications[type][key])) {
currentNotifications.push(notifications[type][key]); currentNotifications.push(notifications[type][key]);
self.addNotification(type, notifications[type][key]);
self.add(type, notifications[type][key]);
} }
} }
} }
} }


static addNotification(type, text) {

toastr.options.timeOut = 3000;
toastr.options.onHidden = function () {
if ($('#toast-container .toast').length == 0) $('#toast-close-all').remove();
};
static add(type, text) {


toastr[type](text); toastr[type](text);


if ($('#toast-close-all').length == 0) {
$('#toast-container').prepend('<button id="toast-close-all"><i class="fa fa-times"></i></button>');
let $container = $('#toast-container') ;
let selectorButtonCloseAll = '#toast-close-all' ;
let countMessages = $container.find('.toast').length ;

if ($(selectorButtonCloseAll).length == 0 && countMessages > 2) {
$container.prepend('<button id="toast-close-all"><i class="fa fa-times"></i></button>');
} }
$('#toast-close-all').off('click');
$('#toast-close-all').on('click', function () {
$(selectorButtonCloseAll).off('click').on('click', function () {
toastr.remove(); toastr.remove();
if ($('#toast-container .toast').length == 0) $('#toast-close-all').remove();
if (countMessages == 0) {
$('#toast-close-all').remove();
}
}); });
} }



+ 30
- 12
Resources/assets/app/adminlte/common/scss/_toastr.scss View File



#toast-container { #toast-container {
width: 350px;
width: 400px;
} }


.toast {
float: right
#toast-container .toast {
float: right ;
width: 400px ;
opacity: 1 ;
box-shadow: none !important;
-moz-box-shadow: none !important;
-o-box-shadow: none !important;
-webkit-box-shadow: none !important;

&.success {
background-color: $success !important ;
}
&.danger, &.error {
background-color: $danger !important ;
}
&.info {
background-color: $info !important ;
}
&.warning {
background-color: $warning !important ;
}
} }


#toast-container:before:hover { #toast-container:before:hover {
#toast-close-all { #toast-close-all {
border: 0; border: 0;
position: absolute; position: absolute;
bottom: 7px;
left: -15px;
pointer-events: auto; pointer-events: auto;
z-index: 999999999999999999999;
z-index: 1000;
background: #BD362F; background: #BD362F;
border-radius: 3px; border-radius: 3px;
color: #fff; color: #fff;
opacity: 0.8; opacity: 0.8;
top: 2px;
width: 50px; width: 50px;
height: 50px; height: 50px;
font-size: 30px;
left: 0px;
font-size: 20px;
text-align: center; text-align: center;
-webkit-text-shadow: 0 1px 0 #fff;
text-shadow: 0 1px 0 #fff;
-moz-box-shadow: 0 0 12px #999;
-webkit-box-shadow: 0 0 12px #999;
box-shadow: 0 0 12px #999;

//@include text-shadow(0 1px 0 #999) ;

border-radius: 50px ;
} }



+ 2
- 2
Resources/assets/app/adminlte/index/index.js View File

let toggleRequest = $.ajax({type: "POST", url: toggleUrl, data: {}, dataType: 'json'}); let toggleRequest = $.ajax({type: "POST", url: toggleUrl, data: {}, dataType: 'json'});


toggleRequest.done(function (response) { toggleRequest.done(function (response) {
Notification.addNotification('success', 'La propriété a bien été mise à jour.');
Notification.add('success', 'La propriété a bien été mise à jour.');
}); });


toggleRequest.fail(function () { toggleRequest.fail(function () {
toggle.checked = oldValue; toggle.checked = oldValue;
toggle.disabled = true; toggle.disabled = true;
toggle.closest('.checkbox-switch').classList.add('disabled'); toggle.closest('.checkbox-switch').classList.add('disabled');
Notification.addNotification('error', 'Une erreur est survenue.');
Notification.add('error', 'Une erreur est survenue.');
}); });
}); });
} }

+ 5
- 0
Resources/translations/admin.fr.yaml View File

content_admin:
flash_message:
create: Le contenu "%name%" a été créé avec succès.
update: Le contenu "%name%" a été mis à jour avec succès.
delete: Le contenu "%name%" a été supprimé avec succès.

+ 1
- 0
Resources/views/adminlte/block/flash_messages.html.twig View File

{# Gérés en Javascript dans layout.html.twig #}

+ 1
- 0
Resources/views/adminlte/crud/index.html.twig View File

{# @var entities \EasyCorp\Bundle\EasyAdminBundle\Collection\EntityDtoCollection #} {# @var entities \EasyCorp\Bundle\EasyAdminBundle\Collection\EntityDtoCollection #}
{# @var paginator \EasyCorp\Bundle\EasyAdminBundle\Orm\EntityPaginator #} {# @var paginator \EasyCorp\Bundle\EasyAdminBundle\Orm\EntityPaginator #}
{% extends ea.templatePath('layout') %} {% extends ea.templatePath('layout') %}

{% trans_default_domain ea.i18n.translationDomain %} {% trans_default_domain ea.i18n.translationDomain %}


{% block body_id entities|length > 0 ? 'ea-index-' ~ entities|first.name : '' %} {% block body_id entities|length > 0 ? 'ea-index-' ~ entities|first.name : '' %}

+ 13
- 0
Resources/views/adminlte/layout.html.twig View File



{% include '@LcSov/adminlte/filemanager/file-manager-modal.html.twig' %} {% include '@LcSov/adminlte/filemanager/file-manager-modal.html.twig' %}


{% if app.session is not null and app.session.started %}
{% set flash_messages = app.session.flashbag.all %}
{% if flash_messages|length > 0 %}
{% for label, messages in flash_messages %}
{% for message in messages %}
<script type="text/javascript">
Notification.add("{{ label }}", "{{ message|trans|raw|replace({'"': '\"'}) }}") ;
</script>
{% endfor %}
{% endfor %}
{% endif %}
{% endif %}

</body> </body>
{% endblock body %} {% endblock body %}
</html> </html>

Loading…
Cancel
Save