[TASK] Move classAliasMaps into compatibility6 extension part 6
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Repository / ContentRepository.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Repository;
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 use TYPO3\CMS\Backend\Utility\BackendUtility;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Repository for \TYPO3\CMS\Form\Domain\Model\Content
21 *
22 * @author Patrick Broens <patrick@patrickbroens.nl>
23 */
24 class ContentRepository {
25
26 /**
27 * Get the referenced record from the database
28 *
29 * Using the GET or POST variable 'P'
30 *
31 * @return bool|\TYPO3\CMS\Form\Domain\Model\Content if found, FALSE if not
32 */
33 public function getRecord() {
34 $record = FALSE;
35 $getPostVariables = GeneralUtility::_GP('P');
36 $table = (string)$getPostVariables['table'];
37 $recordId = (int)$getPostVariables['uid'];
38 $row = BackendUtility::getRecord($table, $recordId);
39 if (is_array($row)) {
40 // strip off the leading "[Translate to XY]" text after localizing the original record
41 $languageField = $GLOBALS['TCA']['tt_content']['ctrl']['languageField'];
42 $transOrigPointerField = $GLOBALS['TCA']['tt_content']['ctrl']['transOrigPointerField'];
43 if ($row[$languageField] > 0 && $row[$transOrigPointerField] > 0) {
44 $bodytext = preg_replace('/^\[.*?\] /', '', $row['bodytext'], 1);
45 } else {
46 $bodytext = $row['bodytext'];
47 }
48
49 /** @var $typoScriptParser \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */
50 $typoScriptParser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class);
51 $typoScriptParser->parse($bodytext);
52 /** @var $record \TYPO3\CMS\Form\Domain\Model\Content */
53 $record = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Domain\Model\Content::class);
54 $record->setUid($row['uid']);
55 $record->setPageId($row['pid']);
56 $record->setTyposcript($typoScriptParser->setup);
57 }
58 return $record;
59 }
60
61 /**
62 * Check if the referenced record exists
63 *
64 * @return bool TRUE if record exists, FALSE if not
65 */
66 public function hasRecord() {
67 return $this->getRecord() !== FALSE;
68 }
69
70 /**
71 * Convert and save the incoming data of the FORM wizard
72 *
73 * @return bool TRUE if succeeded, FALSE if not
74 */
75 public function save() {
76 $json = GeneralUtility::_GP('configuration');
77 $parameters = GeneralUtility::_GP('P');
78 $success = FALSE;
79 /** @var $converter \TYPO3\CMS\Form\Domain\Factory\JsonToTypoScript */
80 $converter = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Domain\Factory\JsonToTypoScript::class);
81 $typoscript = $converter->convert($json);
82 if ($typoscript) {
83 // Make TCEmain object:
84 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
85 $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
86 $tce->stripslashes_values = 0;
87 // Put content into the data array:
88 $data = array();
89 $data[$parameters['table']][$parameters['uid']][$parameters['field']] = $typoscript;
90 // Perform the update:
91 $tce->start($data, array());
92 $tce->process_datamap();
93 $success = TRUE;
94 }
95 return $success;
96 }
97
98 /**
99 * Read and convert the content record to JSON
100 *
101 * @return string The JSON object if record exists, FALSE if not
102 */
103 public function getRecordAsJson() {
104 $json = FALSE;
105 $record = $this->getRecord();
106 if ($record) {
107 $typoscript = $record->getTyposcript();
108 /** @var $converter \TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter */
109 $converter = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter::class);
110 $json = $converter->convert($typoscript);
111 }
112 return $json;
113 }
114
115 }