[TASK] Migrate EXT:form Backend Wizard to Routing
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Controller / WizardController.php
1 <?php
2 namespace TYPO3\CMS\Form\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Form\Domain\Repository\ContentRepository;
21
22 /**
23 * The form wizard controller
24 */
25 class WizardController
26 {
27 /**
28 * The constructor to load the LL file
29 */
30 public function __construct()
31 {
32 $this->getLanguageService()->includeLLFile('EXT:form/Resources/Private/Language/locallang_wizard.xlf');
33 }
34
35 /**
36 * The index action
37 *
38 * The action which should be taken when the wizard is loaded
39 *
40 * @param ServerRequestInterface $request
41 * @param ResponseInterface $response
42 * @return ResponseInterface returns a 500 error or a valid JSON response
43 */
44 public function indexAction(ServerRequestInterface $request, ResponseInterface $response)
45 {
46 /** @var $view \TYPO3\CMS\Form\View\Wizard\WizardView */
47 $view = GeneralUtility::makeInstance(\TYPO3\CMS\Form\View\Wizard\WizardView::class, $this->getRepository());
48 $content = $view->render();
49 $response->getBody()->write($content);
50 return $response;
51 }
52
53 /**
54 * The save action called via AJAX
55 *
56 * The action which should be taken when the form in the wizard is saved
57 *
58 * @param ServerRequestInterface $request
59 * @param ResponseInterface $response
60 * @return ResponseInterface returns a 500 error or a valid JSON response
61 */
62 public function saveAction(ServerRequestInterface $request, ResponseInterface $response)
63 {
64 $repository = $this->getRepository();
65 $success = false;
66 // Check if the referenced record is available
67 if ($repository->hasRecord()) {
68 // Save the data
69 $success = $repository->save();
70 }
71
72 if (!$success) {
73 $response = $response->withStatus(500);
74 $message = $this->getLanguageService()->getLL('action_save_message_failed', false);
75 } else {
76 $message = $this->getLanguageService()->getLL('action_save_message_saved', false);
77 }
78 $response->getBody()->write(json_encode(['message' => $message]));
79 return $response
80 ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
81 ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . 'GMT')
82 ->withHeader('Cache-Control', 'no-cache, must-revalidate')
83 ->withHeader('Pragma', 'no-cache');
84 }
85
86 /**
87 * The load action called via AJAX
88 *
89 * The action which should be taken when the form in the wizard is loaded
90 *
91 * @param ServerRequestInterface $request
92 * @param ResponseInterface $response the response object
93 * @return ResponseInterface returns a 500 error or a valid JSON response
94 */
95 public function loadAction(ServerRequestInterface $request, ResponseInterface $response)
96 {
97 $result = $this->getRepository()->getRecordAsJson();
98 if (!$result) {
99 $response = $response->withStatus(500);
100 $result = ['message' => $this->getLanguageService()->getLL('action_load_message_failed', false)];
101 } else {
102 $result = ['configuration' => $result];
103 }
104 $response->getBody()->write(json_encode($result));
105 return $response
106 ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
107 ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . 'GMT')
108 ->withHeader('Cache-Control', 'no-cache, must-revalidate')
109 ->withHeader('Pragma', 'no-cache');
110 }
111
112 /**
113 * Gets the repository object.
114 *
115 * @return ContentRepository
116 */
117 protected function getRepository()
118 {
119 return GeneralUtility::makeInstance(ContentRepository::class);
120 }
121
122 /**
123 * Returns an instance of LanguageService
124 *
125 * @return \TYPO3\CMS\Lang\LanguageService
126 */
127 protected function getLanguageService()
128 {
129 return $GLOBALS['LANG'];
130 }
131 }