您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

122 行
4.2KB

  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\Definition\SiteSettingDefinition;
  12. use Lc\SovBundle\Form\Setting\SiteSettingsFormType;
  13. use Lc\SovBundle\Repository\Site\SiteRepository;
  14. use Lc\SovBundle\Translation\TranslatorAdmin;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class SettingAdminController extends SovSettingController
  17. {
  18. protected $em;
  19. protected $translatorAdmin;
  20. protected $merchantResolver;
  21. protected $merchantSettingDefinition;
  22. protected $sectionResolver;
  23. protected $sectionSettingDefinition;
  24. protected $siteSettingDefinition;
  25. protected $siteRepository;
  26. public function __construct(
  27. EntityManagerInterface $em,
  28. TranslatorAdmin $translatorAdmin,
  29. MerchantResolver $merchantResolver,
  30. SectionResolver $sectionResolver,
  31. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  32. SectionSettingDefinitionInterface $sectionSettingDefinition,
  33. SiteSettingDefinition $siteSettingDefinition,
  34. SiteRepository $siteRepository
  35. ) {
  36. $this->em = $em;
  37. $this->translatorAdmin = $translatorAdmin;
  38. $this->merchantResolver = $merchantResolver;
  39. $this->sectionResolver = $sectionResolver;
  40. $this->merchantSettingDefinition = $merchantSettingDefinition;
  41. $this->sectionSettingDefinition = $sectionSettingDefinition;
  42. $this->siteSettingDefinition = $siteSettingDefinition;
  43. $this->siteRepository = $siteRepository;
  44. }
  45. public function manageMerchant(Request $request)
  46. {
  47. return $this->manage($request, 'merchant');
  48. }
  49. public function manageSection(Request $request)
  50. {
  51. return $this->manage($request, 'section');
  52. }
  53. public function manage(Request $request, $type)
  54. {
  55. $entity = null;
  56. if ($type == 'merchant') {
  57. $resolver = $this->merchantResolver;
  58. $formClass = MerchantSettingsFormType::class;
  59. $settingDefinition = $this->merchantSettingDefinition;
  60. } elseif ($type == 'section') {
  61. $resolver = $this->sectionResolver;
  62. $formClass = SectionSettingsFormType::class;
  63. $settingDefinition = $this->sectionSettingDefinition;
  64. }
  65. $entity = $resolver->getCurrent();
  66. if ($entity) {
  67. $form = $this->createForm($formClass, $entity);
  68. $form->handleRequest($request);
  69. if ($form->isSubmitted() && $form->isValid()) {
  70. $this->em->update($entity);
  71. $this->em->flush();
  72. $this->addFlash('success', $this->translatorAdmin->transFlashMessage('settings_saved'));
  73. }
  74. return $this->render(
  75. '@LcCaracole/admin/setting/' . $type . '.html.twig',
  76. [
  77. 'resolver' => $resolver,
  78. 'setting_definition' => $settingDefinition,
  79. 'form' => $form->createView()
  80. ]
  81. );
  82. }
  83. }
  84. public function manageGlobal(Request $request)
  85. {
  86. $site = $this->siteRepository->findOneByDevAlias('default') ;
  87. $form = $this->createForm(SiteSettingsFormType::class, $site);
  88. $form->handleRequest($request);
  89. if ($form->isSubmitted() && $form->isValid()) {
  90. $this->em->update($site);
  91. $this->em->flush();
  92. $this->addFlash('success', $this->translatorAdmin->transFlashMessage('settings_saved'));
  93. }
  94. return $this->render(
  95. '@LcCaracole/admin/setting/global.html.twig' ,
  96. [
  97. 'setting_definition' => $this->siteSettingDefinition,
  98. 'form' => $form->createView()
  99. ]
  100. );
  101. }
  102. }