NoticeController::indexAction()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 25
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 6
nop 1
crap 56
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Controller;
11
12
use AnimeDb\Bundle\AppBundle\Entity\Notice;
13
use AnimeDb\Bundle\AppBundle\Repository\Notice as NoticeRepository;
14
use Symfony\Component\HttpFoundation\Request;
15
use AnimeDb\Bundle\CatalogBundle\Form\Type\Notice\Change as ChangeNotice;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * Notice.
20
 *
21
 * @author  Peter Gribanov <[email protected]>
22
 */
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()
126
    {
127
        return $this->getDoctrine()->getManager()->getRepository('AnimeDbAppBundle:Notice');
128
    }
129
}
130