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.

90 lines
3.0KB

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