2025-04-15 09:39:35 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CakeProducts\Controller;
|
|
|
|
|
|
|
|
use Cake\Log\Log;
|
2025-09-05 09:51:43 +00:00
|
|
|
use Cake\Utility\Hash;
|
2025-04-15 09:39:35 +00:00
|
|
|
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
2025-09-05 09:51:43 +00:00
|
|
|
use function BenTools\CartesianProduct\combinations;
|
2025-04-15 09:39:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ProductSkus Controller
|
|
|
|
*
|
|
|
|
* @property \CakeProducts\Model\Table\ProductSkusTable $ProductSkus
|
|
|
|
*/
|
|
|
|
class ProductSkusController extends AppController
|
|
|
|
{
|
|
|
|
use OverrideTableTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function initialize(): void
|
|
|
|
{
|
|
|
|
parent::initialize(); // TODO: Change the autogenerated stub
|
|
|
|
// $this->_defaultTable = 'CakeProducts.ProductSkus';
|
|
|
|
// $this->_tableConfigKey = 'CakeProducts.ProductSkus.table';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index method
|
|
|
|
*
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$query = $this->ProductSkus->find()
|
|
|
|
->contain(['Products']);
|
|
|
|
$productSkus = $this->paginate($query);
|
|
|
|
|
|
|
|
$this->set(compact('productSkus'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product Skus id.
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
|
|
|
*/
|
|
|
|
public function view($id = null)
|
|
|
|
{
|
|
|
|
$productSku = $this->ProductSkus->get($id, contain: ['Products']);
|
|
|
|
$this->set(compact('productSku'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add method
|
|
|
|
*
|
|
|
|
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
|
|
|
*/
|
2025-09-05 09:51:43 +00:00
|
|
|
public function add($productId = null)
|
2025-04-15 09:39:35 +00:00
|
|
|
{
|
2025-09-05 09:51:43 +00:00
|
|
|
$toGetCartesianProductsFrom = [];
|
|
|
|
$productSkus = [];
|
|
|
|
$table = $this->getTable();
|
|
|
|
|
|
|
|
$productCategoryVariants = $table->Products->ProductCategoryVariants->find()
|
|
|
|
->contain(['ProductCategoryVariantOptions'])
|
|
|
|
->where(['product_id' => $productId])
|
|
|
|
->toArray();
|
|
|
|
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
|
|
|
|
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
|
|
|
|
foreach ($productCategoryVariants as $productCategoryVariant) {
|
|
|
|
$options = Hash::extract($productCategoryVariant->product_category_variant_options ?? [], '{n}.id');
|
|
|
|
$toGetCartesianProductsFrom[$productCategoryVariant->id] = $options;
|
|
|
|
}
|
|
|
|
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
|
|
|
|
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
|
|
|
$productSkus[$i] = $this->getTable()->newEmptyEntity();
|
|
|
|
}
|
2025-09-06 08:11:51 +00:00
|
|
|
$this->set(compact(
|
|
|
|
'productSkus',
|
|
|
|
'productCategoryVariants',
|
|
|
|
'toGetCartesianProductsFrom',
|
|
|
|
'optionMapping',
|
|
|
|
'variantNameMapping',
|
|
|
|
'numSkusToAdd'
|
|
|
|
));
|
|
|
|
|
2025-04-15 09:39:35 +00:00
|
|
|
if ($this->request->is('post')) {
|
2025-09-06 08:11:51 +00:00
|
|
|
$postedSkus = $this->request->getData();
|
2025-04-15 09:39:35 +00:00
|
|
|
$saveOptions = [
|
|
|
|
'associated' => [],
|
|
|
|
];
|
2025-09-06 08:11:51 +00:00
|
|
|
|
|
|
|
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
|
|
|
|
|
|
|
|
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
|
|
|
|
if ($postedSku['add'] ?? false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($postedSkus[$postedSkuCnt]);
|
|
|
|
if ($productSkus[$postedSkuCnt] ?? false) {
|
|
|
|
unset($productSkus[$postedSkuCnt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$productSkus || !$postedSkus) {
|
|
|
|
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$productSkus = $table->patchEntities($productSkus, $postedSkus, $saveOptions);
|
|
|
|
if ($table->saveManyOrFail($productSkus, $saveOptions)) {
|
|
|
|
$this->Flash->success(__(count($productSkus) . ' New SKUs have been saved.'));
|
2025-04-15 09:39:35 +00:00
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
2025-09-06 08:11:51 +00:00
|
|
|
|
2025-09-05 09:51:43 +00:00
|
|
|
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
|
2025-04-15 09:39:35 +00:00
|
|
|
}
|
2025-09-05 09:51:43 +00:00
|
|
|
$this->set(compact(
|
2025-09-06 08:11:51 +00:00
|
|
|
'productSkus'
|
2025-09-05 09:51:43 +00:00
|
|
|
));
|
2025-04-15 09:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product Skus 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)
|
|
|
|
{
|
|
|
|
$productSku = $this->ProductSkus->get($id, contain: []);
|
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
|
|
|
$postData = $this->request->getData();
|
|
|
|
$saveOptions = [
|
|
|
|
'associated' => [],
|
|
|
|
];
|
|
|
|
// Log::debug(print_r('$postData', true));
|
|
|
|
// Log::debug(print_r($postData, true));
|
|
|
|
// Log::debug(print_r('$saveOptions', true));
|
|
|
|
// Log::debug(print_r($saveOptions, true));
|
|
|
|
$productSku = $this->ProductSkus->patchEntity($productSku, $postData, $saveOptions);
|
|
|
|
if ($this->ProductSkus->save($productSku)) {
|
|
|
|
$this->Flash->success(__('The product skus has been saved.'));
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
|
|
|
Log::debug(print_r('$productSku->getErrors() next - failed in productSkus/edit', true));
|
|
|
|
Log::debug(print_r($productSku->getErrors(), true));
|
|
|
|
$this->Flash->error(__('The product skus could not be saved. Please, try again.'));
|
|
|
|
}
|
|
|
|
$products = $this->ProductSkus->Products->find('list', limit: 200)->all();
|
|
|
|
$this->set(compact('productSku', 'products'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete method
|
|
|
|
*
|
|
|
|
* @param string|null $id Product Skus 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']);
|
|
|
|
$productSku = $this->ProductSkus->get($id);
|
|
|
|
if ($this->ProductSkus->delete($productSku)) {
|
|
|
|
$this->Flash->success(__('The product skus has been deleted.'));
|
|
|
|
} else {
|
|
|
|
$this->Flash->error(__('The product skus could not be deleted. Please, try again.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
}
|
|
|
|
}
|