|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
- use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
- use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
- use Lc\CaracoleBundle\Repository\Section\SectionRepository;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SettingEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $merchantRepository;
- protected $sectionRepository;
- protected $merchantSettingDefinition;
- protected $sectionSettingDefinition;
- protected $merchantSettingFactory;
- protected $sectionSettingFactory;
-
- public function __construct(
- EntityManagerInterface $em,
- MerchantSettingDefinitionInterface $merchantSettingDefinition,
- SectionSettingDefinitionInterface $sectionSettingDefinition,
- MerchantRepository $merchantRepository,
- SectionRepository $sectionRepository,
- MerchantSettingFactory $merchantSettingFactory,
- SectionSettingFactory $sectionSettingFactory
- ) {
- $this->em = $em;
- $this->merchantRepository = $merchantRepository;
- $this->sectionRepository = $sectionRepository;
- $this->merchantSettingDefinition = $merchantSettingDefinition;
- $this->sectionSettingDefinition = $sectionSettingDefinition;
- $this->merchantSettingFactory = $merchantSettingFactory;
- $this->sectionSettingFactory = $sectionSettingFactory;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $this->initSettingsGeneric(
- 'merchant',
- $this->merchantSettingDefinition->getSettings(),
- $this->merchantRepository->findAll(),
- $this->merchantSettingFactory
- );
-
- $this->initSettingsGeneric(
- 'section',
- $this->sectionSettingDefinition->getSettings(),
- $this->sectionRepository->findAll(),
- $this->sectionSettingFactory
- );
- }
-
- public function initSettingsGeneric($type, $settings, $entities, $factory)
- {
- foreach ($entities as $entity) {
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $entity->getSetting($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) {
- if ($type == 'section') {
- $factory->setSection($entity);
- } elseif ($type == 'merchant') {
- $factory->setMerchant($entity);
- }
-
- $text = null;
- $date = null;
- $file = null;
-
- $fieldValue = isset($setting['default']) ? $setting['default'] : null;
-
- if ($setting['field'] == 'text') {
- $text = $fieldValue;
- } elseif ($setting['field'] == 'date') {
- $date = $fieldValue;
- } elseif ($setting['field'] == 'file') {
- $file = $fieldValue;
- }
-
- $entitySetting = $factory->create($setting['name'], $text, $date, $file);
-
- $this->em->persist($entitySetting);
- }
- } else {
- if ($entitySetting->getValue() === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($setting['default']);
- $this->em->update($entitySetting);
- }
- }
- }
- }
- }
-
- $this->em->flush();
- }
-
- }
|