|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Article; |
|
6
|
|
|
use AppBundle\Form\Type\ArticleType; |
|
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class UserController |
|
16
|
|
|
* @package AppBundle\Controller\Admin |
|
17
|
|
|
* @Route("/admin") |
|
18
|
|
|
*/ |
|
19
|
|
|
class ArticleController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param Request $request |
|
23
|
|
|
* @param $page |
|
24
|
|
|
* @Method({"GET", "POST"}) |
|
25
|
|
|
* @Route("/articles/{pager}/{page}", name="articlesAdmin", |
|
26
|
|
|
* defaults={"pager": "page", "page": 1}, |
|
27
|
|
|
* requirements={ |
|
28
|
|
|
* "pager": "page", |
|
29
|
|
|
* "page": "[1-9]\d*", |
|
30
|
|
|
* }) |
|
31
|
|
|
* @Template("AppBundle:admin:articles.html.twig") |
|
32
|
|
|
* |
|
33
|
|
|
* @return Response |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
public function roleAction(Request $request, $page = 1) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
38
|
|
|
$articles = $em->getRepository("AppBundle:Article") |
|
39
|
|
|
->getArticlesWithCountComment($page); |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
|
|
'articles' => $articles, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $id |
|
48
|
|
|
* @param $action |
|
49
|
|
|
* @param Request $request |
|
50
|
|
|
* @Route("/article/{action}/{id}", name="articleEdit", |
|
51
|
|
|
* defaults={"id": 0}, |
|
52
|
|
|
* requirements={ |
|
53
|
|
|
* "action": "new|edit", |
|
54
|
|
|
* "id": "\d+" |
|
55
|
|
|
* }) |
|
56
|
|
|
* @Method({"GET", "POST"}) |
|
57
|
|
|
* @Template("AppBundle:admin/form:article.html.twig") |
|
58
|
|
|
* |
|
59
|
|
|
* @return Response |
|
60
|
|
|
*/ |
|
61
|
|
|
public function editArticleAction($id, $action, Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
64
|
|
|
if ($action == "edit") { |
|
65
|
|
|
$article = $em->getRepository('AppBundle:Article') |
|
66
|
|
|
->find($id); |
|
67
|
|
|
$title = 'Edit article id: '.$id; |
|
68
|
|
|
} |
|
69
|
|
|
else { |
|
70
|
|
|
$article = new Article(); |
|
71
|
|
|
$title = 'Create new article'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$form = $this->createForm(ArticleType::class, $article, [ |
|
75
|
|
|
'em' => $em, |
|
76
|
|
|
'action' => $this->generateUrl('articleEdit', ['action' => $action, 'id' => $id]), |
|
77
|
|
|
'method' => Request::METHOD_POST, |
|
78
|
|
|
]) |
|
79
|
|
|
->add('save', SubmitType::class, array('label' => 'Save')); |
|
80
|
|
|
|
|
81
|
|
|
if ($request->getMethod() == 'POST') { |
|
82
|
|
|
$form->handleRequest($request); |
|
83
|
|
|
if ($form->isValid()) { |
|
84
|
|
|
$em->persist($article); |
|
85
|
|
|
$em->flush(); |
|
86
|
|
|
|
|
87
|
|
|
return $this->redirectToRoute('articlesAdmin'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return [ |
|
92
|
|
|
'title' => $title, |
|
93
|
|
|
'form' => $form->createView(), |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $id |
|
99
|
|
|
* @param Request $request |
|
100
|
|
|
* @Route("/article/delete/{id}", name="articleDelete", |
|
101
|
|
|
* requirements={ |
|
102
|
|
|
* "id": "\d+" |
|
103
|
|
|
* }) |
|
104
|
|
|
* @Method({"GET", "POST"}) |
|
105
|
|
|
* @Template("AppBundle:admin/form:delete.html.twig") |
|
106
|
|
|
* |
|
107
|
|
|
* @return Response |
|
108
|
|
|
*/ |
|
109
|
|
|
public function deleteArticleAction($id, Request $request) |
|
110
|
|
|
{ |
|
111
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
112
|
|
|
$article = $em->getRepository('AppBundle:Article') |
|
113
|
|
|
->find($id); |
|
114
|
|
|
$message = 'You want to delete article "' . $article->getTitle() . '" (id: ' . $id . '). '; |
|
115
|
|
|
$message .= 'Related records will be deleted: comments (count: ' . count($article->getComments()) . '). '; |
|
116
|
|
|
$message .= 'Are you sure, you want to continue?'; |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$form = $this->createFormBuilder($article) |
|
120
|
|
|
->setAction($this->generateUrl('articleDelete', ['id' => $id])) |
|
121
|
|
|
->setMethod('POST') |
|
122
|
|
|
->add('delete', SubmitType::class, array( |
|
123
|
|
|
'label' => 'Continue', |
|
124
|
|
|
'attr' => [ |
|
125
|
|
|
'class' => 'btn btn-default' |
|
126
|
|
|
], |
|
127
|
|
|
) |
|
128
|
|
|
) |
|
129
|
|
|
->getForm(); |
|
130
|
|
|
|
|
131
|
|
|
if ($request->getMethod() == 'POST') { |
|
132
|
|
|
$form->handleRequest($request); |
|
133
|
|
|
if ($form->isValid()) { |
|
134
|
|
|
$em->remove($article); |
|
135
|
|
|
$em->flush(); |
|
136
|
|
|
|
|
137
|
|
|
return $this->redirectToRoute('articlesAdmin'); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return [ |
|
142
|
|
|
'message' => $message, |
|
143
|
|
|
'form' => $form->createView(), |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.