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.

128 lines
5.2KB

  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. foreach ($entities as $entity) {
  73. foreach ($settings as $category => $settingList) {
  74. foreach ($settingList as $settingName => $setting) {
  75. $entitySetting = $entity->getSetting($settingName);
  76. if (!$entitySetting) {
  77. // gestion du cas des SectionSetting spécifiques à une section
  78. $createEntitySetting = true;
  79. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  80. $createEntitySetting = false;
  81. }
  82. if ($createEntitySetting) {
  83. $text = null;
  84. $date = null;
  85. $file = null;
  86. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  87. if ($setting['field'] == 'text') {
  88. $text = $fieldValue;
  89. } elseif ($setting['field'] == 'date') {
  90. $date = $fieldValue;
  91. } elseif ($setting['field'] == 'file') {
  92. $file = $fieldValue;
  93. }
  94. $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
  95. $this->entityManager->persist($entitySetting);
  96. }
  97. } else {
  98. if ($this->settingSolver->getValue($entitySetting) === null
  99. && isset($setting['default'])
  100. && $setting['default'] !== null) {
  101. $methodSetValue = 'set' . ucfirst($setting['field']);
  102. $entitySetting->$methodSetValue($setting['default']);
  103. $this->entityManager->update($entitySetting);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. $this->entityManager->flush();
  110. }
  111. }