44
55namespace Shopsys \FrameworkBundle \Controller \Admin ;
66
7+ use Shopsys \FrameworkBundle \Component \ConfirmDelete \ConfirmDeleteResponseFactory ;
8+ use Shopsys \FrameworkBundle \Component \Domain \Domain ;
79use Shopsys \FrameworkBundle \Component \Router \Security \Annotation \CsrfProtection ;
10+ use Shopsys \FrameworkBundle \Form \Admin \Product \Flag \FlagFormType ;
11+ use Shopsys \FrameworkBundle \Model \AdminNavigation \BreadcrumbOverrider ;
812use Shopsys \FrameworkBundle \Model \Product \Flag \Exception \FlagNotFoundException ;
13+ use Shopsys \FrameworkBundle \Model \Product \Flag \FlagDataFactory ;
914use Shopsys \FrameworkBundle \Model \Product \Flag \FlagFacade ;
10- use Shopsys \FrameworkBundle \Model \Product \Flag \FlagInlineEdit ;
15+ use Shopsys \FrameworkBundle \Model \Product \Flag \FlagGridFactory ;
16+ use Symfony \Component \HttpFoundation \Request ;
17+ use Symfony \Component \HttpFoundation \Response ;
1118use Symfony \Component \Routing \Annotation \Route ;
1219
1320class FlagController extends AdminBaseController
1421{
1522 /**
1623 * @param \Shopsys\FrameworkBundle\Model\Product\Flag\FlagFacade $flagFacade
17- * @param \Shopsys\FrameworkBundle\Model\Product\Flag\FlagInlineEdit $flagInlineEdit
24+ * @param \Shopsys\FrameworkBundle\Model\Product\Flag\FlagGridFactory $flagGridFactory
25+ * @param \Shopsys\FrameworkBundle\Component\ConfirmDelete\ConfirmDeleteResponseFactory $confirmDeleteResponseFactory
26+ * @param \Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider $breadcrumbOverrider
27+ * @param \Shopsys\FrameworkBundle\Model\Product\Flag\FlagDataFactory $flagDataFactory
28+ * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
1829 */
1930 public function __construct (
2031 protected readonly FlagFacade $ flagFacade ,
21- protected readonly FlagInlineEdit $ flagInlineEdit ,
32+ protected readonly FlagGridFactory $ flagGridFactory ,
33+ protected readonly ConfirmDeleteResponseFactory $ confirmDeleteResponseFactory ,
34+ protected readonly BreadcrumbOverrider $ breadcrumbOverrider ,
35+ protected readonly FlagDataFactory $ flagDataFactory ,
36+ protected readonly Domain $ domain ,
2237 ) {
2338 }
2439
2540 #[Route(path: '/product/flag/list/ ' )]
2641 public function listAction ()
2742 {
28- $ productInlineEdit = $ this ->flagInlineEdit ;
29-
30- $ grid = $ productInlineEdit ->getGrid ();
43+ $ grid = $ this ->flagGridFactory ->create ();
3144
3245 return $ this ->render ('@ShopsysFramework/Admin/Content/Flag/list.html.twig ' , [
3346 'gridView ' => $ grid ->createView (),
3447 ]);
3548 }
3649
50+ /**
51+ * @param \Symfony\Component\HttpFoundation\Request $request
52+ * @return \Symfony\Component\HttpFoundation\Response
53+ */
54+ #[Route(path: '/product/flag/new/ ' )]
55+ public function newAction (Request $ request ): Response
56+ {
57+ $ flagData = $ this ->flagDataFactory ->create ();
58+
59+ $ form = $ this ->createForm (FlagFormType::class, $ flagData , [
60+ 'flag ' => null ,
61+ ]);
62+ $ form ->handleRequest ($ request );
63+
64+ if ($ form ->isSubmitted () && $ form ->isValid ()) {
65+ if (!$ this ->domain ->hasAdminAllDomainsEnabled ()) {
66+ $ this ->addErrorFlash (t ('Creating a record requires all domains to be enabled as domain-specific fields cannot be empty. If you want to proceed, select all domains in the Domain filter in the header first. ' ));
67+
68+ return $ this ->redirectToRoute ('admin_flag_new ' );
69+ }
70+
71+ $ flag = $ this ->flagFacade ->create ($ flagData );
72+
73+ $ this
74+ ->addSuccessFlashTwig (
75+ t ('Flag <strong><a href="{{ url }}">{{ name }}</a></strong> created ' ),
76+ [
77+ 'name ' => $ flag ->getName (),
78+ 'url ' => $ this ->generateUrl ('admin_flag_edit ' , ['id ' => $ flag ->getId ()]),
79+ ],
80+ );
81+
82+ return $ this ->redirectToRoute ('admin_flag_list ' );
83+ }
84+
85+ if ($ form ->isSubmitted () && !$ form ->isValid ()) {
86+ $ this ->addErrorFlashTwig (t ('Please check the correctness of all data filled. ' ));
87+ }
88+
89+ return $ this ->render ('@ShopsysFramework/Admin/Content/Flag/new.html.twig ' , [
90+ 'form ' => $ form ->createView (),
91+ ]);
92+ }
93+
94+ /**
95+ * @param \Symfony\Component\HttpFoundation\Request $request
96+ * @param int $id
97+ * @return \Symfony\Component\HttpFoundation\Response
98+ */
99+ #[Route(path: '/product/flag/edit/{id} ' , requirements: ['id ' => '\d+ ' ])]
100+ public function editAction (Request $ request , int $ id ): Response
101+ {
102+ $ flag = $ this ->flagFacade ->getById ($ id );
103+ $ flagData = $ this ->flagDataFactory ->createFromFlag ($ flag );
104+
105+ $ form = $ this ->createForm (FlagFormType::class, $ flagData , [
106+ 'flag ' => $ flag ,
107+ ]);
108+ $ form ->handleRequest ($ request );
109+
110+ if ($ form ->isSubmitted () && $ form ->isValid ()) {
111+ $ this ->flagFacade ->edit ($ id , $ flagData );
112+
113+ $ this
114+ ->addSuccessFlashTwig (
115+ t ('Flag <strong><a href="{{ url }}">{{ name }}</a></strong> modified ' ),
116+ [
117+ 'name ' => $ flag ->getName (),
118+ 'url ' => $ this ->generateUrl ('admin_flag_edit ' , ['id ' => $ flag ->getId ()]),
119+ ],
120+ );
121+
122+ return $ this ->redirectToRoute ('admin_flag_list ' );
123+ }
124+
125+ if ($ form ->isSubmitted () && !$ form ->isValid ()) {
126+ $ this ->addErrorFlashTwig (t ('Please check the correctness of all data filled. ' ));
127+ }
128+
129+ $ this ->breadcrumbOverrider ->overrideLastItem (t ('Editing flag - {{ name }} ' , ['{{ name }} ' => $ flag ->getName ()]));
130+
131+ return $ this ->render ('@ShopsysFramework/Admin/Content/Flag/edit.html.twig ' , [
132+ 'form ' => $ form ->createView (),
133+ 'flag ' => $ flag ,
134+ ]);
135+ }
136+
137+ /**
138+ * @param int $id
139+ * @return \Symfony\Component\HttpFoundation\Response
140+ */
141+ #[Route(path: '/product/flag/delete-confirm/{id} ' , requirements: ['id ' => '\d+ ' ])]
142+ public function deleteConfirmAction (int $ id ): Response
143+ {
144+ try {
145+ $ flag = $ this ->flagFacade ->getById ($ id );
146+ $ flagDependencies = $ this ->flagFacade ->getFlagDependencies ($ flag ->getId ());
147+ $ hasDependency = $ flagDependencies ->hasPromoCodeDependency || $ flagDependencies ->hasSeoMixDependency ;
148+
149+ if ($ hasDependency ) {
150+ return $ this ->render ('@ShopsysFramework/Admin/Content/Flag/deleteForbidden.html.twig ' , [
151+ 'hasPromoCodeDependency ' => $ flagDependencies ->hasPromoCodeDependency ,
152+ 'hasSeoMixDependency ' => $ flagDependencies ->hasSeoMixDependency ,
153+ ]);
154+ }
155+ $ message = t ('Do you really want to remove this flag? ' );
156+
157+ return $ this ->confirmDeleteResponseFactory ->createDeleteResponse (
158+ $ message ,
159+ 'admin_flag_delete ' ,
160+ $ id ,
161+ );
162+ } catch (FlagNotFoundException $ ex ) {
163+ return new Response (t ('Selected flag doesn \'t exist. ' ));
164+ }
165+ }
166+
37167 /**
38168 * @CsrfProtection
39169 * @param int $id
170+ * @return \Symfony\Component\HttpFoundation\Response
40171 */
41172 #[Route(path: '/product/flag/delete/{id} ' , requirements: ['id ' => '\d+ ' ])]
42- public function deleteAction ($ id )
173+ public function deleteAction ($ id ): Response
43174 {
44175 try {
45- $ fullName = $ this ->flagFacade ->getById ($ id )->getName ();
176+ $ flag = $ this ->flagFacade ->getById ($ id );
177+ $ fullName = $ flag ->getName ();
178+
179+ $ flagDependencies = $ this ->flagFacade ->getFlagDependencies ($ flag ->getId ());
180+
181+ if ($ flagDependencies ->hasSeoMixDependency || $ flagDependencies ->hasPromoCodeDependency ) {
182+ $ this ->addErrorFlash (t ('The selected flag cannot be deleted. ' ));
183+
184+ return $ this ->redirectToRoute ('admin_flag_list ' );
185+ }
46186
47187 $ this ->flagFacade ->deleteById ($ id );
48188
@@ -52,7 +192,7 @@ public function deleteAction($id)
52192 'name ' => $ fullName ,
53193 ],
54194 );
55- } catch (FlagNotFoundException $ ex ) {
195+ } catch (FlagNotFoundException ) {
56196 $ this ->addErrorFlash (t ('Selected flag doesn \'t exist. ' ));
57197 }
58198
0 commit comments