FEATURE #1507: adapt static pages for new modular content management

This commit is contained in:
Michal Kváček
2016-03-11 15:47:27 +01:00
parent 9f5bd46ca3
commit eed7e15650
12 changed files with 62 additions and 476 deletions
@@ -1,9 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use Pingpong\Modules\Routing\Controller as BaseController;
class Controller extends BaseController
{
}
@@ -1,70 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Enumerations\ModulesHandlerToId;
use App\Models\Content;
use App\Models\Template;
use Illuminate\Support\Facades\Event;
use Modules\StaticPages\Http\Requests\CreateNewStaticPageRequest;
class CreateNewStaticPage extends Controller
{
/**
* Display form for new static page
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getNew()
{
$statuses = ContentStatuses::getWithTranslations();
// we do not want to create deleted page
unset($statuses[Content::STATUS_DELETED]);
$allModules = ModulesHandlerToId::getWithTranslations();
$moduleId = $allModules[config('staticpages.handler')];
$templates = Template::where('module_id', $moduleId)
->select(['template_name', 'name', 'description'])
->get();
// render view
return view('staticpages::admin.new', [
'contentStatuses' => $statuses,
'templates' => $templates,
'selectedStatus' => Content::STATUS_DRAFT
]);
}
/**
* Save new static page
*
* @param CreateNewStaticPageRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postNew(CreateNewStaticPageRequest $request)
{
$page = new Content();
$page->user_id = \Auth::user()->id;
$page->title = $request->title;
$page->url = $request->url;
$page->published_from = $request->publishedFrom;
$page->published_to = $request->publishedTo;
$page->meta_keywords = $request->meta_description;
$page->meta_description = $request->meta_description;
$page->content = $request->pageContent;
$page->status = $request->status;
$page->module_handler = config('staticpages.handler');
$page->template = $request->template;
$page->save();
Event::fire(new \Modules\StaticPages\Events\CreateNewStaticPage($page));
return redirect()
->route('staticpage.new')
->with('success', trans('staticpages::admin.successfullyCreated'));
}
}
@@ -1,213 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Enumerations\ModulesHandlerToId;
use App\Models\Content;
use App\Models\ContentHistory;
use App\Models\Template;
use Illuminate\Support\Facades\Event;
use Modules\StaticPages\Events\DeleteStaticPage;
use Modules\StaticPages\Events\EditStaticPage as EditStaticPageEvent;
use Modules\StaticPages\Events\MakeDraftFromStaticPage;
use Modules\StaticPages\Events\PublishStaticPage;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditStaticPage extends Controller
{
/**
* @var ContentHistory
*/
private $history;
public function __construct(ContentHistory $history)
{
$this->history = $history;
}
/**
* Display form for editing static page
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getEdit($id)
{
// select all content statuses
$statuses = ContentStatuses::getWithTranslations();
// try to find requested page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
$allModules = ModulesHandlerToId::getWithTranslations();
$moduleId = $allModules[config('staticpages.handler')];
$templates = Template::where('module_id', $moduleId)
->select(['template_name', 'name', 'description'])
->get();
$history = $this->history->getStructuredHistoryOf($page);
// render view
return view('staticpages::admin.edit', [
'contentStatuses' => $statuses,
'page' => $page,
'templates' => $templates,
'historySize' => count($history['changesets'])
]);
}
/**
* 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',
'template' => 'template',
'published_from' => 'publishedFrom',
'published_to' => 'publishedTo',
'meta_keywords' => 'metaKeywords',
'meta_description' => 'metaDescription',
'content' => 'pageContent',
'status' => 'status'
];
// save new values with history
list($revision, $changedColumns) = $page->updateWithHistory($fields, $request->all());
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// fire event with
Event::fire(new EditStaticPageEvent($page, $revision));
// 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);
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
// try to change the page
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// fire events
if ($status != Content::STATUS_DRAFT) {
Event::fire(new PublishStaticPage($page, $revision));
} else {
Event::fire(new MakeDraftFromStaticPage($page, $revision));
}
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.statusChanged'));
}
/**
* 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);
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// trigger delete event
Event::fire(new DeleteStaticPage($page, $revision));
// 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
]));
}
}
@@ -1,22 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Models\Content;
class StaticPageOverview extends Controller
{
public function getOverview($perPage = 20, $status = null)
{
$model = new Content();
$pages = $model->getByType(config('staticpages.handler'), true);
// add filtering by status
if (!is_null($status) && in_array($status, ContentStatuses::getSimple())) {
$pages = $pages->where('status', $status);
}
$pages = $pages->paginate($perPage);
return view('staticpages::admin.overview', ['pages' => $pages]);
}
}
@@ -1,12 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Controllers;
use Pingpong\Modules\Routing\Controller;
class StaticPagesController extends Controller
{
public function index()
{
return view('staticpages::index');
}
}
@@ -1,26 +0,0 @@
<?php
namespace Modules\StaticPages\Http\Requests;
use App\Http\Requests\Request;
class CreateNewStaticPageRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'url' => 'required|unique:contents,url',
'template' => 'required|exists:templates,template_name',
'published_from' => 'date',
'published_to' => 'date',
'title_photo' => 'exists:files,id',
'module_settings' => 'json',
'status' => 'required|in:draft,protected,public'
];
}
}
@@ -1,30 +0,0 @@
<?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,
'template' => 'required|exists:templates,template_name',
'published_from' => 'date',
'published_to' => 'date',
'title_photo' => 'exists:files,id',
'status' => 'required|in:'.$statuses
];
}
}
-53
View File
@@ -1,54 +1 @@
<?php
Route::group([
'middleware' => 'web',
'prefix' => 'page',
'namespace' => 'Modules\StaticPages\Http\Controllers'
], function () {
Route::get('/{url}', 'StaticPagesController@index');
});
// routes group for admin area
Route::group([
'middleware' => ['web', 'auth'],
'prefix' => 'admin/pages',
'namespace' => 'Modules\StaticPages\Http\Controllers\Admin'
], function () {
// list of all static pages
Route::get('overview', 'StaticPageOverview@getOverview')
->name('staticpage.overview');
// form for creating new static page
Route::get('new', 'CreateNewStaticPage@getNew')
->name('staticpage.new');
// form for editing existing static page
Route::get('edit/{id}', 'EditStaticPage@getEdit')
->name('staticpage.edit');
// 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.');
});