Completed
Push — sf2.7 ( fb6965...00100f )
by Laurent
03:55
created

AbstractController::setOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace AppBundle\Controller;
10
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Doctrine\ORM\QueryBuilder;
13
14
/**
15
 * Description of AbstractController
16
 *
17
 * @author Laurent
18
 */
19
abstract class AbstractController extends Controller
20
{
21
    /**
22
     * @param string $name  session name
23
     * @param string $field field name
24
     * @param string $type  sort type ("ASC"/"DESC")
25
     */
26
    protected function setOrder($name, $field, $type = 'ASC')
27
    {
28
        $this->getRequest()->getSession()->set('sort.' . $name, array('field' => $field, 'type' => $type));
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
29
    }
30
31
    /**
32
     * @param  string $name
33
     * @return array
34
     */
35
    protected function getOrder($name)
36
    {
37
        $session = $this->getRequest()->getSession();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
39
        return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null;
40
    }
41
42
    /**
43
     * @param QueryBuilder $qb
44
     * @param string       $name
45
     */
46
    protected function addQueryBuilderSort(QueryBuilder $qb, $name)
47
    {
48
        $alias = current($qb->getDQLPart('from'))->getAlias();
49
        if (is_array($order = $this->getOrder($name))) {
50
            $qb->orderBy($alias . '.' . $order['field'], $order['type']);
51
        }
52
    }
53
54
    /**
55
     * Create Delete form
56
     *
57
     * @param integer                       $id
58
     * @param string                        $route
59
     * @return \Symfony\Component\Form\Form
60
     */
61
    protected function createDeleteForm($id, $route)
62
    {
63
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
64
            ->setAction($this->generateUrl($route, array('id' => $id)))
65
            ->setMethod('DELETE')
66
            ->getForm()
67
        ;
68
    }
69
}
70