TechTipsController::forceDelete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.9
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use Inertia\Inertia;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use App\Http\Controllers\Controller;
10
11
use App\Traits\TechTipTrait;
12
13
use App\Models\TechTip;
14
use App\Models\TechTipType;
15
use App\Models\EquipmentCategory;
16
use App\Models\UserTechTipBookmark;
17
18
use App\Events\TechTips\TechTipCreatedEvent;
19
use App\Events\TechTips\TechTipDeletedEvent;
20
use App\Events\TechTips\TechTipForceDeletedEvent;
21 78
use App\Events\TechTips\TechTipRestoredEvent;
22
use App\Events\TechTips\TechTipUpdatedEvent;
23 78
use App\Http\Requests\TechTips\CreateTipRequest;
24 78
use App\Http\Requests\TechTips\UpdateTipRequest;
25
use App\Models\TechTipFile;
26
use App\Traits\FileTrait;
27 2
28
class TechTipsController extends Controller
29 2
{
30 2
    use TechTipTrait;
31 2
    // use FileTrait;
32
33
    /**
34
     * Tech Tips search page
35
     */
36 8
    public function index(Request $request)
37
    {
38 8
        $filterData = [
39
            'tip_types'   => TechTipType::all(),
40
            'equip_types' => EquipmentCategory::with('EquipmentType')->get(),
41
        ];
42 4
43
        return Inertia::render('TechTips/Index', [
44 4
            'filter_data' => $filterData,
45
            'create'      => $request->user()->can('create', TechTip::class),
46 2
        ]);
47 2
    }
48
49
    /**
50
     * Show the form for creating a new Tech Tip
51
     */
52 4
    public function create()
53
    {
54 4
        $this->authorize('create', TechTip::class);
55
56 2
        return Inertia::render('TechTips/Create', [
57 2
            'tip_types' => TechTipType::all(),
58 2
            'equipment' => EquipmentCategory::with('EquipmentType')->get(),
59
        ]);
60
    }
61
62
    /**
63 6
     * Store a newly created Tech Tip
64
     */
65 6
    public function store(CreateTipRequest $request)
66
    {
67 4
        //  Create the new Tip
68 4
        $newTip = TechTip::create([
69
            'user_id'     => $request->user()->user_id,
70
            'tip_type_id' => $request->tip_type_id,
71
            'sticky'      => $request->sticky,
72 2
            'subject'     => $request->subject,
73
            'slug'        => Str::slug($request->subject),
74 2
            'details'     => $request->details,
75
        ]);
76 2
77
        $this->addEquipment($newTip->tip_id, $request->equipment);
78
        $this->processNewFiles($newTip->tip_id, true);
79
80
        event(new TechTipCreatedEvent($newTip, !$request->noEmail));
81 2
        return redirect(route('tech-tips.show', $newTip->slug));
82 2
    }
83 2
84
    /**
85
     * Display the Tech Tip
86
     */
87
    public function show($id)
88
    {
89
        //  Check if we are passing the Tech Tip name or number
90
        if(is_numeric($id))
91
        {
92
            //  To keep things uniform, redirect to a link that has the Tech Tip subject rather than the ID
93
            $tip = TechTip::findOrFail($id);
94
            return redirect(route('tech-tips.show', $tip->slug));
95
        }
96
97
        //  Pull the Tech Tip Information
98
        $tip = TechTip::where('slug', $id)
99
                ->with('EquipmentType')
100
                ->with('FileUploads')
101
                ->with('TechTipComment.User')
102
                ->firstOrFail()
103
                ->makeHidden(['summary', 'sticky']);
104
105
        //  Determine if the Tech Tip is bookmarked by the user
106
        $isFav = UserTechTipBookmark::where('user_id', Auth::user()->user_id)
107
                    ->where('tip_id', $tip->tip_id)
108 6
                    ->count();
109
110 6
        return Inertia::render('TechTips/Show', [
111
            'tip' => $tip,
112 4
            //  User Permissions for Tech Tips
113 4
            'user_data' => [
114
                'fav' => $isFav,
115 2
                'permissions' => [
116
                    'edit'      => Auth::user()->can('update', $tip),
117
                    'delete'    => Auth::user()->can('delete', $tip),
118 2
                    'manage'    => Auth::user()->can('manage', TechTip::class),
119 2
                    'comment'   => [
120 2
                        'create'  => Auth::user()->can('create', TechTipComment::class),
121 2
                        'manage'  => Auth::user()->can('manage', TechTipComment::class),
122
                    ],
123
                ],
124
            ]
125
        ]);
126 6
    }
127
128 6
    /**
129
     * Show the form to Edit the Tech Tip
130 4
     */
131
    public function edit($id)
132 4
    {
133
        $tip = TechTip::with(['EquipmentType', 'FileUploads'])->findOrFail($id)->makeVisible(['tip_type_id']);
134
        $this->authorize('update', $tip);
135
136 4
        return Inertia::render('TechTips/Edit', [
137
            'data'      => $tip,
138 4
            'tip_types' => TechTipType::all(),
139
            'equipment' => EquipmentCategory::with('EquipmentType')->get(),
140 2
        ]);
141 2
    }
142
143
    /**
144
     * Update the Tech Tip
145
     */
146
    public function update(UpdateTipRequest $request, $id)
147
    {
148
        //  Update the Tech Tip
149
        $tip = TechTip::findOrFail($id);
150
        $tip->update([
151
            'updated_id'  => $request->user()->user_id,
152
            'tip_type_id' => $request->tip_type_id,
153
            'sticky'      => $request->sticky,
154
            'subject'     => $request->subject,
155
            'slug'        => Str::slug($request->subject),
156
            'details'     => $request->details,
157
        ]);
158
159
        $this->processUpdatedEquipment($tip->tip_id, $request->equipment);
160
        $this->removeFiles($tip->tip_id, $request->removedFiles);
161
        $this->processNewFiles($tip->tip_id);
162
163
        event(new TechTipUpdatedEvent($tip, $request->resendNotif));
164
        return redirect(route('tech-tips.show', $tip->slug))->with([
165
            'message' => 'Tech Tip Updated',
166
            'type'    => 'success',
167
        ]);
168
    }
169
170
    /**
171
     * Soft Delete the Tech Tip
172
     */
173
    public function destroy($id)
174
    {
175
        $tip = TechTip::findOrFail($id);
176
        $this->authorize('delete', $tip);
177
        $tip->delete();
178
179
        event(new TechTipDeletedEvent($tip));
180
        return redirect(route('tech-tips.index'))->with([
181
            'message' => 'Tech Tip Deleted',
182
            'type'    => 'danger',
183
        ]);
184
    }
185
186
    /**
187
     * Restore a Tech Tip that was Soft Deleted
188
     */
189
    public function restore($id)
190
    {
191
        $tip = TechTip::withTrashed()->find($id);
192
        $this->authorize('manage', $tip);
193
        $tip->restore();
194
195
        event(new TechTipRestoredEvent($tip));
196
        return redirect(route('tech-tips.show', $tip->slug))->with([
197
            'message' => 'Tech Tip Restored',
198
            'type'    => 'success',
199
        ]);
200
    }
201
202
    /**
203
     * Permanently delete a Tech Tip from the database
204
     */
205
    public function forceDelete($id)
206
    {
207
        $tip = TechTip::withTrashed()->find($id);
208
        $this->authorize('manage', $tip);
209
210
        //  Determine if the Tech Tip has files that need to be deleted
211
        $files = TechTipFile::where('tip_id', $tip->tip_id)->get();
212
        foreach($files as $file)
213
        {
214
            $file->delete();
215
            $this->deleteFile($file->file_id);
216
        }
217
218
        $tip->forceDelete();
219
220
        event(new TechTipForceDeletedEvent($tip));
221
        return redirect(route('admin.tips.deleted'))->with([
222
            'message' => 'Tech Tip Permanently Deleted',
223
            'type'    => 'danger',
224
        ]);
225
    }
226
}
227