source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 0a1fb54

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

bug fixes

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