src/EntityManager/ProductManager.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\EntityManager;
  3. use App\Entity\GalleryPic;
  4. use App\Entity\Product;
  5. use App\Entity\ProductAttribute;
  6. use App\Entity\ProductAttributeOption;
  7. use App\Entity\ProductCategory;
  8. use App\Entity\ProductCompany;
  9. use App\Entity\ProductFavorite;
  10. use App\Entity\ProductSell;
  11. use App\Entity\ProductShop;
  12. use App\Entity\ProductShopBrand;
  13. use App\Entity\ProductShopVote;
  14. use App\Entity\ProductVote;
  15. use App\Entity\User;
  16. use App\Model\SearchProduct;
  17. use App\Model\SearchShopList;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  22. use Psr\SimpleCache\CacheInterface;
  23. use Symfony\Component\Asset\Package;
  24. use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  27. use Symfony\Component\Security\Core\Security;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. class ProductManager extends AbstractEntityManager
  30. {
  31.     protected $translator;
  32.     protected $package;
  33.     protected $requestStack;
  34.     protected $imagineCacheManager;
  35.     public function __construct(
  36.         EntityManagerInterface $entityManager,
  37.         TokenStorageInterface $tokenStorage,
  38.         CacheInterface $cache,
  39.         RequestStack $requestStack,
  40.         PaginatorInterface $paginator,
  41.         Security $security,
  42.         TranslatorInterface $translator,
  43.         CacheManager $liipCache
  44.     ) {
  45.         parent::__construct($entityManager$tokenStorage$cache$requestStack$paginator$security);
  46.         $this->translator $translator;
  47.         $this->requestStack $requestStack;
  48.         $this->package = new Package(new EmptyVersionStrategy());
  49.         $this->imagineCacheManager $liipCache;
  50.     }
  51.     public function getCategories(?SearchProduct $searchObject): array
  52.     {
  53.         return $this->entityManager->getRepository(ProductCategory::class)->getCategs($searchObject);
  54.     }
  55.     public function getListCategories(SearchProduct $searchObject): array
  56.     {
  57.         return $this->entityManager->getRepository(ProductCategory::class)->getListCategs($searchObject);
  58.     }
  59.     public function getCategoryTree($categid 0$type 0$order 'name')
  60.     {
  61.         return $this->entityManager->getRepository(ProductCategory::class)->getCategTree($categid$type$order);
  62.     }
  63.     public function getAllParentCategories(SearchProduct $searchObject): array
  64.     {
  65.         return $this->entityManager->getRepository(ProductCategory::class)->getAllParentCategories($searchObject);
  66.     }
  67.     public function getAttributes(SearchProduct $searchObject): array
  68.     {
  69.         $attributes $this->entityManager->getRepository(ProductAttribute::class)->getAttributes($searchObject);
  70.         foreach ($attributes as $key => $attribute) {
  71.             $attributes[$key]->options $this->entityManager
  72.                 ->getRepository(ProductAttributeOption::class)
  73.                 ->getAttributeOptions($attribute);
  74.         }
  75.         return $attributes;
  76.     }
  77.     public function getProductList(SearchProduct $searchObject, ?array $listCategories): SlidingPagination
  78.     {
  79.         /** @var SlidingPagination $products */
  80.         $products $this->paginator->paginate(
  81.             $this->entityManager->getRepository(Product::class)->getProducts(
  82.                 $searchObject,
  83.                 $listCategories,
  84.                 $this->getUser()
  85.             ),
  86.             $this->getRequest()->query->getInt('page'1),
  87.             Product::LIMIT_PER_PAGE,
  88.             [
  89.                 PaginatorInterface::DEFAULT_SORT_FIELD_NAME => 'p.createdAt',
  90.                 PaginatorInterface::DEFAULT_SORT_DIRECTION => 'desc',
  91.                 PaginatorInterface::SORT_FIELD_WHITELIST => [
  92.                     'p.createdAt',
  93.                     'p.name',
  94.                     'p.totalFavorite',
  95.                 ],
  96.             ]
  97.         );
  98.         foreach ($products as $key => $product) {
  99.             $products[$key]->votes $this->getProductVoteDetails($product);
  100.             $products[$key]->imgs $this->getThumbPicture($product);
  101.         }
  102.         return $products;
  103.     }
  104.     public function getShopList(SearchShopList $searchObject): SlidingPagination
  105.     {
  106.         /** @var SlidingPagination $shops */
  107.         $shops $this->paginator->paginate(
  108.             $this->entityManager->getRepository(ProductShop::class)->getShops($searchObject$this->getUser()),
  109.             $this->getRequest()->query->getInt('page'1),
  110.             ProductShop::LIMIT_PER_PAGE,
  111.             [
  112.                 PaginatorInterface::DEFAULT_SORT_FIELD_NAME => 'ps.updatedAt',
  113.                 PaginatorInterface::DEFAULT_SORT_DIRECTION => 'desc',
  114.                 PaginatorInterface::SORT_FIELD_WHITELIST => [
  115.                     'ps.updatedAt',
  116.                     'ps.name',
  117.                     'totalvotes',
  118.                 ],
  119.             ]
  120.         );
  121.         return $shops;
  122.     }
  123.     public function getProductVoteDetails(Product $product)
  124.     {
  125.         $details $this->entityManager->getRepository(ProductVote::class)->getProductVoteDetails($product);
  126.         $details[0]['avgvotes'] = intval(round($details[0]['avgvotes'], 0));
  127.         return $details[0];
  128.     }
  129.     public function getProductVotes(Product $product)
  130.     {
  131.         return $this->entityManager->getRepository(ProductVote::class)->getProductVotes($product);
  132.     }
  133.     public function getProductFavorite(Product $product)
  134.     {
  135.         return $this->entityManager->getRepository(ProductFavorite::class)->getProductFavorite($product);
  136.     }
  137.     public function getBrandResellers(Product $product)
  138.     {
  139.         $brandresellers $this->entityManager->getRepository(ProductShop::class)->getBrandResellers($product);
  140.         foreach ($brandresellers as $key => $brandreseller) {
  141.             $brandresellers[$key]['avgvotes'] = intval(round($brandreseller['avgvotes'], 0));
  142.         }
  143.         return $brandresellers;
  144.     }
  145.     public function getTotalResellers(Product $product)
  146.     {
  147.         $totalresellers $this->entityManager->getRepository(ProductSell::class)->getTotalResellers($product);
  148.         return $totalresellers[0];
  149.     }
  150.     public function getResellers(Product $productbool $onlineShop false, ?int $limit null)
  151.     {
  152.         $resellers null;
  153.         $totalresellers $this->entityManager->getRepository(ProductSell::class)->getResellers(
  154.             $product,
  155.             $onlineShop,
  156.             $limit
  157.         );
  158.         foreach ($totalresellers as $key => $totalreseller) {
  159.             $resellers[$key]['data'] = $totalreseller;
  160.             $resellers[$key]['count'] = $this->getCountShopVotes($totalreseller->getShop());
  161.         }
  162.         return $resellers;
  163.     }
  164.     public function getSells(Product $product)
  165.     {
  166.         return $this->entityManager->getRepository(ProductSell::class)->findBy(['product' => $product]);
  167.     }
  168.     public function getLastComment(SearchProduct $searchObject)
  169.     {
  170.         return $this->entityManager->getRepository(ProductVote::class)->getLastComment($searchObject);
  171.     }
  172.     public function getThumbPicture(Product $product)
  173.     {
  174.         $picture null;
  175.         $pos1 strpos($product->getDescription(), '[thumb=');
  176.         if ($pos1 !== false) {
  177.             $pos1 $pos1 strlen('[thumb=');
  178.             $pos2 strpos($product->getDescription(), ']'$pos1);
  179.             $picid substr($product->getDescription(), $pos1$pos2 $pos1);
  180.             $picture $this->entityManager->getRepository(GalleryPic::class)->find($picid);
  181.         }
  182.         return $picture;
  183.     }
  184.     public function getModerators(): array
  185.     {
  186.         return $this->entityManager->getRepository(User::class)->getUsersWithRoles(
  187.             User::ROLE_PRODUCT_ADMIN
  188.         );
  189.     }
  190.     public function getAllCompanies(): array
  191.     {
  192.         return $this->entityManager->getRepository(ProductCompany::class)->getAllCompanies();
  193.     }
  194.     public function createCompany(ProductCompany $company): ProductCompany
  195.     {
  196.         $this->entityManager->persist($company);
  197.         $this->entityManager->flush();
  198.         return $company;
  199.     }
  200.     public function getShopBrands(ProductShop $productShop)
  201.     {
  202.         $shopBrands $this->entityManager->getRepository(ProductShopBrand::class)->getShopBrands(
  203.             $productShop,
  204.             $this->getUser()
  205.         );
  206.         return $shopBrands;
  207.     }
  208.     public function getCountShopVotes(ProductShop $productShop)
  209.     {
  210.         $shopVotes $this->entityManager->getRepository(ProductShopVote::class)->getCountShopVotes($productShop);
  211.         $shopVotes $shopVotes[0];
  212.         $shopVotes['avgvotes'] = intval(round($shopVotes['avgvotes'], 0));
  213.         return $shopVotes;
  214.     }
  215.     public function getShopVotes(ProductShop $productShop)
  216.     {
  217.         return $this->entityManager->getRepository(ProductShopVote::class)->getShopVotes($productShop);
  218.     }
  219.     public function toggleFavorite(Product $product): bool
  220.     {
  221.         $added true;
  222.         $isFavorite $this->entityManager->getRepository(ProductFavorite::class)
  223.             ->findOneBy(
  224.                 [
  225.                     'user' => $this->getUser(),
  226.                     'product' => $product,
  227.                 ]
  228.             );
  229.         if ($isFavorite instanceof ProductFavorite) {
  230.             $added false;
  231.             $product->setTotalFavorite($product->getTotalFavorite() - 1);
  232.             $this->entityManager->remove($isFavorite);
  233.             $this->entityManager->flush();
  234.         } else {
  235.             $favorite = (new ProductFavorite())
  236.                 ->setProduct($product)
  237.                 ->setUser($this->getUser());
  238.             $product->setTotalFavorite($product->getTotalFavorite() + 1);
  239.             $this->entityManager->persist($favorite);
  240.         }
  241.         $this->entityManager->persist($product);
  242.         $this->entityManager->flush();
  243.         return $added;
  244.     }
  245.     public function hasFavorite(Product $product): bool
  246.     {
  247.         $isFavorite $this->entityManager->getRepository(ProductFavorite::class)
  248.             ->findOneBy(
  249.                 [
  250.                     'user' => $this->getUser(),
  251.                     'product' => $product,
  252.                 ]
  253.             );
  254.         return ($isFavorite instanceof ProductFavorite);
  255.     }
  256.     public function getEvaluation(Product $product): ?ProductVote
  257.     {
  258.         return $this->entityManager->getRepository(ProductVote::class)
  259.             ->findOneBy(
  260.                 [
  261.                     'product' => $product,
  262.                     'createdBy' => $this->getUser(),
  263.                 ]
  264.             );
  265.     }
  266.     public function hasEvaluation(Product $product)
  267.     {
  268.         return $this->getEvaluation($product) instanceof ProductVote;
  269.     }
  270.     public function addEvaluation(Product $productProductVote $evaluation): void
  271.     {
  272.         $evaluation->setProduct($product);
  273.         $evaluation->setCreatedBy($this->getUser());
  274.         $this->entityManager->persist($evaluation);
  275.         $this->entityManager->flush();
  276.     }
  277.     public function editEvaluation(ProductVote $evaluation): void
  278.     {
  279. //        $this->updateScore($evaluation->getProduct());
  280.         $this->entityManager->persist($evaluation->getProduct());
  281.         $this->entityManager->persist($evaluation);
  282.         $this->entityManager->flush();
  283.     }
  284.     public function deleteEvaluation(ProductVote $evaluation)
  285.     {
  286.         $this->entityManager->remove($evaluation);
  287.         $this->entityManager->flush();
  288.     }
  289.     public function getShopEvaluation(ProductShop $shop): ?ProductShopVote
  290.     {
  291.         return $this->entityManager->getRepository(ProductShopVote::class)
  292.             ->findOneBy(
  293.                 [
  294.                     'shop' => $shop,
  295.                     'user' => $this->getUser(),
  296.                 ]
  297.             );
  298.     }
  299.     public function hasShopEvaluation(ProductShop $shop)
  300.     {
  301.         return $this->getShopEvaluation($shop) instanceof ProductShopVote;
  302.     }
  303.     public function addShopEvaluation(ProductShop $shopProductShopVote $evaluation): void
  304.     {
  305.         $evaluation->setShop($shop);
  306.         $evaluation->setUser($this->getUser());
  307.         $this->entityManager->persist($evaluation);
  308.         $this->entityManager->flush();
  309.     }
  310.     public function editShopEvaluation(ProductShopVote $evaluation): void
  311.     {
  312. //        $this->updateScore($evaluation->getShop());
  313.         $this->entityManager->persist($evaluation->getShop());
  314.         $this->entityManager->persist($evaluation);
  315.         $this->entityManager->flush();
  316.     }
  317.     public function deleteShopEvaluation(ProductShopVote $evaluation)
  318.     {
  319.         $this->entityManager->remove($evaluation);
  320.         $this->entityManager->flush();
  321.     }
  322. }