Merge branch 'static_pages' into dev

This commit is contained in:
Michal Kváček
2016-01-29 00:40:23 +01:00
10 changed files with 521 additions and 35 deletions
+55
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
@@ -55,6 +56,60 @@ class Content extends Model
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
*
+3 -2
View File
@@ -9,13 +9,14 @@ use Illuminate\Database\Eloquent\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 = [
'user_id',
'content_id',
'changed_at',
'edit_batch',
'column',
'new_value',
@@ -1,10 +1,162 @@
<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Models\Content;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditStaticPage extends Controller
{
/**
* Display form for editing static page
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
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'));
}
/**
* Method for changing page status
*
* This method is called in static page overview from quick actions. It checks if status is valid, if page exists
* and updates status.
*
* @param $id
* @param $status
*
* @return \Illuminate\Http\RedirectResponse
*/
public function changeStatus($id, $status)
{
// get possible statuses
$statuses = ContentStatuses::getWithTranslations();
unset($statuses[Content::STATUS_DELETED]);
// check if given status is valid
if (!isset($statuses[$status])) {
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidStatus'));
}
try {
// try to find this page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
// page found, save status
$page->status = $status;
$page->save();
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.statusChanged'));
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
}
/**
* Delete page
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deletePage($id)
{
try {
// try to find this page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
// page found, save status
$page->status = Content::STATUS_DELETED;
$page->save();
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.pageDeleted', [
'name' => $page->title,
'url' => $page->url
]));
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
}
}
@@ -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
];
}
}
+21 -1
View File
@@ -8,7 +8,6 @@ Route::group([
Route::get('/{url}', 'StaticPagesController@index');
});
// routes group for admin area
Route::group([
'middleware' => ['web', 'auth'],
@@ -31,4 +30,25 @@ Route::group([
// post method for creating new static page
Route::post('new', 'CreateNewStaticPage@postNew')
->name('staticpage.newPost');
// post method for creating new static page
Route::post('edit/{id}', 'EditStaticPage@postEdit')
->name('staticpage.editPost');
// routes for quick actions
// change status of page (protected, public or draft)
Route::get('status/{id}/{status}', 'EditStaticPage@changeStatus')
->name('staticpage.changeStatus');
// delete page
Route::get('delete/{id}', 'EditStaticPage@deletePage')
->name('staticpage.delete');
// display change history of given page
// Route::get('history/{id}', 'StaticPageHistory@getHistory')->name('staticpage.history');
// Route::get('', '')->name('staticpage.');
});
@@ -16,8 +16,16 @@ return [
'successfullyCreated' => 'Page successfully created',
'noPagesAvailable' => 'No static pages available. Try to create one first.',
'viewPage' => 'View page on web',
'edit' => 'Edit page',
'edit' => 'Edit page :name',
'deletePage' => 'Delete page (ireversible!)',
'makePublic' => 'Publish page',
'makeDraft' => 'Disable page and make it draft'
'makePublic' => 'Publish page',
'makeProtected' => 'Publish page only for logged users',
'makeDraft' => 'Disable page and make it draft',
'invalidStatus' => 'Cannot change status to something undefined.',
'statusChanged' => 'Status successfully changed.',
'invalidPage' => 'Specified page does not exist.',
'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
@@ -3,49 +3,66 @@
@section('content')
<div class="row">
<h1>@lang('staticpages::admin.overview')</h1>
@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
</div>
<div class="row" id="static-page-overview" data-equalizer>
@forelse($pages as $page)
<div class="columns large-3">
<div class="callout" data-equalizer-watch>
<div class="page callout" data-equalizer-watch>
<div class="controls pull-right">
<button class="button tiny secondary" data-toggle="static-page-{{$page->id}}actions"><i class="fa fa-caret-down"></i></button>
{{--<div class="controls pull-right">--}}
{{--<button class="button tiny secondary" data-toggle="static-page-{{$page->id}}actions"><i class="fa fa-caret-down"></i></button>--}}
<div class="dropdown-pane" id="static-page-{{$page->id}}actions" data-dropdown>
<div class="row text-center">
{{--<div class="dropdown-pane" id="static-page-{{$page->id}}actions" data-dropdown>--}}
{{----}}
{{--</div>--}}
{{--</div>--}}
<a href="#" class="button secondary has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.viewPage')"><i class="fa fa-desktop"></i></a>
<a href="#" class="button success has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.edit')"><i class="fa fa-edit"></i></a>
@if($page->status == \App\Models\Content::STATUS_DRAFT)
<a href="#" class="button success has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.makePublic')"><i class="fa fa-eye"></i></a>
@endif
@if(in_array($page->status, [\App\Models\Content::STATUS_PROTECTED, \App\Models\Content::STATUS_PUBLIC]))
<a href="#" class="button warning has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.makeDraft')"><i class="fa fa-eye-slash"></i></a>
@endif
<a href="#" class="button alert confirm pull-right has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.deletePage')"><i class="fa fa-remove"></i></a>
</div>
</div>
</div>
<h3>{{$page->title}}</h3>
<span class="date">{{$page->created_at}}</span>
<h4>{{$page->title}}</h4>
<span class="date">{{date('d.m.Y', strtotime($page->created_at))}}</span>
<span class="author">{{$page->author_name}}</span>
@if ($page->meta_description)
<p>{{str_limit($page->meta_description)}}</p>
@else
<p>{{str_limit(strip_tags($page->content))}}</p>
@endif
<p class="perex">
@if (trim($page->meta_description))
{{str_limit($page->meta_description)}}
@else
{{str_limit(strip_tags($page->content))}}
@endif
</p>
@if ($page->meta_keywords)
<p class="keywords">{{$page->meta_keywords}}</p>
@endif
<p class="controls row text-center">
{{--<a href="#" class="button tiny secondary has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.viewPage')"><i class="fa fa-desktop"></i></a>--}}
<a href="{{route('staticpage.edit', ['id' => $page->id])}}" class="button tiny success has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.edit')"><i class="fa fa-edit"></i></a>
@if($page->status == \App\Models\Content::STATUS_DRAFT)
<a href="{{route('staticpage.changeStatus', ['id' => $page->id, 'status' => \App\Models\Content::STATUS_PUBLIC])}}" class="button tiny success confirm has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.makePublic')"><i class="fa fa-eye"></i></a>
<a href="{{route('staticpage.changeStatus', ['id' => $page->id, 'status' => \App\Models\Content::STATUS_PROTECTED])}}" class="button tiny warning confirm has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.makeProtected')"><i class="fa fa-lock"></i></a>
@endif
@if(in_array($page->status, [\App\Models\Content::STATUS_PROTECTED, \App\Models\Content::STATUS_PUBLIC]))
<a href="{{route('staticpage.changeStatus', ['id' => $page->id, 'status' => \App\Models\Content::STATUS_DRAFT])}}" class="button tiny warning confirm has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.makeDraft')"><i class="fa fa-eye-slash"></i></a>
@endif
<a href="{{route('staticpage.delete', ['id' => $page->id])}}" class="button tiny alert confirm has-tip" data-tooltip aria-haspopup="true" title="@lang('staticpages::admin.deletePage')"><i class="fa fa-remove"></i></a>
</p>
</div>
</div>
@empty
+27
View File
@@ -34,6 +34,33 @@
list-style-type: none;
}
#static-page-overview .page {
position: relative;
padding-bottom: 3em;
}
#static-page-overview .page .controls {
position: absolute;
bottom: 0;
}
#static-page-overview .page .controls .button {
margin-bottom: 10px;
}
#static-page-overview .date, #static-page-overview .author {
font-size: 80%;
}
#static-page-overview .perex {
}
#static-page-overview .keywords {
margin-bottom: 0;
font-size: 80%;
}
.red {
color: red;