FEATURE #1450: add widget overview

This commit is contained in:
Michal Kváček
2016-02-15 12:25:56 +01:00
parent ccc2d918c9
commit 1d553d1a15
6 changed files with 170 additions and 8 deletions
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\EditWidgetRequest;
use App\Models\Widget;
use App\Widgets\Exceptions\MissingParameterException;
class EditWidget extends Controller
{
public function getEdit($id)
{
$widget = Widget::findOrFail($id);
// decompose widget settings
$settings = json_decode($widget->settings);
$widget->containerClass = $settings->containerClass;
$widget->code = $settings->code;
$presenter = call_user_func([$widget->widget_type_handler, 'getPresenter']);
return call_user_func([$presenter, 'renderEditForm'], $widget);
}
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, $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()]));
}
// save into database
$widget->save();
// redirect with success notification
return redirect()
->route('widgets.overview')
->with('success', trans('admin/widgets.successfullyEdited'));
}
private function invalidWidget($error)
{
return redirect()
->route('widgets.overview')
->withInput()
->with('error', $error);
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Widget;
class WidgetsOverview extends Controller
{
public function getOverview()
{
$widgets = Widget::paginate(25);
return view('admin.widgets.overview', ['widgets' => $widgets]);
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Http\Requests;
class EditWidgetRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required'
];
}
}
+10 -4
View File
@@ -3,22 +3,28 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/** /**
* Class WidgetArea * Class WidgetArea
*/ */
class WidgetArea extends Model class WidgetArea extends Model
{ {
public $timestamps = true; public $timestamps = true;
protected $fillable = [ protected $fillable = [
'name', 'name',
'code', 'code',
'description' 'description'
]; ];
protected $guarded = []; protected $guarded = [];
public function getWidgets($uniqueCode)
{
return DB::table('widget_areas')
->select('widget_areas.*', 'widgets.widget_type_handler', 'widgets.name as widget_name', 'widgets.settings')
->where('code', $uniqueCode)
->join('widget_in_widget_area', 'widget_in_widget_area.widget_area_id', '=', 'widget_areas.id')
->join('widgets', 'widget_in_widget_area.widget_id', '=', 'widgets.id')
->orderBy('widget_in_widget_area.position');
}
} }
+13 -4
View File
@@ -9,16 +9,25 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class WidgetInWidgetArea extends Model class WidgetInWidgetArea extends Model
{ {
protected $table = 'widget_in_widget_area';
public $timestamps = false; public $timestamps = false;
protected $fillable = [ protected $fillable = [
'widget_id', 'widget_id',
'widget_area_id', 'widget_area_id',
'position' 'position'
]; ];
protected $guarded = []; protected $guarded = [];
public static function getWidgetIdsFromArea($widgetAreaId)
{
$models = WidgetInWidgetArea::where('widget_area_id', $widgetAreaId)
->get();
$ids = [];
foreach ($models as $model) {
$ids[] = $model->widget_id;
}
return $ids;
}
} }
@@ -0,0 +1,50 @@
@extends('admin.layout')
@section('pageName', @trans('admin/widgets.overview'))
@section('content')
<div class="row">
<h2>@lang('admin/widgets.overview')</h2>
</div>
<div class="row">
@if (session('success'))
<div class="callout success">
{{ session('success') }}
</div>
@endif
@if (session('error'))
<div class="callout alert">
{{ session('error') }}
</div>
@endif
@if (!empty($widgets))
<table class="full-width">
<tr>
<th>@lang('admin/widgets.widgetType')</th>
<th>@lang('admin/widgets.name')</th>
<th>@lang('admin/widgets.description')</th>
<th>@lang('admin/widgets.actions')</th>
</tr>
@foreach($widgets as $widget)
<tr>
<td>{{trans(call_user_func([$widget->widget_type_handler, 'getName']))}}</td>
<td>{{$widget->name}}</td>
<td>{{$widget->description}}</td>
<td class="text-center">
<a href="{{route('widgets.edit', ['id' => $widget->id])}}"><i class="fa fa-edit"></i></a>
</td>
</tr>
@endforeach
</table>
{!! $widgets->render() !!}
@else
<em>@lang('admin/widgets.noEntries')</em>
@endif
</div>
@stop