FEATURE #1372: add feature for editing static pages.

User can edit static pages. The edit itself also stores history of changed columns.
This commit is contained in:
Michal Kváček
2016-01-28 15:51:41 +01:00
parent 1b2d7db8f3
commit 73ff4e9331
8 changed files with 341 additions and 5 deletions
+55
View File
@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@@ -55,6 +56,60 @@ class Content extends Model
return $query; return $query;
} }
/**
* Updates current model with information about changed columns
*
* @param array $fields
* @param Request $request
*
* @return int number of changed columns
*/
public function updateWithHistory(array $fields, Request $request)
{
$editBatch = uniqid();
$changed = 0;
foreach ($fields as $fieldDB => $fieldRequest) {
// remove any whitespaces around real content
$request->$fieldRequest = trim($request->$fieldRequest);
if ($this->$fieldDB != $request->$fieldRequest) {
// setup history object
$history = [
'user_id' => \Auth::user()->id,
'content_id' => $this->id,
'edit_batch' => $editBatch,
'column' => $fieldRequest,
'old_value' => $this->$fieldDB,
'new_value' => $request->$fieldRequest
];
// save history
ContentHistory::create($history);
// save change into database
$this->$fieldDB = $request->$fieldRequest;
$changed++;
}
}
$this->save();
return $changed;
}
/**
* Mark relationship with its historical records
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function history()
{
return $this->hasMany('App\Models\ContentHistory');
}
/** /**
* Mark relationship with users table * Mark relationship with users table
* *
+3 -2
View File
@@ -9,13 +9,14 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class ContentHistory extends Model class ContentHistory extends Model
{ {
protected $table = 'content_history';
public $timestamps = true; // this table does not have created_at and updated_at fields
public $timestamps = false;
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'content_id', 'content_id',
'changed_at',
'edit_batch', 'edit_batch',
'column', 'column',
'new_value', 'new_value',
@@ -3,13 +3,81 @@ namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses; use App\Enumerations\ContentStatuses;
use App\Models\Content; use App\Models\Content;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditStaticPage extends Controller class EditStaticPage extends Controller
{ {
/**
* Display form for editing static page
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getEdit($id) public function getEdit($id)
{ {
return view('staticpages::index'); // select all content statuses
$statuses = ContentStatuses::getWithTranslations();
// try to find requested page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
// render view
return view('staticpages::admin.edit', [
'contentStatuses' => $statuses,
'page' => $page
]);
}
/**
* Edit static page and save changes into history.
*
* @param EditStaticPageRequest $request
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit(EditStaticPageRequest $request, $id)
{
try {
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
} catch (NotFoundHttpException $e) {
// page for editing not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
// array of fields in database and fields from request
$fields = [
'title' => 'title',
'url' => 'url',
'published_from' => 'publishedFrom',
'published_to' => 'publishedTo',
'meta_keywords' => 'metaKeywords',
'meta_description' => 'metaDescription',
'content' => 'pageContent',
'status' => 'status'
];
// save new values with history
$changedColumns = $page->updateWithHistory($fields, $request);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// something was changed, inform user
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('success', trans('staticpages::admin.successfullyEdited'));
} }
/** /**
@@ -0,0 +1,29 @@
<?php
namespace Modules\StaticPages\Http\Requests;
use App\Enumerations\ContentStatuses;
use App\Http\Requests\Request;
class EditStaticPageRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
// get all available statuses
$statuses = ContentStatuses::getSimple();
$statuses = implode(',', $statuses);
return [
'title' => 'required',
'url' => 'required|unique:contents,url,'.$this->id,
'published_from' => 'date',
'published_to' => 'date',
'title_photo' => 'exists:files,id',
'status' => 'required|in:'.$statuses
];
}
}
+4
View File
@@ -31,6 +31,10 @@ Route::group([
Route::post('new', 'CreateNewStaticPage@postNew') Route::post('new', 'CreateNewStaticPage@postNew')
->name('staticpage.newPost'); ->name('staticpage.newPost');
// post method for creating new static page
Route::post('edit/{id}', 'EditStaticPage@postEdit')
->name('staticpage.editPost');
// routes for quick actions // routes for quick actions
// change status of page (protected, public or draft) // change status of page (protected, public or draft)
@@ -16,7 +16,7 @@ return [
'successfullyCreated' => 'Page successfully created', 'successfullyCreated' => 'Page successfully created',
'noPagesAvailable' => 'No static pages available. Try to create one first.', 'noPagesAvailable' => 'No static pages available. Try to create one first.',
'viewPage' => 'View page on web', 'viewPage' => 'View page on web',
'edit' => 'Edit page', 'edit' => 'Edit page :name',
'deletePage' => 'Delete page (ireversible!)', 'deletePage' => 'Delete page (ireversible!)',
'makePublic' => 'Publish page', 'makePublic' => 'Publish page',
'makeProtected' => 'Publish page only for logged users', 'makeProtected' => 'Publish page only for logged users',
@@ -24,6 +24,8 @@ return [
'invalidStatus' => 'Cannot change status to something undefined.', 'invalidStatus' => 'Cannot change status to something undefined.',
'statusChanged' => 'Status successfully changed.', 'statusChanged' => 'Status successfully changed.',
'invalidPage' => 'Specified page does not exist.', 'invalidPage' => 'Specified page does not exist.',
'pageDeleted' => 'Page :name (:url) successfully deleted.' 'pageDeleted' => 'Page :name (:url) successfully deleted.',
'nothingToEdit' => 'You did not submit any changes. Nothing was edited.',
'successfullyEdited' => 'Edit successfull'
]; ];
@@ -0,0 +1,15 @@
@extends('admin.layout')
@section('pageName', @trans('staticpages::admin.edit', ['name' => $page->title]))
@section('content')
<div class="row">
<h2>@lang('staticpages::admin.edit', ['name' => $page->title])</h2>
</div>
<div class="row">
@include('staticpages::admin.editForm')
</div>
@stop
@@ -0,0 +1,162 @@
@if (session('success'))
<div data-alert class="callout success">
<strong>{{ session('success') }}</strong>
</div>
@endif
@if (session('error'))
<div data-alert class="callout alert">
<strong>{{ session('error') }}</strong>
</div>
@endif
<form method="POST" action="{{route('staticpage.editPost', ['id' => $page->id])}}">
{!! csrf_field() !!}
<ul class="tabs" data-tabs id="static-page-controls">
<li class="tabs-title is-active">
<a href="#basic-info" aria-selected="true">@lang('staticpages::admin.basicInfo')</a>
</li>
<li class="tabs-title">
<a href="#meta">@lang('staticpages::admin.metaOptions')</a>
</li>
<li class="tabs-title">
<a href="#publication">@lang('staticpages::admin.publicationOptions')</a>
</li>
</ul>
<div class="tabs-content" data-tabs-content="static-page-controls">
<div class="tabs-panel is-active" id="basic-info">
<div class="row">
<div class="columns large-6">
<label>
@lang('staticpages::admin.field_title')
<input type="text" name="title" placeholder="@lang('staticpages::admin.field_title')"
value="{{$page->title}}" />
</label>
</div>
<div class="columns large-6">
<label>
@lang('staticpages::admin.field_url')
<input type="text" name="url" placeholder="@lang('staticpages::admin.field_url')"
value="{{$page->url}}" />
@if (isset($errors) && $errors->has('url'))
<div class="callout alert">
{{ $errors->first('url') }}
</div>
@endif
</label>
</div>
</div>
<div class="row">
<div class="columns large-12">
<label>
<select name="status">
@foreach($contentStatuses as $status => $translation)
<option value="{{$status}}" @if($page->status == $status) selected @endif>
{{$translation}}
</option>
@endforeach
</select>
</label>
@if (isset($errors) && $errors->has('status'))
<div class="callout alert">
{{ $errors->first('status') }}
</div>
@endif
</div>
</div>
<div class="row">
<div class="columns large-12">
<label>
<textarea name="pageContent" id="pageContent" class="wysiwyg-editor">
{{$page->content}}
</textarea>
</label>
</div>
</div>
</div>
<div class="tabs-panel" id="publication">
<div class="row">
<div class="columns large-6">
<label>
@lang('staticpages::admin.field_published_from')
<input type="datetime" name="publishedFrom" value="{{$page->published_from}}"
placeholder="@lang('staticpages::admin.field_published_from')" />
@if (isset($errors) && $errors->has('published_from'))
<div class="callout alert">
{{ $errors->first('published_from') }}
</div>
@endif
</label>
</div>
<div class="columns large-6">
<label>
@lang('staticpages::admin.field_published_to')
<input type="datetime" name="publishedTo" value="{{$page->published_to}}"
placeholder="@lang('staticpages::admin.field_published_to')" />
@if (isset($errors) && $errors->has('published_to'))
<div class="callout alert">
{{ $errors->first('published_to') }}
</div>
@endif
</label>
</div>
</div>
</div>
<div class="tabs-panel" id="meta">
<div class="row">
<div class="columns large-12">
<label>
@lang('staticpages::admin.field_meta_keywords')
<input type="text" name="metaKeywords" value="{{$page->meta_keywords}}"
placeholder="@lang('staticpages::admin.field_meta_keywords')" />
</label>
</div>
</div>
<div class="row">
<div class="columns large-12">
<label>
@lang('staticpages::admin.field_meta_description')
<textarea name="metaDescription"
placeholder="@lang('staticpages::admin.field_meta_description')" rows="5">{{$page->meta_description}}</textarea>
</label>
</div>
</div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<button type="submit" class="button success">@lang('staticpages::admin.edit', ['name' => $page->title])</button>
</div>
</div>
</form>
@section('head_stylesheets')
<link rel="stylesheet" href="{{asset('../resources/assets/js/vendor/trumbowyg/ui/trumbowyg.css')}}" />
@stop
@section('javascripts')
<script src="{{asset('../resources/assets/js/vendor/trumbowyg/trumbowyg.min.js')}}"></script>
<script type="application/javascript">
$('#pageContent').trumbowyg();
</script>
@stop