source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 190db9f

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

edited user permissions

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