-
Notifications
You must be signed in to change notification settings - Fork 25
/
BarcodeController.php
108 lines (101 loc) · 3.42 KB
/
BarcodeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Mopa\Bundle\BarcodeBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Mopa\Bundle\BarcodeBundle\Model\BarcodeTypes;
class BarcodeController extends ContainerAware
{
/**
* This is just an example howto use barcodes and to display them
* @param Request $request
* @return Response
*/
public function playgroundAction(Request $request)
{
$types = BarcodeTypes::getTypes();
$errors = array();
$form = $this->container->get('form.factory')->createBuilder('form')
->add('text')
->add('type', 'choice', array(
'empty_value' => 'Choose an option',
'choices' => $types
))
->getForm();
;
$webfile = false;
if ($request->getMethod() == 'POST') {
$form->submit($request);
$data = $form->getData();
$text = $data['text'];
$type = $data['type'];
if($type){
try{
$bmanager = $this->container->get('mopa_barcode.barcode_service');
$webfile = $bmanager->get($type, $text);
}
catch(\Exception $e){
$errors[] = $e->getMessage();
}
}else{
$errors[] = "Please select a option";
}
if(count($errors)){
$webfile = false;
}
}
return $this->container->get('templating')->renderResponse(
'MopaBarcodeBundle:Barcode:playground.html.twig',
array(
'form'=>$form->createView(),
'barcode_url'=>$webfile,
'errors'=>$errors,
)
);
}
/**
* This might be used to render barcodes dynamically
* Careful to expose this on the web, maybe others could use your site just to generate and display barcodes
* @param $type
* @param $enctext
* @return Response
*/
public function displayBarcodeAction($type, $enctext){
$bservice = $this->container->get('mopa_barcode.barcode_service');
return new Response(
file_get_contents($file = $bservice->get($type, $enctext, true)),
200,
array(
'Content-Type' => 'image/png',
'Content-Disposition' => 'filename="'.$file.'"'
)
);
}
/**
* @param $type
* @param int $level
* @param int $size
* @param int $margin
* @param bool $useOverlay
* @param $enctext
* @return Response
*/
public function downloadBarcodeAction($type, $level = 0, $size = 3, $margin = 4, $useOverlay = false, $enctext)
{
$bservice = $this->container->get('mopa_barcode.barcode_service');
$options = array(
'level' => $level,
'size' => $size,
'margin' => $margin,
'useOverlay' => $useOverlay,
);
return new Response(
file_get_contents($file = $bservice->get($type, $enctext, true, $options)),
200,
array(
'Content-Type' => 'image/png',
'Content-Disposition' => sprintf('attachment; filename="qr_%s"', date("Y_m_d_H_i_s")),
)
);
}
}