source: app/Http/Controllers/Dashboard/DepartmentsController.php@ e78295c

Last change on this file since e78295c was 796e7ac, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[194a359]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewDepartmentRequest;
7use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
8use App\Models\Department;
[6b95845]9use App\Models\File;
[13ff930]10use App\Models\Folder;
[194a359]11use App\Models\User;
[c6b84df]12use App\Notifications\NewDepartmentCreated;
[d795fa6]13use Carbon\Carbon;
[194a359]14use App\Http\Controllers\Controller;
[c6b84df]15use Illuminate\Support\Facades\Notification;
[24a616f]16use Illuminate\Support\Facades\Storage;
[c6b84df]17use Illuminate\Filesystem\Filesystem;
[194a359]18
19class DepartmentsController extends Controller
20{
21 public function index()
22 {
23 return view("dashboard.departments.index")->with([
[24a616f]24 "departments" => Department::all(),
[194a359]25 ]);
26 }
27
28 public function create()
29 {
30 return view("dashboard.departments.create");
31 }
32
[120759b]33 public function store(NewDepartmentRequest $request)
34 {
35 $department = new Department();
36
37 $department->name = $request->name;
38 $department->code = $request->code;
39
[b9c4a92]40 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
[e6c1f87]41
[c6b84df]42 if(!Storage::disk('uploads')->has($location)){
43 Storage::disk('uploads')->makeDirectory($location);
[120759b]44
[b9c4a92]45 }
[c6b84df]46 $department->location = $location;
[120759b]47 $department->user_id = auth()->id();
48
[c6b84df]49 $users = User::all();
50
[120759b]51 $department->save();
52
53 Alert::flash("New Department added successfully");
54
55 return redirect()->route("dashboard.departments.index");
56 }
57
[d795fa6]58 public function editShow($id)
59 {
60 return view("dashboard.departments.edit")->with([
61 "department" => Department::findOrFail($id)
62 ]);
63 }
64
[194a359]65 public function edit(UpdateDepartmentRequest $request, $id)
66 {
67 $department = Department::findOrFail($id);
68
[c6b84df]69 $folders = $department->folder;
[b9c4a92]70 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
[24a616f]71
[194a359]72 $department->name = $request->name;
73 $department->code = $request->code;
[c6b84df]74 $department->updated_at = Carbon::now();
[194a359]75
[24a616f]76 if($department->isDirty('code'))
77 {
[b9c4a92]78 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
[c6b84df]79 if(!Storage::disk('uploads')->has($location)){
80 Storage::disk('uploads')->move($oldLocation, $location);
81 $department->location = $location;
[24a616f]82 }
[6b95845]83
[c6b84df]84 foreach ($folders as $folder) {
85 $currArchId = explode('/', $folder->arch_id)[1];
86 $folder->arch_id = $department->code . '/' . $currArchId;
87 $folder->save();
88 foreach($folder->files as $file) {
89 $file->location = $location . DIRECTORY_SEPARATOR . $folder->name . DIRECTORY_SEPARATOR . $file->name;
[6b95845]90 $file->save();
91 }
92 }
[24a616f]93 }
94
[194a359]95 $department->save();
96
97 Alert::flash("Department edited successfully");
98
99 return redirect()->route("dashboard.departments.index");
100 }
101
[d795fa6]102 public function destroy($id)
[194a359]103 {
104 $department = Department::find($id);
[d795fa6]105 //$department->delete();
[c6b84df]106 $folders = $department->folder()->count();
[194a359]107
[c6b84df]108 if($folders > 0){
109 Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
[d795fa6]110 }
111 else {
112 $department->delete();
[194a359]113
[d795fa6]114 Alert::flash($department->name . " deleted successfully");
115 }
[194a359]116
117 return redirect()->route("dashboard.departments.index");
118 }
[c6b84df]119
120 public function downloadAll()
121 {
[13ff930]122 $departments = Department::all();
123 $flag=false;
124
125 foreach ($departments as $department) {
126 if($department->no_of_folders > 0) {
127 $folders = Folder::where('department_id', $department->id)->get();
128 }
[796e7ac]129 foreach ($folders as $folder){
130 if($folder->no_of_files > 0){
131 $flag=true;
[13ff930]132 break;
[796e7ac]133 }
[13ff930]134 }
135 }
136
137 if($flag) {
[2740efc]138 $zip_file = Storage::disk('uploads')->path('Departments.zip');
139 $zip = new \ZipArchive();
140 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
[2279f71]141
[2740efc]142 $path = Storage::disk('uploads')->path('Departments');
[df6e9ec]143
[68694d4]144 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
145
[159e7df]146 foreach ($files as $file) {
147
[2740efc]148 // We're skipping all subfolders
149 if (!$file->isDir()) {
150 $filePath = $file->getRealPath();
151 // extracting filename with substr/strlen
152 $relativePath = substr($filePath, strlen($path) + 1);
153 $zip->addFile($filePath, $relativePath);
154 }
[bbcbb57]155 }
[2740efc]156 $zip->close();
157 $headers = array('Content-Type' => 'application/octet-stream');
158 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
[13ff930]159 return response()->download($zip_file, $zip_new_name, $headers);
160 }
161
162 else {
[796e7ac]163 Alert::flash("All departments are empty", "warning");
164 return redirect()->back();
[13ff930]165 }
[796e7ac]166
[c6b84df]167 }
168
169 public function downloadDepartment($id)
170 {
[df6e9ec]171 $department = Department::find($id);
[13ff930]172 $folders = Folder::where('department_id', $id)->get();
173 $flag = false;
174
[796e7ac]175 foreach($folders as $folder){
176 if($folder->no_of_files > 0)
177 $flag=true;
178 break;
179 }
[13ff930]180
181 if($flag) {
[df6e9ec]182 $zip_file=Storage::disk('uploads')->path('Department.zip');
183 $zip = new \ZipArchive();
184 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
185 $path = Storage::disk('uploads')->path($department->location);
186
[159e7df]187 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
188 foreach ($files as $file) {
[94f05dc]189
190 // We're skipping all subfolders
[159e7df]191 if (!$file->isDir()) {
192 $filePath = $file->getRealPath();
193 // extracting filename with substr/strlen
194 $relativePath = substr($filePath, strlen($path) + 1);
195 $zip->addFile($filePath, $relativePath);
196 }
[94f05dc]197 }
[159e7df]198 $zip->close();
[94f05dc]199 $headers = array('Content-Type' => 'application/octet-stream');
200 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
[159e7df]201 return response()->download($zip_file, $zip_new_name, $headers);
[13ff930]202 }
203 else {
204 Alert::flash("This department has no files", "warning");
205 return redirect()->back();
206 }
[c6b84df]207 }
[194a359]208}
Note: See TracBrowser for help on using the repository browser.