FEATURE: split controllers/admin into more smaller directories
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers\Admin\Widgets;
|
||||
|
||||
use App\Http\Controllers\Admin\Controller;
|
||||
use App\Http\Requests\EditWidgetRequest;
|
||||
use App\Models\Widget;
|
||||
use App\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);
|
||||
|
||||
$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;
|
||||
|
||||
// save description if available
|
||||
if (isset($request->description)) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user