Files
kupshop/bundles/KupShop/BonusProgramBundle/Controller/BonusProgramExchangeController.php
2025-08-02 16:30:27 +02:00

129 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\BonusProgramBundle\Controller;
use KupShop\BonusProgramBundle\Email\BonusExchangeCouponEmail;
use KupShop\BonusProgramBundle\Utils\Exchange\BonusExchangeUtil;
use KupShop\BonusProgramBundle\View\Exchange\BonusProgramCouponView;
use KupShop\BonusProgramBundle\View\Exchange\BonusProgramExchangeView;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Views\Traits\MessagesTrait;
use KupShop\KupShopBundle\Views\View;
use KupShop\OrderingBundle\Util\Discount\DiscountGenerator;
use Query\Operator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class BonusProgramExchangeController extends AbstractController
{
use MessagesTrait;
/**
* @TranslatedRoute(path="/#account#/#bonus_program#/#bonus_program_coupons#/")
*/
public function couponList(BonusProgramCouponView $view, Request $request): Response
{
if (!$this->isUserLogged()) {
return new RedirectResponse(
path('kupshop_user_login_login')
);
}
return $view->getResponse($request);
}
/**
* @Route("/_focus/bonus-program/send-coupon/")
*/
public function focusSendCoupon(Request $request, BonusExchangeCouponEmail $couponEmail): Response
{
$sent = false;
$couponId = $request->get('couponId');
if (($email = $request->get('email')) && $couponId) {
$couponEmail->setCouponId((int) $couponId);
$message = $couponEmail->getEmail();
$message['to'] = $email;
$sent = $couponEmail->sendEmail($message);
}
$view = (new View())
->setTemplate('focus/bonus-program/focus.send-coupon.tpl')
->addBodyVariables(
[
'sent' => $sent,
'couponId' => $couponId,
]
);
return $view->getResponse($request);
}
/**
* @TranslatedRoute(path="/#account#/#bonus_program#/#bonus_program_exchange#/")
*/
public function exchangeList(Request $request, BonusProgramExchangeView $view, BonusExchangeUtil $bonusExchangeUtil): Response
{
if (!$this->isUserLogged()) {
return new RedirectResponse(
path('kupshop_user_login_login')
);
}
// handle exchange action
if ($id = $request->get('id')) {
if ($bonusExchangeUtil->exchange((int) $id)) {
$this->addSuccessMessage(translate('bonusPointsExchanged', 'bonus_program'));
} else {
$this->addErrorMessage(translate('bonusPointsNotExchangedError', 'bonus_program'));
}
return new RedirectResponse(
path('kupshop_bonusprogram_bonusprogramexchange_exchangelist')
);
}
// display list view
return $view->getResponse($request);
}
/**
* @TranslatedRoute(path="/#account#/#bonus_program#/coupon/{couponId}/", requirements={"couponId": "\d+"})
*/
public function userCouponPDF(DiscountGenerator $discountGenerator, int $couponId)
{
if (!$this->isUserLogged()) {
return new RedirectResponse(
path('kupshop_user_login_login')
);
}
$userId = (int) Contexts::get(UserContext::class)->getActiveId();
$exists = sqlQueryBuilder()
->select('id')
->from('discounts_coupons')
->where(Operator::equals(['id' => $couponId, 'id_user' => $userId]))
->execute()->fetchOne();
if (!$exists) {
throw new NotFoundHttpException('Coupon not found');
}
return $discountGenerator->getPDFResponse($couponId);
}
private function isUserLogged(): bool
{
return Contexts::get(UserContext::class)->isActive();
}
}