Changeset ea7b12a
- Timestamp:
- 10/19/21 17:46:21 (3 years ago)
- Branches:
- develop, master
- Children:
- 6b95845
- Parents:
- b9c4a92
- Files:
-
- 1 added
- 3 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/Dashboard/DocumentsController.php
rb9c4a92 rea7b12a 5 5 use App\Helpers\Alert; 6 6 use App\Http\Requests\Dashboard\DocumentRequest; 7 use App\Http\Requests\Dashboard\FileRequest; 8 use App\Http\Requests\Dashboard\PasswordSettingsRequest; 7 9 use App\Models\Department; 8 10 use App\Models\Document; 9 11 use App\Models\File; 10 12 use App\Services\UploadService; 13 use Carbon\Carbon; 11 14 use Illuminate\Http\Request; 15 use Illuminate\Http\Response; 12 16 use Illuminate\Support\Facades\Storage; 13 17 use App\Http\Controllers\Controller; … … 115 119 $document->description = $request->description; 116 120 117 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;121 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 118 122 119 123 if (!Storage::disk('local')->has($location)) { … … 121 125 } 122 126 123 foreach ($request->file_item as $file) {124 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());125 $newFile = new File();126 $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();127 $newFile->document()->associate($document);128 $newFile->save();129 }130 131 127 $document->save(); 128 129 if ($request->has('file_item')) { 130 foreach ($request->file_item as $file) { 131 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName()); 132 $newFile = new File(); 133 $newFile->name = $file->getClientOriginalName(); 134 $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName(); 135 $newFile->document()->associate($document); 136 $newFile->save(); 137 } 138 } 132 139 133 140 Alert::flash("New document created successfully"); … … 140 147 return view("dashboard.documents.edit")->with([ 141 148 "document" => Document::findOrFail($id), 142 "departments" => Department::all() 149 "departments" => Department::all(), 150 "files" => File::where('document_id', $id)->get() 143 151 ]); 144 152 } … … 147 155 { 148 156 $document = Document::findOrFail($id); 157 $files = File::where('document_id', $id)->get(); 149 158 150 159 $department = Department::find($request->department); 151 160 152 161 $document->department()->associate($department); 162 163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 153 164 154 165 $document->name = $request->name; … … 156 167 $document->description = $request->description; 157 168 158 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name; 159 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name; 169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 160 170 161 171 if ($document->isDirty('name')) { 162 172 if (!Storage::disk('local')->has($location)) { 163 173 Storage::disk('local')->move($oldLocation, $location); 164 } 165 } 166 167 $hasFileError = false; 174 foreach($files as $file){ 175 $file->location = "test"; 176 } 177 } 178 } 179 168 180 if ($request->has('file_item')) { 169 181 foreach ($request->file_item as $file) { 170 182 $fileName = $file->getClientOriginalName(); 171 if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 172 $hasFileError = true; 173 break; 183 if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 184 // $hasFileError = true; 185 // break; 186 $NewFileName = time() . $fileName; 187 $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName); 188 $newFile = new File(); 189 $newFile->name = rand() . $fileName; 190 $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName; 191 $newFile->document()->associate($document); 192 $newFile->save(); 193 } else { 194 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 195 $newFile = new File(); 196 $newFile->name = $fileName; 197 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName; 198 $newFile->document()->associate($document); 199 $newFile->save(); 174 200 } 175 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 176 $newFile = new File(); 177 $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName; 178 $newFile->document()->associate($document); 179 $newFile->save(); 180 } 181 } 182 183 if($hasFileError) { 184 Alert::flash('Document with the same name exists', 'error'); 185 return redirect()->route("dashboard.documents.edit", ['id' => $document->id]); 201 } 186 202 } 187 203 188 204 $document->save(); 189 205 206 // if($hasFileError) { 207 // Alert::flash('Document with the same name exists', 'error'); 208 // return redirect()->route("dashboard.documents.edit", ['id' => $document->id]); 209 // } 210 211 212 190 213 Alert::flash("Document edited successfully"); 191 214 192 return redirect()-> route("dashboard.documents.index");215 return redirect()->back(); 193 216 } 194 217 … … 230 253 { 231 254 $document = Document::find($id); 255 $files = File::where('document_id', $id)->get(); 232 256 if (auth()->user()->hasPermission("delete_all_documents")) { 257 // @dd($files); 258 foreach ($files as $file) { 259 $file->delete(); 260 } 233 261 $document->delete(); 262 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 263 Storage::disk('local')->deleteDirectory($location); 234 264 Alert::flash($document->name . " deleted successfully"); 235 265 } 236 266 return redirect()->route("dashboard.documents.index"); 237 267 } 268 269 public function deleteFile($id) 270 { 271 $file = File::find($id); 272 $file->delete(); 273 Storage::disk('local')->delete($file->location); 274 275 Alert::flash($file->name . " deleted successfully"); 276 277 return redirect()->back(); 278 } 279 280 public function downloadFile($id) 281 { 282 $file = File::find($id); 283 return Storage::download($file->location); 284 } 285 286 public function renameFile(FileRequest $request, $id) 287 { 288 $file = File::find($id); 289 $fileExtension = explode('.', $file->name)[1]; 290 291 $file->name = $request->name . '.' . $fileExtension; 292 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name; 293 294 if(Storage::disk('local')->has($newLocation)) { 295 Alert::flash("A file with the same name already exists", "error"); 296 return redirect()->back(); 297 } 298 else { 299 Storage::disk('local')->copy($file->location, $newLocation); 300 Storage::disk('local')->delete($file->location); 301 302 $file->location = $newLocation; 303 $file->save(); 304 305 Alert::flash($file->name . " updated successfully"); 306 return redirect()->back(); 307 } 308 } 238 309 } -
app/Http/Requests/Dashboard/DocumentRequest.php
rb9c4a92 rea7b12a 5 5 use App\Models\Department; 6 6 use App\Models\Document; 7 use App\Models\FileType;8 7 use Illuminate\Foundation\Http\FormRequest; 9 8 … … 32 31 public function rules() 33 32 { 34 35 33 $rules = [ 36 34 "arch_id" => [ … … 57 55 if ($this->isMethod("patch")) { 58 56 $fileRules = [ 59 "file_item.*" => "mimes:jpg,jpeg,png |max:4096"57 "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096" 60 58 ]; 61 59 } else { 62 60 $fileRules = [ 63 "file_item.*" => "mimes:jpg,jpeg,png |max:4096"61 "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096" 64 62 ]; 65 63 } -
app/Models/Document.php
rb9c4a92 rea7b12a 22 22 } 23 23 24 public function ago() {25 return Carbon::parse($this->created_at)->diffForHumans();26 }27 28 24 public function files() 29 25 { -
app/Models/File.php
rb9c4a92 rea7b12a 4 4 5 5 use Illuminate\Database\Eloquent\Model; 6 use Illuminate\Support\Facades\Storage; 6 7 7 8 class File extends Model 8 9 { 9 10 protected $table = "files"; 10 protected $fillable = [" link", "document_id"];11 protected $fillable = ["name", "location", "document_id"]; 11 12 12 13 public function document() … … 14 15 return $this->belongsTo(Document::class); 15 16 } 17 18 public function getSize($location) 19 { 20 $fileSize = Storage::disk('local')->size($location) / 1024 / 1024; 21 $fileSize = round($fileSize, 2); 22 return $fileSize; 23 } 16 24 } -
database/factories/DepartmentFactory.php
rb9c4a92 rea7b12a 24 24 public function definition() 25 25 { 26 $location = $this->faker-> randomNumber('1', '1');26 $location = $this->faker->unique()->numberBetween('1', '9'); 27 27 Storage::disk('local')->makeDirectory('Departments/' . $location); 28 28 return [ -
database/factories/DocumentFactory.php
rb9c4a92 rea7b12a 26 26 { 27 27 28 $deptID = " 1";28 $deptID = "5"; 29 29 $name = $this->faker->unique()->firstName(); 30 30 Storage::disk('local')->makeDirectory('Departments' . DIRECTORY_SEPARATOR . $deptID . DIRECTORY_SEPARATOR . $name); -
database/migrations/2021_10_10_103309_create_files_table.php
rb9c4a92 rea7b12a 16 16 Schema::create('files', function (Blueprint $table) { 17 17 $table->bigIncrements('id'); 18 $table->bigInteger('document_id')->unsigned()->nullable(); 19 $table->string("link"); 18 $table->bigInteger('document_id')->unsigned(); 19 $table->string("name"); 20 $table->string("location"); 20 21 $table->foreign("document_id")->references("id")->on("documents"); 21 22 $table->timestamps(); -
database/seeders/DatabaseSeeder.php
rb9c4a92 rea7b12a 19 19 $this->call(UsersTableSeeder::class); 20 20 $this->call(DepartmentsTableSeeder::class); 21 $this->call(DocumentsTableSeeder::class);21 //$this->call(DocumentsTableSeeder::class); 22 22 } 23 23 } -
database/seeders/DepartmentsTableSeeder.php
rb9c4a92 rea7b12a 16 16 public function run() 17 17 { 18 Department::factory()->count( 10)->create();18 Department::factory()->count(8)->create(); 19 19 } 20 20 } -
database/seeders/DocumentsTableSeeder.php
rb9c4a92 rea7b12a 106 106 // ], 107 107 // ]); 108 Document::factory()->count( 300)->create();108 Document::factory()->count(10)->create(); 109 109 } 110 110 } -
public/assets/js/examples/pages/user-list.js
rb9c4a92 rea7b12a 14 14 { 15 15 "orderable": false, 16 "targets": [0, 8]16 "targets": [0, 6] 17 17 } 18 18 ], -
resources/views/dashboard/departments/index.blade.php
rb9c4a92 rea7b12a 22 22 </nav> 23 23 <div class="dropdown"> 24 <a href=" {{ route("dashboard.departments.create") }}" class="btn btn-primary text-white">24 <a href="javascript:void(0)" data-toggle="modal" data-target="#createModal" class="btn btn-primary text-white"> 25 25 Add department 26 26 </a> … … 37 37 <tr> 38 38 <th> 39 {{-- <div class="custom-control custom-checkbox">--}}40 {{-- <input type="checkbox" class="custom-control-input" id="user-list-select-all">--}}41 {{-- <label class="custom-control-label" for="user-list-select-all"></label>--}}42 {{-- </div>--}}43 39 </th> 44 40 <th>ID</th> … … 75 71 </td> 76 72 <td> 77 <a href=" {{ route("dashboard.departments.edit", ["id" => $department->id]) }}" class="text-secondary" data-toggle="tooltip" title="Edit">73 <a href="javascript:void(0)" class="text-secondary" data-toggle="modal" data-target="#editModal_{{$department->id}}" title="Edit"> 78 74 <i class="ti-pencil"></i> 79 75 </a> … … 109 105 </div> 110 106 </div> 107 108 <div class="modal fade" id="editModal_{{$department->id}}" tabindex="-1" role="dialog" aria-hidden="true"> 109 <div class="modal-dialog modal-dialog-centered" role="document"> 110 <div class="modal-content"> 111 <div class="modal-header"> 112 <h5 class="modal-title" id="exampleModalCenterTitle">Edit department</h5> 113 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 114 <i class="ti-close"></i> 115 </button> 116 </div> 117 <div class="modal-body"> 118 <form action="{{ route("dashboard.departments.edit", ["id" =>$department->id]) }}" method="post" accept-charset="utf-8"> 119 @method("patch") 120 @csrf 121 <div class="row"> 122 <div class="col-md-6"> 123 <div class="form-group"> 124 <label class="form-label">Name</label> 125 <input type="text" name="name" value="{{ $department->name }}" minlength="2" maxlength="30" class="form-control" placeholder="Name" required> 126 </div> 127 </div> 128 <div class="col-md-6"> 129 <div class="form-group"> 130 <label class="form-label">Code</label> 131 <input type="text" name="code" value="{{ $department->code }}" minlength="2" maxlength="30" class="form-control" placeholder="Code" required> 132 </div> 133 </div> 134 </div> 135 <input type="submit" value="Save changes" class="btn btn-primary pull-right m-10"> 136 </form> 137 </div> 138 139 </div> 140 </div> 141 </div> 111 142 @endforeach 143 144 <div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-hidden="true"> 145 <div class="modal-dialog modal-dialog-centered" role="document"> 146 <div class="modal-content"> 147 <div class="modal-header"> 148 <h5 class="modal-title" id="exampleModalCenterTitle">Create department</h5> 149 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 150 <i class="ti-close"></i> 151 </button> 152 </div> 153 <div class="modal-body"> 154 <form action="{{ route("dashboard.departments.store") }}" method="post" accept-charset="utf-8"> 155 @csrf 156 <div class="row"> 157 <div class="col-md-6"> 158 <div class="form-group"> 159 <label>Name</label> 160 <input type="text" name="name" value="{{ old('name') }}" minlength="2" maxlength="30" class="form-control" placeholder="Name" required> 161 </div> 162 </div> 163 <div class="col-md-6"> 164 <div class="form-group"> 165 <label>Code</label> 166 <input type="text" name="code" value="{{ old('code') }}" minlength="2" maxlength="30" class="form-control" placeholder="Code" required> 167 </div> 168 </div> 169 </div> 170 <input type="submit" value="Save changes" class="submitBtn btn btn-primary pull-right m-10"> 171 </form> 172 </div> 173 174 </div> 175 </div> 176 </div> 177 112 178 </tbody> 113 179 </table> … … 124 190 <!-- Datatable --> 125 191 <script src="{{ url('vendors/dataTable/dataTables.min.js') }}"></script> 126 127 <script>128 $('#deleteModal').on('show.bs.modal', function (event) {129 var button = $(event.relatedTarget)130 var dep_id = button.data('deptId')131 var modal = $(this)132 133 modal.find('.modal-body #dept_id').val(dep_id);134 });135 </script>136 192 @endsection -
resources/views/dashboard/documents/edit.blade.php
rb9c4a92 rea7b12a 4 4 5 5 @section('pageTitle', 'Edit document') 6 7 @section('head') 8 <!-- Datatable --> 9 <link rel="stylesheet" href="{{ url('vendors/dataTable/dataTables.min.css') }}" type="text/css"> 10 @endsection 6 11 7 12 @section('content') … … 79 84 </div> 80 85 </div> 86 87 </div> 88 89 <div class="col-md-12"> 90 <div class="card"> 91 <div class="card-body"> 92 <div class="table-responsive"> 93 <table id="user-list" class="table table-lg"> 94 <thead> 95 <tr> 96 <th></th> 97 <th>ID</th> 98 <th>Size</th> 99 <th>Name</th> 100 <th>Created at</th> 101 <th>Location</th> 102 <th>Actions</th> 103 </tr> 104 </thead> 105 <tbody> 106 @foreach($files as $file) 107 <tr> 108 <td></td> 109 <td>{{ $file->id }}</td> 110 <td>{{ $file->getSize($file->location) }} MB</td> 111 <td>{{ $file->name }}</td> 112 <td>{{ date('d.m.Y - H:i', strtotime($file->created_at)) }}</td> 113 <!-- Trigger --> 114 <td> <span id="copy_{{$department->id}}"></span> 115 <button class="btn btn-sm btn-primary text-white" data-clipboard-target="#copy_{{ $department->id }}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16"> 116 <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/> 117 <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/> 118 </svg></button> 119 </td> 120 <td> 121 <a href="javascript:void(0)" class="text-secondary" data-toggle="modal" data-target="#editModal_{{$file->id}}" title="Edit"> 122 <i class="ti-pencil"></i> 123 </a> 124 <a href="{{ route("dashboard.documents.downloadFile", $file->id) }}" class="text-secondary" title="Edit"> 125 <i class="ti-download"></i> 126 </a> 127 <a href="javascript:void(0)" class="text-danger ml-2" data-toggle="modal" data-target="#deleteModal_{{$file->id}}" title="Delete"> 128 <i class="ti-trash"></i> 129 </a> 130 </td> 131 </tr> 132 <div class="modal fade" id="deleteModal_{{$file->id}}" tabindex="-1" role="dialog" aria-hidden="true"> 133 <div class="modal-dialog modal-dialog-centered" role="document"> 134 <div class="modal-content"> 135 <div class="modal-header"> 136 <h5 class="modal-title" id="exampleModalCenterTitle">Delete confirmation</h5> 137 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 138 <i class="ti-close"></i> 139 </button> 140 </div> 141 <div class="modal-body"> 142 <form action="{{ route("dashboard.documents.deleteFile", $file->id) }}" method="POST"> 143 @csrf 144 @method('DELETE') 145 <p>Are you sure you want to delete file {{$file->name}}?</p> 146 <div class="modal-footer"> 147 <button type="button" class="btn btn-secondary" data-dismiss="modal">Close 148 </button> 149 <button type="submit" class="btn btn-primary">Save changes</button> 150 </div> 151 </form> 152 </div> 153 154 </div> 155 </div> 156 </div> 157 158 <div class="modal fade" id="editModal_{{$file->id}}" tabindex="-1" role="dialog" aria-hidden="true"> 159 <div class="modal-dialog modal-dialog-centered" role="document"> 160 <div class="modal-content"> 161 <div class="modal-header"> 162 <h5 class="modal-title" id="exampleModalCenterTitle">Edit department</h5> 163 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 164 <i class="ti-close"></i> 165 </button> 166 </div> 167 <div class="modal-body"> 168 <form action="{{ route("dashboard.documents.renameFile", ["id" =>$file->id]) }}" method="post" accept-charset="utf-8"> 169 @method("patch") 170 @csrf 171 <div class="row"> 172 <div class="col-md-6"> 173 <div class="form-group"> 174 <label class="form-label">Name</label> 175 <input type="text" name="name" value="{{ explode('.', $file->name)[0] }}" minlength="2" maxlength="30" class="form-control" placeholder="Name" required> 176 </div> 177 </div> 178 </div> 179 <input type="submit" value="Save changes" class="btn btn-primary pull-right m-10"> 180 </form> 181 </div> 182 183 </div> 184 </div> 185 </div> 186 @endforeach 187 188 <div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-hidden="true"> 189 <div class="modal-dialog modal-dialog-centered" role="document"> 190 <div class="modal-content"> 191 <div class="modal-header"> 192 <h5 class="modal-title" id="exampleModalCenterTitle">Create department</h5> 193 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 194 <i class="ti-close"></i> 195 </button> 196 </div> 197 <div class="modal-body"> 198 <form action="{{ route("dashboard.departments.store") }}" method="post" accept-charset="utf-8"> 199 @csrf 200 <div class="row"> 201 <div class="col-md-6"> 202 <div class="form-group"> 203 <label>Name</label> 204 <input type="text" name="name" value="{{ old('name') }}" minlength="2" maxlength="30" class="form-control" placeholder="Name" required> 205 </div> 206 </div> 207 <div class="col-md-6"> 208 <div class="form-group"> 209 <label>Code</label> 210 <input type="text" name="code" value="{{ old('code') }}" minlength="2" maxlength="30" class="form-control" placeholder="Code" required> 211 </div> 212 </div> 213 </div> 214 <input type="submit" value="Save changes" class="submitBtn btn btn-primary pull-right m-10"> 215 </form> 216 </div> 217 218 </div> 219 </div> 220 </div> 221 222 </tbody> 223 </table> 224 </div> 225 </div> 226 </div> 81 227 </div> 82 228 </div> 83 229 84 230 @endsection 231 232 @section('script') 233 <script src="{{ url('assets/js/examples/pages/user-list.js') }}"></script> 234 <!-- Datatable --> 235 <script src="{{ url('vendors/dataTable/dataTables.min.js') }}"></script> 236 @endsection -
resources/views/dashboard/users/index.blade.php
rb9c4a92 rea7b12a 75 75 <td>{{$user->phone_number}}</td> 76 76 <td>{{ $user->getCreatedByName() }}</td> 77 <td>{{ date('d.m.Y ', strtotime($user->created_at)) }}</td>77 <td>{{ date('d.m.Y - H:i', strtotime($user->created_at)) }}</td> 78 78 @if($user->updated_at==NULL) 79 79 <td>/</td> -
routes/web.php
rb9c4a92 rea7b12a 94 94 Route::delete("/documents/{id}/destroy", "Dashboard\DocumentsController@destroy")->name("dashboard.documents.destroy"); 95 95 Route::patch('/documents/toggle-important/{id}', "Dashboard\DocumentsController@toggleImportant")->name("dashboard.documents.toggleImportant"); 96 Route::delete("documents/{id}/deleteFile", "Dashboard\DocumentsController@deleteFile")->name("dashboard.documents.deleteFile"); 97 Route::get("documents/{id}/downloadFile", "Dashboard\DocumentsController@downloadFile")->name("dashboard.documents.downloadFile"); 98 Route::patch("documents/{id}/renameFile", "Dashboard\DocumentsController@renameFile")->name("dashboard.documents.renameFile"); 96 99 });
Note:
See TracChangeset
for help on using the changeset viewer.