FEATURE: split controllers/admin into more smaller directories
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\Widgets\Areas;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\NewWidgetAreaRequest;
|
||||
use App\Models\WidgetArea;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class CreateNewWidgetArea extends Controller
|
||||
{
|
||||
/**
|
||||
* Form for new widget area
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getNew()
|
||||
{
|
||||
return view('admin.widgets.area.new', ['area' => new WidgetArea()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NewWidgetAreaRequest $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postNew(NewWidgetAreaRequest $request)
|
||||
{
|
||||
$parameters = [
|
||||
'code' => $request->code,
|
||||
'name' => $request->name,
|
||||
'description' => $request->description
|
||||
];
|
||||
|
||||
$area = WidgetArea::create($parameters);
|
||||
|
||||
// fire event with new widget area
|
||||
Event::fire(new \App\Events\CreateNewWidgetArea($area));
|
||||
|
||||
// redirect with success notification
|
||||
return redirect()
|
||||
->route('widgetareas.overview')
|
||||
->with('success', trans('admin/widgetAreas.successfullyCreated'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\Widgets\Areas;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\EditWidgetAreaRequest;
|
||||
use App\Models\WidgetArea;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class EditWidgetArea extends Controller
|
||||
{
|
||||
/**
|
||||
* Display form for editing given widget area
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getEdit($id)
|
||||
{
|
||||
$area = WidgetArea::findOrFail($id);
|
||||
|
||||
return view('admin.widgets.area.edit', ['area' => $area]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EditWidgetAreaRequest $request
|
||||
* @param $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postEdit(EditWidgetAreaRequest $request, $id)
|
||||
{
|
||||
try {
|
||||
$area = WidgetArea::findOrFail($id);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return $this->invalidArea();
|
||||
}
|
||||
|
||||
$area->name = $request->name;
|
||||
$area->description = $request->description;
|
||||
|
||||
$area->save();
|
||||
|
||||
// fire event with new widget area
|
||||
Event::fire(new \App\Events\EditWidgetArea($area));
|
||||
|
||||
return redirect()
|
||||
->route('widgetareas.overview')
|
||||
->with('success', trans('admin/widgetAreas.editSuccessfull'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hard deletes widget area
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function deleteArea($id)
|
||||
{
|
||||
// try to find area
|
||||
try {
|
||||
$area = WidgetArea::findOrFail($id);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return $this->invalidArea();
|
||||
}
|
||||
|
||||
// check if widget area is deletable
|
||||
if (!$area->deletable) {
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', trans('admin/widgetAreas.notDeletableArea'));
|
||||
}
|
||||
|
||||
// delete area
|
||||
$area->delete();
|
||||
|
||||
// fire event with new widget area
|
||||
Event::fire(new \App\Events\DeleteWidgetArea($area));
|
||||
|
||||
// redirect and inform user
|
||||
return redirect()
|
||||
->route('widgetareas.overview')
|
||||
->with('success', trans('admin/widgetAreas.successfullyDeleted'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect user to overview with message about invalid widget area
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
private function invalidArea()
|
||||
{
|
||||
return redirect()
|
||||
->route('widgetareas.overview')
|
||||
->with('error', trans('admin/widgetAreas.invalidArea'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\Widgets\Areas;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Models\WidgetArea;
|
||||
|
||||
class WidgetAreaOverview extends Controller
|
||||
{
|
||||
/**
|
||||
* List of all widget area. Displayed paginated
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getOverview()
|
||||
{
|
||||
// get widget areas ordered by created_at
|
||||
$areas = WidgetArea::orderBy('created_at', 'DESC')
|
||||
->paginate(25);
|
||||
|
||||
return view('admin.widgets.area.overview', ['areas' => $areas]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\Widgets;
|
||||
|
||||
use App\Events\AssignWidget;
|
||||
use App\Events\RemoveWidgetFromArea;
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Models\Widget;
|
||||
use App\Models\WidgetArea;
|
||||
use App\Models\WidgetInWidgetArea;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class AssociateWidget extends Controller
|
||||
{
|
||||
public function getList($widgetArea)
|
||||
{
|
||||
$area = WidgetArea::findOrFail($widgetArea);
|
||||
$associatedWidgetIds = WidgetInWidgetArea::getWidgetIdsFromArea($area->id);
|
||||
$widgets = Widget::where('language', LaravelLocalization::getCurrentLocale())
|
||||
->paginate(25);
|
||||
|
||||
return view('admin.widgets.association', [
|
||||
'widgetArea' => $area,
|
||||
'widgets' => $widgets,
|
||||
'associatedWidgetIds' => $associatedWidgetIds
|
||||
]);
|
||||
}
|
||||
|
||||
public function makeAssociation($widgetId, $widgetAreaId)
|
||||
{
|
||||
// check if given values are valid
|
||||
$widget = Widget::findOrFail($widgetId);
|
||||
$widgetArea = WidgetArea::findOrFail($widgetAreaId);
|
||||
|
||||
try {
|
||||
WidgetInWidgetArea::create(['widget_id' => $widgetId, 'widget_area_id' => $widgetAreaId]);
|
||||
} catch (QueryException $e) {
|
||||
return redirect()
|
||||
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
||||
->with('error', trans('admin/widgets.associationExists'));
|
||||
}
|
||||
|
||||
Event::fire(new AssignWidget($widget, $widgetArea));
|
||||
|
||||
return redirect()
|
||||
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
||||
->with('success', trans('admin/widgets.successfullyAssociated'));
|
||||
}
|
||||
|
||||
public function deleteAssociation($widgetId, $widgetAreaId)
|
||||
{
|
||||
// check if given values are valid
|
||||
$widget = Widget::findOrFail($widgetId);
|
||||
$widgetArea = WidgetArea::findOrFail($widgetAreaId);
|
||||
|
||||
$widgetInArea = WidgetInWidgetArea::where('widget_id', $widgetId)
|
||||
->where('widget_area_id', $widgetAreaId);
|
||||
|
||||
if ($widgetInArea->count() == 0) {
|
||||
return redirect()
|
||||
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
||||
->with('error', trans('admin/widgets.associationDoesNotExists'));
|
||||
}
|
||||
|
||||
$widgetInArea->delete();
|
||||
|
||||
Event::fire(new RemoveWidgetFromArea($widget, $widgetArea));
|
||||
|
||||
return redirect()
|
||||
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
||||
->with('success', trans('admin/widgets.successfullyDeassociated'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\Widgets;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\NewWidgetRequest;
|
||||
use App\Models\Widget;
|
||||
use App\Models\WidgetType;
|
||||
use App\Widgets\Exceptions\MissingParameterException;
|
||||
|
||||
class CreateNewWidget extends Controller
|
||||
{
|
||||
/**
|
||||
* @var Widget
|
||||
*/
|
||||
private $widget;
|
||||
|
||||
public function __construct(Widget $widget)
|
||||
{
|
||||
$this->widget = $widget;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form for new widget area
|
||||
*
|
||||
* @param $type
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getNew($type)
|
||||
{
|
||||
$widgetType = WidgetType::findOrFail($type);
|
||||
|
||||
$presenter = call_user_func([$widgetType->handler, 'getPresenter']);
|
||||
|
||||
return call_user_func([$presenter, 'renderAddForm'], $widgetType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new instance of given widget type
|
||||
*
|
||||
* @param NewWidgetRequest $request
|
||||
* @param $type
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postNew(NewWidgetRequest $request, $type)
|
||||
{
|
||||
$widgetType = WidgetType::findOrFail($type);
|
||||
|
||||
// try to save this widget
|
||||
try {
|
||||
$this->widget->createNew($widgetType, $request->all());
|
||||
} catch (MissingParameterException $e) {
|
||||
// user omitted some value
|
||||
return $this->invalidWidget($type, $e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
// something went wrong
|
||||
return $this->invalidWidget($type, trans('admin/widgets.cannotSave', ['exception' => $e->getMessage()]));
|
||||
}
|
||||
|
||||
// redirect with success notification
|
||||
return redirect()
|
||||
->route('widgets.overview')
|
||||
->with('success', trans('admin/widgets.successfullyCreated'));
|
||||
}
|
||||
|
||||
private function invalidWidget($widgetType, $error)
|
||||
{
|
||||
return redirect()
|
||||
->route('widgets.new', ['type' => $widgetType])
|
||||
->withInput()
|
||||
->with('error', $error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers\Admin\Widgets;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\EditWidgetRequest;
|
||||
use App\Models\Widget;
|
||||
use App\Widgets\Exceptions\MissingParameterException;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class EditWidget extends Controller
|
||||
{
|
||||
/**
|
||||
* Render edit form
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getEdit($id)
|
||||
{
|
||||
$widget = Widget::findOrFail($id);
|
||||
|
||||
$presenter = call_user_func([$widget->widget_type_handler, 'getPresenter']);
|
||||
|
||||
return call_user_func([$presenter, 'renderEditForm'], $widget);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save changes into database
|
||||
*
|
||||
* @param EditWidgetRequest $request
|
||||
* @param $widget
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postEdit(EditWidgetRequest $request, $widget)
|
||||
{
|
||||
// create new widget
|
||||
$widget = Widget::findOrFail($widget);
|
||||
|
||||
// update name
|
||||
$widget->name = $request->name;
|
||||
|
||||
// save description if available
|
||||
if (isset($request->description)) {
|
||||
$widget->description = $request->description;
|
||||
}
|
||||
|
||||
// try to save this widget
|
||||
try {
|
||||
$widget = call_user_func([$widget->widget_type_handler, 'save'], $widget, (object)$request->all());
|
||||
} catch (MissingParameterException $e) {
|
||||
// user omitted some value
|
||||
return $this->invalidWidget($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
// something went wrong
|
||||
return $this->invalidWidget(trans('admin/widgets.cannotSave', ['exception' => $e->getMessage()]));
|
||||
}
|
||||
|
||||
// save into database
|
||||
$widget->save();
|
||||
|
||||
// fire event with editing widget
|
||||
Event::fire(new \App\Events\EditWidget($widget));
|
||||
|
||||
// redirect with success notification
|
||||
return redirect()
|
||||
->route('widgets.overview')
|
||||
->with('success', trans('admin/widgets.successfullyEdited'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes widget
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function deleteWidget($id) {
|
||||
// create new widget
|
||||
$widget = Widget::findOrFail($id);
|
||||
|
||||
// check if widget area is deletable
|
||||
if (!$widget->deletable) {
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', trans('admin/widgets.notDeletable'));
|
||||
}
|
||||
|
||||
$widget->delete();
|
||||
|
||||
// redirect with success notification
|
||||
return redirect()
|
||||
->route('widgets.overview')
|
||||
->with('success', trans('admin/widgets.successfullyDeleted'));
|
||||
}
|
||||
|
||||
private function invalidWidget($error)
|
||||
{
|
||||
return redirect()
|
||||
->route('widgets.overview')
|
||||
->withInput()
|
||||
->with('error', $error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers\Admin\Widgets;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Models\Widget;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class WidgetsOverview extends Controller
|
||||
{
|
||||
public function getOverview()
|
||||
{
|
||||
$widgets = Widget::where('language', LaravelLocalization::getCurrentLocale())
|
||||
->paginate(25);
|
||||
|
||||
return view('admin.widgets.overview', ['widgets' => $widgets]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user