From 17de2e97dde8db1442f912bd0286022574bfb068 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Tue, 1 Apr 2025 01:00:09 -0700 Subject: [PATCH] testing --- .../ProductCategoriesController.php | 4 +- .../ProductCategoryAttributesController.php | 8 ++++ src/Controller/ProductsController.php | 5 ++- .../Table/ProductCategoryAttributesTable.php | 8 ++++ templates/Products/add.php | 6 +-- templates/Products/edit.php | 6 +-- .../element/ExternalProductCatalogs/form.php | 1 - templates/element/Products/form.php | 45 +++++++++++++++++++ 8 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 templates/element/Products/form.php diff --git a/src/Controller/ProductCategoriesController.php b/src/Controller/ProductCategoriesController.php index 8a2a168..0229d5d 100644 --- a/src/Controller/ProductCategoriesController.php +++ b/src/Controller/ProductCategoriesController.php @@ -60,7 +60,9 @@ class ProductCategoriesController extends AppController 'ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions', ]); - $this->set(compact('productCategory')); + + $productCategoryAttributes = $this->getTable()->ProductCategoryAttributes->getAllCategoryAttributesForCategoryId($productCategory->internal_id); + $this->set(compact('productCategory', 'productCategoryAttributes')); } /** diff --git a/src/Controller/ProductCategoryAttributesController.php b/src/Controller/ProductCategoryAttributesController.php index 58adeac..d142643 100644 --- a/src/Controller/ProductCategoryAttributesController.php +++ b/src/Controller/ProductCategoryAttributesController.php @@ -61,6 +61,7 @@ class ProductCategoryAttributesController extends AppController 'ProductCategories', 'ProductCategoryAttributeOptions', ]); + $this->set(compact('productCategoryAttribute')); } @@ -165,4 +166,11 @@ class ProductCategoryAttributesController extends AppController return $this->redirect(['action' => 'index']); } + + public function form($categoryId = null) + { + $productCategoryAttributes = $this->getTable()->getAllCategoryAttributesForCategoryId($categoryId); + + $this->set(compact('productCategoryAttributes')); + } } diff --git a/src/Controller/ProductsController.php b/src/Controller/ProductsController.php index 684ee15..f54c288 100644 --- a/src/Controller/ProductsController.php +++ b/src/Controller/ProductsController.php @@ -77,8 +77,9 @@ class ProductsController extends AppController Log::debug(print_r($product->getErrors(), true)); $this->Flash->error(__('The product could not be saved. Please, try again.')); } - $productCategories = $productsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all(); - $this->set(compact('product', 'productCategories')); + $productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null; + $productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray(); + $this->set(compact('product', 'productCatalogs', 'productCategory')); } /** diff --git a/src/Model/Table/ProductCategoryAttributesTable.php b/src/Model/Table/ProductCategoryAttributesTable.php index a2f3cf8..58bd032 100644 --- a/src/Model/Table/ProductCategoryAttributesTable.php +++ b/src/Model/Table/ProductCategoryAttributesTable.php @@ -110,4 +110,12 @@ class ProductCategoryAttributesTable extends Table return $rules; } + + public function getAllCategoryAttributesForCategoryId(string $internalCategoryId) + { + $category = $this->ProductCategories->find()->where(['internal_id' => $internalCategoryId])->firstOrFail(); + $categories = $this->ProductCategories->find('path', for: $category->id)->all(); + + return $categories; + } } diff --git a/templates/Products/add.php b/templates/Products/add.php index 39bf6b0..08c3a0e 100644 --- a/templates/Products/add.php +++ b/templates/Products/add.php @@ -17,11 +17,7 @@ Form->create($product) ?>
- Form->control('name'); - echo $this->Form->control('product_category_id', ['options' => $productCategories]); - echo $this->Form->control('product_type_id'); - ?> + element('Products/form'); ?>
Form->button(__('Submit')) ?> Form->end() ?> diff --git a/templates/Products/edit.php b/templates/Products/edit.php index 5d49a17..ed9457b 100644 --- a/templates/Products/edit.php +++ b/templates/Products/edit.php @@ -22,11 +22,7 @@ Form->create($product) ?>
- Form->control('name'); - echo $this->Form->control('product_category_id', ['options' => $productCategories]); - echo $this->Form->control('product_type_id'); - ?> + element('Products/form'); ?>
Form->button(__('Submit')) ?> Form->end() ?> diff --git a/templates/element/ExternalProductCatalogs/form.php b/templates/element/ExternalProductCatalogs/form.php index d8bcffc..646aab0 100644 --- a/templates/element/ExternalProductCatalogs/form.php +++ b/templates/element/ExternalProductCatalogs/form.php @@ -19,7 +19,6 @@ echo $this->Form->control('api_url'); ?>
-

Product Catalogs Html->link('Add Catalog', '#', [ diff --git a/templates/element/Products/form.php b/templates/element/Products/form.php new file mode 100644 index 0000000..fefd293 --- /dev/null +++ b/templates/element/Products/form.php @@ -0,0 +1,45 @@ + +

Basic Info

+ +Form->control('name'); +echo $this->Form->control('product_catalog_id', [ + 'options' => $productCatalogs, + 'hx-trigger' => 'change, load delay:1s', + 'hx-get' => $this->Url->build([ + 'controller' => 'ProductCategories', + 'action' => 'select', + 0 => $productCategory ? $productCategory->product_catalog_id : '', + ]), + 'hx-target' => '#product_category_id', +]); +echo $this->Form->control('product_category_id', [ + 'options' => $productCategories ?? [], + 'empty' => true, + 'id' => 'product_category_id', +]); +echo $this->Form->control('product_type_id'); +?> +
+

Product Attributes

+
+ hasValue('product_attributes')) : ?> + product_attributes as $productAttribute) { + $prefix = 'product_attributes.' . $cnt . '.'; + + echo '
'; + + $cnt++; + } ?> + +
\ No newline at end of file