Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123 linhas
4.8KB

  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. }