3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Illuminate\Support\Str;
8 use Oxbow\Http\Requests;
9 use Oxbow\Repos\BookRepo;
10 use Oxbow\Repos\PageRepo;
12 class PageController extends Controller
19 * PageController constructor.
23 public function __construct(PageRepo $pageRepo, BookRepo $bookRepo)
25 $this->pageRepo = $pageRepo;
26 $this->bookRepo = $bookRepo;
31 * Display a listing of the resource.
35 public function index()
41 * Show the form for creating a new resource.
46 public function create($bookSlug)
48 $book = $this->bookRepo->getBySlug($bookSlug);
49 return view('pages/create', ['book' => $book]);
53 * Store a newly created resource in storage.
55 * @param Request $request
59 public function store(Request $request, $bookSlug)
61 $this->validate($request, [
62 'name' => 'required|string|max:255',
63 'html' => 'required|string',
64 'priority' => 'integer'
66 $book = $this->bookRepo->getBySlug($bookSlug);
67 $page = $this->pageRepo->newFromInput($request->all());
68 $slug = Str::slug($page->name);
69 while($this->pageRepo->countBySlug($slug, $book->id) > 0) {
73 $page->book_id = $book->id;
74 $page->text = strip_tags($page->html);
76 return redirect($page->getUrl());
80 * Display the specified resource.
86 public function show($bookSlug, $pageSlug)
88 $book = $this->bookRepo->getBySlug($bookSlug);
89 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
90 return view('pages/show', ['page' => $page]);
94 * Show the form for editing the specified resource.
100 public function edit($bookSlug, $pageSlug)
102 $book = $this->bookRepo->getBySlug($bookSlug);
103 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
104 return view('pages/edit', ['page' => $page]);
108 * Update the specified resource in storage.
110 * @param Request $request
115 public function update(Request $request, $bookSlug, $pageSlug)
117 $book = $this->bookRepo->getBySlug($bookSlug);
118 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
119 $page->fill($request->all());
120 $slug = Str::slug($page->name);
121 while($this->pageRepo->countBySlug($slug, $book->id) > 0 && $slug != $pageSlug) {
124 $page->text = strip_tags($page->html);
126 return redirect($page->getUrl());
130 * Remove the specified resource from storage.
135 public function destroy($id)