5f93f7cc91359e748fa717daa49612defb291d6b
[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 * @return void
41 */
42 public function indexAction()
43 {
44 /** @var $view \TYPO3\CMS\Form\View\Wizard\WizardView */
45 $view = GeneralUtility::makeInstance(\TYPO3\CMS\Form\View\Wizard\WizardView::class, $this->getRepository());
46 $view->render();
47 }
48
49 /**
50 * The save action called via AJAX
51 *
52 * The action which should be taken when the form in the wizard is saved
53 *
54 * @param ServerRequestInterface $request
55 * @param ResponseInterface $response
56 * @return ResponseInterface returns a 500 error or a valid JSON response
57 */
58 public function saveAction(ServerRequestInterface $request, ResponseInterface $response)
59 {
60 $repository = $this->getRepository();
61 $success = false;
62 // Check if the referenced record is available
63 if ($repository->hasRecord()) {
64 // Save the data
65 $success = $repository->save();
66 }
67
68 if (!$success) {
69 $response = $response->withStatus(500);
70 $message = $this->getLanguageService()->getLL('action_save_message_failed', false);
71 } else {
72 $message = $this->getLanguageService()->getLL('action_save_message_saved', false);
73 }
74 $response->getBody()->write(json_encode(['message' => $message]));
75 return $response
76 ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
77 ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . 'GMT')
78 ->withHeader('Cache-Control', 'no-cache, must-revalidate')
79 ->withHeader('Pragma', 'no-cache');
80 }
81
82 /**
83 * The load action called via AJAX
84 *
85 * The action which should be taken when the form in the wizard is loaded
86 *
87 * @param ServerRequestInterface $request
88 * @param ResponseInterface $response the response object
89 * @return ResponseInterface returns a 500 error or a valid JSON response
90 */
91 public function loadAction(ServerRequestInterface $request, ResponseInterface $response)
92 {
93 $result = $this->getRepository()->getRecordAsJson();
94 if (!$result) {
95 $response = $response->withStatus(500);
96 $result = ['message' => $this->getLanguageService()->getLL('action_load_message_failed', false)];
97 } else {
98 $result = ['configuration' => $result];
99 }
100 $response->getBody()->write(json_encode($result));
101 return $response
102 ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
103 ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . 'GMT')
104 ->withHeader('Cache-Control', 'no-cache, must-revalidate')
105 ->withHeader('Pragma', 'no-cache');
106 }
107
108 /**
109 * Gets the repository object.
110 *
111 * @return ContentRepository
112 */
113 protected function getRepository()
114 {
115 return GeneralUtility::makeInstance(ContentRepository::class);
116 }
117
118 /**
119 * Returns an instance of LanguageService
120 *
121 * @return \TYPO3\CMS\Lang\LanguageService
122 */
123 protected function getLanguageService()
124 {
125 return $GLOBALS['LANG'];
126 }
127 }