source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 2eee9a0

develop
Last change on this file since 2eee9a0 was 4b7e2d3, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes, edited export, added fileSeeder for DB testing

  • Property mode set to 100644
File size: 6.2 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\User;
11use App\Notifications\NewDepartmentCreated;
12use Carbon\Carbon;
13use App\Http\Controllers\Controller;
14use Illuminate\Support\Facades\Notification;
15use Illuminate\Support\Facades\Storage;
16use Illuminate\Filesystem\Filesystem;
17
18class DepartmentsController extends Controller
19{
20 public function index()
21 {
22 return view("dashboard.departments.index")->with([
23 "departments" => Department::all(),
24 ]);
25 }
26
27 public function create()
28 {
29 return view("dashboard.departments.create");
30 }
31
32 public function store(NewDepartmentRequest $request)
33 {
34 $department = new Department();
35
36 $department->name = $request->name;
37 $department->code = $request->code;
38
39 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
40
41 if(!Storage::disk('uploads')->has($location)){
42 Storage::disk('uploads')->makeDirectory($location);
43
44 }
45 $department->location = $location;
46 $department->user_id = auth()->id();
47
48 $users = User::all();
49 Notification::send($users, new NewDepartmentCreated("New department created"));
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 $zip_file=Storage::disk('uploads')->path('Departments.zip');
123 $zip = new \ZipArchive();
124 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
125 $path = Storage::disk('uploads')->path('Departments');
126 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
127 $flag=false;
128
129 foreach ($files as $file)
130 {
131 if(File::all()->count() > 0) {
132
133 // We're skipping all subfolders
134 if (!$file->isDir()) {
135 $filePath = $file->getRealPath();
136 // extracting filename with substr/strlen
137 $relativePath = substr($filePath, strlen($path) + 1);
138 $zip->addFile($filePath, $relativePath);
139 }
140 }
141 else
142 {
143 $flag=true;
144 break;
145 }
146 }
147 if(!$flag) {
148 $zip->close();
149 $headers = array('Content-Type' => 'application/octet-stream');
150 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
151 return response()->download($zip_file, $zip_new_name, $headers);
152 }
153 else {
154 Alert::flash("All departments are empty", "warning");
155 return redirect()->back();
156 }
157 }
158
159 public function downloadDepartment($id)
160 {
161 $department = Department::find($id);
162
163 $FileSystem = new Filesystem();
164 $zip_file = Storage::disk('uploads')->path('Department.zip');
165 $zip = new \ZipArchive();
166 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
167 $path = Storage::disk('uploads')->path($department->location) . DIRECTORY_SEPARATOR;
168 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
169
170 $filesInDept = $FileSystem->allFiles($path);
171
172 if(!empty($filesInDept)) {
173 foreach ($files as $file) {
174 if (!$file->isDir()) {
175 $filePath = $file->getRealPath();
176 // extracting filename with substr/strlen
177 $relativePath = substr($filePath, strlen($path) + 1);
178 $zip->addFile($filePath, $relativePath);
179 }
180 }
181 $zip->close();
182 $headers = array('Content-Type' => 'application/octet-stream',);
183 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . $department->name . '.zip';
184 return response()->download($zip_file, $zip_new_name, $headers);
185 }
186 else{
187 Alert::flash("This department has no files", "warning");
188 return redirect()->back();
189 }
190
191 }
192}
Note: See TracBrowser for help on using the repository browser.