1 | <?php |
||
23 | class NoticeController extends BaseController |
||
24 | { |
||
25 | /** |
||
26 | * Number of notices per page. |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | const NOTICE_PER_PAGE = 30; |
||
31 | |||
32 | /** |
||
33 | * Edit list notices. |
||
34 | * |
||
35 | * @param Request $request |
||
36 | * |
||
37 | * @return Response |
||
38 | */ |
||
39 | public function indexAction(Request $request) |
||
40 | { |
||
41 | $response = $this->getCacheTimeKeeper()->getResponse('AnimeDbAppBundle:Notice'); |
||
42 | // response was not modified for this request |
||
43 | if ($response->isNotModified($request)) { |
||
44 | return $response; |
||
45 | } |
||
46 | |||
47 | $rep = $this->getRepository(); |
||
48 | $change_form = $this->createForm(new ChangeNotice())->handleRequest($request); |
||
49 | if ($change_form->isValid() && ($notices = $change_form->getData()['notices'])) { |
||
50 | switch ($change_form->getData()['action']) { |
||
51 | case ChangeNotice::ACTION_SET_STATUS_SHOWN: |
||
52 | $rep->setStatus($notices->toArray(), Notice::STATUS_SHOWN); |
||
53 | break; |
||
54 | case ChangeNotice::ACTION_SET_STATUS_CLOSED: |
||
55 | $rep->setStatus($notices->toArray(), Notice::STATUS_CLOSED); |
||
56 | break; |
||
57 | case ChangeNotice::ACTION_REMOVE: |
||
58 | $rep->remove($notices->toArray()); |
||
59 | } |
||
60 | |||
61 | return $this->redirect($this->generateUrl('notice_list')); |
||
62 | } |
||
63 | |||
64 | return $this->render('AnimeDbCatalogBundle:Notice:index.html.twig', [ |
||
65 | 'has_notices' => $rep->count(), |
||
66 | ], $response); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get notice list. |
||
71 | * |
||
72 | * @param Request $request |
||
73 | * |
||
74 | * @return Response |
||
75 | */ |
||
76 | public function listAction(Request $request) |
||
77 | { |
||
78 | $current_page = $request->get('page', 1); |
||
79 | $current_page = $current_page > 1 ? $current_page : 1; |
||
80 | $rep = $this->getRepository(); |
||
81 | |||
82 | // filter list notice |
||
83 | $filter = $this->createForm('notices_filter')->handleRequest($request); |
||
84 | if ($filter->isValid()) { |
||
85 | $query = $rep->getFilteredQuery($filter->getData()['status'], $filter->getData()['type']); |
||
86 | } else { |
||
87 | $query = $rep->createQueryBuilder('n'); |
||
88 | } |
||
89 | $query |
||
90 | ->orderBy('n.date_created', 'DESC') |
||
91 | ->setFirstResult(($current_page - 1) * self::NOTICE_PER_PAGE) |
||
92 | ->setMaxResults(self::NOTICE_PER_PAGE); |
||
93 | $list = $query->getQuery()->getResult(); |
||
94 | |||
95 | // get count all items |
||
96 | $count = $query |
||
97 | ->select('COUNT(n)') |
||
98 | ->getQuery() |
||
99 | ->getSingleScalarResult(); |
||
100 | |||
101 | // pagination |
||
102 | $that = $this; |
||
103 | $request_query = $request->query->all(); |
||
104 | unset($request_query['page']); |
||
105 | $pagination = $this->get('anime_db.pagination') |
||
106 | ->create(ceil($count / self::NOTICE_PER_PAGE), $current_page) |
||
107 | ->setPageLink(function ($page) use ($that, $request_query) { |
||
108 | return $that->generateUrl('notice_list', array_merge($request_query, ['page' => $page])); |
||
109 | }) |
||
110 | ->setFirstPageLink($this->generateUrl('notice_list', $request_query)) |
||
111 | ->getView(); |
||
112 | |||
113 | return $this->render('AnimeDbCatalogBundle:Notice:list.html.twig', [ |
||
114 | 'list' => $list, |
||
115 | 'pagination' => $pagination, |
||
116 | 'change_form' => $this->createForm(new ChangeNotice())->createView(), |
||
117 | 'filter' => $filter->createView(), |
||
118 | 'action_remove' => ChangeNotice::ACTION_REMOVE, |
||
119 | ]); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return NoticeRepository |
||
124 | */ |
||
125 | protected function getRepository() |
||
129 | } |
||
130 |