FEATURE: split controllers/admin into more smaller directories
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user