diff --git a/laravel_app/app/Http/Controllers/ProjectController.php b/laravel_app/app/Http/Controllers/ProjectController.php new file mode 100644 index 0000000..703da34 --- /dev/null +++ b/laravel_app/app/Http/Controllers/ProjectController.php @@ -0,0 +1,14 @@ +addBoundingBox(); + } + + public function editProject(Project $project) { + $this->showProjectModal = true; + $this->formData = $project->toArray(); + $this->formData['boundingBoxes'] = $project->boundingBoxes->toArray(); + } + + /** + * Create a new API token. + * + * @return void + */ + public function createProject() + { + $this->resetErrorBag(); + Validator::make([ + 'name' => $this->formData['name'], + 'boundingBoxes' => $this->formData['boundingBoxes'], + ], [ + 'name' => ['required', 'string', 'max:255'], + 'boundingBoxes' => ['required', 'array', 'min:1'], + 'boundingBoxes.*.name' => ['required', 'string', 'max:255'], + 'boundingBoxes.*.top_left_latitude' => ['required', 'string'], + 'boundingBoxes.*.top_left_longitude' => ['required', 'string'], + 'boundingBoxes.*.bottom_right_latitude' => ['required', 'string'], + 'boundingBoxes.*.bottom_right_longitude' => ['required', 'string'], + ])->validateWithBag('createProject'); + + +// Define the unique identifier for the Project (for example, 'id') + $uniqueIdentifier = ['id' =>$this->formData['id'] ?? null]; + +// Using updateOrCreate to either update an existing project or create a new one + $project = Project::updateOrCreate($uniqueIdentifier, $this->formData); + +// Preparing the bounding boxes data for upsert +// Ensure that each bounding box data array includes a 'project_id' field + $boundingBoxesData = array_map(function ($boundingBox) use ($project) { + $boundingBox['project_id'] = $project->id; + unset($boundingBox['created_at']); + unset($boundingBox['updated_at']); + if (!array_key_exists('id', $boundingBox)) { + $boundingBox['id'] = null; + } + return $boundingBox; + }, $this->formData['boundingBoxes'] ?? []); + +// Using upsert to update or create bounding boxes +// You need to specify the unique fields and the fields to be updated + ProjectBoundingBox::upsert($boundingBoxesData, ['id', 'project_id'], ['name', 'top_left_latitude', 'top_left_longitude', 'bottom_right_latitude', 'bottom_right_longitude']); + + $this->resetFormData(); + $this->showProjectModal = false; + $this->dispatch('saved'); + } + + public function addBoundingBox() + { + $this->formData['boundingBoxes'][] = + [ + 'name' => '', + 'top_left_latitude' => '', + 'top_left_longitude' => '', + 'bottom_right_latitude' => '', + 'bottom_right_longitude' => '', + ]; + } + + public function deleteBoundingBox($index) + { + if(array_key_exists('id', $this->formData['boundingBoxes'][$index])) { + ProjectBoundingBox::whereId($this->formData['boundingBoxes'][$index]['id'])->delete(); + } + unset($this->formData['boundingBoxes'][$index]); + $this->formData['boundingBoxes'] = array_values($this->formData['boundingBoxes']); + $this->dispatch('saved'); + } + + + /** + * Confirm that the given Project should be deleted. + * + * @param int $reportId + * @return void + */ + public function confirmReportDeletion($reportId) + { + $this->confirmingReportDeletion = true; + + $this->reportIdBeingDeleted = $reportId; + } + + /** + * Delete the project + * + * @return void + */ + public function deleteProject() + { + Project::whereId($this->reportIdBeingDeleted)->first()->delete(); + $this->resetFormData(); + + $this->confirmingReportDeletion = false; + } + + /** + * Get the current user of the application. + * + * @return mixed + */ + public function getProjectsProperty() + { + return Project::all(); + } + + public function render() + { + return view('livewire.project-manager'); + } + + private function resetFormData() + { + $this->formData = [ + 'name' => '', + 'boundingBoxes' => [], + ]; + $this->addBoundingBox(); + } +} diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php new file mode 100644 index 0000000..5fbdee8 --- /dev/null +++ b/laravel_app/app/Models/Project.php @@ -0,0 +1,28 @@ +boundingBoxes()->delete(); + }); + } + + public function boundingBoxes() + { + return $this->hasMany(ProjectBoundingBox::class); + } +} diff --git a/laravel_app/app/Models/ProjectBoundingBox.php b/laravel_app/app/Models/ProjectBoundingBox.php new file mode 100644 index 0000000..eb67fd5 --- /dev/null +++ b/laravel_app/app/Models/ProjectBoundingBox.php @@ -0,0 +1,24 @@ +belongsTo(Project::class); + } +} diff --git a/laravel_app/database/migrations/2023_11_07_154128_create_projects_table.php b/laravel_app/database/migrations/2023_11_07_154128_create_projects_table.php new file mode 100644 index 0000000..bc33cdb --- /dev/null +++ b/laravel_app/database/migrations/2023_11_07_154128_create_projects_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name')->unique(); + $table->text('mail_template')->nullable(); + $table->string('mail_subject')->nullable(); + $table->string('mail_frequentie')->default('weekly'); + $table->string('mail_day')->default('friday'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('projects'); + } +}; diff --git a/laravel_app/database/migrations/2023_11_17_140157_create_project_bounding_boxes_table.php b/laravel_app/database/migrations/2023_11_17_140157_create_project_bounding_boxes_table.php new file mode 100644 index 0000000..b269cf2 --- /dev/null +++ b/laravel_app/database/migrations/2023_11_17_140157_create_project_bounding_boxes_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignIdFor(Project::class); + $table->string('name'); + $table->string('top_left_latitude'); + $table->string('top_left_longitude'); + $table->string('bottom_right_latitude'); + $table->string('bottom_right_longitude'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('project_bounding_boxes'); + } +}; diff --git a/laravel_app/resources/views/api/api-token-manager.blade.php b/laravel_app/resources/views/api/api-token-manager.blade.php index e166c2a..f955970 100644 --- a/laravel_app/resources/views/api/api-token-manager.blade.php +++ b/laravel_app/resources/views/api/api-token-manager.blade.php @@ -12,6 +12,7 @@
+ diff --git a/laravel_app/resources/views/livewire/project-manager.blade.php b/laravel_app/resources/views/livewire/project-manager.blade.php new file mode 100644 index 0000000..b8d77f6 --- /dev/null +++ b/laravel_app/resources/views/livewire/project-manager.blade.php @@ -0,0 +1,165 @@ +
+ + + + {{ __('Project') }} + + + + {{ __('Report generator for generating reports') }} + + + + +
+ + + +
+ @foreach($formData['boundingBoxes'] as $key => $boundingBox) + {{--
--}} +
+ {{ __('Bounding box') }} + @if( count($this->formData['boundingBoxes']) > 1) + + @endif +
+
+ + + +
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ {{--
--}} + @endforeach +
+ + + + {{ __('Saved.') }} + + + {{ __('Add Bounding box') }} + + + {{ __('Cancel') }} + + + {{ __('Save') }} + + + +
+
+ + +
+ + + {{ __('Manage projects') }} + + + + {{ __('You may delete any of your projects if they are no longer needed.') }} + + + + +
+ + {{ __('Create Project') }} + + @foreach ($this->projects->sortBy('name') as $project) +
+
+ {{ $project->name }} +
+ +
+ + +
+
+ @endforeach +
+
+
+
+ + + + + + {{ __('Delete Project') }} + + + + {{ __('Are you sure you would like to delete this Project?') }} + + + + + {{ __('Cancel') }} + + + + {{ __('Delete') }} + + + +
+ diff --git a/laravel_app/resources/views/livewire/reports/report-manager.blade.php b/laravel_app/resources/views/livewire/reports/report-manager.blade.php index 98172da..22e9d71 100644 --- a/laravel_app/resources/views/livewire/reports/report-manager.blade.php +++ b/laravel_app/resources/views/livewire/reports/report-manager.blade.php @@ -1,5 +1,5 @@
- + {{ __('Create report') }} diff --git a/laravel_app/resources/views/navigation-menu.blade.php b/laravel_app/resources/views/navigation-menu.blade.php index acc19ad..c1b41cc 100644 --- a/laravel_app/resources/views/navigation-menu.blade.php +++ b/laravel_app/resources/views/navigation-menu.blade.php @@ -21,6 +21,9 @@ {{ __('Report') }} + + {{ __('Project') }} +
diff --git a/laravel_app/resources/views/projects/index.blade.php b/laravel_app/resources/views/projects/index.blade.php new file mode 100644 index 0000000..b76a74b --- /dev/null +++ b/laravel_app/resources/views/projects/index.blade.php @@ -0,0 +1,16 @@ + + +

+ {{ __('Project') }} +

+
+ +
+
+ +
+ @livewire('projects.project-manager') +
+
+
+
diff --git a/laravel_app/routes/web.php b/laravel_app/routes/web.php index 67c752b..7d37724 100644 --- a/laravel_app/routes/web.php +++ b/laravel_app/routes/web.php @@ -28,4 +28,5 @@ Route::get('/download', [\App\Http\Controllers\DownloadController::class, 'show'])->name('download'); Route::get('/report', [\App\Http\Controllers\ReportController::class, 'index'])->name('report'); + Route::get('/project', [\App\Http\Controllers\ProjectController::class, 'index'])->name('project'); });