Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

132 lines
5.4KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\Setting;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
  5. use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
  6. use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
  7. use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
  8. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  9. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  10. use Lc\SovBundle\Definition\SiteSettingDefinition;
  11. use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
  12. use Lc\SovBundle\Repository\Site\SiteStore;
  13. use Lc\SovBundle\Solver\Setting\SettingSolver;
  14. use Lc\SovBundle\Builder\Setting\SettingBuilder as SovSettingBuilder;
  15. class SettingBuilder extends SovSettingBuilder
  16. {
  17. protected MerchantStore $merchantStore;
  18. protected MerchantSettingDefinition $merchantSettingDefinition;
  19. protected MerchantSettingFactory $merchantSettingFactory;
  20. protected SectionSettingDefinition $sectionSettingDefinition;
  21. protected SectionSettingFactory $sectionSettingFactory;
  22. public function __construct(
  23. EntityManagerInterface $entityManager,
  24. SettingSolver $settingSolver,
  25. SiteStore $siteStore,
  26. SiteSettingDefinition $siteSettingDefinition,
  27. SiteSettingFactory $siteSettingFactory,
  28. MerchantStore $merchantStore,
  29. MerchantSettingDefinition $merchantSettingDefinition,
  30. MerchantSettingFactory $merchantSettingFactory,
  31. SectionSettingDefinition $sectionSettingDefinition,
  32. SectionSettingFactory $sectionSettingFactory
  33. ) {
  34. parent::__construct($entityManager, $settingSolver, $siteStore, $siteSettingDefinition, $siteSettingFactory);
  35. $this->merchantStore = $merchantStore;
  36. $this->merchantSettingDefinition = $merchantSettingDefinition;
  37. $this->merchantSettingFactory = $merchantSettingFactory;
  38. $this->sectionSettingDefinition = $sectionSettingDefinition;
  39. $this->sectionSettingFactory = $sectionSettingFactory;
  40. }
  41. public function initMerchantSettings()
  42. {
  43. $merchants = $this->merchantStore->get();
  44. $this->initSettingsGeneric(
  45. $this->merchantSettingDefinition->getSettings(),
  46. $merchants,
  47. $this->merchantSettingFactory
  48. );
  49. }
  50. public function initSectionSettings()
  51. {
  52. $merchants = $this->merchantStore->get();
  53. foreach($merchants as $merchant) {
  54. $this->initSettingsGeneric(
  55. $this->sectionSettingDefinition->getSettings(),
  56. $merchant->getSections(),
  57. $this->sectionSettingFactory
  58. );
  59. }
  60. }
  61. protected function initSettingsGeneric($settings, $entities, $factory)
  62. {
  63. if($entities) {
  64. foreach ($entities as $entity) {
  65. $this->entityManager->refresh($entity);
  66. foreach ($settings as $category => $settingList) {
  67. foreach ($settingList as $settingName => $setting) {
  68. $entitySetting = $this->settingSolver->getSetting($entity, $settingName);
  69. if (!$entitySetting) {
  70. // gestion du cas des SectionSetting spécifiques à une section
  71. $createEntitySetting = true;
  72. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  73. $createEntitySetting = false;
  74. }
  75. if ($createEntitySetting) {
  76. $text = null;
  77. $date = null;
  78. $file = null;
  79. $valueDefault = isset($setting['default']) ? $this->settingSolver->getDefaultValue($entity, $setting['default']) : null;
  80. if ($setting['field'] == 'text') {
  81. $text = $valueDefault;
  82. } elseif ($setting['field'] == 'date') {
  83. $date = $valueDefault;
  84. } elseif ($setting['field'] == 'file') {
  85. $file = $valueDefault;
  86. }
  87. $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
  88. $this->entityManager->create($entitySetting);
  89. }
  90. } else {
  91. if ($this->settingSolver->getValue($entitySetting) === null
  92. && isset($setting['default'])
  93. && $setting['default'] !== null) {
  94. $valueDefault = $this->settingSolver->getDefaultValue($entity, $setting['default']);
  95. if($valueDefault) {
  96. $methodSetValue = 'set' . ucfirst($setting['field']);
  97. $entitySetting->$methodSetValue($valueDefault);
  98. $this->entityManager->update($entitySetting);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. $this->entityManager->flush();
  106. }
  107. }
  108. }