Added page editing

This commit is contained in:
Dan Brown 2015-07-12 21:31:15 +01:00
parent eaa1765c7a
commit 3c7bd297ea
19 changed files with 252 additions and 23 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
Homestead.yaml
.env
/public/dist
.idea
.idea
/public/plugins

View file

@ -19,4 +19,9 @@ class Book extends Model
return $this->getUrl() . '/edit';
}
public function pages()
{
return $this->hasMany('Oxbow\Page');
}
}

View file

@ -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();

View file

@ -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());
}
/**

View file

@ -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 () {

View file

@ -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;
}
}

View file

@ -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
View 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();
}
}

View file

@ -14,6 +14,13 @@ class CreatePagesTable extends Migration
{
Schema::create('pages', function (Blueprint $table) {
$table->increments('id');
$table->integer('book_id');
$table->integer('page_id');
$table->string('name');
$table->string('slug')->indexed();
$table->longText('html');
$table->longText('text');
$table->integer('priority');
$table->timestamps();
});
}

5
public/css/app.css vendored
View file

@ -941,4 +941,9 @@ header .menu {
display: inline-block;
margin-left: 16px; }
.page-title input {
display: block;
width: 100%;
font-size: 1.4em; }
/*# sourceMappingURL=app.css.map */

File diff suppressed because one or more lines are too long

View file

@ -19,4 +19,10 @@ header .menu {
display: inline-block;
margin-left: $-m;
}
}
.page-title input {
display: block;
width: 100%;
font-size: 1.4em;
}

View file

@ -5,7 +5,9 @@
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/css/app.css">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,400italic,500,500italic,700,700italic,300italic,100,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
@yield('head')
</head>
<body>

View file

@ -4,4 +4,14 @@
<h2>{{$book->name}}</h2>
<p class="text-muted">{{$book->description}}</p>
<a href="{{$book->getUrl() . '/page/create'}}">+ New Page</a>
<h4>Pages:</h4>
@if(count($book->pages) > 0)
@foreach($book->pages as $page)
<a href="{{$page->getUrl()}}">{{$page->name}}</a><br>
@endforeach
@else
<p class="text-muted">This book has no pages</p>
@endif
@stop

View file

@ -1,5 +1,6 @@
<input type="text" id="{{ $name }}" name="{{ $name }}"
@if($errors->has($name)) class="neg" @endif
@if(isset($placeholder)) placeholder="{{$placeholder}}" @endif
@if(isset($model) || old($name)) value="{{ old($name) ? old($name) : $model->$name}}" @endif>
@if($errors->has($name))
<div class="text-neg text-small">{{ $errors->first($name) }}</div>

View file

@ -0,0 +1,19 @@
@extends('base')
@section('head')
<link rel="stylesheet" href="/plugins/css/froala_editor.min.css">
<link rel="stylesheet" href="/plugins/css/froala_style.min.css">
<script src="/plugins/js/froala_editor.min.js"></script>
@stop
@section('content')
<form action="{{$book->getUrl() . '/page'}}" method="POST">
@include('pages/form')
</form>
<script>
$(function() {
$('#html').editable({inlineMode: false});
});
</script>
@stop

View file

@ -0,0 +1,20 @@
@extends('base')
@section('head')
<link rel="stylesheet" href="/plugins/css/froala_editor.min.css">
<link rel="stylesheet" href="/plugins/css/froala_style.min.css">
<script src="/plugins/js/froala_editor.min.js"></script>
@stop
@section('content')
<form action="{{$page->getUrl()}}" method="POST">
<input type="hidden" name="_method" value="PUT">
@include('pages/form', ['model' => $page])
</form>
<script>
$(function() {
$('#html').editable({inlineMode: false});
});
</script>
@stop

View file

@ -0,0 +1,13 @@
{{ csrf_field() }}
<div class="page-title row">
<div class="col-md-10">
@include('form/text', ['name' => 'name', 'placeholder' => 'Enter Page Title'])
</div>
<div class="col-md-2">
<button type="submit" class="button pos">Save</button>
</div>
</div>
<div class="edit-area">
@include('form/textarea', ['name' => 'html'])
</div>

View file

@ -0,0 +1,12 @@
@extends('base')
@section('content')
<a href="{{$page->getUrl() . '/edit'}}" class="button primary float right">Edit Page</a>
<h1>{{$page->name}}</h1>
<div class="page-content">
{!! $page->html !!}
</div>
@stop