Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

104 lines
3.4KB

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