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

Started working on chapters

This commit is contained in:
Dan Brown
2015-07-27 20:17:08 +01:00
parent ef77c10a70
commit b9df3c647a
17 changed files with 317 additions and 40 deletions

View File

@ -24,4 +24,17 @@ class Book extends Model
return $this->hasMany('Oxbow\Page');
}
public function chapters()
{
return $this->hasMany('Oxbow\Chapter');
}
public function children()
{
$pages = $this->pages()->get();
$chapters = $this->chapters()->get();
$children = $pages->merge($chapters);
return $children->sortBy('priority');
}
}

25
app/Chapter.php Normal file
View File

@ -0,0 +1,25 @@
<?php namespace Oxbow;
use Illuminate\Database\Eloquent\Model;
class Chapter extends Model
{
protected $fillable = ['name', 'description', 'priority', 'book_id'];
public function book()
{
return $this->belongsTo('Oxbow\Book');
}
public function children()
{
return $this->hasMany('Oxbow\Page')->orderBy('priority', 'ASC');
}
public function getUrl()
{
return '/books/' . $this->book->slug . '/chapter/' . $this->slug;
}
}

View File

@ -79,7 +79,6 @@ class BookController extends Controller
{
$book = $this->bookRepo->getBySlug($slug);
$pageTree = $this->pageRepo->getTreeByBookId($book->id);
// dd($pageTree);
return view('books/show', ['book' => $book, 'pageTree' => $pageTree]);
}

View File

@ -0,0 +1,116 @@
<?php
namespace Oxbow\Http\Controllers;
use Illuminate\Http\Request;
use Oxbow\Http\Requests;
use Oxbow\Http\Controllers\Controller;
use Oxbow\Repos\BookRepo;
use Oxbow\Repos\ChapterRepo;
class ChapterController extends Controller
{
protected $bookRepo;
protected $chapterRepo;
/**
* ChapterController constructor.
* @param $bookRepo
* @param $chapterRepo
*/
public function __construct(BookRepo $bookRepo,ChapterRepo $chapterRepo)
{
$this->bookRepo = $bookRepo;
$this->chapterRepo = $chapterRepo;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @param $bookSlug
* @return Response
*/
public function create($bookSlug)
{
$book = $this->bookRepo->getBySlug($bookSlug);
return view('chapters/create', ['book' => $book]);
}
/**
* Store a newly created resource in storage.
*
* @param $bookSlug
* @param Request $request
* @return Response
*/
public function store($bookSlug, Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255'
]);
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->newFromInput($request->all());
$chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
$book->chapters()->save($chapter);
return redirect($book->getUrl());
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}

View File

@ -26,10 +26,14 @@ Route::group(['prefix' => 'books'], function() {
Route::post('/{bookSlug}/page', 'PageController@store');
Route::get('/{bookSlug}/sort', 'PageController@sortPages');
Route::put('/{bookSlug}/sort', 'PageController@savePageSort');
Route::get('/{bookSlug}/{pageSlug}', 'PageController@show');
Route::get('/{bookSlug}/{pageSlug}/create', 'PageController@create');
Route::get('/{bookSlug}/{pageSlug}/edit', 'PageController@edit');
Route::put('/{bookSlug}/{pageSlug}', 'PageController@update');
Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
Route::get('/{bookSlug}/page/{pageSlug}/create', 'PageController@create');
Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit');
Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update');
Route::get('/{bookSlug}/chapter/create', 'ChapterController@create');
Route::post('/{bookSlug}/chapter/create', 'ChapterController@store');
});
Route::post('/upload/image', 'ImageController@upload');

View File

@ -34,7 +34,7 @@ class Page extends Model
public function getUrl()
{
return '/books/' . $this->book->slug . '/' . $this->slug;
return '/books/' . $this->book->slug . '/page/' . $this->slug;
}
}

65
app/Repos/ChapterRepo.php Normal file
View File

@ -0,0 +1,65 @@
<?php namespace Oxbow\Repos;
use Illuminate\Support\Str;
use Oxbow\Chapter;
class ChapterRepo
{
protected $chapter;
/**
* ChapterRepo constructor.
* @param $chapter
*/
public function __construct(Chapter $chapter)
{
$this->chapter = $chapter;
}
public function getById($id)
{
return $this->chapter->findOrFail($id);
}
public function getAll()
{
return $this->chapter->all();
}
public function getBySlug($slug, $bookId)
{
return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
}
public function newFromInput($input)
{
return $this->chapter->fill($input);
}
public function destroyById($id)
{
$page = $this->getById($id);
$page->delete();
}
public function doesSlugExist($slug, $bookId, $currentId = false)
{
$query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
if($currentId) {
$query = $query->where('id', '!=', $currentId);
}
return $query->count() > 0;
}
public function findSuitableSlug($name, $bookId)
{
$slug = Str::slug($name);
while($this->doesSlugExist($slug, $bookId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
return $slug;
}
}

View File

@ -114,7 +114,7 @@ class PageRepo
*/
private function getTopLevelPages($bookId)
{
return $this->page->where('book_id', '=', $bookId)->where('page_id', '=', 0)->orderBy('priority')->get();
return $this->page->where('book_id', '=', $bookId)->where('chapter_id', '=', 0)->orderBy('priority')->get();
}
/**