2020-01-07 17:15:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Http\Controller;
|
|
|
|
use App\Http\Requests\Common\Dashboard as Request;
|
|
|
|
use App\Jobs\Common\CreateDashboard;
|
|
|
|
use App\Jobs\Common\DeleteDashboard;
|
|
|
|
use App\Jobs\Common\UpdateDashboard;
|
|
|
|
use App\Models\Common\Dashboard;
|
|
|
|
use App\Models\Common\Widget;
|
|
|
|
use App\Traits\DateTime;
|
|
|
|
use App\Traits\Users;
|
|
|
|
use App\Utilities\Widgets;
|
2020-11-02 16:05:48 +03:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
class Dashboards extends Controller
|
|
|
|
{
|
|
|
|
use DateTime, Users;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Add CRUD permission check
|
2020-06-21 18:00:31 +03:00
|
|
|
$this->middleware('permission:create-common-dashboards')->only('create', 'store', 'duplicate', 'import');
|
|
|
|
$this->middleware('permission:read-common-dashboards')->only('show');
|
|
|
|
$this->middleware('permission:update-common-dashboards')->only('index', 'edit', 'export', 'update', 'enable', 'disable', 'share');
|
2020-01-07 17:15:00 +03:00
|
|
|
$this->middleware('permission:delete-common-dashboards')->only('destroy');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$dashboards = user()->dashboards()->collect();
|
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
return $this->response('common.dashboards.index', compact('dashboards'));
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2020-05-21 00:32:47 +03:00
|
|
|
public function show($dashboard_id = null)
|
2020-01-07 17:15:00 +03:00
|
|
|
{
|
2020-05-21 00:32:47 +03:00
|
|
|
$dashboard_id = $dashboard_id ?? session('dashboard_id');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-11-02 16:05:48 +03:00
|
|
|
try {
|
|
|
|
$dashboard = Dashboard::findOrFail($dashboard_id);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
2020-06-15 14:30:04 +03:00
|
|
|
$dashboard = user()->dashboards()->enabled()->first();
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
2020-05-21 00:32:47 +03:00
|
|
|
if (empty($dashboard)) {
|
|
|
|
$dashboard = $this->dispatch(new CreateDashboard([
|
2021-04-16 00:59:43 +03:00
|
|
|
'company_id' => company_id(),
|
2020-05-21 00:32:47 +03:00
|
|
|
'name' => trans_choice('general.dashboards', 1),
|
2021-03-30 00:12:51 +03:00
|
|
|
'default_widgets' => 'core',
|
2020-05-21 00:32:47 +03:00
|
|
|
]));
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:30:04 +03:00
|
|
|
session(['dashboard_id' => $dashboard->id]);
|
|
|
|
|
2020-01-07 17:15:00 +03:00
|
|
|
$widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) {
|
2020-06-13 00:13:39 +03:00
|
|
|
return Widgets::canShow($widget->class);
|
2020-01-07 17:15:00 +03:00
|
|
|
});
|
|
|
|
|
2021-03-23 17:32:39 +03:00
|
|
|
$date_picker_shortcuts = $this->getDatePickerShortcuts();
|
|
|
|
|
|
|
|
if (!request()->has('start_date')) {
|
|
|
|
request()->merge(['start_date' => $date_picker_shortcuts[trans('reports.this_year')]['start']]);
|
|
|
|
request()->merge(['end_date' => $date_picker_shortcuts[trans('reports.this_year')]['end']]);
|
|
|
|
}
|
|
|
|
|
2021-03-23 14:54:28 +03:00
|
|
|
return view('common.dashboards.show', compact('dashboard', 'widgets', 'date_picker_shortcuts'));
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
$users = company()->users()->get()->sortBy('name');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
return view('common.dashboards.create', compact('users'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param $request
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$response = $this->ajaxDispatch(new CreateDashboard($request));
|
|
|
|
|
|
|
|
if ($response['success']) {
|
2020-01-21 11:59:52 +03:00
|
|
|
$response['redirect'] = route('dashboards.index');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
$message = trans('messages.success.added', ['type' => trans_choice('general.dashboards', 1)]);
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
2020-01-21 11:59:52 +03:00
|
|
|
$response['redirect'] = route('dashboards.create');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
$message = $response['message'];
|
|
|
|
|
2021-02-12 19:26:38 +03:00
|
|
|
flash($message)->error()->important();
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit(Dashboard $dashboard)
|
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
if ($this->isNotUserDashboard($dashboard->id)) {
|
2020-01-07 17:15:00 +03:00
|
|
|
return redirect()->route('dashboards.index');
|
|
|
|
}
|
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
$users = company()->users()->get()->sortBy('name');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
return view('common.dashboards.edit', compact('dashboard', 'users'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
* @param $request
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(Dashboard $dashboard, Request $request)
|
|
|
|
{
|
|
|
|
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, $request));
|
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('dashboards.index');
|
|
|
|
|
|
|
|
$message = trans('messages.success.updated', ['type' => trans_choice('general.dashboards', 1)]);
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$response['redirect'] = route('dashboards.edit', $dashboard->id);
|
|
|
|
|
|
|
|
$message = $response['message'];
|
|
|
|
|
2021-02-12 19:26:38 +03:00
|
|
|
flash($message)->error()->important();
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the specified resource.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function enable(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 1])));
|
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.dashboards', 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the specified resource.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function disable(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 0])));
|
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.dashboards', 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
$response = $this->ajaxDispatch(new DeleteDashboard($dashboard));
|
|
|
|
|
2020-01-21 11:59:52 +03:00
|
|
|
$response['redirect'] = route('dashboards.index');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
$message = trans('messages.success.deleted', ['type' => $dashboard->name]);
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
|
2020-06-20 12:59:47 +03:00
|
|
|
session(['dashboard_id' => user()->dashboards()->enabled()->pluck('id')->first()]);
|
2020-01-07 17:15:00 +03:00
|
|
|
} else {
|
|
|
|
$message = $response['message'];
|
|
|
|
|
2021-02-12 19:26:38 +03:00
|
|
|
flash($message)->error()->important();
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the active dashboard.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function switch(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
if ($this->isUserDashboard($dashboard->id)) {
|
|
|
|
session(['dashboard_id' => $dashboard->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
|
|
|
}
|