您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

108 行
3.6KB

  1. <?php
  2. /**
  3. * @author La clic ! <contact@laclic.fr>
  4. */
  5. namespace Lc\CaracoleBundle\Resolver;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  9. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  10. use Lc\SovBundle\Resolver\UrlResolver;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Security;
  13. class SectionResolver
  14. {
  15. protected bool $isCachedSection = false;
  16. protected ?SectionInterface $section = null;
  17. protected EntityManagerInterface $entityManager;
  18. protected MerchantResolver $merchantResolver;
  19. protected SectionStore $sectionStore;
  20. protected RequestStack $requestStack;
  21. protected UrlResolver $urlResolver;
  22. public function __construct(
  23. EntityManagerInterface $entityManager,
  24. MerchantResolver $merchantResolver,
  25. SectionStore $sectionStore,
  26. RequestStack $requestStack,
  27. UrlResolver $urlResolver
  28. )
  29. {
  30. $this->entityManager = $entityManager;
  31. $this->merchantResolver = $merchantResolver;
  32. $this->sectionStore = $sectionStore;
  33. $this->requestStack = $requestStack;
  34. $this->urlResolver = $urlResolver;
  35. }
  36. public function getCurrent()
  37. {
  38. $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();
  39. // admin
  40. if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') {
  41. //dump($requestAttributesArray);
  42. if (!$this->isCachedSection) {
  43. $currentAdminSection = null;
  44. $userMerchant = $this->merchantResolver->getUserMerchant();
  45. if ($userMerchant !== null) {
  46. $currentAdminSection = $userMerchant->getCurrentAdminSection();
  47. }
  48. /*if ($currentAdminSection === null) {
  49. $currentAdminSection = $this->sectionStore
  50. ->setMerchant($userMerchant->getMerchant())
  51. ->getOneDefault();
  52. if ($currentAdminSection === null) {
  53. throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
  54. }
  55. }*/
  56. $this->isCachedSection = true;
  57. $this->section = $currentAdminSection;
  58. return $currentAdminSection;
  59. }else{
  60. return $this->section;
  61. }
  62. } // front
  63. else {
  64. dump('front');
  65. dump($requestAttributesArray);
  66. if($this->section === null) {
  67. $merchantCurrent = $this->merchantResolver->getCurrent();
  68. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  69. $sectionCurrent = null;
  70. $sectionDefault = $sectionStore->getOneDefault();
  71. if (isset($requestAttributesArray['section'])) {
  72. $sectionCurrent = $sectionStore
  73. ->setMerchant($merchantCurrent)
  74. ->getOneBySlug($requestAttributesArray['section']);
  75. }
  76. $this->section = $sectionCurrent ?: $sectionDefault;
  77. }
  78. return $this->section;
  79. }
  80. }
  81. public function isOutOfSection(): bool
  82. {
  83. return $this->getCurrent() == null;
  84. }
  85. public function getDefault(): SectionInterface
  86. {
  87. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  88. }
  89. }