|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
-
- namespace Lc\CaracoleBundle\Builder\Setting;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
- use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
- use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\SovBundle\Definition\SiteSettingDefinition;
- use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
- use Lc\SovBundle\Repository\Site\SiteStore;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Lc\SovBundle\Builder\Setting\SettingBuilder as SovSettingBuilder;
-
- class SettingBuilder extends SovSettingBuilder
- {
- protected MerchantStore $merchantStore;
- protected MerchantSettingDefinition $merchantSettingDefinition;
- protected MerchantSettingFactory $merchantSettingFactory;
- protected SectionSettingDefinition $sectionSettingDefinition;
- protected SectionSettingFactory $sectionSettingFactory;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- SettingSolver $settingSolver,
- SiteStore $siteStore,
- SiteSettingDefinition $siteSettingDefinition,
- SiteSettingFactory $siteSettingFactory,
- MerchantStore $merchantStore,
- MerchantSettingDefinition $merchantSettingDefinition,
- MerchantSettingFactory $merchantSettingFactory,
- SectionSettingDefinition $sectionSettingDefinition,
- SectionSettingFactory $sectionSettingFactory
- ) {
- parent::__construct($entityManager, $settingSolver, $siteStore, $siteSettingDefinition, $siteSettingFactory);
-
- $this->merchantStore = $merchantStore;
- $this->merchantSettingDefinition = $merchantSettingDefinition;
- $this->merchantSettingFactory = $merchantSettingFactory;
- $this->sectionSettingDefinition = $sectionSettingDefinition;
- $this->sectionSettingFactory = $sectionSettingFactory;
- }
-
- public function initMerchantSettings()
- {
- $merchants = $this->merchantStore->get();
-
- $this->initSettingsGeneric(
- $this->merchantSettingDefinition->getSettings(),
- $merchants,
- $this->merchantSettingFactory
- );
- }
-
- public function initSectionSettings()
- {
- $merchants = $this->merchantStore->get();
-
- foreach($merchants as $merchant) {
- $this->initSettingsGeneric(
- $this->sectionSettingDefinition->getSettings(),
- $merchant->getSections(),
- $this->sectionSettingFactory
- );
- }
- }
-
- protected function initSettingsGeneric($settings, $entities, $factory)
- {
- if($entities) {
- foreach ($entities as $entity) {
-
- $this->entityManager->refresh($entity);
-
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
-
- $entitySetting = $this->settingSolver->getSetting($entity, $settingName);
-
- if (!$entitySetting) {
-
- // gestion du cas des SectionSetting spécifiques à une section
- $createEntitySetting = true;
- if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
- $createEntitySetting = false;
- }
-
- if ($createEntitySetting) {
- $text = null;
- $date = null;
- $file = null;
-
- $valueDefault = isset($setting['default']) ? $this->settingSolver->getDefaultValue($entity, $setting['default']) : null;
-
- if ($setting['field'] == 'text') {
- $text = $valueDefault;
- } elseif ($setting['field'] == 'date') {
- $date = $valueDefault;
- } elseif ($setting['field'] == 'file') {
- $file = $valueDefault;
- }
-
- $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
-
- $this->entityManager->create($entitySetting);
- }
- } else {
- if ($this->settingSolver->getValue($entitySetting) === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
-
- $valueDefault = $this->settingSolver->getDefaultValue($entity, $setting['default']);
-
- if($valueDefault) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($valueDefault);
- $this->entityManager->update($entitySetting);
- }
- }
- }
- }
- }
- }
-
- $this->entityManager->flush();
- }
- }
-
- }
|