Browse Source

Refactoring Container

feature/symfony6.1
Fab 2 years ago
parent
commit
af34d2fe60
4 changed files with 169 additions and 46 deletions
  1. +47
    -38
      Controller/ControllerTrait.php
  2. +44
    -8
      Controller/User/UserAdminController.php
  3. +15
    -0
      Repository/AbstractRepositoryQuery.php
  4. +63
    -0
      Resources/views/adminlte/crud/delete.html.twig

+ 47
- 38
Controller/ControllerTrait.php View File

@@ -43,54 +43,54 @@ trait ControllerTrait
//TODO si vous avez une erreur avec le :array c'est qu'il faut passer à symfony 5.4.
//Suivez la procédure suivante : https://symfony.com/doc/current/setup/unstable_versions.html#upgrading-your-project-to-an-unstable-symfony-version
//En gros il faut changer tout les dépendances symfony/ qui sont en 5.3 par 5.4 dans composer.json
public static function getSubscribedServices() :array
public static function getSubscribedServices(): array
{
return array_merge(
parent::getSubscribedServices(),
ApplicationDefinition::getSubscribedContainerServices(),
[
Environment::class => Environment::class,
Security::class => Security::class,
EntityManagerInterface::class => EntityManagerInterface::class,
UrlGeneratorInterface::class => UrlGeneratorInterface::class,
SessionInterface::class => SessionInterface::class,
PaginatorInterface::class => PaginatorInterface::class,
RequestStack::class => RequestStack::class,
EventDispatcherInterface::class => EventDispatcherInterface::class,
LoggerInterface::class => LoggerInterface::class,
ParameterBagInterface::class => ParameterBagInterface::class,
TranslatorInterface::class => TranslatorInterface::class,
PdfGenerator::class => PdfGenerator::class,
TranslatorAdmin::class => TranslatorAdmin::class,
FilterManager::class => FilterManager::class,
FlashBagTranslator::class => FlashBagTranslator::class,
MailjetTransport::class => MailjetTransport::class,
AdminUrlGenerator::class => AdminUrlGenerator::class,
SettingSolver::class => SettingSolver::class,
ComponentContainer::class => ComponentContainer::class,
EntityRepository::class => EntityRepository::class,
FileComponent::class => FileComponent::class,
]
parent::getSubscribedServices(),
ApplicationDefinition::getSubscribedContainerServices(),
[
ApplicationDefinition::class => ApplicationDefinition::class,
Environment::class => Environment::class,
Security::class => Security::class,
EntityManagerInterface::class => EntityManagerInterface::class,
UrlGeneratorInterface::class => UrlGeneratorInterface::class,
SessionInterface::class => SessionInterface::class,
PaginatorInterface::class => PaginatorInterface::class,
RequestStack::class => RequestStack::class,
EventDispatcherInterface::class => EventDispatcherInterface::class,
LoggerInterface::class => LoggerInterface::class,
ParameterBagInterface::class => ParameterBagInterface::class,
TranslatorInterface::class => TranslatorInterface::class,
PdfGenerator::class => PdfGenerator::class,
TranslatorAdmin::class => TranslatorAdmin::class,
FilterManager::class => FilterManager::class,
FlashBagTranslator::class => FlashBagTranslator::class,
MailjetTransport::class => MailjetTransport::class,
AdminUrlGenerator::class => AdminUrlGenerator::class,
SettingSolver::class => SettingSolver::class,
ComponentContainer::class => ComponentContainer::class,
EntityRepository::class => EntityRepository::class,
FileComponent::class => FileComponent::class,
]
);
}

public function addFlashTranslator(
string $type,
$translationKeyName,
$translationEntityName = null,
$translationParam = array()
): void
{
string $type,
$translationKeyName,
$translationEntityName = null,
$translationParam = array()
): void {
if ($translationEntityName === null && method_exists($this, 'getTranslationEntityName')) {
$translationEntityName = $this->getTranslationEntityName();
}


$this->get(FlashBagTranslator::class)->add(
$type,
$translationKeyName,
$translationEntityName,
$translationParam
$type,
$translationKeyName,
$translationEntityName,
$translationParam
);
}

@@ -99,8 +99,12 @@ trait ControllerTrait
return in_array($interfaceName, class_implements($this->getEntityFqcn()));
}

public function generateEaUrl(string $controller = null, string $action = null, int $entityId = null, array $extraParam = array()): string
{
public function generateEaUrl(
string $controller = null,
string $action = null,
int $entityId = null,
array $extraParam = array()
): string {
$adminUrlGenerator = $this->get(AdminUrlGenerator::class);
if ($controller) {
$adminUrlGenerator->setController($controller);
@@ -129,6 +133,11 @@ trait ControllerTrait
return $this->get(Environment::class);
}

public function getApplicationDefinition(): ApplicationDefinition
{
return $this->get(ApplicationDefinition::class);
}

public function getEntityManager(): EntityManagerInterface
{
return $this->get(EntityManagerInterface::class);

+ 44
- 8
Controller/User/UserAdminController.php View File

@@ -6,7 +6,9 @@ use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
@@ -22,6 +24,7 @@ use Lc\SovBundle\Definition\ApplicationDefinition;
use Lc\SovBundle\Definition\RolesDefinition;
use Lc\SovBundle\Definition\RolesDefinitionInterface;
use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Doctrine\Extension\BlameableInterface;
use Lc\SovBundle\Factory\User\UserFactory;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -98,23 +101,56 @@ abstract class UserAdminController extends AbstractAdminController
return $event->getResponse();
}

$entityInstance = $event->getEntityInstance();

ApplicationDefinition::getSubscribedContainerServices();
$user = $event->getEntityInstance();


$metas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
foreach ($metas as $meta) {
$entities[] = $meta->getName();
$entityFqcnList[] = $meta->getName();
}


// Creéer formulaire avec un champ confirm

//si clqiue suppression
$entitiesToDelete = array();
foreach ($entityFqcnList as $entityFqcn){
if(!is_null($this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn))) {
$repositoryQuery = $this->get(
$this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn)
)->getRepositoryQuery();

if (new $entityFqcn instanceof BlameableInterface) {
//CreatedBy
$query = $repositoryQuery->create();
$query->filterByCreatedBy($user);
$entitiesToDelete[$entityFqcn]['nullify']['createdBy'] = $query->find();

//UpdatedBy
$query = $repositoryQuery->create();
$query->filterByUpdatedBy($user);
$entitiesToDelete[$entityFqcn]['nullify']['updatedBy'] = $query->find();
}
}
dump($entityFqcn);
}

foreach ($entities as $entity){
dump($entity);
dump($context->getCrudControllers());

$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
'pageName' => Crud::PAGE_DETAIL,
'templatePath' => '@LcSov/adminlte/crud/delete.html.twig',
'global_actions' => array(),
'batch_actions' => array(),
'entities_delete' => $entitiesToDelete,
]));

$event = new AfterCrudActionEvent($context, $responseParameters);
$this->get('event_dispatcher')->dispatch($event);
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
die();

return $responseParameters;


}

+ 15
- 0
Repository/AbstractRepositoryQuery.php View File

@@ -6,6 +6,7 @@ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\User\UserInterface;

abstract class AbstractRepositoryQuery implements RepositoryQueryInterface
{
@@ -147,6 +148,20 @@ abstract class AbstractRepositoryQuery implements RepositoryQueryInterface
->setParameter('id', $id);
}

public function filterByCreatedBy(UserInterface $user):self
{
return $this
->andWhere('.createdBy = :user')
->setParameter('user', $user);
}

public function filterByUpdatedBy(UserInterface $user):self
{
return $this
->andWhere('.updatedBy = :user')
->setParameter('user', $user);
}

public function andWhereEqual($field, $value)
{
return $this->andWhere('.'.$field.' = :'.$field)->setParameter($field, $value);

+ 63
- 0
Resources/views/adminlte/crud/delete.html.twig View File

@@ -0,0 +1,63 @@
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var entities \EasyCorp\Bundle\EasyAdminBundle\Collection\EntityDtoCollection #}
{# @var paginator \EasyCorp\Bundle\EasyAdminBundle\Orm\EntityPaginator #}
{% extends ea.templatePath('layout') %}

{% trans_default_domain ea.i18n.translationDomain %}

{% block content_title %}
{{ 'delete'|sov_trans_admin_title(translation_entity_name, {id: ea.getEntity().getInstance().getId()}) }}
{% endblock %}

{% block content_breadcrumb %}
{{ include('@LcSov/adminlte/block/breadcrumb.html.twig') }}
{% endblock content_breadcrumb %}


{% block page_actions %}
{% endblock page_actions %}

{% block main %}
{% block main_prepend %}{% endblock %}
<div class="col-12" id="{% block detail_id %}{% endblock detail_id %}">
{% block card_wrapper %}
<div class="card card-outline card-primary">
{% block card_header_wrapper %}
<div class="card-header">
<div class="btn-list float-sm-right">
</div>
</div>
{% endblock %}
{% block card_body_wrapper %}
<div class="card-body">
{% block detail_fields %}
{% for entity_fqcn, entity_actions in entities_delete %}
<div class="callout callout-danger">
<h5>{{ entity_fqcn }}</h5>
{% for action, entity_field_list in entity_actions %}
{% for field, entity_list in entity_field_list %}
<p>{{ entity_list|length }} entités à {{ action }} sur le champ {{ field }}</p>
{% endfor %}
{% endfor %}
</div>
{% endfor %}
{% endblock %}
</div>
{% endblock %}
{% block card_footer_wrapper %}
<div class="card-footer">
<div class="row">
{# {% block delete_form %} #}
{# {{ include('@EasyAdmin/crud/includes/_delete_form.html.twig', { entity_id: entity.primaryKeyValue }, with_context = false) }} #}
{# {% endblock delete_form %} #}
</div>
</div>
{% endblock %}


</div>
{% endblock card_wrapper %}
</div>
{% block main_append %}{% endblock %}

{% endblock main %}

Loading…
Cancel
Save