source: app/Http/Controllers/Dashboard/DepartmentsController.php@ dbc5976

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

bug fixes

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