111 lines
3.7 KiB
PHP
111 lines
3.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace CakeProducts\Controller;
|
|
|
|
use Cake\Log\Log;
|
|
use CakeProducts\Controller\AppController;
|
|
|
|
/**
|
|
* Products Controller
|
|
*
|
|
* @property \CakeProducts\Model\Table\ProductsTable $Products
|
|
*/
|
|
class ProductsController extends AppController
|
|
{
|
|
/**
|
|
* Index method
|
|
*
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
*/
|
|
public function index()
|
|
{
|
|
$query = $this->Products->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->Products->get($id, contain: ['ProductCategories']);
|
|
$this->set(compact('product'));
|
|
}
|
|
|
|
/**
|
|
* Add method
|
|
*
|
|
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
|
*/
|
|
public function add()
|
|
{
|
|
$product = $this->Products->newEmptyEntity();
|
|
if ($this->request->is('post')) {
|
|
$product = $this->Products->patchEntity($product, $this->request->getData());
|
|
if ($this->Products->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/add', true));
|
|
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();
|
|
$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)
|
|
{
|
|
$product = $this->Products->get($id, contain: []);
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
|
$product = $this->Products->patchEntity($product, $this->request->getData());
|
|
if ($this->Products->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.'));
|
|
}
|
|
$productCategories = $this->Products->ProductCategories->find('list', limit: 200)->all();
|
|
$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']);
|
|
$product = $this->Products->get($id);
|
|
if ($this->Products->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']);
|
|
}
|
|
}
|