- Timestamp:
- 10/31/21 21:28:46 (3 years ago)
- Branches:
- master
- Children:
- 4521f25
- Parents:
- a55bb54
- Location:
- app/Http
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/Dashboard/FilesController.php
ra55bb54 re78295c 54 54 if ($request->has('file_item')) { 55 55 foreach ($request->file_item as $file) { 56 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension(); 56 $fileName = $file->getClientOriginalName(); 57 58 if(File::where(['folder_id' => $folder->id, 'name' => $fileName])->count() > 0) { 59 Alert::flash("The uploaded file already exists", "error"); 60 61 return redirect()->back(); 62 } 63 57 64 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 58 65 $newFile = new File(); … … 63 70 $newFile->save(); 64 71 } 65 Notification::send($users, new NewFileCreated("New files added"));66 72 67 73 Alert::flash("New files added successfully"); … … 81 87 return Storage::download($file->location); 82 88 } 83 84 public function renameFile(FileNameRequest $request, $id)85 {86 $file = File::find($id);87 $fileExtension = explode('.', $file->name)[1];88 89 $file->name = $request->name . '.' . $fileExtension;90 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;91 92 if(Storage::disk('uploads')->has($newLocation)) {93 Alert::flash("A file with the same name already exists", "error");94 return redirect()->back();95 }96 else {97 Storage::disk('uploads')->move($file->location, $newLocation);98 99 $file->location = $newLocation;100 $file->updated_at = Carbon::now();101 $file->save();102 103 Alert::flash($file->name . " updated successfully");104 return redirect()->back();105 }106 }107 89 } -
app/Http/Controllers/Dashboard/FoldersController.php
ra55bb54 re78295c 18 18 use Illuminate\Support\Facades\Storage; 19 19 use App\Http\Controllers\Controller; 20 use function Sodium\increment; 20 21 21 22 class FoldersController extends Controller … … 58 59 else if ($request->query('sort') == 'no_of_files') { 59 60 $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12); 60 }61 else if ($request->query('sort') == 'important') {62 $folders = Folder::where('is_important', true)->paginate(12);63 61 } 64 62 else if ($request->query('sort') == 'recent') { … … 96 94 "docsCount" => Department::withCount('folder')->get(), 97 95 "totalDocs" => Folder::all()->count(), 98 "countImportant" => Folder::where('is_important', true)->get()->count(),99 96 "diskTotal" => $diskTotal, 100 97 "diskTotalSize" => $diskTotalSize, … … 118 115 { 119 116 $folder = new Folder(); 117 118 $existingFolder = Folder::where(['department_id' => $request->department, 'name' => $request->name, 'arch_id' => $request->arch_id])->count(); 119 120 $existingFolderName = Folder::where(['department_id' => $request->department, 'name' => $request->name])->count(); 121 $existingFolderArchId = Folder::where(['department_id' => $request->department, 'arch_id' => $request->arch_id])->count(); 122 123 if($existingFolder > 0) { 124 $folder->version = $existingFolder + 1; 125 } 126 127 if(($existingFolderName > 0 && $existingFolderArchId <= 0) || ($existingFolderName <= 0 && $existingFolderArchId > 0)) { 128 Alert::flash("Can't create another version since folder name or archive ID is different", "error"); 129 130 return redirect()->route("dashboard.folders.index"); 131 } 132 133 $department = Department::find($request->department); 134 120 135 $user = auth()->user(); 121 $department = Department::find($request->department);122 136 123 137 $folder->user()->associate($user); … … 128 142 $folder->name = $request->name; 129 143 $folder->note = $request->note; 130 131 if($request->has('is_important')){132 $folder->is_important = true;133 }else{134 $folder->is_important = false;135 }136 144 137 145 $location = $folder->department->location . DIRECTORY_SEPARATOR . $request->name; … … 178 186 } 179 187 180 public function edit(FolderRequest $request, $id) 181 { 182 $folder = Folder::findOrFail($id); 188 public function destroy($id) 189 { 190 $folder = Folder::find($id); 191 192 $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id])->count(); 193 194 if($existingFolders > 1 && $folder->version == 1) { 195 Alert::flash($folder->name . " has versions", "error"); 196 return redirect()->back(); 197 } 198 183 199 $files = File::where('folder_id', $id)->get(); 184 200 185 $department = Department::find($request->department); 186 187 $folder->department()->increment('no_of_folders'); 188 189 $oldLocation = $folder->location; 190 191 $folder->name = $request->name; 192 $folder->arch_id = $request->arch_id; 193 $folder->note = $request->note; 194 $folder->updated_at = Carbon::now(); 195 196 $newLocation = Department::find($request->department)->location . DIRECTORY_SEPARATOR . $request->name; 197 198 if($folder->department_id != $request->department){ 199 $folder->department()->decrement('no_of_folders'); 200 if (!Storage::disk('uploads')->has($newLocation)) { 201 Storage::disk('uploads')->move($oldLocation, $newLocation); 202 foreach($files as $file) { 203 $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name; 204 $file->save(); 205 } 206 } 207 } 208 if($folder->isDirty('name')) { 209 if (!Storage::disk('uploads')->has($newLocation)) { 210 Storage::disk('uploads')->move($oldLocation, $newLocation); 211 foreach($files as $file) { 212 $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name; 213 $file->save(); 214 } 215 } 216 } 217 218 $folder->department()->associate($department); 219 220 $folder->location = $newLocation; 221 222 if ($request->has('file_item')) { 223 foreach ($request->file_item as $file) { 224 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension(); 225 $file->storeAs($newLocation . DIRECTORY_SEPARATOR, $fileName); 226 $newFile = new File(); 227 $newFile->name = $fileName; 228 $newFile->location = $newLocation . DIRECTORY_SEPARATOR . $fileName; 229 $newFile->folder()->associate($folder); 230 $newFile->folder()->increment('no_of_files'); 231 $newFile->save(); 232 } 233 } 234 235 $folder->save(); 236 237 Alert::flash("Folder edited successfully"); 238 239 return redirect()->back(); 240 } 241 242 public function toggleImportant($id) 243 { 244 $folder = Folder::find($id); 245 $folder->is_important = !$folder->is_important; 246 $folder->save(); 247 248 if ($folder->is_important == true) 249 Alert::flash("Folder marked as important successfully"); 250 else 251 Alert::flash("Folder marked as not important successfully"); 252 253 return redirect()->back(); 254 } 255 256 public function destroy($id) 257 { 258 $folder = Folder::find($id); 259 $files = File::where('folder_id', $id)->get(); 260 if (auth()->user()->hasPermission("delete_all_folders") && !$folder->is_important) { 201 if($files->count() > 0) { 202 Alert::flash($folder->name . " contains files", "error"); 203 return redirect()->back(); 204 } 205 206 if (auth()->user()->hasPermission("delete_all_folders")) { 261 207 262 208 foreach ($files as $file) { 263 209 $file->delete(); 264 210 } 211 265 212 $folder->delete(); 266 213 $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name; … … 270 217 return redirect()->back(); 271 218 } 272 Alert::flash($folder->name . " is important", "error");219 Alert::flash($folder->name . " cannot be deleted", "error"); 273 220 return redirect()->back(); 274 221 } … … 337 284 "imageExt" => array("png", "jpg", "jpeg"), 338 285 "folders" => $folders, 339 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)) 286 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)), 340 287 ]); 341 288 } -
app/Http/Requests/Dashboard/FolderRequest.php
ra55bb54 re78295c 35 35 "arch_id" => [ 36 36 "required", 37 "unique:folders,arch_id,$this->id,id",38 37 function ($attribute, $value, $fail) { 39 38
Note:
See TracChangeset
for help on using the changeset viewer.