225 lines
5.3 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Common;
use App\Abstracts\Http\Controller;
use App\Http\Requests\Common\Report as Request;
use App\Jobs\Common\CreateReport;
use App\Jobs\Common\DeleteReport;
use App\Jobs\Common\UpdateReport;
use App\Models\Common\Report;
use App\Utilities\Reports as Utility;
class Reports extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2020-01-18 13:11:19 +03:00
$totals = $icons = $categories = [];
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
$reports = Report::all();
2019-11-16 10:21:14 +03:00
2020-01-04 13:42:58 +03:00
foreach ($reports as $report) {
2020-01-04 17:49:11 +03:00
if (!Utility::canRead($report->class)) {
2019-11-16 10:21:14 +03:00
continue;
}
2020-01-04 17:49:11 +03:00
$class = Utility::getClassInstance($report);
2020-01-18 13:11:19 +03:00
$totals[$report->id] = $class->getTotal();
$icons[$report->id] = $class->getIcon();
2020-01-04 13:42:58 +03:00
$categories[$class->getCategory()][] = $report;
2019-11-16 10:21:14 +03:00
}
2020-01-18 13:11:19 +03:00
return view('common.reports.index', compact('categories', 'totals', 'icons'));
2019-11-16 10:21:14 +03:00
}
/**
* Show the form for viewing the specified resource.
*
* @param Report $report
* @return Response
*/
public function show(Report $report)
{
2020-01-04 17:49:11 +03:00
if (!Utility::canRead($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-04 17:49:11 +03:00
return Utility::getClassInstance($report)->show();
2019-11-16 10:21:14 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$classes = Utility::getClasses();
2020-01-16 15:39:37 +03:00
return view('common.reports.create', compact('classes'));
2019-11-16 10:21:14 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return Response
*/
public function store(Request $request)
{
$response = $this->ajaxDispatch(new CreateReport($request));
if ($response['success']) {
$response['redirect'] = route('reports.index');
$message = trans('messages.success.added', ['type' => trans_choice('general.reports', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('reports.create');
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Show the form for editing the specified resource.
*
* @param Report $report
*
* @return Response
*/
public function edit(Report $report)
{
$classes = Utility::getClasses();
2020-01-16 15:39:37 +03:00
$class = Utility::getClassInstance($report);
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
return view('common.reports.edit', compact('report', 'classes', 'class'));
2019-11-16 10:21:14 +03:00
}
/**
* Update the specified resource in storage.
*
* @param Report $report
* @param $request
* @return Response
*/
public function update(Report $report, Request $request)
{
$response = $this->ajaxDispatch(new UpdateReport($report, $request));
if ($response['success']) {
$response['redirect'] = route('reports.index');
$message = trans('messages.success.updated', ['type' => $report->name]);
flash($message)->success();
} else {
$response['redirect'] = route('reports.edit', $report->id);
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Remove the specified resource from storage.
*
* @param Report $report
*
* @return Response
*/
public function destroy(Report $report)
{
$response = $this->ajaxDispatch(new DeleteReport($report));
$response['redirect'] = route('reports.index');
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => $report->name]);
flash($message)->success();
} else {
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Print the report.
*
* @param Report $report
* @return Response
*/
public function print(Report $report)
{
2020-01-04 17:49:11 +03:00
if (!Utility::canRead($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-04 17:49:11 +03:00
return Utility::getClassInstance($report)->print();
2019-11-16 10:21:14 +03:00
}
/**
* Export the report.
*
* @param Report $report
* @return Response
*/
public function export(Report $report)
{
2020-01-04 17:49:11 +03:00
if (!Utility::canRead($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-04 17:49:11 +03:00
return Utility::getClassInstance($report)->export();
2019-11-16 10:21:14 +03:00
}
/**
2020-01-16 15:39:37 +03:00
* Get fields of the specified resource.
2019-11-16 10:21:14 +03:00
*
* @return Response
*/
2020-01-16 15:39:37 +03:00
public function fields()
2019-11-16 10:21:14 +03:00
{
$class = request('class');
if (!class_exists($class)) {
return response()->json([
'success' => false,
'error' => true,
2020-01-16 15:39:37 +03:00
'message' => 'Class does not exist',
'html' => '',
2019-11-16 10:21:14 +03:00
]);
}
2020-01-16 15:39:37 +03:00
$fields = (new $class())->getFields();
$html = view('partials.reports.fields', compact('fields'))->render();
2019-11-16 10:21:14 +03:00
return response()->json([
'success' => true,
'error' => false,
'message' => '',
2020-01-16 15:39:37 +03:00
'html' => $html,
2019-11-16 10:21:14 +03:00
]);
}
}