]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/EntityRepo.php
Updated Dutch language files
[bookstack] / app / Repos / EntityRepo.php
index 312e3536ecad99338165bd4e322eb4255d6d3e44..8a8740d76cf6cf18faf5a9163e2f75bb0b4d5a79 100644 (file)
@@ -13,7 +13,6 @@ use Carbon\Carbon;
 use DOMDocument;
 use DOMXPath;
 use Illuminate\Support\Collection;
 use DOMDocument;
 use DOMXPath;
 use Illuminate\Support\Collection;
-use Symfony\Component\DomCrawler\Crawler;
 
 class EntityRepo
 {
 
 class EntityRepo
 {
@@ -140,7 +139,7 @@ class EntityRepo
      */
     public function getById($type, $id, $allowDrafts = false)
     {
      */
     public function getById($type, $id, $allowDrafts = false)
     {
-        return $this->entityQuery($type, $allowDrafts)->findOrFail($id);
+        return $this->entityQuery($type, $allowDrafts)->find($id);
     }
 
     /**
     }
 
     /**
@@ -333,12 +332,12 @@ class EntityRepo
                 $parents[$key] = $entities[$index];
                 $parents[$key]->setAttribute('pages', collect());
             }
                 $parents[$key] = $entities[$index];
                 $parents[$key]->setAttribute('pages', collect());
             }
-            if ($entities[$index]->chapter_id === 0) $tree[] = $entities[$index];
+            if ($entities[$index]->chapter_id === 0 || $entities[$index]->chapter_id === '0') $tree[] = $entities[$index];
             $entities[$index]->book = $book;
         }
 
         foreach ($entities as $entity) {
             $entities[$index]->book = $book;
         }
 
         foreach ($entities as $entity) {
-            if ($entity->chapter_id === 0) continue;
+            if ($entity->chapter_id === 0 || $entity->chapter_id === '0') continue;
             $parentKey = 'BookStack\\Chapter:' . $entity->chapter_id;
             $chapter = $parents[$parentKey];
             $chapter->pages->push($entity);
             $parentKey = 'BookStack\\Chapter:' . $entity->chapter_id;
             $chapter = $parents[$parentKey];
             $chapter->pages->push($entity);
@@ -805,34 +804,42 @@ class EntityRepo
      */
     public function renderPage(Page $page)
     {
      */
     public function renderPage(Page $page)
     {
-        libxml_use_internal_errors(true);
-        $doc = new DOMDocument();
-        $doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
-        $xpath = new DOMXpath($doc);
-
-        $bsElems = $xpath->query('body/div[@bs-embed-page]');
-        if (is_null($bsElems)) return $page->html;
-        foreach ($bsElems as $bsElem) {
-            $pageId = intval($bsElem->getAttribute('bs-embed-page'));
-            $embeddedPage = $this->getById('page', $pageId);
-            if ($embeddedPage !== null) {
-                $innerPage = $doc->createDocumentFragment();
-                $innerPage->appendXML($embeddedPage->html);
-                // Empty div then append in child content
-                foreach ($bsElem->childNodes as $child) {
-                    $bsElem->removeChild($child);
-                }
-                $bsElem->appendChild($innerPage);
+        $content = $page->html;
+        $matches = [];
+        preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
+        if (count($matches[0]) === 0) return $content;
+
+        foreach ($matches[1] as $index => $includeId) {
+            $splitInclude = explode('#', $includeId, 2);
+            $pageId = intval($splitInclude[0]);
+            if (is_nan($pageId)) continue;
+
+            $page = $this->getById('page', $pageId);
+            if ($page === null) {
+                $content = str_replace($matches[0][$index], '', $content);
+                continue;
             }
             }
-        }
 
 
-        $body = $doc->getElementsByTagName('body')->item(0);
-        $html = '';
-        foreach ($body->childNodes as $node) {
-            $html .= $doc->saveHTML($node);
+            if (count($splitInclude) === 1) {
+                $content = str_replace($matches[0][$index], $page->html, $content);
+                continue;
+            }
+
+            $doc = new DOMDocument();
+            $doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
+            $matchingElem = $doc->getElementById($splitInclude[1]);
+            if ($matchingElem === null) {
+                $content = str_replace($matches[0][$index], '', $content);
+                continue;
+            }
+            $innerContent = '';
+            foreach ($matchingElem->childNodes as $childNode) {
+                $innerContent .= $doc->saveHTML($childNode);
+            }
+            $content = str_replace($matches[0][$index], trim($innerContent), $content);
         }
 
         }
 
-        return $html;
+        return $content;
     }
 
     /**
     }
 
     /**
@@ -874,15 +881,15 @@ class EntityRepo
 
     /**
      * Parse the headers on the page to get a navigation menu
 
     /**
      * Parse the headers on the page to get a navigation menu
-     * @param Page $page
+     * @param String $pageContent
      * @return array
      */
      * @return array
      */
-    public function getPageNav(Page $page)
+    public function getPageNav($pageContent)
     {
     {
-        if ($page->html == '') return [];
+        if ($pageContent == '') return [];
         libxml_use_internal_errors(true);
         $doc = new DOMDocument();
         libxml_use_internal_errors(true);
         $doc = new DOMDocument();
-        $doc->loadHTML(mb_convert_encoding($page->html, 'HTML-ENTITIES', 'UTF-8'));
+        $doc->loadHTML(mb_convert_encoding($pageContent, 'HTML-ENTITIES', 'UTF-8'));
         $xPath = new DOMXPath($doc);
         $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
 
         $xPath = new DOMXPath($doc);
         $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");