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