99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Widgets;
|
|
|
|
use App\CMS\Repositories\Widgets\Widget as WidgetRepository;
|
|
use App\CMS\Widgets\Exceptions\MissingParameterException;
|
|
use App\Http\Controllers\Admin\Controller;
|
|
use App\Http\Requests\EditWidgetRequest;
|
|
use App\Models\Widgets\Widget;
|
|
|
|
class EditWidget extends Controller
|
|
{
|
|
/**
|
|
* @var WidgetRepository
|
|
*/
|
|
private $widgetRepository;
|
|
|
|
/**
|
|
* EditWidget constructor.
|
|
*
|
|
* @param WidgetRepository $widgetRepository
|
|
*/
|
|
public function __construct(WidgetRepository $widgetRepository)
|
|
{
|
|
$this->widgetRepository = $widgetRepository;
|
|
}
|
|
|
|
/**
|
|
* Render edit form
|
|
*
|
|
* @param Widget $widget
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function getEdit(Widget $widget)
|
|
{
|
|
$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 $widget
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function postEdit(EditWidgetRequest $request, Widget $widget)
|
|
{
|
|
try {
|
|
$this->widgetRepository->edit($widget, $request);
|
|
} 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()]));
|
|
}
|
|
|
|
// redirect with success notification
|
|
return redirect()
|
|
->back()
|
|
->with('success', trans('admin/widgets.successfullyEdited'));
|
|
}
|
|
|
|
/**
|
|
* Deletes widget
|
|
*
|
|
* @param Widget $widget
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function deleteWidget(Widget $widget)
|
|
{
|
|
// 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);
|
|
}
|
|
} |