-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDownloadController.php
More file actions
181 lines (145 loc) · 5.33 KB
/
DownloadController.php
File metadata and controls
181 lines (145 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace App\Http\Controllers\CMS;
use App\Http\Controllers\AppBaseController;
use App\Http\Repository\DownloadRepository;
use App\Http\Requests\CreateDownloadRequest;
use App\Http\Requests\UpdateDownloadRequest;
use App\Http\Transformers\DownloadTransformer;
use App\Models\Enums\StatusEnum;
use App\Traits\UploadedFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class DownloadController extends AppBaseController
{
use UploadedFile;
/** @var DownloadRepository */
private $downloadRepository;
protected $permission = 'website-downloads';
public function __construct(DownloadRepository $downloadRepo)
{
$this->downloadRepository = $downloadRepo;
$this->pathFolder = 'uploads/downloads';
}
/**
* Display a listing of the Download.
*/
public function index(Request $request)
{
if ($request->ajax()) {
return $this->fractal($this->downloadRepository->listDownload(), new DownloadTransformer, 'downloads')->respond();
}
$listPermission = $this->generateListPermission();
return view('downloads.index')->with($listPermission);
}
/**
* Show the form for creating a new Download.
*/
public function create()
{
return view('downloads.create', $this->getOptionItems());
}
/**
* Store a newly created Download in storage.
*/
public function store(CreateDownloadRequest $request)
{
$input = $request->all();
try {
if ($request->file('download_file')) {
// Upload as generic file (not image)
$input['url'] = $this->uploadFile($request, 'download_file', null, 5120);
}
$this->downloadRepository->create($input);
Session::flash('success', 'File berhasil disimpan.');
return redirect(route('downloads.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['download_file' => 'Gagal mengunggah file: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat menyimpan file. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}
/**
* Display the specified Download.
*/
public function show($id)
{
$download = $this->downloadRepository->find($id);
if (empty($download)) {
Session::flash('error', 'File download tidak ditemukan');
return redirect(route('downloads.index'));
}
return view('downloads.show')->with('download', $download);
}
/**
* Show the form for editing the specified Download.
*/
public function edit($id)
{
$download = $this->downloadRepository->find($id);
if (empty($download)) {
Session::flash('error', 'File download tidak ditemukan');
return redirect(route('downloads.index'));
}
return view('downloads.edit', $this->getOptionItems($id))->with('download', $download);
}
/**
* Update the specified Download in storage.
*/
public function update($id, UpdateDownloadRequest $request)
{
$download = $this->downloadRepository->find($id);
if (empty($download)) {
Session::flash('error', 'File download tidak ditemukan');
return redirect(route('downloads.index'));
}
$input = $request->all();
try {
if ($request->file('download_file')) {
// Upload as generic file (not image)
$input['url'] = $this->uploadFile($request, 'download_file', null, 5120);
}
$download = $this->downloadRepository->update($input, $id);
Session::flash('success', 'File berhasil diupdate.');
return redirect(route('downloads.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['download_file' => 'Gagal mengunggah file: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat mengupdate file. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}
/**
* Remove the specified Download from storage.
*
* @throws \Exception
*/
public function destroy($id)
{
$download = $this->downloadRepository->find($id);
if (empty($download)) {
Session::flash('error', 'File download tidak ditemukan');
return redirect(route('downloads.index'));
}
$this->downloadRepository->delete($id);
if (request()->ajax()) {
return $this->sendSuccess('File download berhasil dihapus.');
}
Session::flash('success', 'File download berhasil dihapus.');
return redirect(route('downloads.index'));
}
protected function getOptionItems($id = null)
{
return [
'stateItem' => [StatusEnum::aktif => 'Ya', StatusEnum::tidakAktif => 'Tidak'],
];
}
}