Completed
Push — Order ( f4bad4...a8dbff )
by Laurent
03:01
created

AbstractOrdersController::abstractPrintAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
nc 1
nop 2
1
<?php
2
/**
3
 * AbstractOrdersController Méthodes communes InventoryController.
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 AppBundle\Controller\AbstractController;
18
use Symfony\Component\HttpFoundation\Response;
19
20
use AppBundle\Entity\Orders;
21
use AppBundle\Form\Type\OrdersType;
22
23
/**
24
 * Abstract controller.
25
 *
26
 * @category Controller
27
 */
28
class AbstractOrdersController extends AbstractController
29
{
30
    /**
31
     * Displays a form to edit an existing item entity.
32
     *
33
     * @param Object $entity     Entity
34
     * @param string $entityName Name of Entity
35
     * @param string $typePath   Path of FormType
36
     * @return array
37
     */
38
    public function abstractEditAction($entity, $entityName, $typePath)
39
    {
40
        $param = $this->get('app.helper.controller')->testReturnParam($entity, $entityName);
41
        $editForm = $this->createForm($typePath, $entity, array(
42
            'action' => $this->generateUrl($entityName.'_update', $param),
43
            'method' => 'PUT',
44
        ));
45
        if ($entityName === 'group') {
46
            $this->addRolesAction($editForm, $entity);
47
        }
48
49
        return [$entityName => $entity, 'edit_form' => $editForm->createView(),];
50
    }
51
52
    /**
53
     * Create CreateForm.
54
     *
55
     * @param string $route Route of action form
56
     * @return \Symfony\Component\Form\Form
57
     */
58
    protected function createCreateForm($route)
59
    {
60
        $orders = new Orders();
61
        return $this->createForm(
62
            OrdersType::class,
63
            $orders,
64
            ['attr' => ['id' => 'create'], 'action' => $this->generateUrl($route), 'method' => 'POST',]
65
        );
66
    }
67
68
    /**
69
     * Print a document.<br />Creating a `PDF` file for viewing on paper
70
     *
71
     * @param \AppBundle\Entity\Orders $orders Order item to print
72
     * @param string $from The Controller who call this action
73
     * @return \Symfony\Component\HttpFoundation\Response
74
     */
75
    protected function abstractPrintAction(Orders $orders, $from)
76
    {
77
        $file = $from . '-' . $orders->getId() . '.pdf';
78
        $company = $this->getDoctrine()->getManager()->getRepository('AppBundle:Company')->find(1);
79
        // Create and save the PDF file to print
80
        $html = $this->renderView(
81
            'AppBundle:' . $from . ':print.pdf.twig',
82
            ['articles' => $orders->getArticles(), 'orders' => $orders, 'company' => $company, ]
83
        );
84
        return new Response(
85
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
86
                $html,
87
                $this->get('app.helper.controller')->getArray((string)date('d/m/y - H:i:s'), '')
88
            ),
89
            200,
90
            array(
91
                'Content-Type' => 'application/pdf',
92
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
93
            )
94
        );
95
    }
96
}
97