CakeProducts/src/Controller/ProductsController.php

149 lines
5.3 KiB
PHP
Raw Normal View History

<?php
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;
use CheeseCake\Controller\Traits\OverrideTableTrait;
;
/**
* Products Controller
*
* @property \CakeProducts\Model\Table\ProductsTable $Products
*/
class ProductsController extends AppController
{
use OverrideTableTrait;
/**
* @return void
*/
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
// $this->_defaultTable = 'CakeProducts.Products';
// $this->_tableConfigKey = 'CakeProducts.Products.table';
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->getTable()->find()
->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)
{
$product = $this->getTable()->get($id, contain: [
'ProductCategories',
'ProductAttributes',
'ProductAttributes.ProductCategoryAttributes',
'ProductAttributes.ProductCategoryAttributeOptions',
'ProductCategoryVariants',
'ProductSkus'
]);
$this->set(compact('product'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productsTable = $this->getTable();
$product = $productsTable->newEmptyEntity();
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)) {
$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'));
}
/**
* 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)
{
$productsTable = $this->getTable();
$product = $productsTable->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$product = $productsTable->patchEntity($product, $this->request->getData());
if ($productsTable->save($product)) {
$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'));
}
/**
* 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']);
$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.'));
}
return $this->redirect(['action' => 'index']);
}
}