Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SettingEventSubscriber.php 4.6KB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Section\SectionRepository;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class SettingEventSubscriber implements EventSubscriberInterface
  14. {
  15. protected $em;
  16. protected $merchantRepository;
  17. protected $sectionRepository;
  18. protected $merchantSettingDefinition;
  19. protected $sectionSettingDefinition;
  20. protected $merchantSettingFactory;
  21. protected $sectionSettingFactory;
  22. public function __construct(
  23. EntityManagerInterface $em,
  24. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  25. SectionSettingDefinitionInterface $sectionSettingDefinition,
  26. MerchantRepository $merchantRepository,
  27. SectionRepository $sectionRepository,
  28. MerchantSettingFactory $merchantSettingFactory,
  29. SectionSettingFactory $sectionSettingFactory
  30. ) {
  31. $this->em = $em;
  32. $this->merchantRepository = $merchantRepository;
  33. $this->sectionRepository = $sectionRepository;
  34. $this->merchantSettingDefinition = $merchantSettingDefinition;
  35. $this->sectionSettingDefinition = $sectionSettingDefinition;
  36. $this->merchantSettingFactory = $merchantSettingFactory;
  37. $this->sectionSettingFactory = $sectionSettingFactory;
  38. }
  39. public static function getSubscribedEvents()
  40. {
  41. return [
  42. KernelEvents::CONTROLLER => ['initSettings']
  43. ];
  44. }
  45. public function initSettings()
  46. {
  47. $this->initSettingsGeneric(
  48. 'merchant',
  49. $this->merchantSettingDefinition->getSettings(),
  50. $this->merchantRepository->findAll(),
  51. $this->merchantSettingFactory
  52. );
  53. $this->initSettingsGeneric(
  54. 'section',
  55. $this->sectionSettingDefinition->getSettings(),
  56. $this->sectionRepository->findAll(),
  57. $this->sectionSettingFactory
  58. );
  59. }
  60. public function initSettingsGeneric($type, $settings, $entities, $factory)
  61. {
  62. foreach ($entities as $entity) {
  63. foreach ($settings as $category => $settingList) {
  64. foreach ($settingList as $settingName => $setting) {
  65. $entitySetting = $entity->getSetting($settingName);
  66. if (!$entitySetting) {
  67. // gestion du cas des SectionSetting spécifiques à une section
  68. $createEntitySetting = true;
  69. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  70. $createEntitySetting = false;
  71. }
  72. if ($createEntitySetting) {
  73. $text = null;
  74. $date = null;
  75. $file = null;
  76. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  77. if ($setting['field'] == 'text') {
  78. $text = $fieldValue;
  79. } elseif ($setting['field'] == 'date') {
  80. $date = $fieldValue;
  81. } elseif ($setting['field'] == 'file') {
  82. $file = $fieldValue;
  83. }
  84. $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
  85. $this->em->persist($entitySetting);
  86. }
  87. } else {
  88. if ($entitySetting->getValue() === null
  89. && isset($setting['default'])
  90. && $setting['default'] !== null) {
  91. $methodSetValue = 'set' . ucfirst($setting['field']);
  92. $entitySetting->$methodSetValue($setting['default']);
  93. $this->em->update($entitySetting);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. $this->em->flush();
  100. }
  101. }