You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 line
3.5KB

  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. $methodGetValue = 'get' . ucfirst($setting['field']);
  71. if ($entitySetting->$methodGetValue() === null
  72. && isset($setting['default'])
  73. && $setting['default'] !== null) {
  74. $methodSetValue = 'set' . ucfirst($setting['field']);
  75. $entitySetting->$methodSetValue($setting['default']);
  76. $this->em->update($entitySetting);
  77. }
  78. }
  79. }
  80. }
  81. $this->em->flush();
  82. }
  83. public function getSiteDefault()
  84. {
  85. return $this->siteRepository->findOneByDevAlias('default');
  86. }
  87. }