Пример #1
0
 /**
  * Insert page toc
  *
  * @param string output
  * @return string output
  */
 function insertPageToc($a_output)
 {
     global $lng;
     include_once "./Services/Utilities/classes/class.ilStr.php";
     // extract all headings
     $offsets = ilStr::strPosAll($a_output, "ilPageTocH");
     $page_heads = array();
     foreach ($offsets as $os) {
         $level = (int) substr($a_output, $os + 10, 1);
         if (in_array($level, array(1, 2, 3))) {
             $anchor = str_replace("TocH", "TocA", substr($a_output, $os, strpos($a_output, "<", $os) - $os - 3));
             // get heading
             $tag_start = stripos($a_output, "<h" . $level . " ", $os);
             $tag_end = stripos($a_output, "</h" . $level . ">", $tag_start);
             $head = substr($a_output, $tag_start, $tag_end - $tag_start);
             // get headings text
             $text_start = stripos($head, ">") + 1;
             $text_end = strripos($head, "<!--", $text_start);
             $text = substr($head, $text_start, $text_end - $text_start);
             $page_heads[] = array("level" => $level, "text" => $text, "anchor" => $anchor);
         }
     }
     if (count($page_heads) > 1) {
         include_once "./Services/UIComponent/NestedList/classes/class.ilNestedList.php";
         $list = new ilNestedList();
         $list->setAutoNumbering(true);
         $list->setListClass("ilc_page_toc_PageTOCList");
         $list->setItemClass("ilc_page_toc_PageTOCItem");
         $i = 0;
         $c_depth = 1;
         $c_par[1] = 0;
         $c_par[2] = 0;
         $nr[1] = 1;
         $nr[2] = 1;
         $nr[3] = 1;
         foreach ($page_heads as $ind => $h) {
             $i++;
             $par = 0;
             // check if we have a parent for one level up
             $par = 0;
             if ($h["level"] == 2 && $c_par[1] > 0) {
                 $par = $c_par[1];
             }
             if ($h["level"] == 3 && $c_par[2] > 0) {
                 $par = $c_par[2];
             }
             $h["text"] = str_replace("<!--PageTocPH-->", "", $h["text"]);
             // add the list node
             $list->addListNode("<a href='#" . $h["anchor"] . "' class='ilc_page_toc_PageTOCLink'>" . $h["text"] . "</a>", $i, $par);
             // set the node as current parent of the level
             if ($h["level"] == 1) {
                 $c_par[1] = $i;
                 $c_par[2] = 0;
             }
             if ($h["level"] == 2) {
                 $c_par[2] = $i;
             }
         }
         $tpl = new ilTemplate("tpl.page_toc.html", true, true, "Services/COPage");
         $tpl->setVariable("PAGE_TOC", $list->getHTML());
         $tpl->setVariable("TXT_PAGE_TOC", $lng->txt("cont_page_toc"));
         $tpl->setVariable("TXT_HIDE", $lng->txt("hide"));
         $tpl->setVariable("TXT_SHOW", $lng->txt("show"));
         $a_output = str_replace("{{{{{PageTOC}}}}}", $tpl->get(), $a_output);
         $numbers = $list->getNumbers();
         if (count($numbers) > 0) {
             include_once "./Services/Utilities/classes/class.ilStr.php";
             foreach ($numbers as $n) {
                 $a_output = ilStr::replaceFirsOccurence("<!--PageTocPH-->", $n . " ", $a_output);
             }
         }
     } else {
         $a_output = str_replace("{{{{{PageTOC}}}}}", "", $a_output);
     }
     return $a_output;
 }