private function getArrayOfEntities(\KG\BeekeepingManagementBundle\Entity\Rucher $rucher) { $repo = $this->em->getRepository('KGBeekeepingManagementBundle:Ruche'); return $repo->getRucheByRucher($rucher->getId()); }
/** * @Security("has_role('ROLE_USER')") */ private function downloadQRCodeFile(Rucher $rucher, $ruches) { //Création de l'objet phpWord pour le fichier word $phpWord = new \PhpOffice\PhpWord\PhpWord(); //Création du path pour gérer les fichiers temporaires $path = $this->get('kernel')->getRootDir() . "/../web/generate/"; //Ajout d'une section $section = $phpWord->addSection(); //Ajout d'un en-tête $phpWord->addFontStyle('eStyle', array('bold' => true, 'size' => 16)); $phpWord->addFontStyle('rStyle', array('size' => 14)); $header = $section->addHeader(); $headerTable = $header->addTable(); $headerTable->addRow(); $headerTable->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(5))->addImage('logo.png', array('height' => 80)); $cellText = $headerTable->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(11)); $cellText->addText(htmlspecialchars($rucher->getExploitation()->getNom()), 'eStyle', array('align' => 'right')); $cellText->addText(htmlspecialchars($rucher->getNom()), 'rStyle', array('align' => 'right')); //Ajout d'un pied de page $footer = $section->addFooter(); $footer->addPreserveText(htmlspecialchars('{PAGE}/{NUMPAGES}'), null, array('align' => 'right')); //Création du style des cellules $cellStyle = array('valign' => 'center'); $phpWord->addFontStyle('qStyle', array('size' => 12)); //Ajout de la table contenant les qr codes $table = $section->addTable(); //Nombre de ruches dans le fichier, utile pour créer une nouvelle ligne $nbRuches = 0; foreach ($ruches as $ruche) { //3 QRCodes par ligne if ($nbRuches % 3 === 0) { $table->addRow(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(5.5)); } $nbRuches++; //Construction de l'url pour accéder à la ruche $url = $this->generateUrl('kg_beekeeping_management_view_ruche', array('ruche_id' => $ruche->getId()), true); //Construction du QRCode pointant sur l'url de la ruche $options = array('code' => $url, 'type' => 'qrcode', 'format' => 'png'); $barcode = $this->get('sgk_barcode.generator')->generate($options); //Path du fichier avec le QRCode $filename = 'qrcode' . $ruche->getId() . '.png'; //Sauvegarde du fichier file_put_contents($path . $filename, base64_decode($barcode)); //Ajout du QRCode dans le fichier ODT $cell = $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(5.33), $cellStyle); $cell->addText(htmlspecialchars($ruche->getNom()), 'qStyle', array('align' => 'center')); $cell->addImage('generate/' . $filename, array('width' => 151.18, 'height' => 151.18, 'wrappingStyle' => 'behind', 'align' => 'center')); } //Ajout de cellules vides si ligne incomplète $reste = 3 - $nbRuches % 3; if ($reste < 3) { while ($reste > 0) { $cell = $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(5.33), $cellStyle); $reste--; } } //Sauvegarde du fichier ODT $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $filename = $rucher->getId() . '_qr_codes_rucher_' . $rucher->getNom() . '.docx'; $objWriter->save($path . $filename, 'Word2007', true); //Récupération du contenu du fichier $content = file_get_contents($path . $filename); //Création de la réponse avec le contentu du fichier (pour le download) $response = new Response(); $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'); $response->headers->set('Content-Disposition', 'attachment;filename="' . $filename); $response->setContent($content); //Suppression des fichiers créés durant la création du fichier word unlink($path . $filename); foreach ($ruches as $ruche) { $filename = 'qrcode' . $ruche->getId() . '.png'; unlink($path . $filename); } //Retour de la réponse return $response; }