[BUGFIX] Translation of a form makes the form wizard unusable
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Repository / ContentRepository.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Repository;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Patrick Broens <patrick@patrickbroens.nl>
8 *
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Repository for tx_form_Domain_Model_Content
29 *
30 * @category Repository
31 * @author Patrick Broens <patrick@patrickbroens.nl>
32 */
33 class ContentRepository {
34
35 /**
36 * Get the referenced record from the database
37 *
38 * Using the GET or POST variable 'P'
39 *
40 * @return boolean|tx_form_Domain_Model_Content if found, FALSE if not
41 */
42 public function getRecord() {
43 $record = FALSE;
44 $getPostVariables = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('P');
45 $table = (string) $getPostVariables['table'];
46 $recordId = (int) $getPostVariables['uid'];
47 $row = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord($table, $recordId);
48 if (is_array($row)) {
49 // strip off the leading "[Translate to XY]" text after localizing the original record
50 $languageField = $GLOBALS['TCA']['tt_content']['ctrl']['languageField'];
51 $transOrigPointerField = $GLOBALS['TCA']['tt_content']['ctrl']['transOrigPointerField'];
52 if ($row[$languageField] > 0 && $row[$transOrigPointerField] > 0) {
53 $bodytext = preg_replace('/^\[.*?\] /', '', $row['bodytext'], 1);
54 } else {
55 $bodytext = $row['bodytext'];
56 }
57
58 /** @var $typoScriptParser t3lib_tsparser */
59 $typoScriptParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('t3lib_tsparser');
60 $typoScriptParser->parse($bodytext);
61 /** @var $record \TYPO3\CMS\Form\Domain\Model\Content */
62 $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Model\\Content');
63 $record->setUid($row['uid']);
64 $record->setPageId($row['pid']);
65 $record->setTyposcript($typoScriptParser->setup);
66 }
67 return $record;
68 }
69
70 /**
71 * Check if the referenced record exists
72 *
73 * @return boolean TRUE if record exists, FALSE if not
74 */
75 public function hasRecord() {
76 return $this->getRecord() !== FALSE;
77 }
78
79 /**
80 * Convert and save the incoming data of the FORM wizard
81 *
82 * @return boolean TRUE if succeeded, FALSE if not
83 */
84 public function save() {
85 $json = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('configuration');
86 $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('P');
87 $success = FALSE;
88 /** @var $converter \TYPO3\CMS\Form\Domain\Factory\JsonToTypoScript */
89 $converter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\JsonToTypoScript');
90 $typoscript = $converter->convert($json);
91 if ($typoscript) {
92 // Make TCEmain object:
93 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
94 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
95 $tce->stripslashes_values = 0;
96 // Put content into the data array:
97 $data = array();
98 $data[$parameters['table']][$parameters['uid']][$parameters['field']] = $typoscript;
99 // Perform the update:
100 $tce->start($data, array());
101 $tce->process_datamap();
102 $success = TRUE;
103 }
104 return $success;
105 }
106
107 /**
108 * Read and convert the content record to JSON
109 *
110 * @return The JSON object if record exists, FALSE if not
111 */
112 public function getRecordAsJson() {
113 $json = FALSE;
114 $record = $this->getRecord();
115 if ($record) {
116 $typoscript = $record->getTyposcript();
117 /** @var $converter \TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter */
118 $converter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Utility\\TypoScriptToJsonConverter');
119 $json = $converter->convert($typoscript);
120 }
121 return $json;
122 }
123
124 }
125
126
127 ?>