2024-11-24 18:38:29 -08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace CakeProducts\Controller;
|
|
|
|
|
|
2025-03-27 01:11:41 -07:00
|
|
|
use Cake\Core\Configure;
|
2024-11-24 18:38:29 -08:00
|
|
|
use Cake\Datasource\Exception\RecordNotFoundException;
|
|
|
|
|
use Cake\Http\Response;
|
|
|
|
|
use Cake\Log\Log;
|
2025-03-28 02:01:53 -07:00
|
|
|
use Cake\ORM\Table;
|
|
|
|
|
use Cake\ORM\TableRegistry;
|
2024-11-24 18:38:29 -08:00
|
|
|
use CakeProducts\Controller\AppController;
|
2025-03-29 00:52:38 -07:00
|
|
|
use CakeProducts\Controller\Traits\OverrideTableTrait;
|
2024-11-24 18:38:29 -08:00
|
|
|
use CakeProducts\Model\Table\ProductCatalogsTable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ProductCatalogs Controller
|
|
|
|
|
*
|
|
|
|
|
* @property ProductCatalogsTable $ProductCatalogs
|
|
|
|
|
*/
|
|
|
|
|
class ProductCatalogsController extends AppController
|
|
|
|
|
{
|
2025-03-29 00:52:38 -07:00
|
|
|
use OverrideTableTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function initialize(): void
|
|
|
|
|
{
|
|
|
|
|
parent::initialize(); // TODO: Change the autogenerated stub
|
|
|
|
|
$this->_defaultTable = 'CakeProducts.ProductCatalogs';
|
|
|
|
|
$this->_tableConfigKey = 'CakeProducts.ProductCatalogs.table';
|
|
|
|
|
}
|
2025-03-28 02:01:53 -07:00
|
|
|
|
2024-11-24 18:38:29 -08:00
|
|
|
/**
|
|
|
|
|
* Index method
|
|
|
|
|
*
|
|
|
|
|
* @return Response|null|void Renders view
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2025-03-28 23:34:21 -07:00
|
|
|
$query = $this->getTable()->find();
|
2024-11-24 18:38:29 -08:00
|
|
|
$productCatalogs = $this->paginate($query);
|
|
|
|
|
|
|
|
|
|
$this->set(compact('productCatalogs'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* View method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Catalog id.
|
|
|
|
|
* @return Response|null|void Renders view
|
|
|
|
|
* @throws RecordNotFoundException When record not found.
|
|
|
|
|
*/
|
2025-03-27 01:11:41 -07:00
|
|
|
public function view($id = null)
|
2024-11-24 18:38:29 -08:00
|
|
|
{
|
2025-03-27 01:11:41 -07:00
|
|
|
$contain = ['ProductCategories'];
|
|
|
|
|
if (Configure::read('CakeProducts.internal.syncExternally', false)) {
|
|
|
|
|
$contain[] = 'ExternalProductCatalogs';
|
|
|
|
|
}
|
2025-03-28 23:34:21 -07:00
|
|
|
$productCatalog = $this->getTable()->get($id, contain: $contain);
|
2024-11-24 18:38:29 -08:00
|
|
|
$this->set(compact('productCatalog'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add method
|
|
|
|
|
*
|
|
|
|
|
* @return Response|null|void Redirects on successful add, renders view otherwise.
|
|
|
|
|
*/
|
|
|
|
|
public function add()
|
|
|
|
|
{
|
2025-03-28 23:34:21 -07:00
|
|
|
$productCatalogsTable = $this->getTable();
|
2025-03-28 02:01:53 -07:00
|
|
|
$productCatalog = $productCatalogsTable->newEmptyEntity();
|
2024-11-24 18:38:29 -08:00
|
|
|
if ($this->request->is('post')) {
|
2025-03-28 02:01:53 -07:00
|
|
|
$productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData());
|
|
|
|
|
if ($productCatalogsTable->save($productCatalog)) {
|
2024-11-24 18:38:29 -08:00
|
|
|
$this->Flash->success(__('The product catalog has been saved.'));
|
|
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
|
|
|
|
Log::debug('failed to save new product catalog errors next');
|
|
|
|
|
Log::debug(print_r('$productCatalog->getErrors()', true));
|
|
|
|
|
Log::debug(print_r($productCatalog->getErrors(), true));
|
|
|
|
|
|
|
|
|
|
$this->Flash->error(__('The product catalog could not be saved. Please, try again.'));
|
|
|
|
|
}
|
|
|
|
|
$this->set(compact('productCatalog'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Edit method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Catalog id.
|
|
|
|
|
* @return Response|null|void Redirects on successful edit, renders view otherwise.
|
|
|
|
|
* @throws RecordNotFoundException When record not found.
|
|
|
|
|
*/
|
|
|
|
|
public function edit($id = null)
|
|
|
|
|
{
|
2025-03-28 23:34:21 -07:00
|
|
|
$productCatalogsTable = $this->getTable();
|
2025-03-28 02:01:53 -07:00
|
|
|
$productCatalog = $productCatalogsTable->get($id, contain: []);
|
2024-11-24 18:38:29 -08:00
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
2025-03-28 02:01:53 -07:00
|
|
|
$productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData());
|
|
|
|
|
if ($productCatalogsTable->save($productCatalog)) {
|
2024-11-24 18:38:29 -08:00
|
|
|
$this->Flash->success(__('The product catalog has been saved.'));
|
|
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
|
|
|
|
$this->Flash->error(__('The product catalog could not be saved. Please, try again.'));
|
|
|
|
|
}
|
|
|
|
|
$this->set(compact('productCatalog'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Catalog id.
|
|
|
|
|
* @return Response|null Redirects to index.
|
|
|
|
|
* @throws RecordNotFoundException When record not found.
|
|
|
|
|
*/
|
|
|
|
|
public function delete($id = null)
|
|
|
|
|
{
|
|
|
|
|
$this->request->allowMethod(['post', 'delete']);
|
2025-03-28 23:34:21 -07:00
|
|
|
$productCatalogsTable = $this->getTable();
|
2025-03-28 02:01:53 -07:00
|
|
|
$productCatalog = $productCatalogsTable->get($id);
|
|
|
|
|
if ($productCatalogsTable->delete($productCatalog)) {
|
2024-11-24 18:38:29 -08:00
|
|
|
$this->Flash->success(__('The product catalog has been deleted.'));
|
|
|
|
|
} else {
|
|
|
|
|
$this->Flash->error(__('The product catalog could not be deleted. Please, try again.'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
2025-03-28 02:01:53 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the users table instance
|
|
|
|
|
*
|
|
|
|
|
* @return Table
|
|
|
|
|
*/
|
2025-03-28 23:34:21 -07:00
|
|
|
public function getTable()
|
2025-03-28 02:01:53 -07:00
|
|
|
{
|
2025-03-28 23:34:21 -07:00
|
|
|
if ($this->_table instanceof Table) {
|
|
|
|
|
return $this->_table;
|
2025-03-28 02:01:53 -07:00
|
|
|
}
|
2025-03-28 23:34:21 -07:00
|
|
|
$this->_table = TableRegistry::getTableLocator()->get(
|
2025-03-28 02:17:28 -07:00
|
|
|
Configure::read('CakeProducts.ProductCatalogs.table', 'CakeProducts.ProductCatalogs')
|
|
|
|
|
);
|
2025-03-28 02:01:53 -07:00
|
|
|
|
2025-03-28 23:34:21 -07:00
|
|
|
return $this->_table;
|
2025-03-28 02:01:53 -07:00
|
|
|
}
|
2024-11-24 18:38:29 -08:00
|
|
|
}
|