[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 | Notification::send($users, new NewDepartmentCreated("New department created"));
|
---|
| 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;
|
---|
[c6b84df] | 79 | if(!Storage::disk('uploads')->has($location)){
|
---|
| 80 | Storage::disk('uploads')->move($oldLocation, $location);
|
---|
| 81 | $department->location = $location;
|
---|
[24a616f] | 82 | }
|
---|
[6b95845] | 83 |
|
---|
[c6b84df] | 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;
|
---|
[6b95845] | 90 | $file->save();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[24a616f] | 93 | }
|
---|
| 94 |
|
---|
[194a359] | 95 | $department->save();
|
---|
| 96 |
|
---|
| 97 | Alert::flash("Department edited successfully");
|
---|
| 98 |
|
---|
| 99 | return redirect()->route("dashboard.departments.index");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[d795fa6] | 102 | public function destroy($id)
|
---|
[194a359] | 103 | {
|
---|
| 104 | $department = Department::find($id);
|
---|
[d795fa6] | 105 | //$department->delete();
|
---|
[c6b84df] | 106 | $folders = $department->folder()->count();
|
---|
[194a359] | 107 |
|
---|
[c6b84df] | 108 | if($folders > 0){
|
---|
| 109 | Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
|
---|
[d795fa6] | 110 | }
|
---|
| 111 | else {
|
---|
| 112 | $department->delete();
|
---|
[194a359] | 113 |
|
---|
[d795fa6] | 114 | Alert::flash($department->name . " deleted successfully");
|
---|
| 115 | }
|
---|
[194a359] | 116 |
|
---|
| 117 | return redirect()->route("dashboard.departments.index");
|
---|
| 118 | }
|
---|
[c6b84df] | 119 |
|
---|
| 120 | public function downloadAll()
|
---|
| 121 | {
|
---|
| 122 | $zip_file=Storage::disk('uploads')->path('Departments.zip');
|
---|
| 123 | $zip = new \ZipArchive();
|
---|
| 124 | $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
---|
| 125 | $path = Storage::disk('uploads')->path('Departments');
|
---|
| 126 |
|
---|
[159e7df] | 127 | try {
|
---|
| 128 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
| 129 | $flag = false;
|
---|
[c6b84df] | 130 |
|
---|
[159e7df] | 131 | foreach ($files as $file) {
|
---|
| 132 |
|
---|
| 133 | // We're skipping all subfolders
|
---|
| 134 | if (!$file->isDir()) {
|
---|
| 135 | $filePath = $file->getRealPath();
|
---|
| 136 | // extracting filename with substr/strlen
|
---|
| 137 | $relativePath = substr($filePath, strlen($path) + 1);
|
---|
| 138 | $zip->addFile($filePath, $relativePath);
|
---|
| 139 | }
|
---|
[c6b84df] | 140 | }
|
---|
[159e7df] | 141 | $zip->close();
|
---|
| 142 | $headers = array('Content-Type' => 'application/octet-stream');
|
---|
| 143 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
|
---|
| 144 | return response()->download($zip_file, $zip_new_name, $headers);
|
---|
[c6b84df] | 145 | }
|
---|
[159e7df] | 146 | catch(\Exception $e){
|
---|
| 147 | Alert::flash("All departments are empty", "warning");
|
---|
[4b7e2d3] | 148 | return redirect()->back();
|
---|
[c6b84df] | 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | public function downloadDepartment($id)
|
---|
| 153 | {
|
---|
| 154 | $department = Department::find($id);
|
---|
| 155 |
|
---|
| 156 | $FileSystem = new Filesystem();
|
---|
| 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) . DIRECTORY_SEPARATOR;
|
---|
| 161 |
|
---|
[159e7df] | 162 | try{
|
---|
| 163 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
| 164 | foreach ($files as $file) {
|
---|
| 165 | if (!$file->isDir()) {
|
---|
| 166 | $filePath = $file->getRealPath();
|
---|
| 167 | // extracting filename with substr/strlen
|
---|
| 168 | $relativePath = substr($filePath, strlen($path) + 1);
|
---|
| 169 | $zip->addFile($filePath, $relativePath);
|
---|
| 170 | }
|
---|
| 171 | $zip->close();
|
---|
| 172 | $headers = array('Content-Type' => 'application/octet-stream',);
|
---|
[c6b84df] | 173 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . $department->name . '.zip';
|
---|
[159e7df] | 174 | return response()->download($zip_file, $zip_new_name, $headers);
|
---|
[c6b84df] | 175 | }
|
---|
[159e7df] | 176 | }
|
---|
| 177 | catch(\Exception $e) {
|
---|
[c6b84df] | 178 | Alert::flash("This department has no files", "warning");
|
---|
[4b7e2d3] | 179 | return redirect()->back();
|
---|
[159e7df] | 180 | }
|
---|
[c6b84df] | 181 | }
|
---|
[194a359] | 182 | }
|
---|