Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezsimplifiedxmleditoutput.php
Go to the documentation of this file.
1 <?php
11 // if ( !class_exists( 'eZXMLSchema' ) )
12 class eZSimplifiedXMLEditOutput
13 {
14 public $OutputTags = array(
15
16 'section' => array( 'handler' => 'outputSection' ),
17
18 'embed' => array( 'handler' => 'outputEmbed',
19 'attributes' => array( 'xhtml:id' => 'id',
20 'object_id' => false,
21 'node_id' => false,
22 'show_path' => false ),
23 'isSingle' => true ),
24
25 'embed-inline' => array( 'handler' => 'outputEmbed',
26 'attributes' => array( 'xhtml:id' => 'id',
27 'object_id' => false,
28 'node_id' => false,
29 'show_path' => false ),
30 'isSingle' => true ),
31
32 'object' => array( 'handler' => 'outputObject',
33 'attributes' => array( 'xhtml:id' => 'id',
34 'image:ezurl_target' => 'target',
35 'image:ezurl_href' => 'href',
36 'image:ezurl_id' => false ),
37 'isSingle' => true ),
38
39 'td' => array( 'handler' => 'outputTd',
40 'attributes' => array( 'xhtml:width' => 'width',
41 'xhtml:colspan' => 'colspan',
42 'xhtml:rowspan' => 'rowspan' ) ),
43
44 'th' => array( 'handler' => 'outputTd',
45 'attributes' => array( 'xhtml:width' => 'width',
46 'xhtml:colspan' => 'colspan',
47 'xhtml:rowspan' => 'rowspan' ) ),
48
49 'header' => array( 'handler' => 'outputHeader' ),
50
51 'paragraph' => array( 'handler' => 'outputParagraph' ),
52
53 'line' => array( 'handler' => 'outputLine' ),
54
55 'link' => array( 'handler' => 'outputLink',
56 'attributes' => array( 'xhtml:id' => 'id',
57 'xhtml:title' => 'title',
58 'url_id' => false,
59 'object_id' => false,
60 'node_id' => false,
61 'show_path' => false,
62 'ezurl_id' => false,
63 'anchor_name' => false ) ),
64 'anchor' => array( 'isSingle' => true ),
65
66 '#text' => array( 'handler' => 'outputText' )
67 );
68
69 // Call this function to obtain edit output string
70
71 function performOutput( $dom )
72 {
73 $this->XMLSchema = eZXMLSchema::instance();
74 $this->NestingLevel = 0;
75 $this->Output = '';
76 $sectionLevel = -1;
77
78 $this->createLinksArray( $dom );
79
80 $this->outputTag( $dom->documentElement, $sectionLevel );
81
82 return $this->Output;
83 }
84
85 function outputTag( $element, $sectionLevel )
86 {
87 $tagName = $element->nodeName;
88 if ( isset( $this->OutputTags[$tagName] ) )
89 {
90 $currentTag = $this->OutputTags[$tagName];
91
92 if ( isset( $currentTag['attributes'] ) )
93 $attributeRules = $currentTag['attributes'];
94 }
95 else
96 {
97 $currentTag = null;
98 }
99
100 //eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', 'outputting tag ' . $element->nodeName );
101
102 // Prepare attributes array
103
104 $attributes = array();
105
106 if ( $element->hasAttributes() )
107 {
108 $attributeNodes = $element->attributes;
109
110 foreach ( $attributeNodes as $attrNode )
111 {
112 if ( $attrNode->prefix && $attrNode->prefix != 'custom' )
113 {
114 $attrName = $attrNode->prefix . ':' . $attrNode->localName;
115 }
116 else
117 {
118 $attrName = $attrNode->nodeName;
119 }
120
121 $attributes[$attrName] = $attrNode->value;
122 }
123 }
124
125 // Call tag handler
126 $result = null;
127 if ( $currentTag && isset( $currentTag['handler'] ) )
128 {
129 $result = $this->callOutputHandler( 'handler', $element, $attributes, $sectionLevel );
130 }
131
132 $hasChildren = $element->hasChildNodes();
133 $isInline = $this->XMLSchema->isInline( $element );
134 //eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $isInline ? 'yes' : 'no', 'is inline element?' );
135 $isSingle = isset( $currentTag['isSingle'] ) && $currentTag['isSingle'];
136 //eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $isSingle ? 'yes' : 'no', 'is single element?' );
137
138 // If output was not set by handler, do a normal tag output
139 if ( !is_string( $result ) )
140 {
141 // Convert (if needed) and output attributes
142 $attrString = '';
143 foreach ( array_keys( $attributes ) as $name )
144 {
145 $value = $attributes[$name];
146 if ( isset( $attributeRules ) && isset( $attributeRules[$name] ) )
147 {
148 if ( !$attributeRules[$name] )
149 continue;
150
151 $name = $attributeRules[$name];
152 }
153 $attrString .= ' ' . $name . '="' . $value . '"';
154 }
155
156 $this->formatBeforeOpeningTag( $element, $isInline, $hasChildren );
157
158 //Output opening tag
159 $closing = $isSingle ? ' />' : '>';
160
161 $this->Output .= '<' . $tagName . $attrString . $closing;
162
163 if ( !$isSingle )
164 {
165 $this->formatAfterOpeningTag( $element, $isInline, $hasChildren );
166 }
167
168 $this->NestingLevel++;
169
170 //eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $this->Output, 'output so far' );
171 }
172
173 if ( $element->hasChildNodes() )
174 {
175 // Process children
176 foreach ( $element->childNodes as $child )
177 {
178 $this->outputTag( $child, $sectionLevel );
179 }
180 }
181
182 if ( is_string( $result ) )
183 {
184 $this->Output .= $result;
185 return;
186 }
187 else
188 {
189 $this->NestingLevel--;
190 if ( !$isSingle )
191 {
192 $this->formatBeforeClosingTag( $element, $isInline, $hasChildren );
193
194 $this->Output .= '</' . $tagName . '>';
195 }
196
197 $this->formatAfterClosingTag( $element, $isInline, $hasChildren );
198 }
199
200 return;
201 }
202
203 function formatBeforeOpeningTag( $element, $isInline, $hasChildren )
204 {
205 // Add indenting for block tags
206 if ( !$isInline )
207 {
208 if ( $this->NestingLevel > 0 )
209 {
210 $this->Output .= str_repeat( ' ', $this->NestingLevel );
211 }
212 }
213 }
214
215 function formatAfterOpeningTag( $element, $isInline, $hasChildren )
216 {
217 // Add linebreak in case we have block tag as a first child
218 if ( !$isInline && $hasChildren )
219 {
220 $firstChild = $element->firstChild;
221 if ( $firstChild && $firstChild->nodeName == 'paragraph' && !$firstChild->hasAttributes() )
222 {
223 $firstChild = $firstChild->firstChild;
224 }
225 if ( $firstChild && $firstChild->nodeName == 'line' )
226 {
227 $firstChild = $firstChild->firstChild;
228 }
229 if ( $firstChild && !$this->XMLSchema->isInline( $firstChild ) )
230 {
231 $this->Output .= "\n";
232 }
233 }
234 }
235
236 function formatBeforeClosingTag( $element, $isInline, $hasChildren )
237 {
238 if ( !$isInline && $hasChildren )
239 {
240 $lastChild = $element->lastChild;
241 if ( $lastChild && $lastChild->nodeName == 'paragraph' && !$lastChild->hasAttributes() )
242 {
243 $lastChild = $lastChild->lastChild;
244 }
245 if ( $lastChild && $lastChild->nodeName == 'line' )
246 {
247 $lastChild = $lastChild->lastChild;
248 }
249 if ( $lastChild && !$this->XMLSchema->isInline( $lastChild ) )
250 {
251 // Add line breaks and indenting for block tags
252 $this->Output .= "\n";
253 if ( $this->NestingLevel > 0 )
254 {
255 $this->Output .= str_repeat( ' ', $this->NestingLevel );
256 }
257 }
258 }
259 }
260
261 function formatAfterClosingTag( $element, $isInline, $hasChildren )
262 {
263 if ( !$isInline )
264 {
265 $next = $element->nextSibling;
266 if ( $next )
267 {
268 $this->Output .= "\n";
269 }
270 else
271 {
272 $parent = $element->parentNode;
273 while( $parent && $parent->nodeName == 'section' )
274 {
275 $next = $parent->nextSibling;
276 if ( $next )
277 {
278 $this->Output .= "\n";
279 break;
280 }
281 $parent = $parent->parentNode;
282 }
283 }
284 }
285 }
286
287 function callOutputHandler( $handlerName, $element, &$params, &$sectionLevel )
288 {
289 $result = null;
290 $thisOutputTag = $this->OutputTags[$element->nodeName];
291 if ( isset( $thisOutputTag[$handlerName] ) )
292 {
293 if ( is_callable( array( $this, $thisOutputTag[$handlerName] ) ) )
294 {
295 $result = call_user_func_array( array( $this, $thisOutputTag[$handlerName] ),
296 array( $element, &$params, &$sectionLevel ) );
297 }
298 else
299 {
300 eZDebug::writeWarning( "'$handlerName' output handler for tag <$element->nodeName> doesn't exist: '" . $thisOutputTag[$handlerName] . "'.", 'eZXML converter' );
301 }
302 }
303 return $result;
304 }
305
306 /*
307 Tag handlers
308 */
309 function outputSection( $element, &$attributes, &$sectionLevel )
310 {
311 $sectionLevel++;
312
313 $ret = '';
314 return $ret;
315 }
316
317 function outputText( $element, &$attributes, &$sectionLevel )
318 {
319 $text = $element->textContent;
320
321 if ( $element->parentNode->nodeName != 'literal' )
322 {
323 $text = htmlspecialchars( $text );
324 $text = str_replace ( '&amp;nbsp;', '&nbsp;', $text);
325 $text = str_replace( "\n", '', $text );
326 }
327 return $text;
328 }
329
330 function outputTd( $element, &$attributes, &$sectionLevel )
331 {
332 $ret = null;
333
334 // We have to reset section level in the table cell
335 $sectionLevel = 0;
336 return $ret;
337 }
338
339 function outputHeader( $element, &$attributes, &$sectionLevel )
340 {
341 $ret = null;
342 $attributes['level'] = $sectionLevel;
343 return $ret;
344 }
345
346 function outputParagraph( $element, &$attributes, &$sectionLevel )
347 {
348 $ret = null;
349 if ( count( $attributes ) == 0 )
350 {
351 $next = $element->nextSibling;
352 if ( $next )
353 {
354 $ret = "\n\n";
355 }
356 else
357 {
358 $ret = "";
359 $parent = $element->parentNode;
360 while( $parent && $parent->nodeName == 'section' )
361 {
362 $next = $parent->nextSibling;
363 if ( $next )
364 {
365 $ret = "\n\n";
366 break;
367 }
368 $parent = $parent->parentNode;
369 }
370 }
371 }
372 return $ret;
373 }
374
375 function outputLine( $element, &$attributes, &$sectionLevel )
376 {
377 $ret = '';
378 $next = $element->nextSibling;
379 if ( is_object( $next ) )
380 {
381 $ret = "\n";
382 }
383 return $ret;
384 }
385
386 function outputEmbed( $element, &$attributes, &$sectionLevel )
387 {
388 $ret = null;
389 $href = '';
390 $objectID = isset( $attributes['object_id'] ) ? $attributes['object_id'] : null;
391 $nodeID = isset( $attributes['node_id'] ) ? $attributes['node_id'] : null;
392 $showPath = isset( $attributes['show_path'] ) ? $attributes['show_path'] : null;
393 if ( $objectID )
394 {
395 $href = 'ezobject://' .$objectID;
396 }
397 elseif ( $nodeID )
398 {
399 $href = eZSimplifiedXMLEditOutput::eznodeHref( $nodeID, $showPath );
400 }
401 $attributes['href'] = $href;
402 return $ret;
403 }
404
405 function outputObject( $element, &$attributes, &$sectionLevel )
406 {
407 $ret = null;
408 if ( isset( $attributes['image:ezurl_id'] ) )
409 {
410 $linkID = $attributes['image:ezurl_id'];
411 if ( $linkID != null )
412 {
413 $href = eZURL::url( $linkID );
414 $attributes['href'] = $href;
415 }
416 }
417 return $ret;
418 }
419
420 function outputLink( $element, &$attributes, &$sectionLevel )
421 {
422 $ret = null;
423 $href = '';
424 $linkID = isset( $attributes['url_id'] ) ? $attributes['url_id'] : null;
425 $objectID = isset( $attributes['object_id'] ) ? $attributes['object_id'] : null;
426 $nodeID = isset( $attributes['node_id'] ) ? $attributes['node_id'] : null;
427 $anchorName = isset( $attributes['anchor_name'] ) ? $attributes['anchor_name'] : null;
428 $showPath = isset( $attributes['show_path'] ) ? $attributes['show_path'] : null;
429
430 if ( $objectID )
431 {
432 $href = 'ezobject://' .$objectID;
433 }
434 elseif ( $nodeID )
435 {
436 $href = eZSimplifiedXMLEditOutput::eznodeHref( $nodeID, $showPath );
437 }
438 elseif ( $linkID )
439 {
440 // Fetch URL from cached array
441 $href = $this->LinkArray[$linkID];
442 }
443 else
444 {
445 $href = isset( $attributes['href'] ) ? $attributes['href'] : '';
446 }
447
448 if ( $anchorName != null )
449 {
450 $href .= '#' . $anchorName;
451 }
452
453 $attributes['href'] = $href;
454 return $ret;
455 }
456
460 static function eznodeHref( $nodeID, $showPath )
461 {
462 if ( $showPath == 'true' )
463 {
464 $node = eZContentObjectTreeNode::fetch( $nodeID, false, false );
465 $href = $node ? 'eznode://' . $node['path_identification_string'] : 'eznode://' . $nodeID;
466 }
467 else
468 {
469 $href = 'eznode://' . $nodeID;
470 }
471
472 return $href;
473 }
474
478 function createLinksArray( $dom )
479 {
480 $links = array();
481 $node = array();
482
483 if ( $dom )
484 {
485 // Fetch all links and cache the url's
486 $links = $dom->getElementsByTagName( "link" );
487 }
488
489 $linkIDArray = array();
490 // Find all Link id's
491 foreach ( $links as $link )
492 {
493 $linkIDValue = $link->getAttribute( 'url_id' );
494 if ( !$linkIDValue )
495 {
496 continue;
497 }
498 if ( !in_array( $linkIDValue, $linkIDArray ) )
499 {
500 $linkIDArray[] = $linkIDValue;
501 }
502 }
503
504 if ( count( $linkIDArray ) > 0 )
505 {
506 $inIDSQL = implode( ', ', $linkIDArray );
507 $db = eZDB::instance();
508 $linkArray = $db->arrayQuery( "SELECT * FROM ezurl WHERE id IN ( $inIDSQL ) " );
509 foreach ( $linkArray as $linkRow )
510 {
511 $this->LinkArray[$linkRow['id']] = $linkRow['url'];
512 }
513 }
514 }
515
516 public $XMLSchema;
517 public $Output = '';
518 public $NestingLevel = 0;
519
520 // Contains all links hashed by ID
521 public $LinkArray = array();
522 }
523 ?>
eZContentObjectTreeNode\fetch
static fetch( $nodeID=false, $lang=false, $asObject=true, $conditions=false)
Definition: ezcontentobjecttreenode.php:3058
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZSimplifiedXMLEditOutput
Definition: ezsimplifiedxmleditoutput.php:13
eZSimplifiedXMLEditOutput\outputLink
outputLink( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:420
eZSimplifiedXMLEditOutput\performOutput
performOutput( $dom)
Definition: ezsimplifiedxmleditoutput.php:71
eZSimplifiedXMLEditOutput\$Output
$Output
Definition: ezsimplifiedxmleditoutput.php:517
eZSimplifiedXMLEditOutput\outputObject
outputObject( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:405
eZSimplifiedXMLEditOutput\outputHeader
outputHeader( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:339
eZSimplifiedXMLEditOutput\formatBeforeClosingTag
formatBeforeClosingTag( $element, $isInline, $hasChildren)
Definition: ezsimplifiedxmleditoutput.php:236
eZSimplifiedXMLEditOutput\callOutputHandler
callOutputHandler( $handlerName, $element, &$params, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:287
eZSimplifiedXMLEditOutput\outputParagraph
outputParagraph( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:346
eZSimplifiedXMLEditOutput\formatBeforeOpeningTag
formatBeforeOpeningTag( $element, $isInline, $hasChildren)
Definition: ezsimplifiedxmleditoutput.php:203
eZSimplifiedXMLEditOutput\$XMLSchema
$XMLSchema
Definition: ezsimplifiedxmleditoutput.php:516
eZSimplifiedXMLEditOutput\eznodeHref
static eznodeHref( $nodeID, $showPath)
Definition: ezsimplifiedxmleditoutput.php:460
eZSimplifiedXMLEditOutput\createLinksArray
createLinksArray( $dom)
Definition: ezsimplifiedxmleditoutput.php:478
eZSimplifiedXMLEditOutput\$LinkArray
$LinkArray
Definition: ezsimplifiedxmleditoutput.php:521
eZSimplifiedXMLEditOutput\$NestingLevel
$NestingLevel
Definition: ezsimplifiedxmleditoutput.php:518
eZSimplifiedXMLEditOutput\outputTd
outputTd( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:330
eZSimplifiedXMLEditOutput\outputTag
outputTag( $element, $sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:85
eZSimplifiedXMLEditOutput\outputSection
outputSection( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:309
eZSimplifiedXMLEditOutput\formatAfterOpeningTag
formatAfterOpeningTag( $element, $isInline, $hasChildren)
Definition: ezsimplifiedxmleditoutput.php:215
eZSimplifiedXMLEditOutput\outputEmbed
outputEmbed( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:386
eZSimplifiedXMLEditOutput\$OutputTags
$OutputTags
Definition: ezsimplifiedxmleditoutput.php:14
eZSimplifiedXMLEditOutput\outputLine
outputLine( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:375
eZSimplifiedXMLEditOutput\outputText
outputText( $element, &$attributes, &$sectionLevel)
Definition: ezsimplifiedxmleditoutput.php:317
eZSimplifiedXMLEditOutput\formatAfterClosingTag
formatAfterClosingTag( $element, $isInline, $hasChildren)
Definition: ezsimplifiedxmleditoutput.php:261
eZURL\url
static url( $id, $onlyValid=false)
Definition: ezurl.php:374
eZXMLSchema\instance
static instance()
Definition: ezxmlschema.php:223
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.