Exemplo n.º 1
0
 /**
  * Link terms in a dom page object in bb style
  *
  * @param
  * @return
  */
 protected static function linkTermsInDom($a_dom, $a_terms, $a_par_node = null)
 {
     // sort terms by their length (shortes first)
     // to prevent that nested tags are builded
     foreach ($a_terms as $k => $t) {
         $a_terms[$k]["termlength"] = strlen($t["term"]);
     }
     $a_terms = ilUtil::sortArray($a_terms, "termlength", "asc", true);
     if ($a_dom instanceof php4DOMDocument) {
         $a_dom = $a_dom->myDOMDocument;
     }
     if ($a_par_node instanceof php4DOMElement) {
         $a_par_node = $a_par_node->myDOMNode;
     }
     $xpath = new DOMXPath($a_dom);
     if ($a_par_node == null) {
         $parnodes = $xpath->query('//Paragraph');
     } else {
         $parnodes = $xpath->query('//Paragraph', $a_par_node);
     }
     include_once "./Services/Utilities/classes/class.ilStr.php";
     foreach ($parnodes as $parnode) {
         $textnodes = $xpath->query('//text()', $parnode);
         foreach ($textnodes as $node) {
             $p = $node->getNodePath();
             // we do not change text nodes inside of links
             if (!is_int(strpos($p, "/IntLink")) && !is_int(strpos($p, "/ExtLink"))) {
                 $node_val = $node->nodeValue;
                 // all terms
                 foreach ($a_terms as $t) {
                     $pos = ilStr::strIPos($node_val, $t["term"]);
                     // if term found
                     while (is_int($pos)) {
                         // check if the string is not included in another word
                         // note that []
                         $valid_limiters = array("", " ", " ", ".", ",", ":", ";", "!", "?", "\"", "'", "(", ")");
                         $b = $pos > 0 ? ilStr::subStr($node_val, $pos - 1, 1) : "";
                         $a = ilStr::subStr($node_val, $pos + ilStr::strLen($t["term"]), 1);
                         if ((in_array($b, $valid_limiters) || htmlentities($b, null, 'utf-8') == " ") && in_array($a, $valid_limiters)) {
                             $mid = '[iln term="' . $t["id"] . '"]' . ilStr::subStr($node_val, $pos, ilStr::strLen($t["term"])) . "[/iln]";
                             $node_val = ilStr::subStr($node_val, 0, $pos) . $mid . ilStr::subStr($node_val, $pos + ilStr::strLen($t["term"]));
                             $pos += ilStr::strLen($mid);
                         } else {
                             $pos += ilStr::strLen($t["term"]);
                         }
                         $pos = ilStr::strIPos($node_val, $t["term"], $pos);
                     }
                     // insert [iln] tags
                 }
                 $node->nodeValue = $node_val;
             }
             //			var_dump($p);
             //			var_dump($node->nodeValue);
         }
         // dump paragraph node
         $text = $a_dom->saveXML($parnode);
         $text = substr($text, 0, strlen($text) - strlen("</Paragraph>"));
         $text = substr($text, strpos($text, ">") + 1);
         // replace [iln] by tags with xml representation
         $text = self::intLinks2xml($text);
         // "set text"
         $temp_dom = domxml_open_mem('<?xml version="1.0" encoding="UTF-8"?><Paragraph>' . $text . '</Paragraph>', DOMXML_LOAD_PARSING, $error);
         $temp_dom = $temp_dom->myDOMDocument;
         if (empty($error)) {
             // delete children of paragraph node
             $children = $parnode->childNodes;
             while ($parnode->hasChildNodes()) {
                 $parnode->removeChild($parnode->firstChild);
             }
             // copy new content children in paragraph node
             $xpath_temp = new DOMXPath($temp_dom);
             $temp_pars = $xpath_temp->query("//Paragraph");
             foreach ($temp_pars as $new_par_node) {
                 $new_childs = $new_par_node->childNodes;
                 foreach ($new_childs as $new_child) {
                     //$cloned_child = $new_child->cloneNode(true);
                     $cloned_child = $a_dom->importNode($new_child, true);
                     $parnode->appendChild($cloned_child);
                 }
             }
         }
     }
 }