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.

91 lines
3.0KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Setting;
  3. use Lc\SovBundle\Controller\Setting\SettingAdminController as SovSettingController;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
  6. use Lc\CaracoleBundle\Form\Setting\MerchantSettingsFormType;
  7. use Lc\CaracoleBundle\Form\Setting\SectionSettingsFormType;
  8. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  9. use Lc\CaracoleBundle\Resolver\SectionResolver;
  10. use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
  11. use Lc\SovBundle\Translation\TranslatorAdmin;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class SettingAdminController extends SovSettingController
  14. {
  15. protected $em;
  16. protected $merchantResolver;
  17. protected $merchantSettingDefinition;
  18. protected $sectionResolver;
  19. protected $sectionSettingDefinition;
  20. protected $translatorAdmin;
  21. public function __construct(
  22. EntityManagerInterface $em,
  23. MerchantResolver $merchantResolver,
  24. SectionResolver $sectionResolver,
  25. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  26. SectionSettingDefinitionInterface $sectionSettingDefinition,
  27. TranslatorAdmin $translatorAdmin
  28. )
  29. {
  30. $this->em = $em;
  31. $this->merchantResolver = $merchantResolver;
  32. $this->sectionResolver = $sectionResolver;
  33. $this->merchantSettingDefinition = $merchantSettingDefinition;
  34. $this->sectionSettingDefinition = $sectionSettingDefinition;
  35. $this->translatorAdmin = $translatorAdmin;
  36. }
  37. public function manageMerchant(Request $request)
  38. {
  39. return $this->manage($request, 'merchant');
  40. }
  41. public function manageSection(Request $request)
  42. {
  43. return $this->manage($request, 'section');
  44. }
  45. public function manage(Request $request, $type)
  46. {
  47. $entity = null;
  48. if ($type == 'merchant') {
  49. $resolver = $this->merchantResolver;
  50. $formClass = MerchantSettingsFormType::class;
  51. $settingDefinition = $this->merchantSettingDefinition;
  52. } elseif ($type == 'section') {
  53. $resolver = $this->sectionResolver;
  54. $formClass = SectionSettingsFormType::class;
  55. $settingDefinition = $this->sectionSettingDefinition;
  56. }
  57. $entity = $resolver->getCurrent();
  58. if ($entity) {
  59. $form = $this->createForm($formClass, $entity);
  60. $form->handleRequest($request);
  61. if ($form->isSubmitted() && $form->isValid()) {
  62. $this->em->update($entity);
  63. $this->em->flush();
  64. $this->addFlash('success', $this->translatorAdmin->transFlashMessage('settings_saved'));
  65. }
  66. return $this->render(
  67. '@LcCaracole/admin/setting/' . $type . '.html.twig',
  68. [
  69. 'resolver' => $resolver,
  70. 'setting_definition' => $settingDefinition,
  71. 'form' => $form->createView()
  72. ]
  73. );
  74. }
  75. }
  76. }