1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\NewDepartmentRequest;
|
---|
7 | use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
|
---|
8 | use App\Models\Department;
|
---|
9 | use App\Models\File;
|
---|
10 | use App\Models\Folder;
|
---|
11 | use App\Models\User;
|
---|
12 | use App\Notifications\NewDepartmentCreated;
|
---|
13 | use Carbon\Carbon;
|
---|
14 | use App\Http\Controllers\Controller;
|
---|
15 | use Illuminate\Support\Facades\Notification;
|
---|
16 | use Illuminate\Support\Facades\Storage;
|
---|
17 | use Illuminate\Filesystem\Filesystem;
|
---|
18 |
|
---|
19 | class 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 | if($folders->where('no_of_files' > 0)->count()) {
|
---|
130 | $flag = true;
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if($flag) {
|
---|
136 | $zip_file = Storage::disk('uploads')->path('Departments.zip');
|
---|
137 | $zip = new \ZipArchive();
|
---|
138 | $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
---|
139 |
|
---|
140 | $path = Storage::disk('uploads')->path('Departments');
|
---|
141 |
|
---|
142 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
143 |
|
---|
144 | foreach ($files as $file) {
|
---|
145 |
|
---|
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 | }
|
---|
153 | }
|
---|
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';
|
---|
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 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | public function downloadDepartment($id)
|
---|
167 | {
|
---|
168 | $department = Department::find($id);
|
---|
169 | $folders = Folder::where('department_id', $id)->get();
|
---|
170 | $flag = false;
|
---|
171 |
|
---|
172 | if($folders->where('no_of_files' > 0)->count()){
|
---|
173 | $flag = true;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if($flag) {
|
---|
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 |
|
---|
182 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
183 | foreach ($files as $file) {
|
---|
184 |
|
---|
185 | // We're skipping all subfolders
|
---|
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 | }
|
---|
192 | }
|
---|
193 | $zip->close();
|
---|
194 | $headers = array('Content-Type' => 'application/octet-stream');
|
---|
195 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
|
---|
196 | return response()->download($zip_file, $zip_new_name, $headers);
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | Alert::flash("This department has no files", "warning");
|
---|
200 | return redirect()->back();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|