00001 <?php
00010 class STransStdXMLHelper
00011 {
00012 protected $parent;
00013 protected $translator;
00014
00022 public function doc() {
00023 return "STransStdXMLHelper is a class that helps STransStdXML"
00024 . " perform its translation.";
00025 }
00026
00027 function __construct ($parent)
00028 {
00029 $this->parent = $parent;
00030 }
00031
00032
00041 public function setTranslator($t) {
00042 $this->translator = $t;
00043 }
00044
00052 public function getTranslator() {
00053 return $t;
00054 }
00055
00064 public function formatSectionHtml($section)
00065 {
00066 $htmlOutput = "<div class=\"section\">";
00067 if (isset($section->name)){
00068 $htmlOutput .= "<div class=\"sectionName\">" . $section->name . "</div>";
00069 }
00070 if (isset($section->block)){
00071 foreach ($section->block as $currentBlock) {
00072 $htmlOutput .= "<div class=\"block\" ";
00073 if (isset($currentBlock['align'])){
00074 $htmlOutput .= "align=\"" . $currentBlock['align'] . "\" ";
00075 }
00076 $htmlOutput .= ">";
00077 $htmlOutput .= $this->formatBlockHtml($currentBlock) . "</div>";
00078 }
00079 }
00080 $htmlOutput .= "</div>";
00081 return $htmlOutput;
00082 }
00083
00092 public function formatTextHtml($text)
00093 {
00094 $htmlOutput = "<div class=\"text\" ";
00095
00096 if (isset($text['align'])){
00097 $alignment = $text['align'];
00098 if ($alignment == "indent")
00099 {
00100 $htmlOutput .= "style=\"padding-left:20px;\" ";
00101 } else {
00102 $htmlOutput .= "align=\"" . $alignment . "\" ";
00103 }
00104 }
00105 $htmlOutput .= ">";
00106 $htmlOutput .= $this->formatParagraphHtml($text) . "</div>";
00107
00108 return $htmlOutput;
00109 }
00110
00119 public function formatBlockHtml($block)
00120 {
00121 $htmlOutput = "";
00122
00123
00124 if (count($block->xpath('ul')) > 0 || count($block->xpath('ol')) > 0 || count($block->xpath('table')) > 0 || count($block->xpath('text')) > 0){
00125
00126 foreach ($block->children() as $tag => $child){
00127 switch($tag){
00128 case 'ul':
00129 $htmlOutput .= $this->formatUl($child);
00130 break;
00131 case 'ol':
00132 $htmlOutput .= $this->formatOl($child);
00133 break;
00134 case 'table':
00135 $htmlOutput .= $this->formatTable($child);
00136 break;
00137 case 'text':
00138 $htmlOutput .= $this->formatTextHtml($child);
00139 break;
00140 default:
00141 break;
00142 }
00143 }
00144 } else {
00145
00146 $htmlOutput .= $this->formatParagraphHtml($block);
00147 }
00148 return $htmlOutput;
00149 }
00150
00159 public function formatUl($child)
00160 {
00161 $htmlOutput = "<ul>";
00162 foreach($child->li as $currentLi)
00163 {
00164 $htmlOutput .= "<li> " . $this->formatBlockHtml($currentLi) . "</li>";
00165 }
00166 $htmlOutput .= "</ul>";
00167 return $htmlOutput;
00168 }
00169
00178 public function formatOl($child)
00179 {
00180 $htmlOutput = "<ol>";
00181 foreach($child->li as $currentLi)
00182 {
00183 $htmlOutput .= "<li> " . $this->formatBlockHtml($currentLi) . "</li>";
00184 }
00185 $htmlOutput .= "</ol>";
00186 return $htmlOutput;
00187 }
00188
00197 public function formatTable($child)
00198 {
00199 $htmlOutput = "<table class=\"contentTable\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >";
00200 foreach ($child->children() as $tag => $content){
00201 switch($tag){
00202 case 'tr':
00203 $htmlOutput .= $this->formatTr($content);
00204 break;
00205 case 'tbody':
00206 $htmlOutput .= $this->formatTbody($content);
00207 break;
00208 default:
00209 break;
00210 }
00211 }
00212 $htmlOutput .= "</table>";
00213 return $htmlOutput;
00214 }
00215
00224 public function formatTbody($child)
00225 {
00226 $htmlOutput = "<tbody>";
00227 foreach ($child->children() as $tag => $content){
00228 switch($tag){
00229 case 'tr':
00230 $htmlOutput .= $this->formatTr($content);
00231 break;
00232 default:
00233 break;
00234 }
00235 }
00236 $htmlOutput .= "</tbody>";
00237 return $htmlOutput;
00238 }
00239
00248 public function formatTr($child)
00249 {
00250 $htmlOutput = "<tr>";
00251
00252 foreach ($child->children() as $tag => $content){
00253 switch($tag){
00254 case 'td':
00255 case 'th':
00256
00257 $attributes = "";
00258 if (isset ($content['colspan']))
00259 {
00260 $attributes .= " colspan=\"" . $content['colspan'] . "\"";
00261 }
00262 $htmlOutput .= "<$tag$attributes> " . $this->formatBlockHtml($content) . "</$tag>";
00263 break;
00264 default:
00265 break;
00266 }
00267 }
00268
00269 $htmlOutput .= "</tr>";
00270 return $htmlOutput;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00290 public function formatParagraphHtml($paragraph)
00291 {
00292
00293
00294
00295 $htmlOutput = "";
00296
00297 $parArray = $this->xmlToArray($paragraph->asXML());
00298
00299 $numParts = count($parArray);
00300 for ($i=0; $i<$numParts; $i++)
00301 {
00302 switch($parArray[$i]['tag'])
00303 {
00304 case "BLOCK":
00305 case "TD":
00306 case "TH":
00307 case "LI":
00308 case "TEXT":
00309 case "TITLE":
00310 $htmlOutput .= $this->formatParagraphTextHtml($parArray[$i]);
00311 break;
00312 case "IMAGE":
00313
00314 $htmlOutput .= $this->formatParagraphImageHtml($parArray[$i]);
00315 break;
00316 case "BOLD":
00317 case "B":
00318 $htmlOutput .= $this->formatParagraphBoldHtml($parArray[$i]);
00319 break;
00320 case "ITALIC":
00321 case "I":
00322 $htmlOutput .= $this->formatParagraphItalicHtml($parArray[$i]);
00323 break;
00324 case "U":
00325 $htmlOutput .= $this->formatParagraphUnderlineHtml($parArray[$i]);
00326 break;
00327 case "SUB":
00328 $htmlOutput .= $this->formatParagraphSubHtml($parArray[$i]);
00329 break;
00330 case "SUP":
00331 $htmlOutput .= $this->formatParagraphSupHtml($parArray[$i]);
00332 break;
00333 case "STRIKE":
00334 $htmlOutput .= $this->formatParagraphStrikeHtml($parArray[$i]);
00335 break;
00336 case "LINK":
00337 $htmlOutput .= $this->formatParagraphLinkHtml($parArray[$i]);
00338 break;
00339 case "BR":
00340 $htmlOutput .= "<br />";
00341 break;
00342 case "SPACE":
00343 $htmlOutput .= " ";
00344 break;
00345 case "MULTISPACE":
00346 $htmlOutput .= $this->formatMultispace($parArray[$i]);
00347 break;
00348 case "TEXTCOLOR":
00349 case "COLOR":
00350 $htmlOutput .= $this->formatParagraphTextColor($parArray[$i]);
00351 break;
00352 case "LATEX":
00353 $htmlOutput .= " <br />[[LaTEX Equation will appear here]]<br /> ";
00354 break;
00355 case "MEDIA":
00356 $htmlOutput .= $this->translateMedia($parArray[$i]);
00357 break;
00358 case "CONTENT":
00359 $htmlOutput .= $this->translateContent($parArray[$i]);
00360 break;
00361 case 'ENTITY':
00362 $htmlOutput .= '&' . (string)$parArray[$i]['value'] . ';';
00363 break;
00364 default:
00365 $htmlOutput .= $this->moreFormatOptions($parArray[$i]);
00366 break;
00367 }
00368 }
00369
00370 return ($htmlOutput);
00371 }
00372
00381 public function translateMedia($partArray) {
00382 $snapid = $partArray['attributes']['SNAPID'];
00383 if(isset($partArray['attributes']['WHICH']))
00384 $which = $partArray['attributes']['WHICH'];
00385 else
00386 $which = "original";
00387
00388 if(strpos($snapid, "/") !== false)
00389 $res = SnapResource::lookup($snapid);
00390 else {
00391 $res = SnapResource::retrieve($snapid);
00392 }
00393
00394 if($res != null) {
00395 $version = $res->getActiveVersion();
00396
00397 $html = $version->getContentModule()->retrieveContent(
00398 $version->getContent(), array('which' => $which));
00399 }
00400 else
00401 $html = false;
00402
00403 if($html === false)
00404 $html = "<div style=\"padding: 4px; border: 1px solid black; "
00405 . "background-color: #E0E0E0; width: 8em; text-align: center;\">No Content</div>";
00406
00407 return $html;
00408 }
00409
00418 public function translateContent($partArray) {
00419 $snapid = $partArray['attributes']['SNAPID'];
00420 if(isset($partArray['attributes']['WHICH']))
00421 $which = $partArray['attributes']['WHICH'];
00422 else
00423 $which = "original";
00424
00425 if(strpos($snapid, "/") !== false)
00426 $res = SnapResource::lookup($snapid);
00427 else {
00428 $res = new SnapResource($snapid);
00429 if(!$res->isValid())
00430 $res = null;
00431 }
00432
00433 if($res != null) {
00434 $version = $res->getActiveVersion();
00435
00436 $html = $version->getContentModule()->retrieveContent(
00437 $version->getContent(), array('which' => $which));
00438 }
00439 else
00440 $html = false;
00441
00442 if($html === false)
00443 $html = "<div style=\"padding: 4px; border: 1px solid black; "
00444 . "background-color: #E0E0E0; width: 8em; text-align: center;\">No Content</div>";
00445
00446 return $html;
00447 }
00448
00449
00450
00459 public function moreFormatOptions($partArray)
00460 {
00461 switch($partArray['tag'])
00462 {
00463 default:
00464 return "";
00465 break;
00466 }
00467 }
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 function formatMultispace($partArray)
00482 {
00483 $num = 10;
00484 $htmlOutput = "";
00485 if (isset($partArray['attributes']['NUMBER'])) $num = ($partArray['attributes']['NUMBER']);
00486 for ($n = 0; $n < $num; $n++)
00487 {
00488 $htmlOutput .= " ";
00489 }
00490 return $htmlOutput;
00491 }
00492
00493 function formatParagraphImageHtml($partArray)
00494 {
00495 $localImgDir = $this->parent->getPath('relLocalImgDirURL') . "/images";
00496
00497 $width = ""; $height = "";
00498 $start = ""; $end = "";
00499 $alt = "";
00500 $path = "";
00501 $class = "";
00502 $format = "centered";
00503 if (isset($partArray['attributes']['ALT'])) $alt = ($partArray['attributes']['ALT']);
00504 if (isset($partArray['attributes']['PATH'])) $alt = ($partArray['attributes']['PATH']);
00505 if (isset($partArray['attributes']['WIDTH'])) $width = " width =\"" . ($partArray['attributes']['WIDTH']) . "\"";
00506 if (isset($partArray['attributes']['HEIGHT'])) $height = " height =\"" . ($partArray['attributes']['HEIGHT']) . "\" ";
00507 if (isset($partArray['attributes']['CLASS'])) $class = ($partArray['attributes']['CLASS']);
00508
00509
00510 if (isset($partArray['attributes']['FORMAT'])){
00511 $format = $partArray['attributes']['FORMAT'];
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 if (isset($partArray['attributes']['TYPE']))
00526 {
00527 switch ($partArray['attributes']['TYPE']){
00528 case 'fullpath':
00529 return ("$start<img class=\"$class\" src=\"" . $partArray['attributes']['NAME'] . "\" alt=\" \"". $width . $height . "/>$end");
00530 case 'localImg':
00531 case 'localimg':
00532 return ("$start<img class=\"$class\" src=\"" . $localImgDir . "/" . $partArray['attributes']['NAME'] . "\" alt=\" \"". $width . $height . "/>$end");
00533 default;
00534 break;
00535 }
00536 } else {
00537 return "<img />";
00538 }
00539 }
00540
00551 public function formatStandardTag($openTag, $closeTag, $partArray)
00552 {
00553 $text = "";
00554 if (isset($partArray['value'])){ $text = $partArray['value'];}
00555
00556 switch($partArray['type'])
00557 {
00558 case 'open':
00559 return $openTag . $text;
00560 case 'close':
00561 return $text . $closeTag;
00562 case 'complete':
00563 return $openTag . $text . $closeTag;
00564 case 'cdata':
00565 return $text;
00566 default:
00567 return "";
00568 }
00569 }
00570
00579 public function formatParagraphTextHtml($partArray)
00580 {
00581 if (isset($partArray['value'])){
00582 return preg_replace('!<entity>([^<]+)</entity>!', '&$1;', $partArray['value']);
00583
00584 }
00585 }
00586
00595 public function formatParagraphBoldHtml($partArray)
00596 {
00597 $openTag = "<span class=\"bold\">";
00598 $closeTag = "</span>";
00599 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00600 }
00601
00610 public function formatParagraphItalicHtml($partArray)
00611 {
00612 $openTag = "<span class=\"italic\">";
00613 $closeTag = "</span>";
00614 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00615 }
00616
00625 public function formatParagraphUnderlineHtml($partArray)
00626 {
00627 $openTag = "<u>";
00628 $closeTag = "</u>";
00629 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00630 }
00631
00640 public function formatParagraphSupHtml($partArray)
00641 {
00642 $openTag = "<sup>";
00643 $closeTag = "</sup>";
00644 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00645 }
00646
00655 public function formatParagraphSubHtml($partArray)
00656 {
00657 $openTag = "<sub>";
00658 $closeTag = "</sub>";
00659 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00660 }
00661
00670 public function formatParagraphStrikeHtml($partArray)
00671 {
00672 $openTag = "<strike>";
00673 $closeTag = "</strike>";
00674 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00675 }
00676
00677 function formatParagraphTextColor($partArray)
00678 {
00679 $color = "black";
00680 if (isset($partArray['attributes']['COLOR'])) $color = ($partArray['attributes']['COLOR']);
00681 else if (isset($partArray['attributes']['VALUE'])) $color = ($partArray['attributes']['VALUE']);
00682
00683 $openTag = "<span style=\"color:$color; font-wieght:bold;\">";
00684 $closeTag = "</span>";
00685 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00686 }
00687
00696 public function formatParagraphLinkHtml($partArray)
00697 {
00698 $openTag = "<a href=\"#\">";
00699 $closeTag = "</a>";
00700
00701 if ($partArray['type'] == 'open' || $partArray['type'] == 'complete')
00702 {
00703 if (isset($partArray['attributes']['ID'])){
00704 $name = $partArray['attributes']['ID'];
00705 } else {
00706 $name = "#";
00707 }
00708
00709 if (isset($partArray['attributes']['TYPE']))
00710 {
00711 switch ($partArray['attributes']['TYPE']){
00712 case 'hardLink':
00713 case 'hardlink':
00714 case 'fullpath':
00715 $openTag = "<a href=\"" . $name . "\">";
00716 default:
00717 break;
00718 }
00719 }
00720 }
00721 return $this->formatStandardTag($openTag, $closeTag, $partArray);
00722 }
00723
00732 protected function xmlToArray($contents) {
00733 $xml_parser = xml_parser_create();
00734 xml_parse_into_struct($xml_parser, $contents, $ret_arr);
00735 xml_parser_free($xml_parser);
00736 return $ret_arr;
00737 }
00738
00739 }
00740
00741 ?>