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