mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-05 09:34:37 +08:00
Added page editing
This commit is contained in:
@ -19,4 +19,9 @@ class Book extends Model
|
||||
return $this->getUrl() . '/edit';
|
||||
}
|
||||
|
||||
public function pages()
|
||||
{
|
||||
return $this->hasMany('Oxbow\Page');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,19 +7,23 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Oxbow\Http\Requests;
|
||||
use Oxbow\Repos\BookRepo;
|
||||
use Oxbow\Repos\PageRepo;
|
||||
|
||||
class BookController extends Controller
|
||||
{
|
||||
|
||||
protected $bookRepo;
|
||||
protected $pageRepo;
|
||||
|
||||
/**
|
||||
* BookController constructor.
|
||||
* @param BookRepo $bookRepo
|
||||
* @param PageRepo $pageRepo
|
||||
*/
|
||||
public function __construct(BookRepo $bookRepo)
|
||||
public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
|
||||
{
|
||||
$this->bookRepo = $bookRepo;
|
||||
$this->pageRepo = $pageRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,7 +62,7 @@ class BookController extends Controller
|
||||
$book = $this->bookRepo->newFromInput($request->all());
|
||||
$slug = Str::slug($book->name);
|
||||
while($this->bookRepo->countBySlug($slug) > 0) {
|
||||
$slug += '1';
|
||||
$slug .= '1';
|
||||
}
|
||||
$book->slug = $slug;
|
||||
$book->save();
|
||||
|
@ -4,11 +4,29 @@ namespace Oxbow\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Oxbow\Http\Requests;
|
||||
use Oxbow\Http\Controllers\Controller;
|
||||
use Oxbow\Repos\BookRepo;
|
||||
use Oxbow\Repos\PageRepo;
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
|
||||
protected $pageRepo;
|
||||
protected $bookRepo;
|
||||
|
||||
/**
|
||||
* PageController constructor.
|
||||
* @param $pageRepo
|
||||
* @param $bookRepo
|
||||
*/
|
||||
public function __construct(PageRepo $pageRepo, BookRepo $bookRepo)
|
||||
{
|
||||
$this->pageRepo = $pageRepo;
|
||||
$this->bookRepo = $bookRepo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@ -22,56 +40,88 @@ class PageController extends Controller
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @param $bookSlug
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
public function create($bookSlug)
|
||||
{
|
||||
//
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
return view('pages/create', ['book' => $book]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Request $request
|
||||
* @param $bookSlug
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store(Request $request, $bookSlug)
|
||||
{
|
||||
//
|
||||
$this->validate($request, [
|
||||
'name' => 'required|string|max:255',
|
||||
'html' => 'required|string',
|
||||
'priority' => 'integer'
|
||||
]);
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$page = $this->pageRepo->newFromInput($request->all());
|
||||
$slug = Str::slug($page->name);
|
||||
while($this->pageRepo->countBySlug($slug, $book->id) > 0) {
|
||||
$slug .= '1';
|
||||
}
|
||||
$page->slug =$slug;
|
||||
$page->book_id = $book->id;
|
||||
$page->text = strip_tags($page->html);
|
||||
$page->save();
|
||||
return redirect($page->getUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @param $bookSlug
|
||||
* @param $pageSlug
|
||||
* @return Response
|
||||
*/
|
||||
public function show($id)
|
||||
public function show($bookSlug, $pageSlug)
|
||||
{
|
||||
//
|
||||
$page = $this->pageRepo->getBySlug($pageSlug);
|
||||
return view('pages/show', ['page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @param $bookSlug
|
||||
* @param $pageSlug
|
||||
* @return Response
|
||||
*/
|
||||
public function edit($id)
|
||||
public function edit($bookSlug, $pageSlug)
|
||||
{
|
||||
//
|
||||
$page = $this->pageRepo->getBySlug($pageSlug);
|
||||
return view('pages/edit', ['page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $id
|
||||
* @param Request $request
|
||||
* @param $bookSlug
|
||||
* @param $pageSlug
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
public function update(Request $request, $bookSlug, $pageSlug)
|
||||
{
|
||||
//
|
||||
$page = $this->pageRepo->getBySlug($pageSlug);
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$page->fill($request->all());
|
||||
$slug = Str::slug($page->name);
|
||||
while($this->pageRepo->countBySlug($slug, $book->id) > 0 && $slug != $pageSlug) {
|
||||
$slug .= '1';
|
||||
}
|
||||
$page->text = strip_tags($page->html);
|
||||
$page->save();
|
||||
return redirect($page->getUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,12 @@ Route::group(['prefix' => 'books'], function() {
|
||||
Route::put('/{slug}', 'BookController@update');
|
||||
Route::delete('/{id}/destroy', 'BookController@destroy');
|
||||
Route::get('/{slug}', 'BookController@show');
|
||||
|
||||
Route::get('/{bookSlug}/page/create', 'PageController@create');
|
||||
Route::post('/{bookSlug}/page', 'PageController@store');
|
||||
Route::get('/{bookSlug}/{pageSlug}', 'PageController@show');
|
||||
Route::get('/{bookSlug}/{pageSlug}/edit', 'PageController@edit');
|
||||
Route::put('/{bookSlug}/{pageSlug}', 'PageController@update');
|
||||
});
|
||||
|
||||
Route::get('/', function () {
|
||||
|
12
app/Page.php
12
app/Page.php
@ -6,5 +6,15 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Page extends Model
|
||||
{
|
||||
//
|
||||
protected $fillable = ['name', 'html', 'priority'];
|
||||
|
||||
public function book()
|
||||
{
|
||||
return $this->belongsTo('Oxbow\Book');
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return '/books/' . $this->book->slug . '/' . $this->slug;
|
||||
}
|
||||
}
|
||||
|
@ -6,14 +6,17 @@ class BookRepo
|
||||
{
|
||||
|
||||
protected $book;
|
||||
protected $pageRepo;
|
||||
|
||||
/**
|
||||
* BookRepo constructor.
|
||||
* @param $book
|
||||
* @param Book $book
|
||||
* @param PageRepo $pageRepo
|
||||
*/
|
||||
public function __construct(Book $book)
|
||||
public function __construct(Book $book, PageRepo $pageRepo)
|
||||
{
|
||||
$this->book = $book;
|
||||
$this->pageRepo = $pageRepo;
|
||||
}
|
||||
|
||||
public function getById($id)
|
||||
@ -44,6 +47,9 @@ class BookRepo
|
||||
public function destroyById($id)
|
||||
{
|
||||
$book = $this->getById($id);
|
||||
foreach($book->pages as $page) {
|
||||
$this->pageRepo->destroyById($page->id);
|
||||
}
|
||||
$book->delete();
|
||||
}
|
||||
|
||||
|
52
app/Repos/PageRepo.php
Normal file
52
app/Repos/PageRepo.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php namespace Oxbow\Repos;
|
||||
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Oxbow\Page;
|
||||
|
||||
class PageRepo
|
||||
{
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* PageRepo constructor.
|
||||
* @param $page
|
||||
*/
|
||||
public function __construct(Page $page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
public function getById($id)
|
||||
{
|
||||
return $this->page->findOrFail($id);
|
||||
}
|
||||
|
||||
public function getAll()
|
||||
{
|
||||
return $this->page->all();
|
||||
}
|
||||
|
||||
public function getBySlug($slug)
|
||||
{
|
||||
return $this->page->where('slug', '=', $slug)->first();
|
||||
}
|
||||
|
||||
public function newFromInput($input)
|
||||
{
|
||||
$page = $this->page->fill($input);
|
||||
return $page;
|
||||
}
|
||||
|
||||
public function countBySlug($slug, $bookId)
|
||||
{
|
||||
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
|
||||
}
|
||||
|
||||
public function destroyById($id)
|
||||
{
|
||||
$page = $this->getById($id);
|
||||
$page->delete();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user