first commit splitting onto its own repo

This commit is contained in:
2025-10-11 18:31:07 -07:00
commit 48df5db468
27 changed files with 1661 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace CakeCarts\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
}

View File

@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace CakeCarts\Controller;
use Cake\Core\Configure;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Http\Client;
use Cake\Log\Log;
use CakeCarts\Controller\AppController;
/**
* CartItems Controller
*
* @property \CakeCarts\Model\Table\CartItemsTable $CartItems
* @property \Authorization\Controller\Component\AuthorizationComponent $Authorization
*/
class CartItemsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => ['add'],
]);
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$this->request->allowMethod(['post', 'put', 'patch']);
$this->Authorization->skipAuthorization();
Log::debug(print_r('$this->request->getData()', true));
Log::debug(print_r($this->request->getData(), true));
$cart = $this->viewBuilder()->getVar('cart');
$postData = $this->request->getData();
$postData['cart_id'] = $cart->id;
// get product skus with variants
$price = $this->request->getData('price');
$qty = $this->request->getData('qty', 1);
$postData['price'] = $price;
$postData['subtotal'] = isset($price) ? bcmul("$price", "$qty", 5) : null;
$newCartItem = $this->Carts->CartItems->newEntity($postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requirePricing' : 'default',
]);
if ($this->Carts->CartItems->save($newCartItem)) {
$this->Flash->success('Added to cart');
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
Log::debug(print_r('$newCartItem->getErrors()', true));
Log::debug(print_r($newCartItem->getErrors(), true));
$this->Flash->error('Failed to add to cart.');
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
/**
* Edit method
*
* @param string|null $id Cart Item 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)
{
$this->request->allowMethod(['post', 'put', 'patch']);
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
$this->Authorization->skipAuthorization();
if ($this->request->is(['patch', 'post', 'put'])) {
$postData = $this->request->getData();
$qty = $this->request->getData('qty', 1);
$price = $this->request->getData('price', null);
$subtotal = isset($price) ? bcmul("$qty", "$price", 5) : null;
$postData['subtotal'] = $subtotal;
$cartItem = $this->CartItems->patchEntity($cartItem, $postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requiresPricing' : 'default',
]);
if ($this->CartItems->save($cartItem)) {
$this->Flash->success(__('The cart item has been saved.'));
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
Log::debug(print_r('$cartItem->getErrors()', true));
Log::debug(print_r($cartItem->getErrors(), true));
$this->Flash->error(__('The cart item could not be saved. Please, try again.'));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
/**
* Delete method
*
* @param string|null $id Cart Item 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']);
$identity = $this->getRequest()->getAttribute('identity');
// $cart = $this->viewBuilder()->getVar('cart');
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
$this->Authorization->skipAuthorization();
unset($cartItem->cart);
if ($this->CartItems->delete($cartItem)) {
$this->Flash->success(__('The cart item has been deleted.'));
} else {
$this->Flash->error(__('The cart item could not be deleted. Please, try again.'));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace CakeCarts\Controller;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use CakeCarts\Model\Enum\CartTypeId;
/**
* Carts Controller
*
* @property \Authorization\Controller\Component\AuthorizationComponent $Authorization
*/
class CartsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => true,
]);
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
// use cart from beforeFilter
}
}

View File

@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);
namespace CakeCarts\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventInterface;
use Cake\ORM\TableRegistry;
use CakeCarts\Model\Entity\CartItem;
use CakeCarts\Model\Enum\CartTypeId;
/**
* ShoppingCart component
*/
class ShoppingCartComponent extends Component
{
/**
* @var string $userIdField
*/
protected string $userIdField;
public function initialize(array $config): void
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
}
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
public function beforeFilter(EventInterface $event): void
{
if (!$this->_isActionEnabled()) {
return;
}
$sessionId = $this->getSessionId();
$cart = $this->findExistingCartOrCreate($sessionId);
$this->getController()->set(compact('cart'));
}
public function findExistingCartOrCreate(string $sessionId, int $cartTypeId = null)
{
$cartsTable = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
$userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'integer' ? 'user_id' : 'user_id_uuid';
$identity = $this->getController()->getRequest()->getAttribute('identity');
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
$cart = $cartsTable
->findBySessionId($sessionId)
->contain(['CartItems'])
->where(['cart_type_id' => $cartTypeId])
->first();
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
$cart = $cartsTable->patchEntity([
$this->userIdField => $identity->getIdentifier(),
]);
$cart = $cartsTable->saveOrFail($cart);
}
if (!isset($cart)) {
$cart = $cartsTable->newEntity([
'cart_type_id' => $cartTypeId,
'session_id' => $sessionId,
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
'num_items' => 0,
'cart_items' => [],
]);
$cart = $cartsTable->saveOrFail($cart);
}
return $cart;
}
public function getUserIdField()
{
return $this->userIdField;
}
/**
* @return string
*/
public function getSessionId(): string
{
if (!$this->getController()->getRequest()->getSession()->started()) {
$this->getController()->getRequest()->getSession()->start();
}
if (!$this->getController()->getRequest()->getSession()->check('CakeCarts.session_id')) {
$this->getController()->getRequest()->getSession()->write('CakeCarts.session_id', $this->getController()->getRequest()->getSession()->id());
}
return $this->getController()->getRequest()->getSession()->read('CakeCarts.session_id');
}
/**
* @param EntityInterface $cart
* @throws RecordNotFoundException
*
* @return void
*/
public function checkIfIsOwnCart(EntityInterface $cart): void
{
$identity = $this->getController()->getRequest()->getAttribute('identity');
if (!isset($identity) && isset($cart->session_id) && ($cart->session_id != $this->getSessionId())) {
throw new RecordNotFoundException();
}
if (isset($identity) && $identity->getIdentifier() != $cart->get($this->getUserIdField())) {
throw new RecordNotFoundException();
}
}
/**
* @return bool
*/
protected function _isActionEnabled(): bool
{
$actions = $this->getConfig('actions');
if (is_bool($actions)) {
return $actions;
}
return in_array($this->getController()->getRequest()->getParam('action'), (array)$actions, true);
}
}