254 lines
9.3 KiB
PHP
254 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace KupShop\ShoppingListBundle\Controller;
|
|
|
|
use KupShop\GraphQLBundle\EventListener\JsShopRefreshListener;
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\KupShopBundle\Routing\TranslatedRoute;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\ShoppingListBundle\Util\ShoppingListUtil;
|
|
use KupShop\ShoppingListBundle\View\ShoppingListCreateView;
|
|
use KupShop\ShoppingListBundle\View\ShoppingListProductsView;
|
|
use KupShop\ShoppingListBundle\View\ShoppingListSharedView;
|
|
use KupShop\ShoppingListBundle\View\ShoppingListView;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class ShoppingListController extends AbstractController
|
|
{
|
|
public function __construct(protected readonly ShoppingListUtil $shoppingListUtil)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#")
|
|
*/
|
|
public function shoppingListAction(Request $request, ShoppingListView $view)
|
|
{
|
|
if (!\User::getCurrentUser()) {
|
|
addUserMessage(translate('need_log_in', 'shopping_list'), 'danger');
|
|
|
|
return new RedirectResponse(
|
|
path('kupshop_user_login_login', ['url' => urlencode($request->getUri())])
|
|
);
|
|
}
|
|
|
|
return $view->getResponse($request);
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/odebrat/{id}")
|
|
*/
|
|
public function removeAction($id)
|
|
{
|
|
if (!$this->shoppingListUtil->isUserOwner($id)) {
|
|
throw new NotFoundHttpException(translate('delete_permissions', 'shopping_list'));
|
|
}
|
|
|
|
$this->shoppingListUtil->deleteShoppingList($id);
|
|
addUserMessage(translate('list_delete', 'shopping_list'), 'success');
|
|
|
|
return new RedirectResponse(path('kupshop_shoppinglist_shoppinglist_shoppinglist'));
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/odebrat-produkt/{id}/")
|
|
*/
|
|
public function removeProductAction(Request $request, $id)
|
|
{
|
|
$idSl = $this->shoppingListUtil->getProductlistIdForProduct($id);
|
|
if (!$this->shoppingListUtil->isUserOwner($idSl)) {
|
|
throw new NotFoundHttpException(translate('delete_permissions', 'shopping_list'));
|
|
}
|
|
|
|
$this->shoppingListUtil->removeProductFromList($id);
|
|
addUserMessage(translate('list_product_delete', 'shopping_list'), 'success');
|
|
|
|
if (findModule(\Modules::JS_SHOP) && $this->shoppingListUtil->getShoppingListLabel($idSl) === 'favorites') {
|
|
$request->getSession()->set(JsShopRefreshListener::SESSION_NAME, true);
|
|
}
|
|
|
|
return new RedirectResponse($this->getNextUrl($request));
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/vytvorit/")
|
|
*
|
|
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
|
*/
|
|
public function createShoppingList(Request $request, ShoppingListCreateView $view)
|
|
{
|
|
if ($request->request->get('newListName')) {
|
|
$listId = $this->shoppingListUtil->createShoppingList($request->request->get('newListName'));
|
|
|
|
return new RedirectResponse(
|
|
path('kupshop_shoppinglist_shoppinglist_showshoppinglistproducts', ['listId' => $listId])
|
|
);
|
|
}
|
|
|
|
addUserMessage(translate('enter_name', 'shopping_list'), 'danger');
|
|
|
|
return $view->getResponse($request);
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/{listId}/", requirements={"listId":"[0-9]+"})
|
|
*/
|
|
public function showShoppingListProducts(ShoppingListProductsView $view, $listId, Request $request)
|
|
{
|
|
if (!$this->shoppingListUtil->isUserOwner($listId)) {
|
|
throw new NotFoundHttpException(translate('not_permission', 'shopping_list'));
|
|
}
|
|
|
|
$view->setIdList($listId);
|
|
|
|
return $view->getResponse($request);
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/{listId}/{hash}", requirements={"listId":"[0-9]+", "hash":".*"})
|
|
*/
|
|
public function showSharedShoppingListProducts(ShoppingListSharedView $view, $listId, $hash, Request $request)
|
|
{
|
|
if (!($this->shoppingListUtil->validHash($listId, $hash) || $this->shoppingListUtil->isUserOwner($listId))) {
|
|
addUserMessage(translate('not_permission', 'shopping_list'), 'danger');
|
|
throw new NotFoundHttpException('Shopping list not found');
|
|
}
|
|
|
|
$view->setIdList($listId);
|
|
|
|
return $view->getResponse($request);
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/pridat/")
|
|
*/
|
|
public function addProductToShoppingList(Request $request)
|
|
{
|
|
$listId = $request->get('shoppingListId');
|
|
if (!$listId && $request->get('newListName')) {
|
|
$listId = $this->shoppingListUtil->createShoppingList($request->request->get('newListName'));
|
|
} elseif (!$listId) {
|
|
addUserMessage(translate('select_shopping_list', 'shopping_list'), 'danger');
|
|
|
|
return new RedirectResponse(
|
|
path('kupshop_shoppinglist_shoppinglist_shoppinglist')
|
|
);
|
|
}
|
|
|
|
if (!$this->shoppingListUtil->isUserOwner($listId)) {
|
|
addUserMessage(translate('not_permission', 'shopping_list'), 'danger');
|
|
|
|
return new RedirectResponse(
|
|
path('kupshop_shoppinglist_shoppinglist_shoppinglist')
|
|
);
|
|
}
|
|
|
|
$this->shoppingListUtil->addToShoppingList($listId, $request->get('products') ?? []);
|
|
addUserMessage(translate('add_to_shopping_list', 'shopping_list'), 'success');
|
|
|
|
if ($request->get('addFromCart')) {
|
|
addUserMessage(translate('copy_to_shopping_list', 'shopping_list'), 'success');
|
|
|
|
$this->shoppingListUtil->addItemsFromCart($listId);
|
|
}
|
|
|
|
return new RedirectResponse($this->getNextUrl($request));
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/do-kosiku/{listId}", requirements={"listId":"[0-9]+"})
|
|
*/
|
|
public function addEntireListToCart($listId, ShoppingListUtil $shoppingListUtil)
|
|
{
|
|
foreach ($this->shoppingListUtil->fetchItems($listId)->getProducts() as $item) {
|
|
$cartItem = [
|
|
'id_product' => $item->id,
|
|
'id_variation' => $item->variationId ?? null,
|
|
'pieces' => $item['pieces'],
|
|
];
|
|
$shoppingListUtil->addItemToCart($cartItem);
|
|
}
|
|
addUserMessage(translate('added_items_to_cart', 'shopping_list'), 'success');
|
|
|
|
return new RedirectResponse(path('kupshop_shoppinglist_shoppinglist_shoppinglist'));
|
|
}
|
|
|
|
/**
|
|
* @TranslatedRoute("/#shopping-list#/do-kosiku/")
|
|
*/
|
|
public function addShoppingListToCart(Request $request)
|
|
{
|
|
$slProducts = $request->get('products');
|
|
|
|
if ($request->get('updateCount') && is_iterable($slProducts)) {
|
|
$this->shoppingListUtil->updateShoppingList($slProducts);
|
|
addUserMessage(translate('update_shopping_list', 'shopping_list'), 'success');
|
|
} elseif ($request->get('addProduct')) {
|
|
$pieces = $slProducts[$request->get('addProduct')] ?? null;
|
|
$this->shoppingListUtil->addItemsToCart([intval($request->get('addProduct')) => $pieces]);
|
|
addUserMessage(translate('added_items_to_cart', 'shopping_list'), 'success');
|
|
} elseif ($request->get('addAllProducts') && is_iterable($slProducts)) {
|
|
$this->shoppingListUtil->addItemsToCart($slProducts);
|
|
addUserMessage(translate('added_items_to_cart', 'shopping_list'), 'success');
|
|
}
|
|
|
|
return new RedirectResponse($this->getNextUrl($request));
|
|
}
|
|
|
|
#[TranslatedRoute(path: '/#shopping-list#/prejmenovat/{listId}')]
|
|
public function renameShoppingList(Request $request, $listId)
|
|
{
|
|
if (!$this->shoppingListUtil->isUserOwner($listId)) {
|
|
throw new NotFoundHttpException('Shopping list not found');
|
|
}
|
|
$name = $request->get('name');
|
|
$from = $request->get('from');
|
|
|
|
if (!empty($name)) {
|
|
$this->shoppingListUtil->renameShoppingList($listId, $name);
|
|
}
|
|
|
|
if ($from == 'detail') {
|
|
return new RedirectResponse(path('kupshop_shoppinglist_shoppinglist_showshoppinglistproducts', ['listId' => $listId]));
|
|
} else {
|
|
return new RedirectResponse(path('kupshop_shoppinglist_shoppinglist_shoppinglist'));
|
|
}
|
|
}
|
|
|
|
#[TranslatedRoute(path: '/#shopping-list#/sdilet/')]
|
|
public function shareShoppingList(Request $request): RedirectResponse
|
|
{
|
|
$email = $request->get('email');
|
|
$listId = $request->get('listId');
|
|
|
|
if (!$nameFrom = $request->get('nameFrom')) {
|
|
$userContext = Contexts::get(UserContext::class);
|
|
$user = $userContext->getActive();
|
|
|
|
$nameFrom = $user->name.' '.$user->surname ?? '';
|
|
}
|
|
|
|
if (!$this->shoppingListUtil->isUserOwner($listId)) {
|
|
throw new NotFoundHttpException('Shopping list not found');
|
|
}
|
|
|
|
$this->shoppingListUtil->shareShoppingList($listId, $email, $nameFrom);
|
|
|
|
addUserMessage(translate('shopping_list_email_sent', 'shopping_list'), 'success');
|
|
|
|
return new RedirectResponse(path('kupshop_shoppinglist_shoppinglist_shoppinglist'));
|
|
}
|
|
|
|
private function getNextUrl(Request $request)
|
|
{
|
|
if ($next = $request->query->get('NEXT')) {
|
|
return $next;
|
|
}
|
|
|
|
return $request->headers->get('referer', '/');
|
|
}
|
|
}
|