From 5936611959023c2d4ba91d749ffcbc9ce93e473c Mon Sep 17 00:00:00 2001 From: Tobias Frauenfelder Date: Sun, 18 Feb 2024 20:14:25 +0100 Subject: [PATCH] Add Location to PageBuilder --- api/app/Http/Controllers/PageController.php | 11 ++++ api/app/Http/Requests/StorePageRequest.php | 6 +- api/app/Http/Requests/UpdatePageRequest.php | 3 +- api/app/Models/LocationItem.php | 30 +++++++++ api/app/Models/Page.php | 8 +++ ..._18_185901_create_location_items_table.php | 31 ++++++++++ frontend/src/components/admin/AddPageItem.vue | 1 + .../admin/PageItems/LocationItem.vue | 61 +++++++++++++++++++ frontend/src/components/main/LocationItem.vue | 18 ++++++ frontend/src/components/main/PageBuilder.vue | 3 + frontend/src/views/Dashboard/Page.vue | 21 +++++++ 11 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 api/app/Models/LocationItem.php create mode 100644 api/database/migrations/2024_02_18_185901_create_location_items_table.php create mode 100644 frontend/src/components/admin/PageItems/LocationItem.vue create mode 100644 frontend/src/components/main/LocationItem.vue diff --git a/api/app/Http/Controllers/PageController.php b/api/app/Http/Controllers/PageController.php index ab39264..d36b55a 100644 --- a/api/app/Http/Controllers/PageController.php +++ b/api/app/Http/Controllers/PageController.php @@ -9,6 +9,7 @@ use App\Models\FormItem; use App\Models\GenericItem; use App\Models\ImageItem; +use App\Models\LocationItem; use App\Models\Page; use App\Models\TextItem; use Illuminate\Http\Request; @@ -219,6 +220,16 @@ private function createPageItemsFromValidatedData(Page $page, $validatedData) ] ); break; + case 'locationItem': + LocationItem::updateOrCreate( + ['id' => $pageItemData['id'] ?? null], + [ + 'page_id' => $page->id, + 'sort' => $sort_counter, + 'location_id' => $pageItemData['location_id'] ?? null, + ] + ); + break; default: throw new \InvalidArgumentException("Unsupported field type: {$pageItemData['type']}"); } diff --git a/api/app/Http/Requests/StorePageRequest.php b/api/app/Http/Requests/StorePageRequest.php index 5d1b192..d5f700f 100644 --- a/api/app/Http/Requests/StorePageRequest.php +++ b/api/app/Http/Requests/StorePageRequest.php @@ -16,12 +16,14 @@ public function rules() 'page_items' => 'nullable|array', 'page_items.*.id' => 'nullable', 'page_items.*.sort' => 'nullable', - 'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem', + 'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem,locationItem', 'page_items.*.title' => 'nullable', 'page_items.*.body' => 'nullable', 'page_items.*.show_fleur_de_lis' => 'nullable', 'page_items.*.files' => 'nullable', - 'page_items.*.file_id' => 'nullable' + 'page_items.*.file_id' => 'nullable', + 'page_items.*.form_id' => 'nullable', + 'page_items.*.location_id' => 'nullable', ]; } } diff --git a/api/app/Http/Requests/UpdatePageRequest.php b/api/app/Http/Requests/UpdatePageRequest.php index f91104a..611e77e 100644 --- a/api/app/Http/Requests/UpdatePageRequest.php +++ b/api/app/Http/Requests/UpdatePageRequest.php @@ -16,12 +16,13 @@ public function rules() 'page_items' => 'nullable|array', 'page_items.*.id' => 'nullable', 'page_items.*.sort' => 'nullable', - 'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem', + 'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem,locationItem', 'page_items.*.title' => 'nullable', 'page_items.*.body' => 'nullable', 'page_items.*.show_fleur_de_lis' => 'nullable', 'page_items.*.files' => 'nullable', 'page_items.*.form_id' => 'nullable', + 'page_items.*.location_id' => 'nullable', ]; } } diff --git a/api/app/Models/LocationItem.php b/api/app/Models/LocationItem.php new file mode 100644 index 0000000..e1d5cf6 --- /dev/null +++ b/api/app/Models/LocationItem.php @@ -0,0 +1,30 @@ +belongsTo(Page::class); + } + + public function location() + { + return $this->belongsTo(Location::class); + } +} diff --git a/api/app/Models/Page.php b/api/app/Models/Page.php index b70fa4c..1815dad 100644 --- a/api/app/Models/Page.php +++ b/api/app/Models/Page.php @@ -54,6 +54,12 @@ public function filesItems() return $this->hasMany(FilesItem::class)->with('files'); } + public function locationItems() + { + + return $this->hasMany(LocationItem::class)->with('location'); + } + public function genericItems() { @@ -69,12 +75,14 @@ public function getAllItems() $formItems = $this->formItems; $filesItems = $this->filesItems; $genericItems = $this->genericItems; + $locationItems = $this->locationItems; $items = $items->concat($textItems); $items = $items->concat($imageItems); $items = $items->concat($formItems); $items = $items->concat($filesItems); $items = $items->concat($genericItems); + $items = $items->concat($locationItems); $items = $items->sortBy('sort')->values()->all();; diff --git a/api/database/migrations/2024_02_18_185901_create_location_items_table.php b/api/database/migrations/2024_02_18_185901_create_location_items_table.php new file mode 100644 index 0000000..4e9831c --- /dev/null +++ b/api/database/migrations/2024_02_18_185901_create_location_items_table.php @@ -0,0 +1,31 @@ +id(); + $table->foreignId('location_id')->nullable()->constrained()->onDelete('cascade'); + $table->integer('sort')->nullable(); + $table->unsignedBigInteger('page_id'); + $table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('location_items'); + } +}; diff --git a/frontend/src/components/admin/AddPageItem.vue b/frontend/src/components/admin/AddPageItem.vue index 3ec17d6..0824a6f 100644 --- a/frontend/src/components/admin/AddPageItem.vue +++ b/frontend/src/components/admin/AddPageItem.vue @@ -49,6 +49,7 @@ export default { { name: "Groups", type: "groupsItem" }, { name: "Sections", type: "sectionsItem" }, { name: "Camps", type: "campsItem" }, + { name: "Location", type: "locationItem" }, ], }; }, diff --git a/frontend/src/components/admin/PageItems/LocationItem.vue b/frontend/src/components/admin/PageItems/LocationItem.vue new file mode 100644 index 0000000..dfb1349 --- /dev/null +++ b/frontend/src/components/admin/PageItems/LocationItem.vue @@ -0,0 +1,61 @@ + + + + + diff --git a/frontend/src/components/main/LocationItem.vue b/frontend/src/components/main/LocationItem.vue new file mode 100644 index 0000000..edceca1 --- /dev/null +++ b/frontend/src/components/main/LocationItem.vue @@ -0,0 +1,18 @@ + + diff --git a/frontend/src/components/main/PageBuilder.vue b/frontend/src/components/main/PageBuilder.vue index c561929..85a25ab 100644 --- a/frontend/src/components/main/PageBuilder.vue +++ b/frontend/src/components/main/PageBuilder.vue @@ -13,6 +13,7 @@ :files="pageItem.files" /> + @@ -26,6 +27,7 @@ import ImageItem from "./ImageItem.vue"; import PageWrapper from "./PageWrapper.vue"; import SectionsItem from "./SectionsItem.vue"; import TextItem from "./TextItem.vue"; +import LocationItem from "./LocationItem.vue"; export default { props: ["page"], @@ -44,6 +46,7 @@ export default { PageWrapper, FilesItem, CampsItem, + LocationItem, }, }; diff --git a/frontend/src/views/Dashboard/Page.vue b/frontend/src/views/Dashboard/Page.vue index 16e7827..9c54f0b 100644 --- a/frontend/src/views/Dashboard/Page.vue +++ b/frontend/src/views/Dashboard/Page.vue @@ -112,6 +112,16 @@ :key="i" :item="pageItem" /> + p.id == pageItemId && p.type == "locationItem", + ); + + this.content.pageItems[itemIndex].locationId = locationId; + }, changeHeaderImages(event) { this.content.files = event.files; },