<?php
namespace App\Controller;
use App\Entity\Product;
use App\Entity\ProductBrand;
use App\Entity\ProductCategory;
use App\Entity\ProductCompany;
use App\Entity\ProductSell;
use App\Entity\ProductShop;
use App\Entity\ProductShopBrand;
use App\Entity\ProductShopVote;
use App\Entity\ProductVote;
use App\Entity\User;
use App\EntityManager\BrandManager;
use App\EntityManager\ProductManager;
use App\Form\Product\AddEvaluationType;
use App\Form\Product\AddShopBrandType;
use App\Form\Product\AddShopEvaluationType;
use App\Form\Product\BrandSearchType;
use App\Form\Product\CreateBrandType;
use App\Form\Product\CreateCompanyType;
use App\Form\Product\CreateProductType;
use App\Form\Product\CreateSellType;
use App\Form\Product\CreateShopType;
use App\Form\Product\ProductSearchType;
use App\Form\Product\ShopListSearchType;
use App\Model\SearchBrand;
use App\Model\SearchProduct;
use App\Model\SearchShopList;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/product")
*/
class ProductController extends AbstractController
{
protected $productManager;
protected $brandManager;
public function __construct(
ProductManager $productManager,
BrandManager $brandManager
) {
$this->productManager = $productManager;
$this->brandManager = $brandManager;
}
/**
* @Route("/", name="product_index", options={"expose": true})
* @Template()
*/
public function productList(Request $request)
{
$searchObject = new SearchProduct();
$searchForm = $this->createForm(
ProductSearchType::class,
$searchObject,
['method' => Request::METHOD_GET]
);
$searchForm->handleRequest($request);
$listCategories = ($searchObject->getCategid() != null) ? array_merge($this->productManager->getListCategories($searchObject), array($searchObject->getCategid())) : null;
$categsTree = $attributes = array();
if ($searchObject->getCategid() != null) {
$categsTree = array_reverse($this->productManager->getAllParentCategories($searchObject));
$attributes = $this->productManager->getAttributes($searchObject);
}
return [
'categsTree' => $categsTree,
'categories' => $this->productManager->getCategories($searchObject),
'brands' => ($listCategories != null) ? $this->brandManager->getBrands($searchObject, $listCategories) : null,
'attributes' => $attributes,
'sort' => $request->query->get('sort'),
'products' => $this->productManager->getProductList($searchObject, $listCategories),
'comments' => $this->productManager->getLastComment($searchObject),
'moderators' => $this->productManager->getModerators(),
'searchObject' => $searchObject,
'searchForm' => $searchForm->createView()
];
}
/**
* @Route("/item/{id}", name="product_view")
* @IsGranted("view", subject="product")
* @Template()
*/
public function productView(Request $request, Product $product)
{
return [
'product' => $product ?? new Product(),
'productVote' => $this->productManager->getProductVoteDetails($product),
'brandresellers' => $this->productManager->getBrandResellers($product),
'totalresellers' => $this->productManager->getTotalResellers($product),
'resellers' => $this->productManager->getResellers($product, true, 5),
'votes' => $this->productManager->getProductVotes($product),
'favorites' => $this->productManager->getProductFavorite($product),
'hasFavorite' => $this->productManager->hasFavorite($product),
'hasEvaluation' => $this->productManager->hasEvaluation($product)
];
}
/**
* @Route("/itemcreate/", name="product_create")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template("product/product_edit.html.twig")
*/
public function productCreate(Request $request)
{
$product = new Product();
$form = $this->createForm(CreateProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$this->addFlash('success', 'product.create');
return $this->redirectToRoute('product_index');
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/itemedit/{id}", name="product_edit")
* @IsGranted("edit", subject="product")
* @Template()
*/
public function productEdit(Request $request, Product $product)
{
$form = $this->createForm(CreateProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$this->addFlash('success', 'product.update');
return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/delete/{id}", name="product_delete")
* @IsGranted("delete", subject="product")
* @Template()
*/
public function productDelete(Request $request, Product $product)
{
$em = $this->getDoctrine()->getManager();
$categid = null;
if ($product->getCategory() instanceof ProductCategory) {
$categid = $product->getCategory()->getId();
}
$em->remove($product);
$em->flush();
$this->addFlash('success', 'product.delete');
if ( $categid == null ) {
return $this->redirectToRoute('product_index');
}
return $this->redirectToRoute('product_index', ['catedid' => $categid]);
}
/**
* @Route("/evaluate/{id}", name="product_evaluate")
* @IsGranted("evaluate", subject="product")
* @Template()
*/
public function evaluate(Request $request, Product $product)
{
$evaluation = $this->productManager->getEvaluation($product);
if (!$evaluation instanceof ProductVote) {
$evaluation = new ProductVote();
}
$isNew = $evaluation->getId() == null;
$form = $this->createForm(AddEvaluationType::class, $evaluation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($isNew) {
$this->productManager->addEvaluation($product, $evaluation);
$this->addFlash('success', 'product.evaluation_add');
} else {
$this->productManager->editEvaluation($evaluation);
$this->addFlash('success', 'product.evaluation_edit');
}
return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
}
return [
'product' => $product,
'evaluation' => $evaluation,
'isNew' => $isNew,
'form' => $form->createView()
];
}
/**
* @Route("/evaluation/{id}/delete", name="product_evaluate_delete")
* @IsGranted("delete", subject="evaluation")
* @Template()
*/
public function deleteEvaluation(ProductVote $evaluation)
{
$productId = $evaluation->getProduct()->getId();
$this->productManager->deleteEvaluation($evaluation);
$this->addFlash('success', 'product.evaluation_delete');
return $this->redirectToRoute('product_view', ['id' => $productId]);
}
/**
* @Route("/favorite/{id}", name="product_favorite")
* @Template()
*/
public function favorite(Product $product)
{
$added = $this->productManager->toggleFavorite($product);
$this->addFlash('success', 'product.favorite.'.($added ? 'added' : 'removed'));
return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
}
/**
* @Route("/shoplist/", name="shop_list")
* @Template()
*/
public function shopList(Request $request)
{
$searchObject = new SearchShopList();
$searchForm = $this->createForm(
ShopListSearchType::class,
$searchObject,
['method' => Request::METHOD_GET]
);
$searchForm->handleRequest($request);
return [
'shops' => $this->productManager->getShopList($searchObject),
'sort' => $request->query->get('sort'),
'form' => $searchForm->createView()
];
}
/**
* @Route("/itemlistshop/{id}", name="item_list_shop")
* @Template()
*/
public function itemListShop(Request $request, Product $product)
{
return [
'product' => $product ?? new Product(),
'totalresellers' => $this->productManager->getTotalResellers($product),
'resellers' => $this->productManager->getResellers($product)
];
}
/**
* @Route("/shop/{id}", name="shop_view")
* @Template()
*/
public function shop(Request $request, ProductShop $shop)
{
return [
'shop' => $shop,
'shopVotes' => $this->productManager->getShopVotes($shop),
'countShopVotes' => $this->productManager->getCountShopVotes($shop),
'shopBrands' => $this->productManager->getShopBrands($shop),
'hasEvaluation' => $this->productManager->hasShopEvaluation($shop)
];
}
/**
* @Route("/shop_create/", name="shop_create")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template("product/shop_edit.html.twig")
*/
public function shopCreate(Request $request)
{
$shop = new ProductShop();
$form = $this->createForm(CreateShopType::class, $shop);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($shop);
$em->flush();
$this->addFlash('success', 'shop.create');
return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
}
return [
'shop' => $shop,
'form' => $form->createView()
];
}
/**
* @Route("/shop_edit/{id}", name="shop_edit")
* @IsGranted("edit", subject="shop")
* @Template()
*/
public function shopEdit(Request $request, ProductShop $shop)
{
$form = $this->createForm(CreateShopType::class, $shop);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($shop);
$em->flush();
$this->addFlash('success', 'shop.update');
return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
}
return [
'shop' => $shop,
'form' => $form->createView()
];
}
/**
* @Route("/shop_delete/{id}", name="shop_delete")
* @IsGranted("delete", subject="shop")
* @Template()
*/
public function shopDelete(Request $request, ProductShop $shop)
{
$em = $this->getDoctrine()->getManager();
$em->remove($shop);
$em->flush();
$this->addFlash('success', 'shop.delete');
return $this->redirectToRoute('shop_list');
}
/**
* @Route("/shopevaluate/{id}", name="shop_evaluate")
* @IsGranted("evaluate", subject="shop")
* @Template()
*/
public function shopEvaluate(Request $request, ProductShop $shop)
{
$evaluation = $this->productManager->getShopEvaluation($shop);
if (!$evaluation instanceof ProductShopVote) {
$evaluation = new ProductShopVote();
}
$isNew = $evaluation->getId() == null;
$form = $this->createForm(AddShopEvaluationType::class, $evaluation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($isNew) {
$this->productManager->addShopEvaluation($shop, $evaluation);
$this->addFlash('success', 'shop.evaluation_add');
} else {
$this->productManager->editShopEvaluation($evaluation);
$this->addFlash('success', 'shop.evaluation_edit');
}
return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
}
return [
'shop' => $shop,
'evaluation' => $evaluation,
'isNew' => $isNew,
'form' => $form->createView()
];
}
/**
* @Route("/shopevaluate/{id}/delete", name="shop_evaluate_delete")
* @IsGranted("delete", subject="evaluation")
* @Template()
*/
public function shopEvaluateDelete(ProductShopVote $evaluation)
{
$shopid = $evaluation->getShop()->getId();
$this->productManager->deleteShopEvaluation($evaluation);
$this->addFlash('success', 'shop.evaluation_delete');
return $this->redirectToRoute('shop_view', ['id' => $shopid]);
}
/**
* @Route("/shop_brand_edit/{id}", name="shop_brand_edit")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function shopBrandEdit(Request $request, ProductShop $shop)
{
$shopbrand = new ProductShopBrand();
$form = $this->createForm(AddShopBrandType::class, $shopbrand);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$shopbrand->setShop($shop);
$em = $this->getDoctrine()->getManager();
$em->persist($shopbrand);
$em->flush();
$this->addFlash('success', 'shopbrand.create');
return $this->redirectToRoute('shop_brand_edit', ['id' => $shop->getId()]);
}
return [
'shop' => $shop,
'shopbrands' => (!empty($shop)) ? $this->productManager->getShopBrands($shop) : null,
'form' => $form->createView()
];
}
/**
* @Route("/shop_brand_delete/{id}", name="shop_brand_delete")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function shopBrandDelete(Request $request, ProductShopBrand $shopbrand)
{
$em = $this->getDoctrine()->getManager();
$shopid = $shopbrand->getShop()->getId();
$em->remove($shopbrand);
$em->flush();
$this->addFlash('success', 'shopbrand.delete');
return $this->redirectToRoute('shop_brand_edit', ['id' => $shopid]);
}
/**
* @Route("/brandlist/", name="brand_list")
* @Template()
*/
public function brandList(Request $request)
{
$searchObject = new SearchBrand();
$searchForm = $this->createForm(
BrandSearchType::class,
$searchObject,
['method' => Request::METHOD_GET]
);
$searchForm->handleRequest($request);
$searchProductObject = new SearchProduct();
$searchProductObject->setCategid($searchObject->getCategid());
$listCategories = ($searchObject->getCategid() != null) ? array_merge($this->productManager->getListCategories($searchProductObject), array($searchObject->getCategid())) : null;
return [
'brands' => $this->brandManager->getAllBrands($searchObject, $listCategories),
'searchObject' => $searchObject
];
}
/**
* @Route("/brand_create/", name="brand_create")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template("product/brand_edit.html.twig")
*/
public function brandCreate(Request $request)
{
$brand = new ProductBrand();
$form = $this->createForm(CreateBrandType::class, $brand);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->brandManager->createBrand($brand);
$this->addFlash('success', 'brand.create_brand');
return $this->redirectToRoute('brand_list');
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/brand_edit/{id}", name="brand_edit")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function brandEdit(Request $request, ProductBrand $brand)
{
$form = $this->createForm(CreateBrandType::class, $brand);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->brandManager->createBrand($brand);
$this->addFlash('success', 'brand.edit_brand');
if ( empty($request->query->get('companyid')) ) {
return $this->redirectToRoute('brand_list');
} else {
return $this->redirectToRoute('brand_list', ['companyid' => $request->query->get('companyid')]);
}
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/companylist/", name="company_list")
* @Template()
*/
public function companyList(Request $request)
{
return ['companies' => $this->productManager->getAllCompanies()];
}
/**
* @Route("/company/{id}", name="company_view")
* @Template()
*/
public function companyView(Request $request, ProductCompany $company)
{
return ['company' => $company];
}
/**
* @Route("/company_create/", name="company_create")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template("product/company_edit.html.twig")
*/
public function companyCreate(Request $request)
{
$company = new ProductCompany();
$form = $this->createForm(CreateCompanyType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->productManager->createCompany($company);
$this->addFlash('success', 'company.create_company');
return $this->redirectToRoute('company_list');
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/company_edit/{id}", name="company_edit")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function companyEdit(Request $request, ProductCompany $company)
{
$form = $this->createForm(CreateCompanyType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->productManager->createCompany($company);
$this->addFlash('success', 'company.edit_company');
return $this->redirectToRoute('company_view', ['id' => $company->getId()]);
}
return [
'form' => $form->createView()
];
}
/**
* @Route("/item_sell_edit/{id}", name="item_sell_edit")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function itemSellEdit(Request $request, Product $product)
{
return [
'product' => $product,
'sells' => $this->productManager->getSells($product)
];
}
/**
* @Route("/sell_create/{id}", name="sell_create")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template("product/sell_edit.html.twig")
*/
public function sellCreate(Request $request, Product $product)
{
$em = $this->getDoctrine()->getManager();
$sell = new ProductSell();
$sell->setProduct($product);
$sell->setStatus(true);
$form = $this->createForm(CreateSellType::class, $sell);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($sell);
$em->flush();
$this->addFlash('success', 'sell.create');
return $this->redirectToRoute('item_sell_edit', ['id' => $product->getId()]);
}
return [
'form' => $form->createView(),
'product' => $product
];
}
/**
* @Route("/sell_edit/{id}", name="sell_edit")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function sellEdit(Request $request, ProductSell $sell)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(CreateSellType::class, $sell);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($sell);
$em->flush();
$this->addFlash('success', 'sell.update');
return $this->redirectToRoute('item_sell_edit', ['id' => $sell->getProduct()->getId()]);
}
return [
'form' => $form->createView(),
'sell' => $sell
];
}
/**
* @Route("/sell_delete/{id}", name="sell_delete")
* @IsGranted(User::ROLE_PRODUCT_ADMIN)
* @Template()
*/
public function sellDelete(Request $request, ProductSell $sell)
{
$em = $this->getDoctrine()->getManager();
$productId = $sell->getProduct()->getId();
$em->remove($sell);
$em->flush();
$this->addFlash('success', 'sell.delete');
return $this->redirectToRoute('item_sell_edit', ['id' => $productId]);
}
}