Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

130 lines
5.4KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
  5. use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
  6. use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
  9. use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
  10. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  11. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  12. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  13. use Lc\SovBundle\Solver\Setting\SettingSolver;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. class SettingEventSubscriber implements EventSubscriberInterface
  17. {
  18. protected EntityManagerInterface $entityManager;
  19. protected MerchantSettingDefinitionInterface $merchantSettingDefinition;
  20. protected SectionSettingDefinitionInterface $sectionSettingDefinition;
  21. protected MerchantStore $merchantStore;
  22. protected SectionStore $sectionStore;
  23. protected MerchantSettingFactory $merchantSettingFactory;
  24. protected SectionSettingFactory $sectionSettingFactory;
  25. protected SettingSolver $settingSolver;
  26. public function __construct(
  27. EntityManagerInterface $entityManager,
  28. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  29. SectionSettingDefinitionInterface $sectionSettingDefinition,
  30. MerchantStore $merchantStore,
  31. SectionStore $sectionStore,
  32. MerchantSettingFactory $merchantSettingFactory,
  33. SectionSettingFactory $sectionSettingFactory,
  34. SettingSolver $settingSolver
  35. ) {
  36. $this->entityManager = $entityManager;
  37. $this->merchantStore = $merchantStore;
  38. $this->sectionStore = $sectionStore;
  39. $this->merchantSettingDefinition = $merchantSettingDefinition;
  40. $this->sectionSettingDefinition = $sectionSettingDefinition;
  41. $this->merchantSettingFactory = $merchantSettingFactory;
  42. $this->sectionSettingFactory = $sectionSettingFactory;
  43. $this->settingSolver = $settingSolver;
  44. }
  45. public static function getSubscribedEvents()
  46. {
  47. return [
  48. KernelEvents::CONTROLLER => ['initSettings']
  49. ];
  50. }
  51. public function initSettings()
  52. {
  53. $merchants = $this->merchantStore->get();
  54. $this->initSettingsGeneric(
  55. 'merchant',
  56. $this->merchantSettingDefinition->getSettings(),
  57. //TODO vérifier que ce soit bien les online que l'on souhaite
  58. $merchants,
  59. $this->merchantSettingFactory
  60. );
  61. foreach($merchants as $merchant) {
  62. $this->initSettingsGeneric(
  63. 'section',
  64. $this->sectionSettingDefinition->getSettings(),
  65. $merchant->getSections(),
  66. $this->sectionSettingFactory
  67. );
  68. }
  69. }
  70. public function initSettingsGeneric($type, $settings, $entities, $factory)
  71. {
  72. if($entities) {
  73. foreach ($entities as $entity) {
  74. foreach ($settings as $category => $settingList) {
  75. foreach ($settingList as $settingName => $setting) {
  76. $entitySetting = $entity->getSetting($settingName);
  77. if (!$entitySetting) {
  78. // gestion du cas des SectionSetting spécifiques à une section
  79. $createEntitySetting = true;
  80. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  81. $createEntitySetting = false;
  82. }
  83. if ($createEntitySetting) {
  84. $text = null;
  85. $date = null;
  86. $file = null;
  87. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  88. if ($setting['field'] == 'text') {
  89. $text = $fieldValue;
  90. } elseif ($setting['field'] == 'date') {
  91. $date = $fieldValue;
  92. } elseif ($setting['field'] == 'file') {
  93. $file = $fieldValue;
  94. }
  95. $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
  96. $this->entityManager->persist($entitySetting);
  97. }
  98. } else {
  99. if ($this->settingSolver->getValue($entitySetting) === null
  100. && isset($setting['default'])
  101. && $setting['default'] !== null) {
  102. $methodSetValue = 'set' . ucfirst($setting['field']);
  103. $entitySetting->$methodSetValue($setting['default']);
  104. $this->entityManager->update($entitySetting);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. $this->entityManager->flush();
  111. }
  112. }
  113. }