2024-11-25 02:38:29 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CakeProducts\Controller;
|
|
|
|
|
2025-03-29 06:34:21 +00:00
|
|
|
use Cake\Core\Configure;
|
2024-11-25 02:38:29 +00:00
|
|
|
use Cake\Log\Log;
|
2025-03-29 06:34:21 +00:00
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\ORM\TableRegistry;
|
2024-11-25 02:38:29 +00:00
|
|
|
use CakeProducts\Controller\AppController;
|
2025-03-29 08:52:02 +00:00
|
|
|
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
|
|
|
;
|
2024-11-25 02:38:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Products Controller
|
|
|
|
*
|
|
|
|
* @property \CakeProducts\Model\Table\ProductsTable $Products
|
|
|
|
*/
|
|
|
|
class ProductsController extends AppController
|
|
|
|
{
|
2025-03-29 07:52:38 +00:00
|
|
|
use OverrideTableTrait;
|
2025-03-29 06:34:21 +00:00
|
|
|
|
|
|
|
/**
|
2025-03-29 07:52:38 +00:00
|
|
|
* @return void
|
2025-03-29 06:34:21 +00:00
|
|
|
*/
|
2025-03-29 07:52:38 +00:00
|
|
|
public function initialize(): void
|
2025-03-29 06:34:21 +00:00
|
|
|
{
|
2025-03-29 07:52:38 +00:00
|
|
|
parent::initialize(); // TODO: Change the autogenerated stub
|
2025-03-29 08:11:09 +00:00
|
|
|
// $this->_defaultTable = 'CakeProducts.Products';
|
|
|
|
// $this->_tableConfigKey = 'CakeProducts.Products.table';
|
2025-03-29 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
2024-11-25 02:38:29 +00:00
|
|
|
/**
|
|
|
|
* Index method
|
|
|
|
*
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2025-03-29 06:34:21 +00:00
|
|
|
$query = $this->getTable()->find()
|
2024-11-25 02:38:29 +00:00
|
|
|
->contain(['ProductCategories']);
|
|
|
|
$products = $this->paginate($query);
|
|
|
|
|
|
|
|
$this->set(compact('products'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product id.
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
|
|
|
*/
|
|
|
|
public function view($id = null)
|
|
|
|
{
|
2025-04-05 09:37:53 +00:00
|
|
|
$product = $this->getTable()->get($id, contain: [
|
|
|
|
'ProductCategories',
|
|
|
|
'ProductAttributes',
|
|
|
|
'ProductAttributes.ProductCategoryAttributes',
|
|
|
|
'ProductAttributes.ProductCategoryAttributeOptions',
|
2025-09-05 05:46:09 +00:00
|
|
|
'ProductCategoryVariants',
|
|
|
|
'ProductSkus'
|
2025-04-05 09:37:53 +00:00
|
|
|
]);
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->set(compact('product'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add method
|
|
|
|
*
|
|
|
|
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
2025-03-29 06:34:21 +00:00
|
|
|
$productsTable = $this->getTable();
|
|
|
|
$product = $productsTable->newEmptyEntity();
|
2024-11-25 02:38:29 +00:00
|
|
|
if ($this->request->is('post')) {
|
2025-04-05 09:06:23 +00:00
|
|
|
$postData = $this->request->getData();
|
|
|
|
$saveOptions = [
|
|
|
|
'associated' => ['ProductAttributes'],
|
|
|
|
];
|
|
|
|
// Log::debug(print_r('$postData', true));
|
|
|
|
// Log::debug(print_r($postData, true));
|
|
|
|
// Log::debug(print_r('$saveOptions', true));
|
|
|
|
// Log::debug(print_r($saveOptions, true));
|
|
|
|
$product = $productsTable->patchEntity($product, $postData, $saveOptions);
|
|
|
|
if ($productsTable->save($product, $saveOptions)) {
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->Flash->success(__('The product has been saved.'));
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
|
|
|
Log::debug(print_r('$product->getErrors() next - failed in products/add', true));
|
|
|
|
Log::debug(print_r($product->getErrors(), true));
|
|
|
|
$this->Flash->error(__('The product could not be saved. Please, try again.'));
|
|
|
|
}
|
2025-04-01 08:00:09 +00:00
|
|
|
$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'));
|
2024-11-25 02:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product id.
|
|
|
|
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
|
|
|
*/
|
|
|
|
public function edit($id = null)
|
|
|
|
{
|
2025-03-29 06:34:21 +00:00
|
|
|
$productsTable = $this->getTable();
|
|
|
|
$product = $productsTable->get($id, contain: []);
|
2024-11-25 02:38:29 +00:00
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
2025-03-29 06:34:21 +00:00
|
|
|
$product = $productsTable->patchEntity($product, $this->request->getData());
|
|
|
|
if ($productsTable->save($product)) {
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->Flash->success(__('The product has been saved.'));
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
|
|
|
Log::debug(print_r('$product->getErrors() next - failed in products/edit', true));
|
|
|
|
Log::debug(print_r($product->getErrors(), true));
|
|
|
|
$this->Flash->error(__('The product could not be saved. Please, try again.'));
|
|
|
|
}
|
2025-04-05 09:06:23 +00:00
|
|
|
$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'));
|
2024-11-25 02:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product id.
|
|
|
|
* @return \Cake\Http\Response|null Redirects to index.
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
|
|
|
*/
|
|
|
|
public function delete($id = null)
|
|
|
|
{
|
|
|
|
$this->request->allowMethod(['post', 'delete']);
|
2025-03-29 06:34:21 +00:00
|
|
|
|
|
|
|
$productsTable = $this->getTable();
|
|
|
|
$product = $productsTable->get($id);
|
|
|
|
if ($productsTable->delete($product)) {
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->Flash->success(__('The product has been deleted.'));
|
|
|
|
} else {
|
|
|
|
$this->Flash->error(__('The product could not be deleted. Please, try again.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
|
|
|
}
|