[TASK] Streamline and optimize JavaScript in install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / View / JsonView.php
1 <?php
2 namespace TYPO3\CMS\Install\View;
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 TYPO3\CMS\Extbase\Mvc\View\AbstractView;
18 use TYPO3\CMS\Install\Status\Exception as StatusException;
19 use TYPO3\CMS\Install\Status\StatusInterface;
20
21 /**
22 * Simple JsonView (currently returns an associative array)
23 */
24 class JsonView extends AbstractView
25 {
26 /**
27 * @return string
28 */
29 public function render()
30 {
31 $renderedData = $this->variables;
32 if (isset($renderedData['status']) && is_array($renderedData['status'])) {
33 try {
34 $renderedData['status'] = $this->transformStatusMessagesToArray($renderedData['status']);
35 } catch (StatusException $e) {
36 $renderedData['status'] = [[
37 'severity' => 'error',
38 'title' => htmlspecialchars($e->getMessage())
39 ]];
40 }
41 }
42
43 return $renderedData;
44 }
45
46 /**
47 * Transform an array of messages to an associative array.
48 *
49 * @param array<StatusInterface>
50 * @return array
51 * @throws StatusException
52 */
53 protected function transformStatusMessagesToArray(array $statusArray = [])
54 {
55 $result = [];
56 foreach ($statusArray as $status) {
57 if (!$status instanceof StatusInterface) {
58 throw new StatusException(
59 'Object must implement StatusInterface',
60 1381059600
61 );
62 }
63 $result[] = $this->transformStatusToArray($status);
64 }
65 return $result;
66 }
67
68 /**
69 * Creates an array from a status object.
70 * Used for example to transfer the message as json.
71 *
72 * @param StatusInterface $status
73 * @return array
74 */
75 public function transformStatusToArray(StatusInterface $status)
76 {
77 $arrayStatus = [];
78 $arrayStatus['severity'] = $this->getSeverityAsNumber($status->getSeverity());
79 $arrayStatus['title'] = htmlspecialchars($status->getTitle());
80 $arrayStatus['message'] = htmlspecialchars($status->getMessage());
81 return $arrayStatus;
82 }
83
84 /**
85 * Return the corresponding integer value for given severity string
86 *
87 * @param string $severity
88 *
89 * @return int
90 */
91 protected function getSeverityAsNumber($severity)
92 {
93 $number = -2;
94 switch (strtolower($severity)) {
95 case 'loading':
96 $number = -3;
97 break;
98 case 'notice':
99 $number = -2;
100 break;
101 case 'info':
102 $number = -1;
103 break;
104 case 'ok':
105 case 'success':
106 $number = 0;
107 break;
108 case 'warning':
109 $number = 1;
110 break;
111 case 'error':
112 case 'danger':
113 case 'fatal':
114 $number = 2;
115 break;
116 }
117 return $number;
118 }
119 }