source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 9c3c224

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

bug fixes

  • Property mode set to 100644
File size: 6.7 KB
Line 
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;
9use App\Models\File;
10use App\Models\Folder;
11use App\Models\User;
12use App\Notifications\NewDepartmentCreated;
13use Carbon\Carbon;
14use App\Http\Controllers\Controller;
15use Illuminate\Support\Facades\Notification;
16use Illuminate\Support\Facades\Storage;
17use Illuminate\Filesystem\Filesystem;
18
19class DepartmentsController extends Controller
20{
21 public function index()
22 {
23 return view("dashboard.departments.index")->with([
24 "departments" => Department::all(),
25 ]);
26 }
27
28 public function create()
29 {
30 return view("dashboard.departments.create");
31 }
32
33 public function store(NewDepartmentRequest $request)
34 {
35 $department = new Department();
36
37 $department->name = $request->name;
38 $department->code = $request->code;
39
40 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
41
42 if(!Storage::disk('uploads')->has($location)){
43 Storage::disk('uploads')->makeDirectory($location);
44
45 }
46 $department->location = $location;
47 $department->user_id = auth()->id();
48
49 $users = User::all();
50
51 $department->save();
52
53 Alert::flash("New Department added successfully");
54
55 return redirect()->route("dashboard.departments.index");
56 }
57
58 public function editShow($id)
59 {
60 return view("dashboard.departments.edit")->with([
61 "department" => Department::findOrFail($id)
62 ]);
63 }
64
65 public function edit(UpdateDepartmentRequest $request, $id)
66 {
67 $department = Department::findOrFail($id);
68
69 $folders = $department->folder;
70 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
71
72 $department->name = $request->name;
73 $department->code = $request->code;
74 $department->updated_at = Carbon::now();
75
76 if($department->isDirty('code'))
77 {
78 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
79 if(!Storage::disk('uploads')->has($location)){
80 Storage::disk('uploads')->move($oldLocation, $location);
81 $department->location = $location;
82 }
83
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;
90 $file->save();
91 }
92 }
93 }
94
95 $department->save();
96
97 Alert::flash("Department edited successfully");
98
99 return redirect()->route("dashboard.departments.index");
100 }
101
102 public function destroy($id)
103 {
104 $department = Department::find($id);
105 //$department->delete();
106 $folders = $department->folder()->count();
107
108 if($folders > 0){
109 Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
110 }
111 else {
112 $department->delete();
113
114 Alert::flash($department->name . " deleted successfully");
115 }
116
117 return redirect()->route("dashboard.departments.index");
118 }
119
120 public function downloadAll()
121 {
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 }
129 foreach ($folders as $folder){
130 if($folder->no_of_files > 0){
131 $flag=true;
132 break;
133 }
134 }
135 }
136
137 if($flag) {
138 $zip_file = Storage::disk('uploads')->path('Departments.zip');
139 $zip = new \ZipArchive();
140 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
141
142 $path = Storage::disk('uploads')->path('Departments');
143
144 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
145
146 foreach ($files as $file) {
147
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 }
155 }
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';
159 return response()->download($zip_file, $zip_new_name, $headers);
160 }
161
162 else {
163 Alert::flash("All departments are empty", "warning");
164 return redirect()->back();
165 }
166
167 }
168
169 public function downloadDepartment($id)
170 {
171 $department = Department::find($id);
172 $folders = Folder::where('department_id', $id)->get();
173 $flag = false;
174
175 foreach($folders as $folder){
176 if($folder->no_of_files > 0)
177 $flag=true;
178 break;
179 }
180
181 if($flag) {
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
187 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
188 foreach ($files as $file) {
189
190 // We're skipping all subfolders
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 }
197 }
198 $zip->close();
199 $headers = array('Content-Type' => 'application/octet-stream');
200 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
201 return response()->download($zip_file, $zip_new_name, $headers);
202 }
203 else {
204 Alert::flash("This department has no files", "warning");
205 return redirect()->back();
206 }
207 }
208}
Note: See TracBrowser for help on using the repository browser.