add docgen context for a list of activities in a course

This commit is contained in:
2022-04-11 16:52:11 +02:00
parent 4257a918f3
commit bb65909bfa
4 changed files with 380 additions and 0 deletions

View File

@@ -0,0 +1,276 @@
<?php
namespace Chill\ActivityBundle\Service\DocGenerator;
use Chill\ActivityBundle\Entity\ActivityPresence;
use Chill\ActivityBundle\Entity\ActivityType;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepositoryInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository;
use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
use Chill\PersonBundle\Service\DocGenerator\AccompanyingPeriodContext;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use libphonenumber\PhoneNumber;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class ListActivitiesByAccompanyingPeriodContext implements
DocGeneratorContextWithAdminFormInterface,
DocGeneratorContextWithPublicFormInterface
{
private AccompanyingPeriodContext $accompanyingPeriodContext;
private ActivityACLAwareRepositoryInterface $activityACLAwareRepository;
private NormalizerInterface $normalizer;
private TranslatableStringHelperInterface $translatableStringHelper;
private PersonRepository $personRepository;
private SocialActionRepository $socialActionRepository;
private SocialIssueRepository $socialIssueRepository;
private UserRepository $userRepository;
private ThirdPartyRepository $thirdPartyRepository;
public function __construct(
AccompanyingPeriodContext $accompanyingPeriodContext,
ActivityACLAwareRepositoryInterface $activityACLAwareRepository,
NormalizerInterface $normalizer,
PersonRepository $personRepository,
SocialActionRepository $socialActionRepository,
SocialIssueRepository $socialIssueRepository,
ThirdPartyRepository $thirdPartyRepository,
TranslatableStringHelperInterface $translatableStringHelper,
UserRepository $userRepository
) {
$this->accompanyingPeriodContext = $accompanyingPeriodContext;
$this->activityACLAwareRepository = $activityACLAwareRepository;
$this->normalizer = $normalizer;
$this->personRepository = $personRepository;
$this->socialActionRepository = $socialActionRepository;
$this->socialIssueRepository = $socialIssueRepository;
$this->thirdPartyRepository = $thirdPartyRepository;
$this->translatableStringHelper = $translatableStringHelper;
$this->userRepository = $userRepository;
}
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
{
$data = $this->accompanyingPeriodContext->getData($template, $entity, $contextGenerationData);
$data['activities'] = $this->getActivitiesSimplified($entity);
return $data;
}
private function getActivitiesSimplified(AccompanyingPeriod $period)
{
$activities =
$this->activityACLAwareRepository->findByAccompanyingPeriodSimplified($period);
$results = [];
foreach ($activities as $row) {
$activity = $row[0];
$activity['date'] = $this->normalizer->normalize($activity['date'], 'docgen', [
AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => \DateTime::class
]);
if (null === $activity['location']) {
$activity['location'] = $this->normalizer->normalize(null, 'docgen', [
AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => Location::class
]);
$activity['location']['type'] = 'location';
} else {
$activity['location']['isNull'] = false;
$activity['location']['type'] = 'location';
foreach (['1', '2'] as $key) {
$activity['location']['phonenumber'.$key] = $this->normalizer->normalize(
$activity['location']['phonenumber'.$key],
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => PhoneNumber::class]
);
}
}
if (is_numeric($activity['location']['locationType']['id'])) {
$activity['location']['locationType']['title'] = $this->translatableStringHelper->localize(
$activity['location']['locationType']['title']
);
$activity['location']['locationType']['isNull'] = false;
$activity['location']['locationType']['type'] = 'locationType';
}
if (null !== $activity['activityType']) {
$activity['activityType']['name'] = $this->translatableStringHelper->localize(
$activity['activityType']['name']
);
$activity['activityType']['isNull'] = false;
$activity['activityType']['type'] = 'activityType';
} else {
$activity['activityType'] = $this->normalizer->normalize(null, 'docgen', [
AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => ActivityType::class
]);
}
if (null !== $activity['attendee']) {
$activity['attendee']['name'] = $this->translatableStringHelper->localize(
$activity['attendee']['name']
);
$activity['attendee']['isNull'] = false;
$activity['attendee']['type'] = 'activityPresence';
} else {
$activity['attendee'] = $this->normalizer->normalize(null, 'docgen', [
AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => ActivityPresence::class
]);
}
$activity['comment'] = (string) $row['comment'];
$activity['travelTimeMinute'] = $row['travelTimeMinute'];
$activity['durationTimeMinute'] = $row['durationTimeMinute'];
if (null !== $row['userIds']) {
foreach ($row['userIds'] as $id) {
$activity['users'][] = $this->normalizer->normalize(
$this->userRepository->find($id),
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => User::class]
);
}
} else {
$activity['users'] = [];
}
if (null !== $row['personIds']) {
foreach ($row['personIds'] as $id) {
$activity['persons'][] = $this->normalizer->normalize(
$this->personRepository->find($id),
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => Person::class]
);
}
} else {
$activity['persons'] = [];
}
if (null !== $row['thirdPartyIds']) {
foreach ($row['thirdPartyIds'] as $id) {
$activity['thirdParties'][] = $this->normalizer->normalize(
$this->thirdPartyRepository->find($id),
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => ThirdParty::class]
);
}
} else {
$activity['thirdParties'] = [];
}
if (null !== $row['socialActionIds']) {
foreach ($row['socialActionIds'] as $id) {
$activity['socialActions'][] = $this->normalizer->normalize(
$this->socialActionRepository->find($id),
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => SocialAction::class]
);
}
} else {
$activity['socialActions'] = [];
}
if (null !== $row['socialIssueIds']) {
foreach ($row['socialIssueIds'] as $id) {
$activity['socialIssues'][] = $this->normalizer->normalize(
$this->socialIssueRepository->find($id),
'docgen',
[AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => SocialIssue::class]
);
}
} else {
$activity['socialIssues'] = [];
}
$results[] = $activity;
}
return $results;
}
public function getDescription(): string
{
return 'docgen.Accompanying period with a list of activities description';
}
public function getEntityClass(): string
{
return AccompanyingPeriod::class;
}
public static function getKey(): string
{
return self::class;
}
public function getName(): string
{
return 'docgen.Accompanying period with a list of activities';
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$this->accompanyingPeriodContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData);
}
public function adminFormReverseTransform(array $data): array
{
return $this->accompanyingPeriodContext->adminFormReverseTransform($data);
}
public function adminFormTransform(array $data): array
{
return $this->accompanyingPeriodContext->adminFormTransform($data);
}
public function buildAdminForm(FormBuilderInterface $builder): void
{
$this->accompanyingPeriodContext->buildAdminForm($builder);
}
public function hasAdminForm(): bool
{
return $this->accompanyingPeriodContext->hasAdminForm();
}
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
{
$this->accompanyingPeriodContext->buildPublicForm($builder, $template, $entity);
}
public function getFormData(DocGeneratorTemplate $template, $entity): array
{
return $this->accompanyingPeriodContext->getFormData($template, $entity);
}
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool
{
return $this->accompanyingPeriodContext->hasPublicForm($template, $entity);
}
}