65a17ae6a7e9288f0c8fc71a47e2ba91194b4f13
[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 /** @var $typoScriptParser t3lib_tsparser */
50 $typoScriptParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('t3lib_tsparser');
51 $typoScriptParser->parse($row['bodytext']);
52 /** @var $record \TYPO3\CMS\Form\Domain\Model\Content */
53 $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Model\\Content');
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 boolean 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 boolean TRUE if succeeded, FALSE if not
74 */
75 public function save() {
76 $json = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('configuration');
77 $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('P');
78 $success = FALSE;
79 /** @var $converter \TYPO3\CMS\Form\Domain\Factory\JsonToTypoScript */
80 $converter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\JsonToTypoScript');
81 $typoscript = $converter->convert($json);
82 if ($typoscript) {
83 // Make TCEmain object:
84 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
85 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
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 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 = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Utility\\TypoScriptToJsonConverter');
110 $json = $converter->convert($typoscript);
111 }
112 return $json;
113 }
114
115 }
116
117
118 ?>