2019-11-16 07:21:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Common;
|
|
|
|
|
|
|
|
|
|
use App\Abstracts\Http\Controller;
|
|
|
|
|
use App\Http\Requests\Common\BulkAction as Request;
|
2021-07-30 13:09:27 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2023-10-03 08:06:08 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-06-22 14:59:33 +00:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 07:21:14 +00:00
|
|
|
|
2020-11-05 22:48:49 +00:00
|
|
|
class BulkActions extends Controller
|
2019-11-16 07:21:14 +00:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @param $group
|
|
|
|
|
* @param $type
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function action($group, $type, Request $request)
|
|
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
$handle = $request->get('handle', '*');
|
|
|
|
|
|
|
|
|
|
if ($handle == '*') {
|
2020-02-22 09:29:46 +00:00
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'redirect' => true,
|
|
|
|
|
'error' => true,
|
|
|
|
|
'data' => [],
|
|
|
|
|
'message' => ''
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
// Check is module
|
|
|
|
|
$module = module($group);
|
2022-06-01 07:15:55 +00:00
|
|
|
$page = ucfirst($type);
|
2019-11-16 07:21:14 +00:00
|
|
|
|
|
|
|
|
if ($module instanceof \Akaunting\Module\Module) {
|
2020-06-22 13:33:20 +00:00
|
|
|
$tmp = explode('.', $type);
|
|
|
|
|
$file_name = !empty($tmp[1]) ? Str::studly($tmp[0]) . '\\' . Str::studly($tmp[1]) : Str::studly($tmp[0]);
|
|
|
|
|
|
|
|
|
|
$bulk_actions = app('Modules\\' . $module->getStudlyName() . '\BulkActions\\' . $file_name);
|
2022-06-01 07:15:55 +00:00
|
|
|
|
|
|
|
|
$page = ucfirst($file_name);
|
2019-11-16 07:21:14 +00:00
|
|
|
} else {
|
|
|
|
|
$bulk_actions = app('App\BulkActions\\' . ucfirst($group) . '\\' . ucfirst($type));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 08:06:08 +00:00
|
|
|
if (
|
|
|
|
|
isset($bulk_actions->actions[$handle]['permission'])
|
|
|
|
|
&& ! user()->can($bulk_actions->actions[$handle]['permission'])
|
|
|
|
|
) {
|
2021-02-12 16:26:38 +00:00
|
|
|
flash(trans('errors.message.403'))->error()->important();
|
2020-02-13 12:49:43 +00:00
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'redirect' => true,
|
|
|
|
|
'error' => true,
|
|
|
|
|
'data' => [],
|
|
|
|
|
'message' => trans('errors.message.403')
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 07:15:55 +00:00
|
|
|
$result = $bulk_actions->{$handle}($request);
|
|
|
|
|
|
2023-02-22 16:17:32 +00:00
|
|
|
$count = count($request->get('selected'));
|
|
|
|
|
$not_passed = 0;
|
2022-06-01 07:15:55 +00:00
|
|
|
|
2023-02-22 16:17:32 +00:00
|
|
|
flash()->messages->each(function ($message) use (&$not_passed) {
|
|
|
|
|
if (in_array($message->level, ['danger', 'warning'])) {
|
|
|
|
|
$not_passed++;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$message = trans($bulk_actions->messages['general'], ['type' => $handle, 'count' => $count - $not_passed]);
|
|
|
|
|
|
|
|
|
|
if (array_key_exists($handle, $bulk_actions->messages) && $not_passed === 0) {
|
2022-06-01 07:15:55 +00:00
|
|
|
$message = trans($bulk_actions->messages[$handle], ['type' => $page]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 16:17:32 +00:00
|
|
|
$level = $not_passed > 0 ? 'info' : 'success';
|
|
|
|
|
|
2023-10-03 08:06:08 +00:00
|
|
|
if (
|
|
|
|
|
(
|
|
|
|
|
isset($bulk_actions->actions[$handle]['type'])
|
|
|
|
|
&& $bulk_actions->actions[$handle]['type'] != 'modal'
|
|
|
|
|
)
|
|
|
|
|
|| ! isset($bulk_actions->actions[$handle]['type'])
|
|
|
|
|
|| $not_passed > 0
|
|
|
|
|
) {
|
|
|
|
|
flash($message)->{$level}();
|
|
|
|
|
}
|
2019-11-22 13:48:44 +00:00
|
|
|
|
2023-10-03 08:06:08 +00:00
|
|
|
if (
|
|
|
|
|
! empty($result)
|
|
|
|
|
&& ($result instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse)
|
|
|
|
|
) {
|
2019-12-27 14:18:45 +00:00
|
|
|
return $result;
|
2023-10-03 08:06:08 +00:00
|
|
|
} else if (
|
|
|
|
|
! empty($result)
|
|
|
|
|
&& ($result instanceof RedirectResponse)
|
|
|
|
|
) {
|
2021-07-30 13:09:27 +00:00
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'redirect' => $result->getTargetUrl(),
|
|
|
|
|
'error' => false,
|
|
|
|
|
'data' => [],
|
2023-10-03 08:06:08 +00:00
|
|
|
'message' => $message,
|
2021-07-30 13:09:27 +00:00
|
|
|
]);
|
2023-10-03 08:06:08 +00:00
|
|
|
} else if (
|
|
|
|
|
! empty($result)
|
|
|
|
|
&& ($result instanceof JsonResponse)
|
|
|
|
|
) {
|
|
|
|
|
return $result;
|
2019-11-22 13:48:44 +00:00
|
|
|
} else {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'redirect' => true,
|
|
|
|
|
'error' => false,
|
|
|
|
|
'data' => [],
|
2023-10-03 08:06:08 +00:00
|
|
|
'message' => $message,
|
2019-11-22 13:48:44 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2019-11-16 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
}
|