[TASK] FAL: Hard-coded flexform on storage creation
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_xml.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
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 * XML class, Used to create XML output from input rows.
29 * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
30 *
31 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 * @deprecated since TYPO3 6.0. It will be removed with TYPO3 6.2, please use other xml helper solutions instead.
35 */
36 class t3lib_xml {
37
38 // Top element name
39 public $topLevelName = 'typo3_test';
40
41 // Contains a list of fields for each table which should be presented in the XML output
42 public $XML_recFields = array();
43
44 public $XMLIndent = 0;
45
46 public $Icode = '';
47
48 public $XMLdebug = 0;
49
50 // If set, all fields from records are rendered no matter their content. If not set, only 'true' (that is '' or zero) fields make it to the document.
51 public $includeNonEmptyValues = 0;
52
53 public $lines = array();
54
55 /**
56 * Constructor, setting topLevelName to the input var
57 *
58 * @param string $topLevelName Top Level Name
59 * @return void
60 */
61 public function __construct($topLevelName) {
62 \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('Class TYPO3\\CMS\\Core\\Utility\\GeneralUtility is deprecated since TYPO3 6.0. ' . 'It will be removed with TYPO3 6.2, please use other xml helper solutions instead.');
63 $this->topLevelName = $topLevelName;
64 }
65
66 /**
67 * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
68 *
69 * @param string $table Table name
70 * @param string $list Commalist of fields names from the table, $table, which is supposed to be rendered in the XML output. If a field is not in this list, it is not rendered.
71 * @return void
72 */
73 public function setRecFields($table, $list) {
74 $this->XML_recFields[$table] = $list;
75 }
76
77 /**
78 * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
79 *
80 * @return string
81 */
82 public function getResult() {
83 $content = implode(LF, $this->lines);
84 return $this->output($content);
85 }
86
87 /**
88 * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
89 *
90 * @return void
91 */
92 public function WAPHeader() {
93 $this->lines[] = '<?xml version="1.0"?>';
94 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . ' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
95 $this->newLevel($this->topLevelName, 1);
96 }
97
98 /**
99 * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
100 * Encoding is set to UTF-8!
101 *
102 * @return void
103 */
104 public function renderHeader() {
105 $this->lines[] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
106 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . '>';
107 $this->newLevel($this->topLevelName, 1);
108 }
109
110 /**
111 * Sets the footer (of ->topLevelName)
112 *
113 * @return void
114 */
115 public function renderFooter() {
116 $this->newLevel($this->topLevelName, 0);
117 }
118
119 /**
120 * Indents/Outdents a new level named, $name
121 *
122 * @param string $name The name of the new element for this level
123 * @param boolean $beginEndFlag If FALSE, then this function call will *end* the level, otherwise create it.
124 * @param array $params Array of attributes in key/value pairs which will be added to the element (tag), $name
125 * @return void
126 */
127 public function newLevel($name, $beginEndFlag = 0, $params = array()) {
128 if ($beginEndFlag) {
129 $pList = '';
130 if (count($params)) {
131 $par = array();
132 foreach ($params as $key => $val) {
133 $par[] = $key . '="' . htmlspecialchars($val) . '"';
134 }
135 $pList = ' ' . implode(' ', $par);
136 }
137 $this->lines[] = $this->Icode . '<' . $name . $pList . '>';
138 $this->indent(1);
139 } else {
140 $this->indent(0);
141 $this->lines[] = $this->Icode . '</' . $name . '>';
142 }
143 }
144
145 /**
146 * Function that will return the content from string $content. If the internal ->XMLdebug flag is set the content returned will be formatted in <pre>-tags
147 *
148 * @param string The XML content to output
149 * @return string Output
150 */
151 public function output($content) {
152 if ($this->XMLdebug) {
153 return '<pre>' . htmlspecialchars($content) . '</pre>
154 <hr /><font color="red">Size: ' . strlen($content) . '</font>';
155 } else {
156 return $content;
157 }
158 }
159
160 /**
161 * Increments/Decrements Indentation counter, ->XMLIndent
162 * Sets and returns ->Icode variable which is a line prefix consisting of a number of tab-chars corresponding to the indent-levels of the current position (->XMLindent)
163 *
164 * @param boolean $b If TRUE the XMLIndent var is increased, otherwise decreased
165 * @return string ->Icode - the prefix string with TAB-chars.
166 */
167 public function indent($b) {
168 if ($b) {
169 $this->XMLIndent++;
170 } else {
171 $this->XMLIndent--;
172 }
173 $this->Icode = '';
174 for ($a = 0; $a < $this->XMLIndent; $a++) {
175 $this->Icode .= TAB;
176 }
177 return $this->Icode;
178 }
179
180 /**
181 * Takes a SQL result for $table and traverses it, adding rows
182 *
183 * @param string $table Tablename
184 * @param pointer $res SQL resource pointer, should be reset
185 * @return void
186 */
187 public function renderRecords($table, $res) {
188 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
189 $this->addRecord($table, $row);
190 }
191 $GLOBALS['TYPO3_DB']->sql_free_result($res);
192 }
193
194 /**
195 * Adds record, $row, from table, $table, to the internal array of XML-lines
196 *
197 * @param string $table Table name
198 * @param array $row The row to add to XML structure from the table name
199 * @return void
200 */
201 public function addRecord($table, $row) {
202 $this->lines[] = $this->Icode . '<' . $table . ' uid="' . $row['uid'] . '">';
203 $this->indent(1);
204 $this->getRowInXML($table, $row);
205 $this->indent(0);
206 $this->lines[] = $this->Icode . '</' . $table . '>';
207 }
208
209 /**
210 * Internal function for adding the actual content of the $row from $table to the internal structure.
211 * Notice that only fields from $table that are listed in $this->XML_recFields[$table] (set by setRecFields()) will be rendered (and in the order found in that array!)
212 * Content from the row will be htmlspecialchar()'ed, UTF-8 encoded and have LF (newlines) exchanged for '<newline/>' tags. The element name for a value equals the fieldname from the record.
213 *
214 * @param string $table Table name
215 * @param array $row Row from table to add.
216 * @return void
217 * @access private
218 */
219 public function getRowInXML($table, $row) {
220 $fields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->XML_recFields[$table], 1);
221 foreach ($fields as $field) {
222 if ($row[$field] || $this->includeNonEmptyValues) {
223 $this->lines[] = $this->Icode . $this->fieldWrap($field, $this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
224 }
225 }
226 }
227
228 /**
229 * UTF-8 encodes the input content (from ISO-8859-1!)
230 *
231 * @param string $content String content to UTF-8 encode
232 * @return string Encoded content.
233 */
234 public function utf8($content) {
235 return utf8_encode($content);
236 }
237
238 /**
239 * Substitutes LF characters with a '<newline/>' tag.
240 *
241 * @param string $string Input value
242 * @return string Processed input value
243 */
244 public function substNewline($string) {
245 return str_replace(LF, '<newline/>', $string);
246 }
247
248 /**
249 * Wraps the value in tags with element name, $field.
250 *
251 * @param string $field Fieldname from a record - will be the element name
252 * @param string $value Value from the field - will be wrapped in the elements.
253 * @return string The wrapped string.
254 */
255 public function fieldWrap($field, $value) {
256 return '<' . $field . '>' . $value . '</' . $field . '>';
257 }
258
259 /**
260 * Creates the BACK button for WAP documents
261 *
262 * @return void
263 */
264 public function WAPback() {
265 $this->newLevel('template', 1);
266 $this->newLevel('do', 1, array('type' => 'accept', 'label' => 'Back'));
267 $this->addLine('<prev/>');
268 $this->newLevel('do');
269 $this->newLevel('template');
270 }
271
272 /**
273 * Add a line to the internal XML structure (automatically prefixed with ->Icode.
274 *
275 * @param string Line to add to the $this->lines array
276 * @return void
277 */
278 public function addLine($str) {
279 $this->lines[] = $this->Icode . $str;
280 }
281
282 }
283
284 ?>