Passed
Push — dev5 ( cbc2fc...7d6fd9 )
by Ron
09:10
created

TechTipsController::details()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use App\Domains\Equipment\GetEquipmentData;
6
use App\Domains\TechTips\GetTechTips;
7
use App\User;
8
use App\Files;
9
use App\TechTips;
10
use App\TechTipFavs;
11
use App\TechTipFiles;
12
use App\TechTipTypes;
13
use App\TechTipSystems;
14
use Illuminate\Http\File;
15
use App\SystemCategories;
16
use Illuminate\Http\Request;
17
use App\Notifications\NewTechTip;
18
use Illuminate\Http\UploadedFile;
19
use Illuminate\Support\Facades\Log;
20
use App\Http\Controllers\Controller;
21
use Illuminate\Support\Facades\Auth;
22
use Illuminate\Support\Facades\Route;
23
use Illuminate\Support\Facades\Storage;
24
use App\Http\Resources\TechTipsCollection;
25
use Illuminate\Support\Facades\Notification;
26
use App\Http\Resources\TechTipTypesCollection;
27
use App\Http\Resources\SystemCategoriesCollection;
28
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
29
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
30
use App\Http\Resources\SystemCategoriesCollection as CategoriesCollection;
31
32
33
use App\Domains\TechTips\GetTechTipTypes;
34
use App\Domains\TechTips\SetTechTips;
35
use App\Domains\Users\GetUserStats;
36
use App\Domains\Users\UserFavs;
37
use App\Http\Requests\TechTipEditTipRequest;
38
use App\Http\Requests\TechTipNewTipRequest;
39
use App\Http\Requests\TechTipProcessImageRequest;
40
use App\Http\Requests\TechTipSearchRequest;
41
42
class TechTipsController extends Controller
43
{
44 78
    public function __construct()
45
    {
46 78
        $this->middleware('auth');
47 78
    }
48
49
    //  Tech Tips landing page
50 2
    public function index()
51
    {
52 2
        return view('tips.index', [
53 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
54 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
55
        ]);
56
    }
57
58
    //  Search for an existing tip - If no paramaters, return all tips
59 8
    public function search(TechTipSearchRequest $request)
60
    {
61 8
        return (new GetTechTips)->searchTips($request);
62
    }
63
64
    //  Process an image that is attached to a tech tip
65 4
    public function processImage(TechTipProcessImageRequest $request)
66
    {
67 4
        $this->authorize('hasAccess', 'Create Tech Tip');
68
69 2
        $imgLocation = (new SetTechTips)->processTipImage($request);
70 2
        return response()->json(['location' => $imgLocation]);
71
72
    }
73
74
    //  Create a new Tech Tip form
75 4
    public function create()
76
    {
77 4
        $this->authorize('hasAccess', 'Create Tech Tip');
78
79 2
        return view('tips.create', [
80 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
81 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
82
        ]);
83
    }
84
85
    //  Submit the form to create a new tech tip
86 6
    public function store(TechTipNewTipRequest $request)
87
    {
88 6
        $this->authorize('hasAccess', 'Create Tech Tip');
89
90 4
        $tipData = (new SetTechTips)->processNewTip($request);
91 4
        return response()->json(['success' => $tipData]);
92
    }
93
94
    //  Details controller - will move to the show controller with just the tech tip id
95 2
    public function details($id)
96
    {
97 2
        $tipData = (new GetTechTips)->getTipDetails($id);
98
99 2
        return view('tips.details', [
100 2
            'details' => $tipData->toJson(),
101 2
            'isFav'   => (new GetUserStats)->checkForTechTipFav($id) ? 'true' : 'false',   // empty($isFav) ? 'false' : 'true',
102
        ]);
103
104
    }
105
106
    //  Show the details about the tech tip
107
    public function show($id)
108
    {
109
        $tipData = (new GetTechTips)->getTipDetails($id);
110
111
        return view('tips.details', [
112
            'details' => $tipData['data'],
113
            'isFav'   => (new GetUserStats)->checkForTechTipFav($id) ? 'true' : 'false',   // empty($isFav) ? 'false' : 'true',
114
            'files'   => $tipData['files'],
115
        ]);
116
    }
117
118
    //  Add or remove this tip as a favorite of the user
119
    public function toggleFav($action, $id)
120
    {
121
        (new UserFavs)->updateTechTipFav($id);
122
        return response()->json(['success' => true]);
123
    }
124
125
    //  Edit an existing tech tip
126 6
    public function edit($id)
127
    {
128 6
        $this->authorize('hasAccess', 'Edit Tech Tip');
129
130 4
        $tipData = (new GetTechTips)->getTipDetails($id);
131 4
        Log::emergency('tip details', array($tipData));
132 4
        if(!$tipData['details'])
133
        {
134 2
            return view('tips.tipNotFound');
135
        }
136
137 2
        return view('tips.editTip', [
138 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
139 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
140 2
            'tipData'  => $tipData,
141
        ]);
142
    }
143
144
    //  Store the edited Tech Tip
145 6
    public function update(TechTipEditTipRequest $request, $id)
146
    {
147 6
        $this->authorize('hasAccess', 'Edit Tech Tip');
148
149 4
        (new SetTechTips)->processEditTip($request, $id);
150
151 4
        return response()->json(['success' => true]);
152
    }
153
154
155
156
157
158
159
160
161
    //  Soft delet the Tech Tip
162 4
    public function destroy($id)
163
    {
164
        // Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
165
166 4
        $this->authorize('hasAccess', 'Delete Tech Tip');
167
168
        // //  Remove the Tip from any users favorites
169
        // TechTipFavs::where('tip_id', $id)->delete();
170
171
        // //  Disable the tip
172
        // TechTips::find($id)->delete();
173
        // Log::warning('User - '.Auth::user()->user_id.' deleted Tech Tip ID - '.$id);
174
175 2
        (new SetTechTips)->deleteTip($id);
176 2
        return response()->json(['success' => true]);
177
    }
178
}
179