redundant require statements, clean up install trait, standardize trait names, remove hard-coded php version require for composer.json
63 lines
2.2 KiB
PHP
63 lines
2.2 KiB
PHP
<?php
|
|
|
|
use Websmid\WsOrderReference\Entity\OrderReference;
|
|
use Websmid\WsOrderReference\Repository\OrderReferenceRepository;
|
|
|
|
if (file_exists(__DIR__ . '../../vendor/autoload.php')) {
|
|
require_once __DIR__ . '../../vendor/autoload.php';
|
|
}
|
|
|
|
class ws_orderreferenceAjaxModuleFrontController extends ModuleFrontController
|
|
{
|
|
public function initContent()
|
|
{
|
|
$orderReference = Tools::getValue('order_reference_input');
|
|
$idCart = $this->context->cart->id;
|
|
|
|
if (!$idCart) {
|
|
$this->ajaxRender(json_encode([
|
|
'success' => false,
|
|
'message' => $this->module->l('No cart found.', 'ajax')
|
|
]));
|
|
return;
|
|
}
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9_\- ]+$/', $orderReference)) {
|
|
$this->ajaxRender(json_encode([
|
|
'success' => false,
|
|
'message' => $this->module->l('Please enter a valid order reference. Characters allowed: a-z, 0-9, -, _ and space', 'ajax')
|
|
]));
|
|
return;
|
|
}
|
|
|
|
try {
|
|
/** @var \Doctrine\ORM\EntityManagerInterface $entityManager */
|
|
$entityManager = $this->get('doctrine.orm.default_entity_manager');
|
|
|
|
/** @var OrderReferenceRepository $repository */
|
|
$repository = $entityManager->getRepository(OrderReference::class);
|
|
|
|
$orderRef = $repository->findByCartId($idCart);
|
|
|
|
if (!$orderRef) {
|
|
$orderRef = new OrderReference();
|
|
$orderRef->setIdCart($idCart);
|
|
}
|
|
|
|
$orderRef->setOrderReference(pSQL($orderReference));
|
|
|
|
$entityManager->persist($orderRef);
|
|
$entityManager->flush();
|
|
|
|
$this->ajaxRender(json_encode([
|
|
'success' => true,
|
|
'message' => $this->module->l('Order reference received!', 'ajax')
|
|
]));
|
|
} catch (Exception $e) {
|
|
$this->ajaxRender(json_encode([
|
|
'success' => false,
|
|
'message' => $this->module->l('Could not save order reference.', 'ajax')
|
|
]));
|
|
}
|
|
}
|
|
} |