You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 line
3.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  4. use Lc\ShopBundle\Context\MerchantInterface;
  5. use Symfony\Component\Security\Core\Security;
  6. class AdminController extends EasyAdminController
  7. {
  8. protected $security;
  9. public function __construct(Security $security)
  10. {
  11. $this->security = $security;
  12. }
  13. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  14. {
  15. $parameters = array_merge(
  16. $parameters,
  17. $this->getTwigExtraParameters()
  18. ) ;
  19. return parent::renderTemplate($actionName, $templatePath, $parameters) ;
  20. }
  21. public function getTwigExtraParameters()
  22. {
  23. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  24. $merchants = $repositoryMerchant->findAll() ;
  25. $user = $this->security->getUser() ;
  26. return [
  27. 'merchants' => $merchants,
  28. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  29. ] ;
  30. }
  31. public function updateEntity($entity)
  32. {
  33. $this->setUpdated($entity);
  34. parent::updateEntity($entity);
  35. }
  36. public function persistEntity($entity)
  37. {
  38. if (method_exists($entity, 'setMerchant')) {
  39. $entity->setMerchant($this->security->getUser()->getMerchant());
  40. }
  41. if (method_exists($entity, 'setCreatedAt')) {
  42. $entity->setCreatedAt(new \DateTime());
  43. }
  44. if (method_exists($entity, 'setCreatedAt')) {
  45. $entity->setCreatedBy($this->security->getUser());
  46. }
  47. if (method_exists($entity, 'getAddress')
  48. && method_exists($entity->getAddress(), 'setCreatedBy')) {
  49. $entity->getAddress()->setCreatedBy($this->security->getUser());
  50. }
  51. $this->setUpdated($entity);
  52. if (method_exists($entity, 'setPosition')) {
  53. $entity->setPosition(0);
  54. }
  55. if (method_exists($entity, 'setStatus')) {
  56. $entity->setStatus(1);
  57. }
  58. parent::persistEntity($entity);
  59. }
  60. public function setUpdated($entity)
  61. {
  62. if (method_exists($entity, 'setUpdatedAt')) {
  63. $entity->setUpdatedAt(new \DateTime());
  64. }
  65. if (method_exists($entity, 'setUpdatedAt')) {
  66. $entity->setUpdatedBy($this->security->getUser());
  67. }
  68. if (method_exists($entity, 'getAddress')
  69. && method_exists($entity->getAddress(), 'setUpdatedBy')) {
  70. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  71. }
  72. }
  73. }