Files
cake-products/src/Controller/ProductVariantsController.php
T
bs 7abbb8a5aa
CI / testsuite (mysql, 8.4, ) (push) Successful in 2m51s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Failing after 5m27s
CI / Coding Standard & Static Analysis (push) Failing after 2m34s
CI / testsuite (mysql, 8.2, ) (push) Successful in 6m45s
stan fixes, code style fixes - all clean
2026-08-26 01:08:03 -07:00

118 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Log\Log;
/**
* ProductVariants Controller
*
* @property \CakeProducts\Model\Table\ProductVariantsTable $ProductVariants
*/
class ProductVariantsController extends AppController {
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index() {
$query = $this->fetchTable()->find()
->contain(['ProductCategoryVariants', 'Products']);
$productVariants = $this->paginate($query);
$this->set(compact('productVariants'));
}
/**
* View method
*
* @param string|null $id Product Variant id.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Renders view
*/
public function view($id = null) {
$productVariant = $this->fetchTable()->get($id, contain: ['ProductCategoryVariants', 'Products']);
$this->set(compact('productVariant'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add($productId) {
$product = $this->fetchTable()->Products->get($productId);
$productVariant = $this->fetchTable()->newEmptyEntity();
if ($this->request->is('post')) {
$saveOptions = [];
$postData = $this->request->getData();
/**
* @var \CakeProducts\Model\Entity\ProductCategoryVariant $productCategoryVariant
*/
$productCategoryVariant = $this->fetchTable('ProductCategoryVariants')->get($this->request->getData('product_category_variant_id', '-1'));
$postData['name'] = $productCategoryVariant->name;
$postData['product_id'] = $productId;
$productVariant = $this->fetchTable()->patchEntity($productVariant, $postData);
if ($this->fetchTable()->save($productVariant)) {
$this->Flash->success(__('The product variant has been saved.'));
return $this->redirect(['action' => 'index']);
}
Log::debug(print_r('$productVariant->getErrors()', true));
Log::debug(print_r($productVariant->getErrors(), true));
$this->Flash->error(__('The product variant could not be saved. Please, try again.'));
}
$productCategoryVariants = $this->fetchTable('ProductCategoryVariants')
->find('list', limit: 200)
->where(['product_category_id' => $product->product_category_id])
->toArray();
$this->set(compact('productVariant', 'productCategoryVariants', 'product'));
}
/**
* Edit method
*
* @param string|null $id Product Variant id.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
*/
public function edit($id = null) {
$productVariant = $this->fetchTable()->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$productVariant = $this->fetchTable()->patchEntity($productVariant, $this->request->getData());
if ($this->fetchTable()->save($productVariant)) {
$this->Flash->success(__('The product variant has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product variant could not be saved. Please, try again.'));
}
$productCategoryVariants = $this->fetchTable('ProductCategoryVariants')->find('list', limit: 200)->all();
$products = $this->fetchTable('Products')->find('list', limit: 200)->all();
$this->set(compact('productVariant', 'productCategoryVariants', 'products'));
}
/**
* Delete method
*
* @param string|null $id Product Variant id.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null Redirects to index.
*/
public function delete($id = null) {
$this->request->allowMethod(['post', 'delete']);
$productVariant = $this->fetchTable()->get($id);
if ($this->fetchTable()->delete($productVariant)) {
$this->Flash->success(__('The product variant has been deleted.'));
} else {
$this->Flash->error(__('The product variant could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}