201 lines
8.0 KiB
PHP
201 lines
8.0 KiB
PHP
<?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',
|
|
'ProductVariants',
|
|
'ProductVariants.ProductCategoryVariants',
|
|
'ProductVariants.ProductCategoryVariants.ProductCategoryVariantOptions',
|
|
'ProductSkus',
|
|
'ProductPhotos',
|
|
]);
|
|
$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')) {
|
|
$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));
|
|
$productVariantsData = [];
|
|
if (isset($postData['product_variants']) && $postData['product_variants']) {
|
|
foreach ($postData['product_variants'] as $postedProductVariant) {
|
|
if (!isset($postedProductVariant['enabled']) || !$postedProductVariant['enabled'] || !isset($postedProductVariant['product_category_variant_id'])) {
|
|
continue;
|
|
}
|
|
$existingVariant = $this->Products->ProductCategories->ProductCategoryVariants->get($postedProductVariant['product_category_variant_id'], contain: ['ProductCategoryVariantOptions']);
|
|
$optionsData = [];
|
|
foreach ($existingVariant->product_category_variant_options as $existingOption) {
|
|
$optionsData[] = [
|
|
'variant_value' => $existingOption->variant_value,
|
|
'variant_label' => $existingOption->variant_label ?? null,
|
|
'enabled' => $existingOption->enabled,
|
|
];
|
|
}
|
|
$tmpVariantData = [
|
|
'name' => $existingVariant->name,
|
|
'product_category_variant_id' => $postedProductVariant['product_category_variant_id'],
|
|
'enabled' => true,
|
|
'product_category_variant_options' => $optionsData,
|
|
];
|
|
$productVariantsData[] = $tmpVariantData;
|
|
}
|
|
}
|
|
if ($productVariantsData) {
|
|
$saveOptions['fields'] = [
|
|
'name',
|
|
'product_category_id',
|
|
'product_type_id',
|
|
'product_attributes',
|
|
'product_category_variants'
|
|
];
|
|
$saveOptions['associated']['ProductCategoryVariants'] = [
|
|
'fields' => [
|
|
'name',
|
|
'enabled',
|
|
'product_category_variant_options',
|
|
]
|
|
];
|
|
$saveOptions['associated'][] = 'ProductCategoryVariants.ProductCategoryVariantOptions';
|
|
$postData['product_category_variants'] = $productVariantsData;
|
|
}
|
|
$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.'));
|
|
}
|
|
$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: [
|
|
'ProductAttributes',
|
|
'ProductAttributes.ProductCategoryAttributes',
|
|
'ProductAttributes.ProductCategoryAttributes.ProductCategoryAttributeOptions',
|
|
]);
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
|
$saveOptions = [
|
|
'associated' => ['ProductAttributes'],
|
|
];
|
|
$product = $productsTable->patchEntity($product, $this->request->getData(), $saveOptions);
|
|
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.'));
|
|
}
|
|
$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']);
|
|
}
|
|
}
|