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.

SiteSettingEventSubscriber.php 3.4KB

3 anos atrás
3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
  5. use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
  6. use Lc\SovBundle\Factory\Site\SiteFactory;
  7. use Lc\SovBundle\Repository\Setting\SiteSettingRepository;
  8. use Lc\SovBundle\Repository\Site\SiteRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class SiteSettingEventSubscriber implements EventSubscriberInterface
  12. {
  13. protected $em;
  14. protected $siteSettingDefinition;
  15. protected $siteSettingFactory;
  16. protected $siteSettingRepository;
  17. protected $siteRepository;
  18. protected $siteFactory;
  19. public function __construct(
  20. EntityManagerInterface $em,
  21. SiteSettingRepository $siteSettingRepository,
  22. SiteRepository $siteRepository,
  23. SiteSettingDefinitionInterface $siteSettingDefinition,
  24. SiteSettingFactory $siteSettingFactory,
  25. SiteFactory $siteFactory
  26. ) {
  27. $this->em = $em;
  28. $this->siteSettingDefinition = $siteSettingDefinition;
  29. $this->siteSettingFactory = $siteSettingFactory;
  30. $this->siteSettingRepository = $siteSettingRepository;
  31. $this->siteRepository = $siteRepository;
  32. $this->siteFactory = $siteFactory;
  33. }
  34. public static function getSubscribedEvents()
  35. {
  36. return [
  37. KernelEvents::CONTROLLER => ['initSettings']
  38. ];
  39. }
  40. public function initSettings()
  41. {
  42. $site = $this->getSiteDefault();
  43. if (!$site) {
  44. $site = $this->siteFactory->create('default');
  45. $this->em->persist($site);
  46. $this->em->flush($site);
  47. }
  48. $settings = $this->siteSettingDefinition->getSettings();
  49. $factory = $this->siteSettingFactory;
  50. foreach ($settings as $category => $settingList) {
  51. foreach ($settingList as $settingName => $setting) {
  52. $entitySetting = $site->getSetting($settingName);
  53. if (!$entitySetting) {
  54. $text = null;
  55. $date = null;
  56. $file = null;
  57. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  58. if($setting['field'] == 'text') {
  59. $text = $fieldValue;
  60. }
  61. if($setting['field'] == 'date') {
  62. $date = $fieldValue;
  63. }
  64. if($setting['field'] == 'file') {
  65. $file = $fieldValue;
  66. }
  67. $entitySetting = $factory->create($site, $setting['name'], $text, $date, $file);
  68. $this->em->persist($entitySetting);
  69. } else {
  70. if ($entitySetting->getValue() === null
  71. && isset($setting['default'])
  72. && $setting['default'] !== null) {
  73. $methodSetValue = 'set' . ucfirst($setting['field']);
  74. $entitySetting->$methodSetValue($setting['default']);
  75. $this->em->update($entitySetting);
  76. }
  77. }
  78. }
  79. }
  80. $this->em->flush();
  81. }
  82. public function getSiteDefault()
  83. {
  84. return $this->siteRepository->findOneByDevAlias('default');
  85. }
  86. }