source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 1f7c934

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

bug fixes

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