mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
generate document with relatorio: config and driver
This commit is contained in:
@@ -16,6 +16,7 @@ use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
|
||||
use Chill\DocGeneratorBundle\Context\ContextManager;
|
||||
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface;
|
||||
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
@@ -54,17 +55,22 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
private TempUrlGeneratorInterface $tempUrlGenerator;
|
||||
|
||||
private DriverInterface $driver;
|
||||
|
||||
public function __construct(
|
||||
ContextManager $contextManager,
|
||||
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
|
||||
DriverInterface $driver,
|
||||
LoggerInterface $logger,
|
||||
PaginatorFactory $paginatorFactory,
|
||||
TempUrlGeneratorInterface $tempUrlGenerator,
|
||||
KernelInterface $kernel,
|
||||
HttpClientInterface $client
|
||||
|
||||
) {
|
||||
$this->contextManager = $contextManager;
|
||||
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
|
||||
$this->driver = $driver;
|
||||
$this->logger = $logger;
|
||||
$this->paginatorFactory = $paginatorFactory;
|
||||
$this->tempUrlGenerator = $tempUrlGenerator;
|
||||
@@ -134,71 +140,41 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
throw new \Exception('Error during Decrypt ', 1);
|
||||
}
|
||||
|
||||
$tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins
|
||||
|
||||
if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) {
|
||||
$this->logger->error("Cannot open file ({$tmpfnameDeCrypted})");
|
||||
if (false === $templateResource = fopen('php://memory', 'r+')) {
|
||||
$this->logger->error("Could not write data to memory");
|
||||
|
||||
throw new HttpException(500);
|
||||
}
|
||||
|
||||
if (false === $ftemplate = fwrite($handle, $dataDecrypted)) {
|
||||
$this->logger->error("Cannot write to file ({$tmpfnameDeCrypted})");
|
||||
|
||||
throw new HttpException(500);
|
||||
}
|
||||
|
||||
dump("Success, wrote (to file ({$tmpfnameDeCrypted})");
|
||||
|
||||
fclose($handle);
|
||||
fwrite($templateResource, $dataDecrypted);
|
||||
rewind($templateResource);
|
||||
|
||||
$datas = $context->getData($template, $entity, $contextGenerationData);
|
||||
dump('datas compiled', $datas);
|
||||
|
||||
dump('process the data', $datas);
|
||||
$generatedResource = $this->driver->generateFromResource($templateResource, $template->getFile()->getType(), $datas, $template->getFile()->getFilename());
|
||||
|
||||
/*
|
||||
$templateProcessor = new TemplateProcessor($tmpfnameDeCrypted);
|
||||
fclose($templateResource);
|
||||
|
||||
foreach ($datas['setValues'] as $setValuesConf) {
|
||||
$templateProcessor->setValues($setValuesConf);
|
||||
}
|
||||
|
||||
foreach ($datas['cloneRowAndSetValues'] as $cloneRowAndSetValues) {
|
||||
$templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]);
|
||||
}
|
||||
$tmpfnameGenerated = tempnam($this->kernel->getCacheDir(), 'DOC_GENERATED');
|
||||
|
||||
$fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content
|
||||
*/
|
||||
|
||||
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx';
|
||||
$genDocName = 'doc_' . sprintf('%010d', mt_rand()).'odt';
|
||||
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
'PUT',
|
||||
$genDocName
|
||||
);
|
||||
|
||||
unlink($tmpfnameDeCrypted);
|
||||
//unlink($tmpfnameGenerated);
|
||||
|
||||
$client = new Client();
|
||||
|
||||
try {
|
||||
/*
|
||||
$putResponse = $client->request('PUT', $getUrlGen->url, [
|
||||
'body' => $fileContent,
|
||||
]);
|
||||
*/
|
||||
$putResponse = $client->request('PUT', $getUrlGen->url, [
|
||||
'body' => $ftemplate,
|
||||
'body' => $generatedResource,
|
||||
]);
|
||||
|
||||
if ($putResponse->getStatusCode() === 201) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$storedObject = new StoredObject();
|
||||
$storedObject
|
||||
// currently, only docx is supported
|
||||
->setType('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|
||||
->setType($template->getFile()->getType())
|
||||
->setFilename($genDocName);
|
||||
|
||||
$em->persist($storedObject);
|
||||
|
Reference in New Issue
Block a user