105 lines
3.0 KiB
PHP
105 lines
3.0 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin\Widgets;
|
|
|
|
use App\Http\Controllers\Admin\Controller;
|
|
use App\Http\Requests\EditWidgetRequest;
|
|
use App\Models\Widgets\Widget;
|
|
use App\CMS\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);
|
|
$widget->settings = json_decode($widget->settings);
|
|
|
|
$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;
|
|
$widget->container_class = $request->containerClass;
|
|
$widget->container_id = empty($request->containerId) ? null : $request->containerId;
|
|
$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);
|
|
}
|
|
} |