Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SettingEventSubscriber.php 4.8KB

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if ($type == 'section') {
  74. $factory->setSection($entity);
  75. } elseif ($type == 'merchant') {
  76. $factory->setMerchant($entity);
  77. }
  78. $text = null;
  79. $date = null;
  80. $file = null;
  81. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  82. if ($setting['field'] == 'text') {
  83. $text = $fieldValue;
  84. } elseif ($setting['field'] == 'date') {
  85. $date = $fieldValue;
  86. } elseif ($setting['field'] == 'file') {
  87. $file = $fieldValue;
  88. }
  89. $entitySetting = $factory->create($setting['name'], $text, $date, $file);
  90. $this->em->persist($entitySetting);
  91. }
  92. } else {
  93. if ($entitySetting->getValue() === null
  94. && isset($setting['default'])
  95. && $setting['default'] !== null) {
  96. $methodSetValue = 'set' . ucfirst($setting['field']);
  97. $entitySetting->$methodSetValue($setting['default']);
  98. $this->em->update($entitySetting);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. $this->em->flush();
  105. }
  106. }