2025-10-11 18:31:07 -07:00
|
|
|
<?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;
|
2025-10-17 00:25:40 -07:00
|
|
|
use Cake\ORM\Table;
|
2025-10-11 18:31:07 -07:00
|
|
|
use Cake\ORM\TableRegistry;
|
|
|
|
|
use CakeCarts\Model\Enum\CartTypeId;
|
2025-10-17 00:25:40 -07:00
|
|
|
use CakeCarts\Model\Table\CartsTable;
|
2025-10-11 18:31:07 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ShoppingCart component
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
class ShoppingCartComponent extends Component {
|
|
|
|
|
|
|
|
|
|
protected string $userIdField;
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
protected CartsTable|Table $Carts;
|
2025-10-17 00:25:40 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
/**
|
2026-01-23 20:42:17 -08:00
|
|
|
* @param array $config
|
2026-01-23 20:32:22 -08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function initialize(array $config): void {
|
|
|
|
|
parent::initialize($config); // TODO: Change the autogenerated stub
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
|
|
|
|
|
$this->Carts = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
|
|
|
|
|
}
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
/**
|
2025-10-11 18:31:07 -07:00
|
|
|
* Default configuration.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, mixed>
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
protected array $_defaultConfig = [];
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
/**
|
2026-01-23 20:42:17 -08:00
|
|
|
* @param \Cake\Event\EventInterface $event
|
2026-01-23 20:32:22 -08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function beforeFilter(EventInterface $event): void {
|
|
|
|
|
if (!$this->_isActionEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
$sessionId = $this->getSessionId();
|
|
|
|
|
$cart = $this->findExistingCartOrCreate($sessionId);
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
$this->getController()->set(compact('cart'));
|
|
|
|
|
}
|
2025-10-11 18:31:07 -07:00
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
/**
|
2025-10-17 00:25:40 -07:00
|
|
|
* @param string $cartId
|
|
|
|
|
*
|
2026-01-23 20:42:17 -08:00
|
|
|
* @return \Cake\Datasource\EntityInterface|\CakeCarts\Model\Entity\Cart
|
2025-10-17 00:25:40 -07:00
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
public function getCartForUserById(string $cartId) {
|
|
|
|
|
$identity = $this->getController()->getRequest()->getAttribute('identity');
|
|
|
|
|
$sessionId = $this->getSessionId();
|
|
|
|
|
|
|
|
|
|
$cartsQ = $this->Carts
|
|
|
|
|
->find()
|
|
|
|
|
->contain(['CartItems'])
|
|
|
|
|
->where(['Carts.id' => $cartId]);
|
|
|
|
|
if ($identity) {
|
|
|
|
|
$cartsQ->where([$this->userIdField => $identity->getIdentifier()]);
|
|
|
|
|
} else {
|
|
|
|
|
$cartsQ->where(['session_id' => $sessionId]);
|
|
|
|
|
}
|
|
|
|
|
return $cartsQ->firstOrFail();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 20:42:17 -08:00
|
|
|
/**
|
|
|
|
|
* @param string $sessionId
|
|
|
|
|
* @param int|null $cartTypeId
|
|
|
|
|
* @return EntityInterface|\CakeCarts\Model\Entity\Cart
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-23 20:32:22 -08:00
|
|
|
public function findExistingCartOrCreate(string $sessionId, int|null $cartTypeId = null) {
|
|
|
|
|
$identity = $this->getController()->getRequest()->getAttribute('identity');
|
|
|
|
|
|
|
|
|
|
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
|
|
|
|
|
|
|
|
|
|
$cart = $this->Carts
|
|
|
|
|
->findBySessionId($sessionId)
|
|
|
|
|
->contain(['CartItems'])
|
|
|
|
|
->where(['cart_type_id' => $cartTypeId])
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
|
|
|
|
|
$cart = $this->Carts->patchEntity($cart, [
|
|
|
|
|
$this->userIdField => $identity->getIdentifier(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$cart = $this->Carts->saveOrFail($cart);
|
|
|
|
|
}
|
|
|
|
|
if (!isset($cart)) {
|
|
|
|
|
$cart = $this->Carts->newEntity([
|
|
|
|
|
'cart_type_id' => $cartTypeId,
|
|
|
|
|
'session_id' => $sessionId,
|
|
|
|
|
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
|
|
|
|
|
'num_items' => 0,
|
|
|
|
|
'cart_items' => [],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$cart = $this->Carts->saveOrFail($cart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 20:42:17 -08:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
public function getUserIdField() {
|
|
|
|
|
return $this->userIdField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-11 18:31:07 -07:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
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 \Cake\Datasource\EntityInterface $cart
|
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException
|
2025-10-11 18:31:07 -07:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-11 18:31:07 -07:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2026-01-23 20:32:22 -08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 18:31:07 -07:00
|
|
|
}
|