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 07:52:38 +00:00
|
|
|
use CakeProducts\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-03-29 06:34:21 +00:00
|
|
|
$product = $this->getTable()->get($id, contain: ['ProductCategories']);
|
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-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/add', true));
|
|
|
|
Log::debug(print_r($product->getErrors(), true));
|
|
|
|
$this->Flash->error(__('The product could not be saved. Please, try again.'));
|
|
|
|
}
|
2025-03-29 06:34:21 +00:00
|
|
|
$productCategories = $productsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all();
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->set(compact('product', 'productCategories'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-03-29 06:34:21 +00:00
|
|
|
$productCategories = $productsTable->ProductCategories->find('list', limit: 200)->all();
|
2024-11-25 02:38:29 +00:00
|
|
|
$this->set(compact('product', 'productCategories'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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']);
|
|
|
|
}
|
|
|
|
}
|