Completed
Push — master ( 2fdc18...f6fd2e )
by Laurent
03:47
created

DefaultController::stockAlertAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * DefaultController controller de l'application GLSR.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Default controller.
25
 *
26
 * @category Controller
27
 *
28
 * @Route("/")
29
 */
30
class DefaultController extends Controller
31
{
32
    private $entities;
33
    
34
    public function __construct()
35
    {
36
        // Tableau des entitées
37
        $this->entities = array(
38
            'AppBundle:User',
39
            'AppBundle:Company',
40
            'AppBundle:Settings',
41
            'AppBundle:FamilyLog',
42
            'AppBundle:ZoneStorage',
43
            'AppBundle:UnitStorage',
44
            'AppBundle:Tva',
45
            'AppBundle:Supplier',
46
            'AppBundle:Article');
47
48
    }
49
50
    /**
51
     * @Route("/", name="_home")
52
     * @Method("GET")
53
     * @Template()
54
     */
55
    public function indexAction()
56
    {
57
        /**
58
         * Test d'installation.
59
         */
60
        $url = $this->testEntities();
61
        if (empty($url)) {
62
            $url = $this->render('default/index.html.twig');
63
        } else {
64
            $url = $this->redirectToRoute($url);
65
        }
66
67
        /**
68
         * Affichage du dernier inventaire.
69
         */
70
        /**
71
         * Affichage des stocks d'alerte.
72
         */
73
        return $url;
74
    }
75
76
    /**
77
     * Récupérer les Alertes.
78
     *
79
     * @param integer $number nombres d'alertes à afficher
80
     * @Route("/alert", name="stockalert")
81
     * @Method("GET")
82
     * @Template("default/stockAlert.html.twig")
83
     *
84
     * @return array|null
85
     */
86
    public function stockAlertAction($number)
87
    {
88
        $em = $this->getDoctrine()->getManager();
89
        $listArticles = $em->getRepository('AppBundle:Article')->getStockAlert($number);
90
91
        return array('listArticles' => $listArticles);
92
    }
93
94
    /**
95
     * Récupérer les Alertes.
96
     *
97
     * @param integer $number nombres d'alertes à afficher
98
     * @Route("/alert", name="stockalert")
99
     * @Method("GET")
100
     * @Template("default/lastInventory.html.twig")
101
     *
102
     * @return array|null
103
     */
104
    public function lastInventoryAction($number)
105
    {
106
        $em = $this->getDoctrine()->getManager();
107
        $listInventories = $em->getRepository('AppBundle:Inventory')->getLastInventory($number);
108
109
        return array('listInventory' => $listInventories);
110
    }
111
112
    /**
113
     * Test des entités
114
     *
115
     * @return string|null
116
     */
117
    private function testEntities()
118
    {
119
        $url = null;
120
        $etm = $this->getDoctrine()->getManager();
121
        // vérifie que les Entitées ne sont pas vides
122
        $nbEntities = count($this->entities);
123
        for ($index = 0; $index < $nbEntities; $index++) {
124
            $entity = $etm->getRepository($this->entities[$index]);
125
            $entityData = $entity->find(1);
126
127
            if (empty($entityData)) {
128
                $message = 'gestock.install.none';
129
                $url = 'gs_install';
130
                break;
131
            }
132
        }
133
        if (isset($message)) {
134
            $this->addFlash('warning', $message);
135
        }
136
        return $url;
137
    }
138
139
    /**
140
     * Récupère les FamilyLog de la requête post.
141
     *
142
     * @Route("/getfamilylog", name="getfamilylog")
143
     * @Method("POST")
144
     * @return \Symfony\Component\HttpFoundation\Response
145
     */
146
    public function getFamilyLogAction()
147
    {
148
        $request = $this->getRequest();
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...
149
        $etm = $this->getDoctrine()->getManager();
150
        if ($request->isXmlHttpRequest()) {
151
            $familyLog = array();
152
            $id = $request->get('id');
153
            if ($id != '') {
154
                $supplier = $etm
155
                    ->getRepository('AppBundle:Supplier')
156
                    ->find($id);
157
                $familyLog['familylog'] = $supplier->getFamilyLog()->getId();
158
                $response = new Response();
159
                $data = json_encode($familyLog);
160
                $response->headers->set('Content-Type', 'application/json');
161
                $response->setContent($data);
162
                return $response;
163
            }
164
        }
165
        return new Response('Error');
166
    }
167
}
168