4343class FaqController extends AbstractController
4444{
4545 /**
46- * @throws \phpMyFAQ\Core\ Exception
46+ * @throws Exception
4747 */
4848 #[Route('admin/api/faq/permissions ' )]
4949 public function listPermissions (Request $ request ): JsonResponse
@@ -84,26 +84,21 @@ public function listByCategory(Request $request): JsonResponse
8484 }
8585
8686 /**
87- * @throws \phpMyFAQ\Core\ Exception
87+ * @throws Exception
8888 */
8989 #[Route('admin/api/faq/activate ' )]
9090 public function activate (Request $ request ): JsonResponse
9191 {
9292 $ this ->userHasPermission (PermissionType::FAQ_APPROVE );
9393
94- $ jsonResponse = new JsonResponse ();
95-
9694 $ data = json_decode ($ request ->getContent ());
9795
9896 $ faqIds = Filter::filterArray ($ data ->faqIds );
9997 $ faqLanguage = Filter::filterVar ($ data ->faqLanguage , FILTER_SANITIZE_SPECIAL_CHARS );
10098 $ checked = Filter::filterVar ($ data ->checked , FILTER_VALIDATE_BOOLEAN );
10199
102100 if (!Token::getInstance ()->verifyToken ('faq-overview ' , $ data ->csrf )) {
103- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
104- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
105-
106- return $ jsonResponse ;
101+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
107102 }
108103
109104 if (!($ faqIds === false || $ faqIds === [] || $ faqIds === null )) {
@@ -117,20 +112,18 @@ public function activate(Request $request): JsonResponse
117112 }
118113
119114 if ($ success ) {
120- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
121- $ jsonResponse ->setData (['success ' => Translation::get ('ad_entry_savedsuc ' )]);
115+ return $ this ->json (['success ' => Translation::get ('ad_entry_savedsuc ' )], Response::HTTP_OK );
122116 } else {
123- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
124- $ jsonResponse ->setData (['error ' => Translation::get ('ad_entry_savedfail ' )]);
117+ return $ this ->json (['error ' => Translation::get ('ad_entry_savedfail ' )], Response::HTTP_BAD_REQUEST );
125118 }
126119 } else {
127- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
128- $ jsonResponse ->setData (['error ' => 'No FAQ IDs provided. ' ]);
120+ return $ this ->json (['error ' => 'No FAQ IDs provided. ' ], Response::HTTP_BAD_REQUEST );
129121 }
130-
131- return $ jsonResponse ;
132122 }
133123
124+ /**
125+ * @throws Exception
126+ */
134127 #[Route('admin/api/faq/sticky ' )]
135128 public function sticky (Request $ request ): JsonResponse
136129 {
@@ -145,10 +138,7 @@ public function sticky(Request $request): JsonResponse
145138 $ checked = Filter::filterVar ($ data ->checked , FILTER_VALIDATE_BOOLEAN );
146139
147140 if (!Token::getInstance ()->verifyToken ('faq-overview ' , $ data ->csrf )) {
148- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
149- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
150-
151- return $ jsonResponse ;
141+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
152142 }
153143
154144 if (!($ faqIds === false || $ faqIds === [] || $ faqIds === null )) {
@@ -162,26 +152,23 @@ public function sticky(Request $request): JsonResponse
162152 }
163153
164154 if ($ success ) {
165- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
166- $ jsonResponse ->setData (['success ' => Translation::get ('ad_entry_savedsuc ' )]);
155+ return $ this ->json (['success ' => Translation::get ('ad_entry_savedsuc ' )], Response::HTTP_OK );
167156 } else {
168- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
169- $ jsonResponse ->setData (['error ' => Translation::get ('ad_entry_savedfail ' )]);
157+ return $ this ->json (['error ' => Translation::get ('ad_entry_savedfail ' )], Response::HTTP_BAD_REQUEST );
170158 }
171159 } else {
172- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
173- $ jsonResponse ->setData (['error ' => 'No FAQ IDs provided. ' ]);
160+ return $ this ->json (['error ' => 'No FAQ IDs provided. ' ], Response::HTTP_BAD_REQUEST );
174161 }
175-
176- return $ jsonResponse ;
177162 }
178163
164+ /**
165+ * @throws Exception
166+ */
179167 #[Route('admin/api/faq/delete ' )]
180168 public function delete (Request $ request ): JsonResponse
181169 {
182170 $ this ->userHasPermission (PermissionType::FAQ_DELETE );
183171
184- $ jsonResponse = new JsonResponse ();
185172 $ configuration = Configuration::getConfigurationInstance ();
186173 $ user = CurrentUser::getCurrentUser ($ configuration );
187174 $ faq = new Faq ($ configuration );
@@ -192,10 +179,7 @@ public function delete(Request $request): JsonResponse
192179 $ faqLanguage = Filter::filterVar ($ data ->faqLanguage , FILTER_SANITIZE_SPECIAL_CHARS );
193180
194181 if (!Token::getInstance ()->verifyToken ('faq-overview ' , $ data ->csrf )) {
195- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
196- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
197-
198- return $ jsonResponse ;
182+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
199183 }
200184
201185 $ adminLog = new AdminLog ($ configuration );
@@ -204,34 +188,27 @@ public function delete(Request $request): JsonResponse
204188 try {
205189 $ faq ->deleteRecord ($ faqId , $ faqLanguage );
206190 } catch (FileException | AttachmentException $ e ) {
207- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
208- $ jsonResponse ->setData (['error ' => $ e ->getMessage ()]);
209-
210- return $ jsonResponse ;
191+ return $ this ->json (['error ' => $ e ->getMessage ()], Response::HTTP_BAD_REQUEST );
211192 }
212193
213- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
214- $ jsonResponse ->setData (['success ' => Translation::get ('ad_entry_delsuc ' )]);
215-
216- return $ jsonResponse ;
194+ return $ this ->json (['success ' => Translation::get ('ad_entry_delsuc ' )], Response::HTTP_OK );
217195 }
218196
197+ /**
198+ * @throws Exception
199+ */
219200 #[Route('admin/api/faq/search ' )]
220201 public function search (Request $ request ): JsonResponse
221202 {
222203 $ this ->userHasPermission (PermissionType::FAQ_EDIT );
223204
224- $ jsonResponse = new JsonResponse ();
225205 $ configuration = Configuration::getConfigurationInstance ();
226206 $ user = CurrentUser::getCurrentUser ($ configuration );
227207
228208 $ data = json_decode ($ request ->getContent ());
229209
230210 if (!Token::getInstance ()->verifyToken ('edit-faq ' , $ data ->csrf )) {
231- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
232- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
233-
234- return $ jsonResponse ;
211+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
235212 }
236213
237214 $ faqPermission = new FaqPermission ($ configuration );
@@ -249,85 +226,66 @@ public function search(Request $request): JsonResponse
249226 $ searchHelper = new SearchHelper ($ configuration );
250227 $ searchHelper ->setSearchTerm ($ searchString );
251228
252- $ jsonResponse -> setStatusCode (Response:: HTTP_OK );
253- $ jsonResponse -> setData (
254- [ ' success ' => $ searchHelper -> renderAdminSuggestionResult ( $ searchResultSet )]
229+ return $ this -> json (
230+ [ ' success ' => $ searchHelper -> renderAdminSuggestionResult ( $ searchResultSet )],
231+ Response:: HTTP_OK
255232 );
256233 } else {
257- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
258- $ jsonResponse ->setData (['error ' => 'No search string provided. ' ]);
234+ return $ this ->json (['error ' => 'No search string provided. ' ], Response::HTTP_BAD_REQUEST );
259235 }
260-
261- return $ jsonResponse ;
262236 }
263237
238+ /**
239+ * @throws Exception
240+ */
264241 #[Route('admin/api/faqs/sticky/order ' )]
265242 public function saveOrderOfStickyFaqs (Request $ request ): JsonResponse
266243 {
267244 $ this ->userHasPermission (PermissionType::FAQ_EDIT );
268245
269- $ jsonResponse = new JsonResponse ();
270246 $ data = json_decode ($ request ->getContent ());
271247
272248 if (!Token::getInstance ()->verifyToken ('order-stickyfaqs ' , $ data ->csrf )) {
273- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
274- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
275- $ jsonResponse ->send ();
276- exit ();
249+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
277250 }
278251
279252 $ faq = new Faq (Configuration::getConfigurationInstance ());
280253 $ faq ->setStickyFaqOrder ($ data ->faqIds );
281254
282- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
283- $ jsonResponse ->setData (['success ' => Translation::get ('ad_categ_save_order ' )]);
284-
285- return $ jsonResponse ;
255+ return $ this ->json (['success ' => Translation::get ('ad_categ_save_order ' )], Response::HTTP_OK );
286256 }
287257
288258 /**
289- * @throws \phpMyFAQ\Core\ Exception
259+ * @throws Exception
290260 */
291261 #[Route('admin/api/faq/import ' )]
292262 public function import (Request $ request ): JsonResponse
293263 {
294264 $ this ->userHasPermission (PermissionType::FAQ_ADD );
295265
296- $ jsonResponse = new JsonResponse ();
297-
298266 $ file = $ request ->files ->get ('file ' );
299267 if (!isset ($ file )) {
300- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
301- $ jsonResponse ->setData (['error ' => 'Bad request: There is no file submitted. ' ]);
302-
303- return $ jsonResponse ;
268+ return $ this ->json (['error ' => 'Bad request: There is no file submitted. ' ], Response::HTTP_BAD_REQUEST );
304269 }
305270
306271 if (!Token::getInstance ()->verifyToken ('importfaqs ' , $ request ->request ->get ('csrf ' ))) {
307- $ jsonResponse ->setStatusCode (Response::HTTP_UNAUTHORIZED );
308- $ jsonResponse ->setData (['error ' => Translation::get ('err_NotAuth ' )]);
309-
310- return $ jsonResponse ;
272+ return $ this ->json (['error ' => Translation::get ('err_NotAuth ' )], Response::HTTP_UNAUTHORIZED );
311273 }
312274
313275 $ faqImport = new FaqImport (Configuration::getConfigurationInstance ());
314276
315- $ result = [];
316277 $ errors = [];
317278
318279 if (0 === $ file ->getError () && $ faqImport ->isCSVFile ($ file )) {
319280 $ handle = fopen ($ file ->getRealPath (), 'r ' );
320281 $ csvData = $ faqImport ->parseCSV ($ handle );
321282
322283 if (!$ faqImport ->validateCSV ($ csvData )) {
323- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
324284 $ result = [
325285 'storedAll ' => false ,
326286 'error ' => Translation::get ('msgCSVFileNotValidated ' ),
327287 ];
328- $ jsonResponse ->setData ($ result );
329-
330- return $ jsonResponse ;
288+ return $ this ->json ($ result , Response::HTTP_BAD_REQUEST );
331289 }
332290
333291 foreach ($ csvData as $ record ) {
@@ -338,26 +296,24 @@ public function import(Request $request): JsonResponse
338296 }
339297
340298 if ($ errors === []) {
341- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
342299 $ result = [
343300 'storedAll ' => true ,
344301 'success ' => Translation::get ('msgImportSuccessful ' ),
345302 ];
303+ return $ this ->json ($ result , Response::HTTP_OK );
346304 } else {
347- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
348305 $ result = [
349306 'storedAll ' => false ,
350307 'messages ' => $ errors ,
351308 ];
309+ return $ this ->json ($ result , Response::HTTP_BAD_REQUEST );
352310 }
353-
354- $ jsonResponse ->setData ($ result );
355-
356- return $ jsonResponse ;
311+ } else {
312+ $ result = [
313+ 'storedAll ' => false ,
314+ 'error ' => 'Bad request: The file is not a CSV file. ' ,
315+ ];
316+ return $ this ->json ($ result , Response::HTTP_BAD_REQUEST );
357317 }
358-
359- $ jsonResponse ->setData ($ result );
360-
361- return $ jsonResponse ;
362318 }
363319}
0 commit comments