Generate a context for docgen, on accompanying period

This commit is contained in:
2021-11-30 23:23:02 +01:00
parent 3404d3669c
commit de4e83b3fb
19 changed files with 433 additions and 128 deletions

View File

@@ -12,7 +12,6 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

View File

@@ -1,14 +1,20 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Context;
class ContextManager
{
private iterable $contexts;
/**
* @param iterable $contexts
*/
public function __construct(iterable $contexts)
{
$this->contexts = $contexts;

View File

@@ -16,11 +16,6 @@ namespace Chill\DocGeneratorBundle\Context;
*/
interface DocGeneratorContextInterface
{
public static function getKey(): string;
public function getName(): string;
/**
* Get the data that will be injected to the generated document.
*
@@ -35,6 +30,10 @@ interface DocGeneratorContextInterface
*/
public function getForm($entity);
public static function getKey(): string;
public function getName(): string;
/**
* has form.
*/

View File

@@ -1,11 +1,25 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Context\Exception;
class UnexpectedTypeException extends \LogicException
use LogicException;
use function get_class;
use function gettype;
use function is_object;
class UnexpectedTypeException extends LogicException
{
public function __construct($value, string $expectedType)
{
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, \is_object($value) ? \get_class($value) : \gettype($value)));
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, is_object($value) ? get_class($value) : gettype($value)));
}
}

View File

@@ -23,16 +23,6 @@ use function get_class;
*/
class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
{
public function getName(): string
{
return 'household member';
}
public static function getKey(): string
{
return self::class;
}
/**
* Get the data that will be injected to the generated document.
*
@@ -99,6 +89,16 @@ class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
return $builder;
}
public static function getKey(): string
{
return self::class;
}
public function getName(): string
{
return 'household member';
}
/**
* has form.
*/

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Controller;
use Base64Url\Base64Url;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
@@ -19,23 +20,19 @@ use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\HeaderUtils;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use Jose\Component\Core\JWK;
use Base64Url\Base64Url;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\Response;
// TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
@@ -43,13 +40,13 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
final class DocGeneratorTemplateController extends AbstractController
{
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private HttpClientInterface $client;
private PaginatorFactory $paginatorFactory;
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private KernelInterface $kernel;
private HttpClientInterface $client;
private PaginatorFactory $paginatorFactory;
public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
@@ -78,7 +75,8 @@ final class DocGeneratorTemplateController extends AbstractController
): Response {
$getUrlGen = $tempUrlGenerator->generate(
'GET',
$template->getFile()->getFilename());
$template->getFile()->getFilename()
);
$data = $this->client->request('GET', $getUrlGen->url);
@@ -92,23 +90,25 @@ final class DocGeneratorTemplateController extends AbstractController
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
if ($dataDecrypted === FALSE) {
throw new \Exception("Error during Decrypt ", 1);
if (false === $dataDecrypted) {
throw new \Exception('Error during Decrypt ', 1);
}
$tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins
if (!$handle = fopen($tmpfnameDeCrypted, 'a')) {
echo "Cannot open file ($tmpfnameDeCrypted)";
if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) {
echo "Cannot open file ({$tmpfnameDeCrypted})";
exit;
}
if (fwrite($handle, $dataDecrypted) === FALSE) {
echo "Cannot write to file ($tmpfnameDeCrypted)";
if (fwrite($handle, $dataDecrypted) === false) {
echo "Cannot write to file ({$tmpfnameDeCrypted})";
exit;
}
dump("Success, wrote ($dataDecrypted) to file ($tmpfnameDeCrypted)");
dump("Success, wrote ({$dataDecrypted}) to file ({$tmpfnameDeCrypted})");
fclose($handle);
@@ -185,7 +185,7 @@ final class DocGeneratorTemplateController extends AbstractController
}
throw new Exception('Unable to generate document.');
}
}
/**
* @Route(

View File

@@ -12,10 +12,11 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\DataFixtures\ORM;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use DateTime;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Persistence\ObjectManager;
use Chill\DocStoreBundle\Entity\StoredObject;
/**
* Load DocGeneratorTemplate.
@@ -32,7 +33,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture
'filename' => 'pKNlhCrQDCRsAuC8vYHDKa',
'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}',
'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]',
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext',
'entities' => [AccompanyingPeriodWorkEvaluation::class],
@@ -43,7 +44,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture
'filename' => 'pKNlhCrQDCRsAuC8vYHDKa',
'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}',
'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]',
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext',
'entities' => ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction', AccompanyingPeriodWorkEvaluation::class],
@@ -57,7 +58,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture
->setFilename($template['file']['filename'])
->setKeyInfos(json_decode($template['file']['key'], true))
->setIv(json_decode($template['file']['iv'], true))
->setCreationDate(new \DateTime('today'))
->setCreationDate(new DateTime('today'))
->setType($template['file']['type']);
$manager->persist($newStoredObj);

View File

@@ -11,9 +11,9 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Entity;
use Chill\DocStoreBundle\Entity\StoredObject;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Chill\DocStoreBundle\Entity\StoredObject;
/**
* @ORM\Entity
@@ -62,7 +62,6 @@ class DocGeneratorTemplate
* @Serializer\Groups({"read"})
*/
private int $id;
/**
* @ORM\Column(type="json")
@@ -95,11 +94,10 @@ class DocGeneratorTemplate
return $this->id;
}
public function getName(): ?array
public function getName(): ?array
{
return $this->name;
}
public function setContext(string $context): self
{

View File

@@ -14,13 +14,13 @@ namespace Chill\DocGeneratorBundle\Form;
use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
class DocGeneratorTemplateType extends AbstractType
{
@@ -33,7 +33,7 @@ class DocGeneratorTemplateType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$contexts = \array_flip(\array_map(function (DocGeneratorContextInterface $c) {
$contexts = array_flip(array_map(static function (DocGeneratorContextInterface $c) {
return $c->getName();
}, $this->contextManager->getContext()));
@@ -44,20 +44,19 @@ class DocGeneratorTemplateType extends AbstractType
->add('context', ChoiceType::class, [
'required' => true,
'label' => 'Context',
'choices' => $contexts
'choices' => $contexts,
])
->add('entities', ChoiceType::class, [
'multiple' => true,
'choices' => [
'choices' => [
'AccompanyingPeriod' => 'Chill\PersonBundle\Entity\AccompanyingPeriod',
'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction',
'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class
]])
'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class,
], ])
->add('description')
->add('file', StoredObjectType::class, [
'error_bubbling' => true
])
;
'error_bubbling' => true,
]);
}
public function configureOptions(OptionsResolver $resolver)

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\DocGenerator;
@@ -8,10 +15,20 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Using DocStore objects inside the DocGenTemplate
* Using DocStore objects inside the DocGenTemplate.
*/
final class Version20211103111010 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_docgen_template DROP CONSTRAINT FK_49A347E893CB796C');
$this->addSql('DROP INDEX IDX_49A347E893CB796C');
$this->addSql('ALTER TABLE chill_docgen_template ADD file VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE chill_docgen_template DROP file_id');
$this->addSql('ALTER TABLE chill_docgen_template ALTER entities DROP NOT NULL');
$this->addSql('ALTER TABLE chill_docgen_template ALTER context DROP NOT NULL');
}
public function getDescription(): string
{
return 'Using DocStore objects inside the DocGenTemplate';
@@ -26,14 +43,4 @@ final class Version20211103111010 extends AbstractMigration
$this->addSql('ALTER TABLE chill_docgen_template ADD CONSTRAINT FK_49A347E893CB796C FOREIGN KEY (file_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_49A347E893CB796C ON chill_docgen_template (file_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_docgen_template DROP CONSTRAINT FK_49A347E893CB796C');
$this->addSql('DROP INDEX IDX_49A347E893CB796C');
$this->addSql('ALTER TABLE chill_docgen_template ADD file VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE chill_docgen_template DROP file_id');
$this->addSql('ALTER TABLE chill_docgen_template ALTER entities DROP NOT NULL');
$this->addSql('ALTER TABLE chill_docgen_template ALTER context DROP NOT NULL');
}
}