115 lines
2.7 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Http\Controllers\Common;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
use App\Models\Common\Dashboard as Model;
use App\Models\Common\DashboardWidget;
use App\Http\Requests\Common\Dashboard as Request;
2017-09-14 22:21:00 +03:00
class Dashboard extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2019-11-16 10:21:14 +03:00
$dashboard_id = session('dashboard_id', 0);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
// Change Dashboard
if (request()->get('dashboard_id', 0)) {
$dashboard_id = request()->get('dashboard_id');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
session(['dashboard_id' => $dashboard_id]);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
$user_id = user()->id;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$dashboards = Model::where('user_id', $user_id)->enabled()->get();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
if (!$dashboard_id) {
$dashboard_id = $dashboards->first()->id;
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
// Dashboard
$dashboard = Model::find($dashboard_id);
2018-01-17 13:23:03 +03:00
2019-11-16 10:21:14 +03:00
// Dashboard Widgets
$widgets = DashboardWidget::where('dashboard_id', $dashboard->id)
->where('user_id', $user_id)
->orderBy('sort', 'asc')->get();
2017-12-12 19:09:16 +03:00
2019-11-16 10:21:14 +03:00
return view('common.dashboard.index', compact('dashboards','dashboard', 'widgets'));
2017-12-12 19:09:16 +03:00
}
2019-11-16 10:21:14 +03:00
/**
* Store a newly created resource in storage.
*
* @param $request
* @return Response
*/
public function store(Request $request)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$request['enabled'] = 1;
$request['user_id'] = user()->id;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$dashboard = Model::create($request->input());
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$response['data'] = $dashboard;
$response['redirect'] = route('dashboard');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
/**
* Show the form for editing the specified resource.
*
* @param Model $dashboard
*
* @return Response
*/
public function edit(Model $dashboard)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
return response()->json($dashboard);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
/**
* Update the specified resource in storage.
*
* @param Model $dashboard
* @param $request
* @return Response
*/
public function update(Model $dashboard, Request $request)
2017-12-12 19:09:16 +03:00
{
2019-11-16 10:21:14 +03:00
$request['enabled'] = 1;
$dashboard->update($request->input());
2017-12-12 19:09:16 +03:00
2019-11-16 10:21:14 +03:00
$response['data'] = $dashboard;
$response['redirect'] = route('dashboard');
2017-12-12 19:09:16 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-12-12 19:09:16 +03:00
}
2019-11-16 10:21:14 +03:00
/**
* Remove the specified resource from storage.
*
* @param Model $dashboard
*
* @return Response
*/
public function destroy(Model $dashboard)
2017-11-07 05:11:03 +03:00
{
2019-11-16 10:21:14 +03:00
$dashboard->delete();
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
session(['dashboard_id' => user()->dashboards()->pluck('id')->first()]);
2018-04-23 22:17:20 +03:00
2019-11-16 10:21:14 +03:00
$response['redirect'] = route('dashboard');
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
}