76 lines
2.1 KiB
PHP
76 lines
2.1 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 $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);
|
|
} 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);
|
|
}
|
|
}
|