66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Widgets;
|
|
|
|
use App\CMS\Repositories\Widgets\Widget;
|
|
use App\CMS\Widgets\Exceptions\MissingParameterException;
|
|
use App\Http\Controllers\Admin\Controller;
|
|
use App\Http\Requests\NewWidgetRequest;
|
|
use App\Models\Widgets\WidgetType;
|
|
|
|
class CreateNewWidget extends Controller
|
|
{
|
|
/**
|
|
* @var Widget
|
|
*/
|
|
private $widget;
|
|
|
|
public function __construct(Widget $widget)
|
|
{
|
|
$this->widget = $widget;
|
|
}
|
|
|
|
/**
|
|
* Form for new widget area
|
|
*
|
|
* @param WidgetType $widgetType
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*
|
|
*/
|
|
public function getNew(WidgetType $widgetType)
|
|
{
|
|
$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 WidgetType $widgetType
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function postNew(NewWidgetRequest $request, WidgetType $widgetType)
|
|
{
|
|
// try to save this widget
|
|
try {
|
|
$this->widget->createNew($widgetType, $request);
|
|
} catch (MissingParameterException $e) {
|
|
// user omitted some value
|
|
return redirect()
|
|
->back()
|
|
->withInput()
|
|
->with('error', $e->getMessage());
|
|
}
|
|
|
|
// redirect with success notification
|
|
return redirect()
|
|
->route('widgets.overview')
|
|
->with('success', trans('admin/widgets.successfullyCreated'));
|
|
}
|
|
}
|