1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Entity\Article; |
6
|
|
|
use App\Entity\Category; |
7
|
|
|
use App\Form\ArticleType; |
8
|
|
|
use App\Repository\ArticleRepository; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class ShopController extends AbstractController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \App\Repository\ArticleRepository |
20
|
|
|
*/ |
21
|
|
|
private $articleRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \App\Repository\CategoryRepository |
25
|
|
|
*/ |
26
|
|
|
private $categoryRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \App\Repository\Object2categoryRepository |
30
|
|
|
*/ |
31
|
|
|
private $object2categoryRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \App\Repository\ArticleRepository $articleRepository |
35
|
|
|
* @param \App\Repository\CategoryRepository $categoryRepository |
36
|
|
|
* @param \App\Repository\Object2categoryRepository $object2categoryRepository |
37
|
|
|
*/ |
38
|
|
|
public function __construct(\App\Repository\ArticleRepository $articleRepository, \App\Repository\CategoryRepository $categoryRepository, \App\Repository\Object2categoryRepository $object2categoryRepository) |
39
|
|
|
{ |
40
|
|
|
$this->articleRepository = $articleRepository; |
41
|
|
|
$this->categoryRepository = $categoryRepository; |
42
|
|
|
$this->object2categoryRepository = $object2categoryRepository; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @Route("/", name="home", methods={"GET"}) |
48
|
|
|
*/ |
49
|
|
|
public function home(): Response |
50
|
|
|
{ |
51
|
|
|
$catsIdsDbResult = $this->object2categoryRepository |
52
|
|
|
->createQueryBuilder('o') |
53
|
|
|
->select('o.catnid') |
54
|
|
|
->distinct() |
55
|
|
|
->getQuery() |
56
|
|
|
->getResult(); |
57
|
|
|
|
58
|
|
|
$catsIds = array_column($catsIdsDbResult, 'catnid'); |
59
|
|
|
|
60
|
|
|
$categories = $this->categoryRepository->findBy(['id' => $catsIds]); |
61
|
|
|
|
62
|
|
|
return $this->render('shop/home.html.twig', [ |
63
|
|
|
'categories' => $categories, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @Route("/category/{id}", name="category", methods={"GET"}) |
70
|
|
|
*/ |
71
|
|
|
public function category(Category $category): Response |
72
|
|
|
{ |
73
|
|
|
$articlesIdsDbResult = $this->object2categoryRepository |
74
|
|
|
->createQueryBuilder('o') |
75
|
|
|
->select('o.objectid') |
76
|
|
|
->where('o.catnid = :catnid') |
77
|
|
|
->setParameter('catnid', $category->getId()) |
78
|
|
|
->getQuery() |
79
|
|
|
->getResult(); |
80
|
|
|
|
81
|
|
|
$articlesIds = array_column($articlesIdsDbResult, 'objectid'); |
82
|
|
|
|
83
|
|
|
$articles = $this->articleRepository->findBy(['id' => $articlesIds]); |
84
|
|
|
|
85
|
|
|
return $this->render('shop/category.html.twig', [ |
86
|
|
|
'articles' => $articles, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @Route("/article/{id}", name="article", methods={"GET"}) |
92
|
|
|
*/ |
93
|
|
|
public function article(Article $article): Response |
94
|
|
|
{ |
95
|
|
|
return $this->render('shop/article.html.twig', [ |
96
|
|
|
'article' => $article, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|