| 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\User;
|
|---|
| 11 | use App\Notifications\NewDepartmentCreated;
|
|---|
| 12 | use Carbon\Carbon;
|
|---|
| 13 | use App\Http\Controllers\Controller;
|
|---|
| 14 | use Illuminate\Support\Facades\Notification;
|
|---|
| 15 | use Illuminate\Support\Facades\Storage;
|
|---|
| 16 | use Illuminate\Filesystem\Filesystem;
|
|---|
| 17 |
|
|---|
| 18 | class 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 |
|
|---|
| 50 | $department->save();
|
|---|
| 51 |
|
|---|
| 52 | Alert::flash("New Department added successfully");
|
|---|
| 53 |
|
|---|
| 54 | return redirect()->route("dashboard.departments.index");
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | public function editShow($id)
|
|---|
| 58 | {
|
|---|
| 59 | return view("dashboard.departments.edit")->with([
|
|---|
| 60 | "department" => Department::findOrFail($id)
|
|---|
| 61 | ]);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public function edit(UpdateDepartmentRequest $request, $id)
|
|---|
| 65 | {
|
|---|
| 66 | $department = Department::findOrFail($id);
|
|---|
| 67 |
|
|---|
| 68 | $folders = $department->folder;
|
|---|
| 69 | $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
|
|---|
| 70 |
|
|---|
| 71 | $department->name = $request->name;
|
|---|
| 72 | $department->code = $request->code;
|
|---|
| 73 | $department->updated_at = Carbon::now();
|
|---|
| 74 |
|
|---|
| 75 | if($department->isDirty('code'))
|
|---|
| 76 | {
|
|---|
| 77 | $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
|
|---|
| 78 | if(!Storage::disk('uploads')->has($location)){
|
|---|
| 79 | Storage::disk('uploads')->move($oldLocation, $location);
|
|---|
| 80 | $department->location = $location;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | foreach ($folders as $folder) {
|
|---|
| 84 | $currArchId = explode('/', $folder->arch_id)[1];
|
|---|
| 85 | $folder->arch_id = $department->code . '/' . $currArchId;
|
|---|
| 86 | $folder->save();
|
|---|
| 87 | foreach($folder->files as $file) {
|
|---|
| 88 | $file->location = $location . DIRECTORY_SEPARATOR . $folder->name . DIRECTORY_SEPARATOR . $file->name;
|
|---|
| 89 | $file->save();
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | $department->save();
|
|---|
| 95 |
|
|---|
| 96 | Alert::flash("Department edited successfully");
|
|---|
| 97 |
|
|---|
| 98 | return redirect()->route("dashboard.departments.index");
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public function destroy($id)
|
|---|
| 102 | {
|
|---|
| 103 | $department = Department::find($id);
|
|---|
| 104 | //$department->delete();
|
|---|
| 105 | $folders = $department->folder()->count();
|
|---|
| 106 |
|
|---|
| 107 | if($folders > 0){
|
|---|
| 108 | Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
|
|---|
| 109 | }
|
|---|
| 110 | else {
|
|---|
| 111 | $department->delete();
|
|---|
| 112 |
|
|---|
| 113 | Alert::flash($department->name . " deleted successfully");
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return redirect()->route("dashboard.departments.index");
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public function downloadAll()
|
|---|
| 120 | {
|
|---|
| 121 | try {
|
|---|
| 122 | $zip_file = Storage::disk('uploads')->path('Departments.zip');
|
|---|
| 123 | $zip = new \ZipArchive();
|
|---|
| 124 | $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|---|
| 125 |
|
|---|
| 126 | $path = Storage::disk('uploads')->path('Departments');
|
|---|
| 127 |
|
|---|
| 128 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
|---|
| 129 |
|
|---|
| 130 | foreach ($files as $file) {
|
|---|
| 131 |
|
|---|
| 132 | // We're skipping all subfolders
|
|---|
| 133 | if (!$file->isDir()) {
|
|---|
| 134 | $filePath = $file->getRealPath();
|
|---|
| 135 | // extracting filename with substr/strlen
|
|---|
| 136 | $relativePath = substr($filePath, strlen($path) + 1);
|
|---|
| 137 | $zip->addFile($filePath, $relativePath);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | $zip->close();
|
|---|
| 141 | $headers = array('Content-Type' => 'application/octet-stream');
|
|---|
| 142 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
|
|---|
| 143 | return response()->download($zip_file, $zip_new_name, $headers);
|
|---|
| 144 | }
|
|---|
| 145 | catch(\Exception $e){
|
|---|
| 146 | abort(403, "No files found");
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | public function downloadDepartment($id)
|
|---|
| 151 | {
|
|---|
| 152 | try {
|
|---|
| 153 | $department = Department::find($id);
|
|---|
| 154 | $zip_file=Storage::disk('uploads')->path('Department.zip');
|
|---|
| 155 | $zip = new \ZipArchive();
|
|---|
| 156 | $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|---|
| 157 | $path = Storage::disk('uploads')->path($department->location);
|
|---|
| 158 |
|
|---|
| 159 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
|---|
| 160 | foreach ($files as $file) {
|
|---|
| 161 |
|
|---|
| 162 | // We're skipping all subfolders
|
|---|
| 163 | if (!$file->isDir()) {
|
|---|
| 164 | $filePath = $file->getRealPath();
|
|---|
| 165 | // extracting filename with substr/strlen
|
|---|
| 166 | $relativePath = substr($filePath, strlen($path) + 1);
|
|---|
| 167 | $zip->addFile($filePath, $relativePath);
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | $zip->close();
|
|---|
| 171 | $headers = array('Content-Type' => 'application/octet-stream');
|
|---|
| 172 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
|
|---|
| 173 | return response()->download($zip_file, $zip_new_name, $headers);
|
|---|
| 174 | }
|
|---|
| 175 | catch(\Exception $e){
|
|---|
| 176 | abort(403, "No files found");
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|