diff --git a/src/Controller/ExternalProductCatalogsController.php b/src/Controller/ExternalProductCatalogsController.php index 1683ce3..04846f7 100644 --- a/src/Controller/ExternalProductCatalogsController.php +++ b/src/Controller/ExternalProductCatalogsController.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace CakeProducts\Controller; +use Cake\Core\Configure; use Cake\Log\Log; +use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; /** @@ -13,6 +16,28 @@ use CakeProducts\Controller\AppController; */ class ExternalProductCatalogsController extends AppController { + /** + * @var Table|null + */ + protected ?Table $_table = null; + + /** + * Gets the table instance + * + * @return Table + */ + public function getTable() + { + if ($this->_table instanceof Table) { + return $this->_table; + } + $this->_table = TableRegistry::getTableLocator()->get( + Configure::read('CakeProducts.ExternalProductCatalogs.table', 'CakeProducts.ExternalProductCatalogs') + ); + + return $this->_table; + } + /** * Index method * diff --git a/src/Controller/ProductCatalogsController.php b/src/Controller/ProductCatalogsController.php index 14e6b26..3e14be5 100644 --- a/src/Controller/ProductCatalogsController.php +++ b/src/Controller/ProductCatalogsController.php @@ -20,7 +20,7 @@ use CakeProducts\Service\CatalogManagerService; */ class ProductCatalogsController extends AppController { - protected ?Table $_productCatalogsTable = null; + protected ?Table $_table = null; /** * Index method @@ -29,7 +29,7 @@ class ProductCatalogsController extends AppController */ public function index() { - $query = $this->getProductCatalogsTable()->find(); + $query = $this->getTable()->find(); $productCatalogs = $this->paginate($query); $this->set(compact('productCatalogs')); @@ -48,7 +48,7 @@ class ProductCatalogsController extends AppController if (Configure::read('CakeProducts.internal.syncExternally', false)) { $contain[] = 'ExternalProductCatalogs'; } - $productCatalog = $this->getProductCatalogsTable()->get($id, contain: $contain); + $productCatalog = $this->getTable()->get($id, contain: $contain); $this->set(compact('productCatalog')); } @@ -59,7 +59,7 @@ class ProductCatalogsController extends AppController */ public function add() { - $productCatalogsTable = $this->getProductCatalogsTable(); + $productCatalogsTable = $this->getTable(); $productCatalog = $productCatalogsTable->newEmptyEntity(); if ($this->request->is('post')) { $productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData()); @@ -86,7 +86,7 @@ class ProductCatalogsController extends AppController */ public function edit($id = null) { - $productCatalogsTable = $this->getProductCatalogsTable(); + $productCatalogsTable = $this->getTable(); $productCatalog = $productCatalogsTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { $productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData()); @@ -110,7 +110,7 @@ class ProductCatalogsController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $productCatalogsTable = $this->getProductCatalogsTable(); + $productCatalogsTable = $this->getTable(); $productCatalog = $productCatalogsTable->get($id); if ($productCatalogsTable->delete($productCatalog)) { $this->Flash->success(__('The product catalog has been deleted.')); @@ -126,15 +126,15 @@ class ProductCatalogsController extends AppController * * @return Table */ - public function getProductCatalogsTable() + public function getTable() { - if ($this->_productCatalogsTable instanceof Table) { - return $this->_productCatalogsTable; + if ($this->_table instanceof Table) { + return $this->_table; } - $this->_productCatalogsTable = TableRegistry::getTableLocator()->get( + $this->_table = TableRegistry::getTableLocator()->get( Configure::read('CakeProducts.ProductCatalogs.table', 'CakeProducts.ProductCatalogs') ); - return $this->_productCatalogsTable; + return $this->_table; } } diff --git a/src/Controller/ProductCategoriesController.php b/src/Controller/ProductCategoriesController.php index 369a099..3fbdb0d 100644 --- a/src/Controller/ProductCategoriesController.php +++ b/src/Controller/ProductCategoriesController.php @@ -8,8 +8,6 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use Cake\Utility\Text; -use CakeProducts\Controller\AppController; -use CakeProducts\Service\CatalogManagerService; /** * ProductCategories Controller @@ -18,23 +16,26 @@ use CakeProducts\Service\CatalogManagerService; */ class ProductCategoriesController extends AppController { - protected ?Table $_productCategoriesTable = null; + /** + * @var Table|null + */ + protected ?Table $_table = null; /** - * Gets the users table instance + * Gets the table instance * * @return Table */ - public function getProductCategoriesTable() + public function getTable() { - if ($this->_productCategoriesTable instanceof Table) { - return $this->_productCategoriesTable; + if ($this->_table instanceof Table) { + return $this->_table; } - $this->_productCategoriesTable = TableRegistry::getTableLocator()->get( + $this->_table = TableRegistry::getTableLocator()->get( Configure::read('CakeProducts.ProductCategories.table', 'CakeProducts.ProductCategories') ); - return $this->_productCategoriesTable; + return $this->_table; } /** * Index method @@ -43,7 +44,7 @@ class ProductCategoriesController extends AppController */ public function index() { - $query = $this->getProductCategoriesTable()->find() + $query = $this->getTable()->find() ->contain(['ProductCatalogs', 'ParentProductCategories']); $productCategories = $this->paginate($query); @@ -59,7 +60,7 @@ class ProductCategoriesController extends AppController */ public function view($id = null) { - $productCategory = $this->getProductCategoriesTable()->get($id, contain: ['ProductCatalogs', 'ParentProductCategories', 'ChildProductCategories']); + $productCategory = $this->getTable()->get($id, contain: ['ProductCatalogs', 'ParentProductCategories', 'ChildProductCategories']); $this->set(compact('productCategory')); } @@ -70,7 +71,7 @@ class ProductCategoriesController extends AppController */ public function add() { - $productCategoriesTable = $this->getProductCategoriesTable(); + $productCategoriesTable = $this->getTable(); $productCategory = $productCategoriesTable->newEmptyEntity(); if ($this->request->is('post')) { $postData = $this->request->getData(); @@ -109,7 +110,7 @@ class ProductCategoriesController extends AppController */ public function edit($id = null) { - $productCategoriesTable = $this->getProductCategoriesTable(); + $productCategoriesTable = $this->getTable(); $productCategory = $productCategoriesTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { $postData = $this->request->getData(); @@ -136,7 +137,7 @@ class ProductCategoriesController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $productCategoriesTable = $this->getProductCategoriesTable(); + $productCategoriesTable = $this->getTable(); $productCategory = $productCategoriesTable->get($id); if ($productCategoriesTable->delete($productCategory)) { @@ -153,7 +154,7 @@ class ProductCategoriesController extends AppController */ public function select() { - $productCategoriesTable = $this->getProductCategoriesTable(); + $productCategoriesTable = $this->getTable(); $productCategoriesTable->behaviors()->get('Tree')->setConfig([ 'scope' => [ 'product_catalog_id' => $this->request->getQuery('product_catalog_id', -1), diff --git a/src/Controller/ProductCategoryAttributeOptionsController.php b/src/Controller/ProductCategoryAttributeOptionsController.php index b166725..e733cba 100644 --- a/src/Controller/ProductCategoryAttributeOptionsController.php +++ b/src/Controller/ProductCategoryAttributeOptionsController.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace CakeProducts\Controller; +use Cake\Core\Configure; use Cake\Log\Log; +use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; /** @@ -13,6 +16,28 @@ use CakeProducts\Controller\AppController; */ class ProductCategoryAttributeOptionsController extends AppController { + /** + * @var Table|null + */ + protected ?Table $_table = null; + + /** + * Gets the table instance + * + * @return Table + */ + public function getTable() + { + if ($this->_table instanceof Table) { + return $this->_table; + } + $this->_table = TableRegistry::getTableLocator()->get( + Configure::read('CakeProducts.ProductCategoryAttributeOptions.table', 'CakeProducts.ProductCategoryAttributeOptions') + ); + + return $this->_table; + } + /** * Add method * @@ -22,7 +47,7 @@ class ProductCategoryAttributeOptionsController extends AppController { Log::debug('inside product category attribute options controller add'); - $productCategoryAttributeOption = $this->ProductCategoryAttributeOptions->newEmptyEntity(); + $productCategoryAttributeOption = $this->getTable()->newEmptyEntity(); $this->set(compact('productCategoryAttributeOption')); } @@ -36,8 +61,10 @@ class ProductCategoryAttributeOptionsController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $productCategoryAttributeOption = $this->ProductCategoryAttributeOptions->get($id); - if ($this->ProductCategoryAttributeOptions->delete($productCategoryAttributeOption)) { + $productCategoryAttributeOptionsTable = $this->getTable(); + + $productCategoryAttributeOption = $productCategoryAttributeOptionsTable->get($id); + if ($productCategoryAttributeOptionsTable->delete($productCategoryAttributeOption)) { $this->Flash->success(__('The product category attribute option has been deleted.')); } else { $this->Flash->error(__('The product category attribute option could not be deleted. Please, try again.')); diff --git a/src/Controller/ProductCategoryAttributesController.php b/src/Controller/ProductCategoryAttributesController.php index 00a4aa2..6a90a6f 100644 --- a/src/Controller/ProductCategoryAttributesController.php +++ b/src/Controller/ProductCategoryAttributesController.php @@ -3,9 +3,12 @@ declare(strict_types=1); namespace CakeProducts\Controller; +use Cake\Core\Configure; use Cake\Datasource\Exception\RecordNotFoundException; use Cake\Http\Response; use Cake\Log\Log; +use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; use CakeProducts\Model\Enum\ProductCategoryAttributeTypeId; use CakeProducts\Model\Table\ProductCategoryAttributesTable; @@ -18,6 +21,28 @@ use CakeProducts\Service\CatalogManagerService; */ class ProductCategoryAttributesController extends AppController { + /** + * @var Table|null + */ + protected ?Table $_table = null; + + /** + * Gets the table instance + * + * @return Table + */ + public function getTable() + { + if ($this->_table instanceof Table) { + return $this->_table; + } + $this->_table = TableRegistry::getTableLocator()->get( + Configure::read('CakeProducts.ProductCategoryAttributes.table', 'CakeProducts.ProductCategoryAttributes') + ); + + return $this->_table; + } + /** * Index method * @@ -25,7 +50,7 @@ class ProductCategoryAttributesController extends AppController */ public function index() { - $query = $this->ProductCategoryAttributes->find() + $query = $this->getTable()->find() ->contain(['ProductCategories']); $productCategoryAttributes = $this->paginate($query); @@ -41,7 +66,7 @@ class ProductCategoryAttributesController extends AppController */ public function view($id = null) { - $productCategoryAttribute = $this->ProductCategoryAttributes->get($id, contain: [ + $productCategoryAttribute = $this->getTable()->get($id, contain: [ 'ProductCategories', 'ProductCategoryAttributeOptions', ]); @@ -55,7 +80,8 @@ class ProductCategoryAttributesController extends AppController */ public function add() { - $productCategoryAttribute = $this->ProductCategoryAttributes->newEmptyEntity(); + $productCategoryAttributesTable = $this->getTable(); + $productCategoryAttribute = $productCategoryAttributesTable->newEmptyEntity(); if ($this->request->is('post')) { $postData = $this->request->getData(); if ($this->request->getSession()->read('Auth.User.id')) { @@ -68,12 +94,12 @@ class ProductCategoryAttributesController extends AppController 'ProductCategoryAttributeOptions' ], ]; - $productCategoryAttribute = $this->ProductCategoryAttributes->patchEntity($productCategoryAttribute, $postData, $saveOptions); + $productCategoryAttribute = $productCategoryAttributesTable->patchEntity($productCategoryAttribute, $postData, $saveOptions); if ($productCategoryAttribute->getErrors()) { Log::debug(print_r('$productCategoryAttribute->getErrors() next - failed to save from create new product category attribute', true)); Log::debug(print_r($productCategoryAttribute->getErrors(), true)); } - if ($this->ProductCategoryAttributes->save($productCategoryAttribute, $saveOptions)) { + if ($productCategoryAttributesTable->save($productCategoryAttribute, $saveOptions)) { $this->Flash->success(__('The product category attribute has been saved.')); return $this->redirect(['action' => 'index']); @@ -83,7 +109,7 @@ class ProductCategoryAttributesController extends AppController Log::debug(print_r($productCategoryAttribute->getErrors(), true)); $this->Flash->error(__('The product category attribute could not be saved. Please, try again.')); } - $productCategories = $this->ProductCategoryAttributes->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name')->all(); + $productCategories = $productCategoryAttributesTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name')->all(); $this->set(compact('productCategoryAttribute', 'productCategories')); } @@ -96,7 +122,8 @@ class ProductCategoryAttributesController extends AppController */ public function edit($id = null) { - $productCategoryAttribute = $this->ProductCategoryAttributes->get($id, contain: ['ProductCategoryAttributeOptions']); + $productCategoryAttributesTable = $this->getTable(); + $productCategoryAttribute = $productCategoryAttributesTable->get($id, contain: ['ProductCategoryAttributeOptions']); if ($this->request->is(['patch', 'post', 'put'])) { $postData = $this->request->getData(); $saveOptions = [ @@ -110,9 +137,9 @@ class ProductCategoryAttributesController extends AppController // } Log::debug(print_r('$postData', true)); Log::debug(print_r($postData, true)); - $productCategoryAttribute = $this->ProductCategoryAttributes->patchEntity($productCategoryAttribute, $postData, $saveOptions); + $productCategoryAttribute = $productCategoryAttributesTable->patchEntity($productCategoryAttribute, $postData, $saveOptions); - if ($this->ProductCategoryAttributes->save($productCategoryAttribute, $saveOptions)) { + if ($productCategoryAttributesTable->save($productCategoryAttribute, $saveOptions)) { $this->Flash->success(__('The product category attribute has been saved.')); return $this->redirect(['action' => 'index']); @@ -122,7 +149,7 @@ class ProductCategoryAttributesController extends AppController Log::debug(print_r($productCategoryAttribute->getErrors(), true)); $this->Flash->error(__('The product category attribute could not be saved. Please, try again.')); } - $productCategories = $this->ProductCategoryAttributes->ProductCategories->find('list', limit: 200, keyField: 'internal_id', valueField: 'name')->all(); + $productCategories = $productCategoryAttributesTable->ProductCategories->find('list', limit: 200, keyField: 'internal_id', valueField: 'name')->all(); $this->set(compact('productCategoryAttribute', 'productCategories')); } @@ -136,8 +163,10 @@ class ProductCategoryAttributesController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $productCategoryAttribute = $this->ProductCategoryAttributes->get($id); - if ($this->ProductCategoryAttributes->delete($productCategoryAttribute)) { + + $productCategoryAttributesTable = $this->getTable(); + $productCategoryAttribute = $productCategoryAttributesTable->get($id); + if ($productCategoryAttributesTable->delete($productCategoryAttribute)) { $this->Flash->success(__('The product category attribute has been deleted.')); } else { $this->Flash->error(__('The product category attribute could not be deleted. Please, try again.')); diff --git a/src/Controller/ProductsController.php b/src/Controller/ProductsController.php index 799ec4c..f026a52 100644 --- a/src/Controller/ProductsController.php +++ b/src/Controller/ProductsController.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace CakeProducts\Controller; +use Cake\Core\Configure; use Cake\Log\Log; +use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; /** @@ -13,6 +16,28 @@ use CakeProducts\Controller\AppController; */ class ProductsController extends AppController { + /** + * @var Table|null + */ + protected ?Table $_table = null; + + /** + * Gets the table instance + * + * @return Table + */ + public function getTable() + { + if ($this->_table instanceof Table) { + return $this->_table; + } + $this->_table = TableRegistry::getTableLocator()->get( + Configure::read('CakeProducts.Products.table', 'CakeProducts.Products') + ); + + return $this->_table; + } + /** * Index method * @@ -20,7 +45,7 @@ class ProductsController extends AppController */ public function index() { - $query = $this->Products->find() + $query = $this->getTable()->find() ->contain(['ProductCategories']); $products = $this->paginate($query); @@ -36,7 +61,7 @@ class ProductsController extends AppController */ public function view($id = null) { - $product = $this->Products->get($id, contain: ['ProductCategories']); + $product = $this->getTable()->get($id, contain: ['ProductCategories']); $this->set(compact('product')); } @@ -47,10 +72,11 @@ class ProductsController extends AppController */ public function add() { - $product = $this->Products->newEmptyEntity(); + $productsTable = $this->getTable(); + $product = $productsTable->newEmptyEntity(); if ($this->request->is('post')) { - $product = $this->Products->patchEntity($product, $this->request->getData()); - if ($this->Products->save($product)) { + $product = $productsTable->patchEntity($product, $this->request->getData()); + if ($productsTable->save($product)) { $this->Flash->success(__('The product has been saved.')); return $this->redirect(['action' => 'index']); @@ -59,7 +85,7 @@ class ProductsController extends AppController Log::debug(print_r($product->getErrors(), true)); $this->Flash->error(__('The product could not be saved. Please, try again.')); } - $productCategories = $this->Products->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all(); + $productCategories = $productsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all(); $this->set(compact('product', 'productCategories')); } @@ -72,10 +98,11 @@ class ProductsController extends AppController */ public function edit($id = null) { - $product = $this->Products->get($id, contain: []); + $productsTable = $this->getTable(); + $product = $productsTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { - $product = $this->Products->patchEntity($product, $this->request->getData()); - if ($this->Products->save($product)) { + $product = $productsTable->patchEntity($product, $this->request->getData()); + if ($productsTable->save($product)) { $this->Flash->success(__('The product has been saved.')); return $this->redirect(['action' => 'index']); @@ -84,7 +111,7 @@ class ProductsController extends AppController Log::debug(print_r($product->getErrors(), true)); $this->Flash->error(__('The product could not be saved. Please, try again.')); } - $productCategories = $this->Products->ProductCategories->find('list', limit: 200)->all(); + $productCategories = $productsTable->ProductCategories->find('list', limit: 200)->all(); $this->set(compact('product', 'productCategories')); } @@ -98,8 +125,10 @@ class ProductsController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $product = $this->Products->get($id); - if ($this->Products->delete($product)) { + + $productsTable = $this->getTable(); + $product = $productsTable->get($id); + if ($productsTable->delete($product)) { $this->Flash->success(__('The product has been deleted.')); } else { $this->Flash->error(__('The product could not be deleted. Please, try again.'));