1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

Extracted page form js and added better page content linking

This commit is contained in:
Dan Brown
2015-10-10 18:57:52 +01:00
parent 644563dbf5
commit 5af6656fae
13 changed files with 279 additions and 191 deletions

View File

@@ -63,22 +63,14 @@ class PageController extends Controller
'html' => 'required|string',
'parent' => 'integer|exists:pages,id'
]);
$input = $request->all();
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->newFromInput($request->all());
$chapterId = ($request->has('chapter') && $this->chapterRepo->idExists($request->get('chapter'))) ? $request->get('chapter') : null;
$input['priority'] = $this->bookRepo->getNewPriority($book);
$page->slug = $this->pageRepo->findSuitableSlug($page->name, $book->id);
$page->priority = $this->bookRepo->getNewPriority($book);
$page = $this->pageRepo->saveNew($input, $book, $chapterId);
if ($request->has('chapter') && $this->chapterRepo->idExists($request->get('chapter'))) {
$page->chapter_id = $request->get('chapter');
}
$page->book_id = $book->id;
$page->text = strip_tags($page->html);
$page->created_by = Auth::user()->id;
$page->updated_by = Auth::user()->id;
$page->save();
$this->pageRepo->saveRevision($page);
Activity::add($page, 'page_create', $book->id);
return redirect($page->getUrl());
}

View File

@@ -1,7 +1,11 @@
<?php namespace BookStack\Repos;
use BookStack\Book;
use BookStack\Chapter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use BookStack\Page;
use BookStack\PageRevision;
@@ -42,6 +46,10 @@ class PageRepo
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
}
/**
* @param $input
* @return Page
*/
public function newFromInput($input)
{
$page = $this->page->fill($input);
@@ -53,6 +61,83 @@ class PageRepo
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
}
/**
* Save a new page into the system.
* Input validation must be done beforehand.
* @param array $input
* @param Book $book
* @param int $chapterId
* @return Page
*/
public function saveNew(array $input, Book $book, $chapterId = null)
{
$page = $this->newFromInput($input);
$page->slug = $this->findSuitableSlug($page->name, $book->id);
if ($chapterId) $page->chapter_id = $chapterId;
$page->html = $this->formatHtml($input['html']);
$page->text = strip_tags($page->html);
$page->created_by = auth()->user()->id;
$page->updated_by = auth()->user()->id;
$book->pages()->save($page);
$this->saveRevision($page);
return $page;
}
/**
* Formats a page's html to be tagged correctly
* within the system.
* @param string $htmlText
* @return string
*/
protected function formatHtml($htmlText)
{
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($htmlText);
$container = $doc->documentElement;
$body = $container->childNodes[0];
$childNodes = $body->childNodes;
// Ensure no duplicate ids are used
$lastId = false;
$idArray = [];
foreach ($childNodes as $index => $childNode) {
/** @var \DOMElement $childNode */
if (get_class($childNode) !== 'DOMElement') continue;
// Overwrite id if not a bookstack custom id
if ($childNode->hasAttribute('id')) {
$id = $childNode->getAttribute('id');
if (strpos($id, 'bkmrk') === 0 && array_search($id, $idArray) === false) {
$idArray[] = $id;
continue;
};
}
// Create an unique id for the element
do {
$id = 'bkmrk-' . substr(uniqid(), -5);
} while ($id == $lastId);
$lastId = $id;
$childNode->setAttribute('id', $id);
$idArray[] = $id;
}
// Generate inner html as a string
$html = '';
foreach ($childNodes as $childNode) {
$html .= $doc->saveHTML($childNode);
}
return $html;
}
public function destroyById($id)
{
$page = $this->getById($id);
@@ -99,8 +184,8 @@ class PageRepo
*/
public function searchForImage($imageString)
{
$pages = $this->page->where('html', 'like', '%'.$imageString.'%')->get();
foreach($pages as $page) {
$pages = $this->page->where('html', 'like', '%' . $imageString . '%')->get();
foreach ($pages as $page) {
$page->url = $page->getUrl();
$page->html = '';
$page->text = '';
@@ -110,15 +195,16 @@ class PageRepo
/**
* Updates a page with any fillable data and saves it into the database.
* @param Page $page
* @param $book_id
* @param $data
* @param Page $page
* @param int $book_id
* @param string $input
* @return Page
*/
public function updatePage(Page $page, $book_id, $data)
public function updatePage(Page $page, $book_id, $input)
{
$page->fill($data);
$page->fill($input);
$page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
$page->html = $this->formatHtml($input['html']);
$page->text = strip_tags($page->html);
$page->updated_by = Auth::user()->id;
$page->save();
@@ -189,7 +275,7 @@ class PageRepo
public function setBookId($bookId, Page $page)
{
$page->book_id = $bookId;
foreach($page->activity as $activity) {
foreach ($page->activity as $activity) {
$activity->book_id = $bookId;
$activity->save();
}