Total Complexity | 10 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
8 | class MassRestoreTool extends AbstractTool |
||
9 | { |
||
10 | /** |
||
11 | * Set CRUD object. |
||
12 | * |
||
13 | * @param CRUD $crud |
||
14 | */ |
||
15 | public function setCrud(CRUD $crud) |
||
16 | { |
||
17 | parent::setCrud($crud); |
||
18 | |||
19 | if ($this->check()) { |
||
20 | $this->crud()->enableBatchCheckboxes(true); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Position where should tool be placed. |
||
26 | */ |
||
27 | public function position() |
||
28 | { |
||
29 | return self::POSITION_BODY_TOP; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Unique tool identifier. |
||
34 | */ |
||
35 | public function identifier(): string |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Tool's view. |
||
42 | */ |
||
43 | public function render() |
||
44 | { |
||
45 | return view('jarboe::crud.toolbar.mass_restore', [ |
||
46 | 'tool' => $this, |
||
47 | ]); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Handle tool execution. |
||
52 | * |
||
53 | * @param Request $request |
||
54 | * |
||
55 | * @return \Illuminate\Http\JsonResponse |
||
56 | */ |
||
57 | public function handle(Request $request) |
||
58 | { |
||
59 | $errors = []; |
||
60 | $restored = []; |
||
61 | foreach ($request->get('ids') as $id) { |
||
62 | try { |
||
63 | if (!$this->crud()->repo()->restore($id)) { |
||
64 | throw new \Exception(__('jarboe::toolbar.mass_restore.restore_event_failed')); |
||
|
|||
65 | } |
||
66 | $restored[] = $id; |
||
67 | } catch (\Exception $e) { |
||
68 | $errors[$id] = $e->getMessage(); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return response()->json([ |
||
73 | 'errors' => $errors, |
||
74 | 'restored' => $restored, |
||
75 | ]); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Check allowance to show and process tool. |
||
80 | */ |
||
81 | public function check(): bool |
||
84 | } |
||
85 | } |
||
86 |