00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 include_once( "lib/ezxml/classes/ezxml.php" );
00032
00033 if ( !class_exists( 'eZXMLSchema' ) )
00034 include_once( 'kernel/classes/datatypes/ezxmltext/ezxmlschema.php' );
00035
00036 class eZSimplifiedXMLEditOutput
00037 {
00038 var $OutputTags = array(
00039
00040 'section' => array( 'handler' => 'outputSection' ),
00041
00042 'embed' => array( 'handler' => 'outputEmbed',
00043 'attributes' => array( 'xhtml:id' => 'id',
00044 'object_id' => false,
00045 'node_id' => false,
00046 'show_path' => false ),
00047 'isSingle' => true ),
00048
00049 'embed-inline' => array( 'handler' => 'outputEmbed',
00050 'attributes' => array( 'xhtml:id' => 'id',
00051 'object_id' => false,
00052 'node_id' => false,
00053 'show_path' => false ),
00054 'isSingle' => true ),
00055
00056 'object' => array( 'handler' => 'outputObject',
00057 'attributes' => array( 'xhtml:id' => 'id',
00058 'image:ezurl_target' => 'target',
00059 'image:ezurl_href' => 'href',
00060 'image:ezurl_id' => false ),
00061 'isSingle' => true ),
00062
00063 'td' => array( 'handler' => 'outputTd',
00064 'attributes' => array( 'xhtml:width' => 'width',
00065 'xhtml:colspan' => 'colspan',
00066 'xhtml:rowspan' => 'rowspan' ) ),
00067
00068 'th' => array( 'handler' => 'outputTd',
00069 'attributes' => array( 'xhtml:width' => 'width',
00070 'xhtml:colspan' => 'colspan',
00071 'xhtml:rowspan' => 'rowspan' ) ),
00072
00073 'header' => array( 'handler' => 'outputHeader' ),
00074
00075 'paragraph' => array( 'handler' => 'outputParagraph' ),
00076
00077 'line' => array( 'handler' => 'outputLine' ),
00078
00079 'link' => array( 'handler' => 'outputLink',
00080 'attributes' => array( 'xhtml:id' => 'id',
00081 'xhtml:title' => 'title',
00082 'url_id' => false,
00083 'object_id' => false,
00084 'node_id' => false,
00085 'show_path' => false,
00086 'ezurl_id' => false,
00087 'anchor_name' => false ) ),
00088 'anchor' => array( 'isSingle' => true ),
00089
00090 '#text' => array( 'handler' => 'outputText' )
00091 );
00092
00093
00094
00095 function performOutput( &$dom )
00096 {
00097 $this->XMLSchema =& eZXMLSchema::instance();
00098 $this->NestingLevel = 0;
00099 $this->Output = '';
00100 $sectionLevel = -1;
00101
00102 $this->createLinksArray( $dom );
00103
00104 if ( is_object( $dom->Root ) )
00105 $this->outputTag( $dom->Root, $sectionLevel );
00106
00107 return $this->Output;
00108 }
00109
00110 function outputTag( &$element, $sectionLevel )
00111 {
00112 $tagName = $element->nodeName;
00113 if ( isset( $this->OutputTags[$tagName] ) )
00114 {
00115 $currentTag =& $this->OutputTags[$tagName];
00116
00117 if ( isset( $currentTag['attributes'] ) )
00118 $attributeRules = $currentTag['attributes'];
00119 }
00120 else
00121 $currentTag = null;
00122
00123
00124 $attributeNodes = $element->attributes();
00125 $attributes = array();
00126 foreach( $attributeNodes as $attrNode )
00127 {
00128 if ( $attrNode->Prefix && $attrNode->Prefix != 'custom' )
00129 $attrName = $attrNode->Prefix . ':' . $attrNode->LocalName;
00130 else
00131 $attrName = $attrNode->nodeName;
00132
00133 $attributes[$attrName] = $attrNode->value;
00134 }
00135
00136
00137 $result = null;
00138 if ( $currentTag && isset( $currentTag['handler'] ) )
00139 {
00140 $result =& $this->callOutputHandler( 'handler', $element, $attributes, $sectionLevel );
00141 }
00142
00143 $hasChildren = $element->hasChildNodes();
00144 $isInline = $this->XMLSchema->isInline( $element );
00145 $isSingle = isset( $currentTag['isSingle'] ) && $currentTag['isSingle'];
00146
00147
00148 if ( !is_string( $result ) )
00149 {
00150
00151 $attrString = '';
00152 foreach( array_keys( $attributes ) as $name )
00153 {
00154 $value = $attributes[$name];
00155 if ( isset( $attributeRules ) && isset( $attributeRules[$name] ) )
00156 {
00157 if ( !$attributeRules[$name] )
00158 continue;
00159
00160 $name = $attributeRules[$name];
00161 }
00162 $attrString .= ' ' . $name . '="' . $value . '"';
00163 }
00164
00165 $this->formatBeforeOpeningTag( $element, $isInline, $hasChildren );
00166
00167
00168 if ( $isSingle )
00169 $closing = ' />';
00170 else
00171 $closing = '>';
00172 $this->Output .= '<' . $tagName . $attrString . $closing;
00173
00174 if ( !$isSingle )
00175 $this->formatAfterOpeningTag( $element, $isInline, $hasChildren );
00176
00177 $this->NestingLevel++;
00178 }
00179
00180
00181 foreach( array_keys( $element->Children ) as $key )
00182 {
00183 $child =& $element->Children[$key];
00184 $this->outputTag( $child, $sectionLevel );
00185 }
00186
00187 if ( is_string( $result ) )
00188 {
00189 $this->Output .= $result;
00190 return;
00191 }
00192 else
00193 {
00194 $this->NestingLevel--;
00195 if ( !$isSingle )
00196 {
00197 $this->formatBeforeClosingTag( $element, $isInline, $hasChildren );
00198
00199 $this->Output .= '</' . $tagName . '>';
00200 }
00201
00202 $this->formatAfterClosingTag( $element, $isInline, $hasChildren );
00203 }
00204
00205 return;
00206 }
00207
00208 function formatBeforeOpeningTag( &$element, $isInline, $hasChildren )
00209 {
00210
00211 if ( !$isInline )
00212 {
00213 if ( $this->NestingLevel > 0 )
00214 {
00215 $this->Output .= str_repeat( ' ', $this->NestingLevel );
00216 }
00217 }
00218 }
00219
00220 function formatAfterOpeningTag( &$element, $isInline, $hasChildren )
00221 {
00222
00223 if ( !$isInline && $hasChildren )
00224 {
00225 $firstChild =& $element->firstChild();
00226 if ( $firstChild && $firstChild->nodeName == 'paragraph' && !$firstChild->hasAttributes() )
00227 {
00228 $tmp =& $firstChild;
00229 $firstChild =& $tmp->firstChild();
00230 }
00231 if ( $firstChild && $firstChild->nodeName == 'line' )
00232 {
00233 $tmp =& $firstChild;
00234 $firstChild =& $tmp->firstChild();
00235 }
00236 if ( $firstChild && !$this->XMLSchema->isInline( $firstChild ) )
00237 $this->Output .= "\n";
00238 }
00239 }
00240
00241 function formatBeforeClosingTag( &$element, $isInline, $hasChildren )
00242 {
00243 if ( !$isInline && $hasChildren )
00244 {
00245 $lastChild =& $element->lastChild();
00246 if ( $lastChild && $lastChild->nodeName == 'paragraph' && !$lastChild->hasAttributes() )
00247 {
00248 $tmp =& $lastChild;
00249 $lastChild =& $tmp->lastChild();
00250 }
00251 if ( $lastChild && $lastChild->nodeName == 'line' )
00252 {
00253 $tmp =& $lastChild;
00254 $lastChild =& $tmp->lastChild();
00255 }
00256 if ( $lastChild && !$this->XMLSchema->isInline( $lastChild ) )
00257 {
00258
00259 $this->Output .= "\n";
00260 if ( $this->NestingLevel > 0 )
00261 {
00262 $this->Output .= str_repeat( ' ', $this->NestingLevel );
00263 }
00264 }
00265 }
00266 }
00267
00268 function formatAfterClosingTag( &$element, $isInline, $hasChildren )
00269 {
00270 if ( !$isInline )
00271 {
00272 $next =& $element->nextSibling();
00273 if ( $next )
00274 {
00275 $this->Output .= "\n";
00276 }
00277 else
00278 {
00279 $parent =& $element->parentNode;
00280 while( $parent && $parent->nodeName == 'section' )
00281 {
00282 $next =& $parent->nextSibling();
00283 if ( $next )
00284 {
00285 $this->Output .= "\n";
00286 break;
00287 }
00288 $parent =& $parent->parentNode;
00289 }
00290 }
00291 }
00292 }
00293
00294 function &callOutputHandler( $handlerName, &$element, &$params, &$sectionLevel )
00295 {
00296 $result = null;
00297 $thisOutputTag =& $this->OutputTags[$element->nodeName];
00298 if ( isset( $thisOutputTag[$handlerName] ) )
00299 {
00300 if ( is_callable( array( $this, $thisOutputTag[$handlerName] ) ) )
00301 eval( '$result =& $this->' . $thisOutputTag[$handlerName] . '( $element, $params, $sectionLevel );' );
00302 else
00303 eZDebug::writeWarning( "'$handlerName' output handler for tag <$element->nodeName> doesn't exist: '" . $thisOutputTag[$handlerName] . "'.", 'eZXML converter' );
00304 }
00305 return $result;
00306 }
00307
00308
00309
00310
00311 function &outputSection( &$element, &$attributes, &$sectionLevel )
00312 {
00313 $sectionLevel++;
00314
00315 $ret = '';
00316 return $ret;
00317 }
00318
00319 function &outputText( &$element, &$attributes, &$sectionLevel )
00320 {
00321 $text = $element->content();
00322
00323 if ( $element->parentNode->nodeName == 'literal' )
00324 {
00325 $text = str_replace("<", "<", $text );
00326 $text = str_replace(">", ">", $text );
00327 $text = str_replace("'", "'", $text );
00328 $text = str_replace(""", '"', $text );
00329 $text = str_replace("&", "&", $text );
00330 }
00331 else
00332 {
00333 $text = str_replace(">", ">", $text );
00334 $text = str_replace("'", "'", $text );
00335 $text = str_replace(""", '"', $text );
00336
00337
00338 $text = preg_replace("#&(?!lt;|gt;|amp;|'|")#", "&", $text );
00339
00340 $text = preg_replace( "#[\n]+#", "", $text );
00341 }
00342 return $text;
00343 }
00344
00345 function &outputTd( &$element, &$attributes, &$sectionLevel )
00346 {
00347 $ret = null;
00348
00349
00350 $sectionLevel = 0;
00351 return $ret;
00352 }
00353
00354 function &outputHeader( &$element, &$attributes, &$sectionLevel )
00355 {
00356 $ret = null;
00357 $attributes['level'] = $sectionLevel;
00358 return $ret;
00359 }
00360
00361 function &outputParagraph( &$element, &$attributes, &$sectionLevel )
00362 {
00363 $ret = null;
00364 if ( count( $attributes ) == 0 )
00365 {
00366 $next =& $element->nextSibling();
00367 if ( $next )
00368 {
00369 $ret = "\n\n";
00370 }
00371 else
00372 {
00373 $ret = "";
00374 $parent =& $element->parentNode;
00375 while( $parent && $parent->nodeName == 'section' )
00376 {
00377 $next =& $parent->nextSibling();
00378 if ( $next )
00379 {
00380 $ret = "\n\n";
00381 break;
00382 }
00383 $parent =& $parent->parentNode;
00384 }
00385 }
00386 }
00387 return $ret;
00388 }
00389
00390 function &outputLine( &$element, &$attributes, &$sectionLevel )
00391 {
00392 $ret = '';
00393 $next =& $element->nextSibling();
00394 if ( is_object( $next ) )
00395 {
00396 $ret = "\n";
00397 }
00398 return $ret;
00399 }
00400
00401 function &outputEmbed( &$element, &$attributes, &$sectionLevel )
00402 {
00403 $ret = null;
00404 $href = '';
00405 $objectID = isset( $attributes['object_id'] ) ? $attributes['object_id'] : null;
00406 $nodeID = isset( $attributes['node_id'] ) ? $attributes['node_id'] : null;
00407 $showPath = isset( $attributes['show_path'] ) ? $attributes['show_path'] : null;
00408 if ( $objectID )
00409 {
00410 $href = 'ezobject://' .$objectID;
00411 }
00412 elseif ( $nodeID )
00413 {
00414 if ( $showPath == 'true' )
00415 {
00416 $node = eZContentObjectTreeNode::fetch( $nodeID, false, false);
00417 if ( $node )
00418 $href = 'eznode://' . $node['path_identification_string'];
00419 else
00420 $href = 'eznode://' . $nodeID;
00421 }
00422 else
00423 $href = 'eznode://' . $nodeID;
00424 }
00425 $attributes['href'] = $href;
00426 return $ret;
00427 }
00428
00429 function &outputObject( &$element, &$attributes, &$sectionLevel )
00430 {
00431 $ret = null;
00432 if ( isset( $attributes['image:ezurl_id'] ) )
00433 {
00434 $linkID = $attributes['image:ezurl_id'];
00435 if ( $linkID != null )
00436 {
00437 $href = eZURL::url( $linkID );
00438 $attributes['href'] = $href;
00439 }
00440 }
00441 return $ret;
00442 }
00443
00444 function &outputLink( &$element, &$attributes, &$sectionLevel )
00445 {
00446 $ret = null;
00447 $href = '';
00448 $linkID = isset( $attributes['url_id'] ) ? $attributes['url_id'] : null;
00449 $objectID = isset( $attributes['object_id'] ) ? $attributes['object_id'] : null;
00450 $nodeID = isset( $attributes['node_id'] ) ? $attributes['node_id'] : null;
00451 $anchorName = isset( $attributes['anchor_name'] ) ? $attributes['anchor_name'] : null;
00452 $showPath = isset( $attributes['show_path'] ) ? $attributes['show_path'] : null;
00453
00454 if ( $objectID != null )
00455 {
00456 $href = 'ezobject://' .$objectID;
00457 }
00458 elseif ( $nodeID != null )
00459 {
00460 if ( $showPath == 'true' )
00461 {
00462 $node = eZContentObjectTreeNode::fetch( $nodeID, false, false );
00463 if ( $node )
00464 $href = 'eznode://' . $node['path_identification_string'];
00465 else
00466 $href = 'eznode://' . $nodeID;
00467 }
00468 else
00469 $href = 'eznode://' . $nodeID;
00470 }
00471 elseif ( $linkID != null )
00472 {
00473
00474 $href = $this->LinkArray[$linkID];
00475 }
00476 else
00477 {
00478 $href = isset( $attributes['href'] ) ? $attributes['href'] : '';
00479 }
00480
00481 if ( $anchorName != null )
00482 {
00483 $href .= '#' . $anchorName;
00484 }
00485
00486 $attributes['href'] = $href;
00487 return $ret;
00488 }
00489
00490
00491
00492 function createLinksArray( &$dom )
00493 {
00494 $links = array();
00495 $node = array();
00496
00497 if ( $dom )
00498 {
00499 $node =& $dom->elementsByName( "section" );
00500
00501
00502 $links =& $dom->elementsByName( "link" );
00503 }
00504 if ( count( $links ) > 0 )
00505 {
00506 $linkIDArray = array();
00507
00508 foreach ( $links as $link )
00509 {
00510 $linkIDValue = $link->attributeValue( 'url_id' );
00511 if ( !$linkIDValue )
00512 continue;
00513 if ( !in_array( $linkIDValue, $linkIDArray ) )
00514 $linkIDArray[] = $linkIDValue;
00515 }
00516
00517 if ( count($linkIDArray) > 0 )
00518 {
00519 $inIDSQL = implode( ', ', $linkIDArray );
00520 $db =& eZDB::instance();
00521 $linkArray = $db->arrayQuery( "SELECT * FROM ezurl WHERE id IN ( $inIDSQL ) " );
00522 foreach ( $linkArray as $linkRow )
00523 {
00524 $this->LinkArray[$linkRow['id']] = $linkRow['url'];
00525 }
00526 }
00527 }
00528 }
00529
00530 var $XMLSchema;
00531 var $Output = '';
00532 var $NestingLevel = 0;
00533
00534
00535 var $LinkArray = array();
00536 }
00537 ?>