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