1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
|
8
|
|
|
class FiCoreController extends FiController |
9
|
|
|
{ |
10
|
|
|
|
11
|
2 |
|
public function stampatabellaAction(Request $request) |
12
|
|
|
{ |
13
|
2 |
|
$this->setup($request); |
14
|
2 |
|
$pdf = new StampatabellaController($this->container); |
15
|
|
|
|
16
|
2 |
|
$parametri = $this->prepareOutput($request); |
17
|
|
|
|
18
|
2 |
|
$pdf->stampa($parametri); |
19
|
|
|
|
20
|
2 |
|
return new Response('OK'); |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
public function esportaexcelAction(Request $request) |
24
|
|
|
{ |
25
|
2 |
|
$this->setup($request); |
26
|
2 |
|
$xls = new StampatabellaController($this->container); |
27
|
|
|
|
28
|
2 |
|
$parametri = $this->prepareOutput($request); |
29
|
|
|
|
30
|
2 |
|
$fileexcel = $xls->esportaexcel($parametri); |
31
|
2 |
|
$response = new Response(); |
|
|
|
|
32
|
|
|
|
33
|
2 |
|
$response->headers->set('Content-Type', 'text/csv'); |
34
|
2 |
|
$response->headers->set('Content-Disposition', 'attachment;filename="' . basename($fileexcel) . '"'); |
35
|
|
|
|
36
|
2 |
|
$response->setContent(file_get_contents($fileexcel)); |
37
|
|
|
|
38
|
2 |
|
@unlink($fileexcel); |
|
|
|
|
39
|
|
|
|
40
|
2 |
|
return $response; |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
public function prepareOutput($request) |
44
|
|
|
{ |
45
|
4 |
|
$namespace = $this->getNamespace(); |
46
|
4 |
|
$bundle = $this->getBundle(); |
|
|
|
|
47
|
4 |
|
$container = $this->container; |
48
|
|
|
|
49
|
4 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
50
|
|
|
|
51
|
4 |
|
$em = $this->getDoctrine()->getManager(); |
52
|
|
|
|
53
|
|
|
$paricevuti = array( |
54
|
4 |
|
'nomebundle' => $nomebundle, |
55
|
4 |
|
'nometabella' => $request->get('nometabella'), |
56
|
4 |
|
'container' => $container, |
57
|
4 |
|
'request' => $request, |
58
|
4 |
|
); |
59
|
|
|
|
60
|
4 |
|
$parametripertestatagriglia = $this->getParametersTestataPerGriglia($request, $container, $em, $paricevuti); |
61
|
|
|
|
62
|
4 |
|
$testatagriglia = Griglia::testataPerGriglia($parametripertestatagriglia); |
63
|
|
|
|
64
|
4 |
|
if ($request->get('titolo')) { |
65
|
|
|
$testatagriglia['titolo'] = $request->get('titolo'); |
66
|
|
|
} |
67
|
4 |
|
$parametridatipergriglia = $this->getParametersDatiPerGriglia($request, $container, $em, $paricevuti); |
68
|
4 |
|
$corpogriglia = Griglia::datiPerGriglia($parametridatipergriglia); |
|
|
|
|
69
|
|
|
|
70
|
4 |
|
$parametri = array('request' => $request, 'testata' => $testatagriglia, 'griglia' => $corpogriglia); |
71
|
4 |
|
return $parametri; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/*public function importaexcelAction(Request $request) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->setup($request); |
77
|
|
|
$return = "OK"; |
78
|
|
|
try { |
79
|
|
|
$em = $this->getDoctrine()->getManager(); |
80
|
|
|
$file = $request->files->get('file'); |
81
|
|
|
$tablenamefile = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getClientOriginalName()); |
82
|
|
|
$parametri = json_decode($request->get("parametrigriglia")); |
83
|
|
|
$bundle = $parametri->nomebundle; |
84
|
|
|
$controller = $parametri->nometabella; |
85
|
|
|
$repo = $em->getRepository($bundle . ":" . $tablenamefile); |
86
|
|
|
$className = $repo->getClassName(); |
87
|
|
|
|
88
|
|
|
$classentitypath = "\\" . $className; |
89
|
|
|
if (strtolower($controller) != strtolower($tablenamefile)) { |
90
|
|
|
$response = new Response("Si sta cercando di caricare i dati di " . $tablenamefile . " in " . $controller); |
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (is_a($repo, "Doctrine\ORM\EntityRepository")) { |
95
|
|
|
$entitycolumns = $em->getClassMetadata($bundle . ":" . $tablenamefile); |
96
|
|
|
$objPHPExcel = \PHPExcel_IOFactory::load($file); |
97
|
|
|
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { |
98
|
|
|
$highestRow = $worksheet->getHighestRow(); |
99
|
|
|
$highestColumn = $worksheet->getHighestColumn(); |
100
|
|
|
$highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn); |
101
|
|
|
$colonne = array(); |
102
|
|
|
for ($index = 0; $index < $highestColumnIndex; $index++) { |
103
|
|
|
$columnname = strtolower($worksheet->getCellByColumnAndRow($index, 1)->getValue()); |
104
|
|
|
if (!$entitycolumns->hasField($columnname) || $columnname == "id") { |
105
|
|
|
//the entity does not have a such property $columnname or id column |
106
|
|
|
continue; |
107
|
|
|
} else { |
108
|
|
|
$colonne[] = array("colxls" => $index, "name" => $columnname, "type" => $entitycolumns->getTypeOfField($columnname)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
for ($rows = 2; $rows < $highestRow + 1; $rows++) { |
113
|
|
|
$newentity = new $classentitypath(); |
114
|
|
|
foreach ($colonne as $colonna) { |
115
|
|
|
$cols = $colonna["colxls"]; |
116
|
|
|
$valore = $worksheet->getCellByColumnAndRow($cols, $rows)->getValue(); |
117
|
|
|
if ($valore) { |
118
|
|
|
$fieldset = "set" . ucfirst($colonna["name"]); |
119
|
|
|
$valore = $this->getValoreCampoExcel($valore, $colonna); |
120
|
|
|
$newentity->$fieldset($valore); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
$em->persist($newentity); |
124
|
|
|
} |
125
|
|
|
$em->flush(); |
126
|
|
|
} |
127
|
|
|
} else { |
128
|
|
|
$response = new Response($return); |
129
|
|
|
return $response; |
130
|
|
|
} |
131
|
|
|
} catch (\Exception $exc) { |
132
|
|
|
$response = new Response($exc->getMessage()); |
133
|
|
|
return $response; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$response = new Response($return); |
137
|
|
|
return $response; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function getValoreCampoExcel($valoreexcel, $colonna) |
141
|
|
|
{ |
142
|
|
|
$valore = $valoreexcel; |
143
|
|
|
if ($colonna["type"] === "date") { |
144
|
|
|
$exceldata = \PHPExcel_Style_NumberFormat::toFormattedString($valoreexcel, 'YYYY-MM-DD'); |
145
|
|
|
$nuovadata = \DateTime::createFromFormat('Y-m-d', $exceldata); |
146
|
|
|
$valore = $nuovadata; |
147
|
|
|
} elseif ($colonna["type"] === "boolean") { |
148
|
|
|
$valore = (($valoreexcel == 'SI') ? true : false); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $valore; |
152
|
|
|
}*/ |
153
|
|
|
} |
154
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.