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

develop
Last change on this file since e954f3e was 55c9542, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 6.6 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 }
[55c9542]129 if($folders->where('no_of_files' > 0)->count()) {
130 $flag = true;
[13ff930]131 break;
132 }
133 }
134
135 if($flag) {
[2740efc]136 $zip_file = Storage::disk('uploads')->path('Departments.zip');
137 $zip = new \ZipArchive();
138 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
[2279f71]139
[2740efc]140 $path = Storage::disk('uploads')->path('Departments');
[df6e9ec]141
[68694d4]142 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
143
[159e7df]144 foreach ($files as $file) {
145
[2740efc]146 // We're skipping all subfolders
147 if (!$file->isDir()) {
148 $filePath = $file->getRealPath();
149 // extracting filename with substr/strlen
150 $relativePath = substr($filePath, strlen($path) + 1);
151 $zip->addFile($filePath, $relativePath);
152 }
[bbcbb57]153 }
[2740efc]154 $zip->close();
155 $headers = array('Content-Type' => 'application/octet-stream');
156 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
[13ff930]157 return response()->download($zip_file, $zip_new_name, $headers);
158 }
159
160 else {
161 Alert::flash("All departments are empty", "warning");
162 return redirect()->back();
163 }
[c6b84df]164 }
165
166 public function downloadDepartment($id)
167 {
[df6e9ec]168 $department = Department::find($id);
[13ff930]169 $folders = Folder::where('department_id', $id)->get();
170 $flag = false;
171
[55c9542]172 if($folders->where('no_of_files' > 0)->count()){
173 $flag = true;
174 }
[13ff930]175
176 if($flag) {
[df6e9ec]177 $zip_file=Storage::disk('uploads')->path('Department.zip');
178 $zip = new \ZipArchive();
179 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
180 $path = Storage::disk('uploads')->path($department->location);
181
[159e7df]182 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
183 foreach ($files as $file) {
[94f05dc]184
185 // We're skipping all subfolders
[159e7df]186 if (!$file->isDir()) {
187 $filePath = $file->getRealPath();
188 // extracting filename with substr/strlen
189 $relativePath = substr($filePath, strlen($path) + 1);
190 $zip->addFile($filePath, $relativePath);
191 }
[94f05dc]192 }
[159e7df]193 $zip->close();
[94f05dc]194 $headers = array('Content-Type' => 'application/octet-stream');
195 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
[159e7df]196 return response()->download($zip_file, $zip_new_name, $headers);
[13ff930]197 }
198 else {
199 Alert::flash("This department has no files", "warning");
200 return redirect()->back();
201 }
[c6b84df]202 }
[194a359]203}
Note: See TracBrowser for help on using the repository browser.