76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Widgets;
|
|
|
|
use App\Events\AssignWidget;
|
|
use App\Events\RemoveWidgetFromArea;
|
|
use App\Http\Controllers\Admin\Controller;
|
|
use App\Models\Widget;
|
|
use App\Models\WidgetArea;
|
|
use App\Models\WidgetInWidgetArea;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
class AssociateWidget extends Controller
|
|
{
|
|
public function getList($widgetArea)
|
|
{
|
|
$area = WidgetArea::findOrFail($widgetArea);
|
|
$associatedWidgetIds = WidgetInWidgetArea::getWidgetIdsFromArea($area->id);
|
|
$widgets = Widget::where('language', LaravelLocalization::getCurrentLocale())
|
|
->paginate(25);
|
|
|
|
return view('admin.widgets.association', [
|
|
'widgetArea' => $area,
|
|
'widgets' => $widgets,
|
|
'associatedWidgetIds' => $associatedWidgetIds
|
|
]);
|
|
}
|
|
|
|
public function makeAssociation($widgetId, $widgetAreaId)
|
|
{
|
|
// check if given values are valid
|
|
$widget = Widget::findOrFail($widgetId);
|
|
$widgetArea = WidgetArea::findOrFail($widgetAreaId);
|
|
|
|
try {
|
|
WidgetInWidgetArea::create(['widget_id' => $widgetId, 'widget_area_id' => $widgetAreaId]);
|
|
} catch (QueryException $e) {
|
|
return redirect()
|
|
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
|
->with('error', trans('admin/widgets.associationExists'));
|
|
}
|
|
|
|
Event::fire(new AssignWidget($widget, $widgetArea));
|
|
|
|
return redirect()
|
|
->back()
|
|
->with('success', trans('admin/widgets.successfullyAssociated'));
|
|
}
|
|
|
|
public function deleteAssociation($widgetId, $widgetAreaId)
|
|
{
|
|
// check if given values are valid
|
|
$widget = Widget::findOrFail($widgetId);
|
|
$widgetArea = WidgetArea::findOrFail($widgetAreaId);
|
|
|
|
$widgetInArea = WidgetInWidgetArea::where('widget_id', $widgetId)
|
|
->where('widget_area_id', $widgetAreaId);
|
|
|
|
if ($widgetInArea->count() == 0) {
|
|
return redirect()
|
|
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
|
->with('error', trans('admin/widgets.associationDoesNotExists'));
|
|
}
|
|
|
|
$widgetInArea->delete();
|
|
|
|
Event::fire(new RemoveWidgetFromArea($widget, $widgetArea));
|
|
|
|
return redirect()
|
|
->route('widgets.associate', ['widgetArea' => $widgetAreaId])
|
|
->with('success', trans('admin/widgets.successfullyDeassociated'));
|
|
}
|
|
}
|