33namespace App \Controller \Admin ;
44
55use App \Entity \Answer ;
6+ use App \Entity \Question ;
7+ use App \Entity \System ;
68use Doctrine \ORM \EntityManagerInterface ;
79use EasyCorp \Bundle \EasyAdminBundle \Config \Action ;
810use EasyCorp \Bundle \EasyAdminBundle \Config \Actions ;
911use EasyCorp \Bundle \EasyAdminBundle \Config \Crud ;
12+ use EasyCorp \Bundle \EasyAdminBundle \Context \AdminContext ;
1013use EasyCorp \Bundle \EasyAdminBundle \Controller \AbstractCrudController ;
1114use EasyCorp \Bundle \EasyAdminBundle \Field \ChoiceField ;
1215use EasyCorp \Bundle \EasyAdminBundle \Field \TextareaField ;
1316use EasyCorp \Bundle \EasyAdminBundle \Field \TextField ;
1417use Symfony \Component \HttpFoundation \RedirectResponse ;
18+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
19+ use Symfony \Component \Translation \TranslatableMessage ;
1520
1621class AnswerCrudController extends AbstractCrudController
1722{
23+ public function __construct (
24+ private readonly EntityManagerInterface $ entityManager ,
25+ ) {
26+ }
27+
1828 public static function getEntityFqcn (): string
1929 {
2030 return Answer::class;
2131 }
2232
2333 #[\Override]
24- public function updateEntity (EntityManagerInterface $ entityManager , mixed $ entityInstance ): void
34+ protected function getRedirectResponseAfterSave (AdminContext $ context , string $ action ): RedirectResponse
35+ {
36+ // Lifted from parent::getRedirectResponseAfterSave().
37+ $ submitButtonName = $ context ->getRequest ()->request ->all ()['ea ' ]['newForm ' ]['btn ' ] ?? null ;
38+
39+ // Answers don't have an index which is where EasyAdmin will return to by default on save.
40+ // Therefore we return to somewhere else on save.
41+ if (Action::SAVE_AND_RETURN === $ submitButtonName ) {
42+ $ request = $ context ->getRequest ();
43+
44+ // Check if we have a “referer” (sic!) set.
45+ if ($ referer = $ request ->get ('referer ' )) {
46+ return $ this ->redirect ($ referer );
47+ }
48+ // Check if we have a “system” set.
49+ if ($ system = $ request ->get ('system ' )) {
50+ return $ this ->redirectToRoute ('admin_system_detail ' , ['entityId ' => $ system ]);
51+ }
52+
53+ // Use system index as fallback.
54+ return $ this ->redirectToRoute ('admin_system_index ' );
55+ }
56+
57+ return parent ::getRedirectResponseAfterSave ($ context , $ action );
58+ }
59+
60+ #[\Override]
61+ public function createEntity (string $ entityFqcn ): Answer
2562 {
26- // TODO: Change the autogenerated stub
27- parent ::updateEntity ($ entityManager , $ entityInstance );
63+ /** @var Answer $entity */
64+ $ entity = parent ::createEntity ($ entityFqcn );
65+
66+ // Set system and question on new answers.
67+ $ params = $ this ->getContext ()->getRequest ()->query ->all ();
68+ if (isset ($ params ['system ' ], $ params ['question ' ])) {
69+ $ system = $ this ->entityManager ->getRepository (System::class)->find ($ params ['system ' ]);
70+ $ question = $ this ->entityManager ->getRepository (Question::class)->find ($ params ['question ' ]);
71+ if ($ system && $ question ) {
72+ $ entity
73+ ->setSystem ($ system )
74+ ->setQuestion ($ question );
75+ } else {
76+ throw new BadRequestHttpException ();
77+ }
78+ }
2879
29- $ url = $ this ->generateUrl ('dashboard ' , ['entityType ' => 'system ' ]);
30- $ response = new RedirectResponse ($ url );
31- $ response ->send ();
80+ return $ entity ;
3281 }
3382
3483 #[\Override]
3584 public function configureActions (Actions $ actions ): Actions
3685 {
3786 return $ actions
38-
39- ->add (Crud::PAGE_EDIT , Action::EDIT )
40- ->add (Crud::PAGE_DETAIL , Action::DETAIL )
41- ->disable (Action::NEW , Action::DELETE , Action::INDEX )
42- ;
87+ ->disable (Action::SAVE_AND_ADD_ANOTHER , Action::DELETE , Action::INDEX , Action::DETAIL );
4388 }
4489
4590 /**
@@ -48,20 +93,21 @@ public function configureActions(Actions $actions): Actions
4893 #[\Override]
4994 public function configureFields (string $ pageName ): iterable
5095 {
51- $ question = TextField::new ('question ' )->setFormTypeOptions (['disabled ' => true ]);
52- $ smiley = ChoiceField::new ('smiley ' )->setChoices ([
53- 'GREEN ' => 'GREEN ' ,
54- 'RED ' => 'RED ' ,
55- 'BLUE ' => 'BLUE ' ,
56- 'YELLOW ' => 'YELLOW ' ,
57- ]);
58- $ note = TextareaField::new ('note ' );
59- if (Crud::PAGE_EDIT === $ pageName ) {
60- return [$ question , $ smiley , $ note ];
61- } elseif (Crud::PAGE_DETAIL === $ pageName ) {
62- return [$ question , $ smiley , $ note ];
63- } else {
64- throw new \Exception ('Invalid page: ' .$ pageName );
96+ $ question = TextField::new ('question ' )
97+ ->setFormTypeOptions (['disabled ' => true ]);
98+ if (Crud::PAGE_NEW === $ pageName ) {
99+ // $question->setRequired(false);
65100 }
101+ yield $ question ;
102+ yield ChoiceField::new ('smiley ' )
103+ ->setTranslatableChoices ([
104+ 'GREEN ' => new TranslatableMessage ('smiley.GREEN ' ),
105+ 'RED ' => new TranslatableMessage ('smiley.RED ' ),
106+ 'BLUE ' => new TranslatableMessage ('smiley.BLUE ' ),
107+ 'YELLOW ' => new TranslatableMessage ('smiley.YELLOW ' ),
108+ ])
109+ // ->setRequired(true)
110+ ;
111+ yield TextareaField::new ('note ' );
66112 }
67113}
0 commit comments