diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index effe9750f..9731cbdc2 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -358,6 +358,27 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac ] ], ] + ], + [ + 'class' => \Chill\PersonBundle\Entity\SocialWork\SocialIssue::class, + 'name' => 'social_work_social_issue', + 'base_path' => '/api/1.0/person/social-work/social-issue', +// 'controller' => \Chill\PersonBundle\Controller\OpeningApiController::class, + 'base_role' => 'ROLE_USER', + 'actions' => [ + '_index' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true + ], + ], + '_entity' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true + ] + ], + ] ] ] ]); diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 7c451200e..a69e3f255 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -27,6 +27,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin; use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -215,6 +216,16 @@ class AccompanyingPeriod */ private $resources; + /** + * @ORM\ManyToMany( + * targetEntity=SocialIssue::class + * ) + * @ORM\JoinTable( + * name="chill_person_accompanying_period_social_issues" + * ) + */ + private Collection $socialIssues; + /** * AccompanyingPeriod constructor. * @@ -225,6 +236,7 @@ class AccompanyingPeriod $this->setOpeningDate($dateOpening); $this->participations = new ArrayCollection(); $this->scopes = new ArrayCollection(); + $this->socialIssues = new ArrayCollection(); } /** @@ -648,6 +660,23 @@ class AccompanyingPeriod $this->resources->removeElement($resource); } + public function getSocialIssues(): Collection + { + return $this->socialIssues; + } + + public function addSocialIssues(SocialIssue $socialIssue): self + { + $this->socialIssues[] = $socialIssue; + + return $this; + } + + public function removeSocialIssue(SocialIssue $socialissue): void + { + $this->socialIssues->removeElement($socialIssue); + } + /** * Get a list of all persons which are participating to this course */ diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index cfec01751..4d663e718 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -6,10 +6,15 @@ use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Annotation\DiscriminatorMap; /** * @ORM\Entity(repositoryClass=SocialIssueRepository::class) * @ORM\Table(name="chill_person_social_issue") + * @DiscriminatorMap(typeProperty="type", mapping={ + * "social_issue"=SocialIssue::class + * }) */ class SocialIssue { @@ -37,6 +42,7 @@ class SocialIssue /** * @ORM\Column(type="json") + * @Groups({"read"}) */ private $title = []; @@ -61,6 +67,11 @@ class SocialIssue return $this->parent; } + public function hasParent(): bool + { + return $this->parent !== null; + } + public function setParent(?self $parent): self { $this->parent = $parent; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php new file mode 100644 index 000000000..e78febcde --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php @@ -0,0 +1,43 @@ +render = $render; + } + + + public function normalize($socialIssue, string $format = null, array $context = []) + { + /** @var SocialIssue $socialIssue */ + return [ + 'type' => 'social_issue', + 'id' => $socialIssue->getId(), + 'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null, + 'children_ids' => $socialIssue->getChildren()->map(function (SocialIssue $si) { return $si->getId(); }), + 'title' => $socialIssue->getTitle(), + 'text' => $this->render->renderString($socialIssue, []) + ]; + } + + public function supportsNormalization($data, string $format = null): bool + { + return $data instanceof SocialIssue; + } +} diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php new file mode 100644 index 000000000..4a7ed0cb5 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php @@ -0,0 +1,50 @@ + ' > ', + ]; + + public function __construct(TranslatableStringHelper $translatableStringHelper) + { + $this->translatableStringHelper = $translatableStringHelper; + } + + public function supports($entity, array $options): bool + { + return $entity instanceof SocialIssueRender; + } + + public function renderString($socialIssue, array $options): string + { + /** @var $socialIssue SocialIssue */ + $options = \array_merge(self::DEFAULT_ARGS, $options); + + $str = $this->translatableStringHelper->localize($socialIssue->getTitle()); + + while ($socialIssue->hasParent()) { + $socialIssue = $socialIssue->getParent(); + $str .= $options[self::SEPARATOR_KEY].$this->translatableStringHelper->localize( + $socialIssue->getTitle() + ); + } + + return $str; + } + + public function renderBox($entity, array $options): string + { + return "renderBox not implemented for social issue"; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/SocialIssueApiControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/SocialIssueApiControllerTest.php new file mode 100644 index 000000000..36d5dca37 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/SocialIssueApiControllerTest.php @@ -0,0 +1,55 @@ +getClientAuthenticated(); + $client->request(Request::METHOD_GET, '/api/1.0/person/social-work/social-issue.json'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $data = \json_decode($client->getResponse()->getContent(), true); + + $this->assertGreaterThan(0, $data['count']); + $this->assertGreaterThan(0, count($data['results'])); + + return $data; + } + + /** + * @depends testList + */ + public function testItem(array $data): void + { + $socialIssues = $data['results']; + shuffle($socialIssues); + $socialIssue = \array_pop($socialIssues); + + + $client = $this->getClientAuthenticated(); + $client->request(Request::METHOD_GET, sprintf('/api/1.0/person/social-work/social-issue/%d.json', $socialIssue['id'])); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $data = \json_decode($client->getResponse()->getContent(), true); + + $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('type', $data); + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/repository.yaml b/src/Bundle/ChillPersonBundle/config/services/repository.yaml index b99402bcf..8bd231aa4 100644 --- a/src/Bundle/ChillPersonBundle/config/services/repository.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/repository.yaml @@ -28,3 +28,8 @@ services: arguments: - '@Doctrine\Persistence\ManagerRegistry' tags: [ doctrine.repository_service ] + + Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository: + arguments: + - '@Doctrine\Persistence\ManagerRegistry' + tags: [ doctrine.repository_service ] diff --git a/src/Bundle/ChillPersonBundle/config/services/serializer.yaml b/src/Bundle/ChillPersonBundle/config/services/serializer.yaml index 092dc2320..1146b76da 100644 --- a/src/Bundle/ChillPersonBundle/config/services/serializer.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/serializer.yaml @@ -1,15 +1,8 @@ --- services: - Chill\PersonBundle\Serializer\Normalizer\PersonNormalizer: - arguments: - $repository: '@Chill\PersonBundle\Repository\PersonRepository' + Chill\PersonBundle\Serializer\Normalizer\: + autowire: true + resource: '../../Serializer/Normalizer' tags: - { name: 'serializer.normalizer', priority: 64 } - Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodNormalizer: - tags: - - { name: 'serializer.normalizer', priority: 64 } - - Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodParticipationNormalizer: - tags: - - { name: 'serializer.normalizer', priority: 64 } diff --git a/src/Bundle/ChillPersonBundle/config/services/templating.yaml b/src/Bundle/ChillPersonBundle/config/services/templating.yaml index 3456c96de..37e904884 100644 --- a/src/Bundle/ChillPersonBundle/config/services/templating.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/templating.yaml @@ -1,12 +1,19 @@ services: + Chill\PersonBundle\Templating\Entity\: + resource: '../../Templating/Entity' + tags: + - 'chill.render_entity' + Chill\PersonBundle\Templating\Entity\PersonRender: arguments: $configAltNamesHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper' tags: - 'chill.render_entity' - + Chill\PersonBundle\Templating\Entity\ClosingMotiveRender: arguments: $translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper' - tags: - - 'chill.render_entity' + + Chill\PersonBundle\Templating\Entity\SocialIssueRender: + arguments: + $translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper' diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20210518075908.php b/src/Bundle/ChillPersonBundle/migrations/Version20210518075908.php new file mode 100644 index 000000000..e2806e761 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20210518075908.php @@ -0,0 +1,33 @@ +addSql('CREATE TABLE chill_person_accompanying_period_social_issues (accompanyingperiod_id INT NOT NULL, socialissue_id INT NOT NULL, PRIMARY KEY(accompanyingperiod_id, socialissue_id))'); + $this->addSql('CREATE INDEX IDX_CAFE078F550B0C53 ON chill_person_accompanying_period_social_issues (accompanyingperiod_id)'); + $this->addSql('CREATE INDEX IDX_CAFE078FA549916C ON chill_person_accompanying_period_social_issues (socialissue_id)'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_social_issues ADD CONSTRAINT FK_CAFE078F550B0C53 FOREIGN KEY (accompanyingperiod_id) REFERENCES chill_person_accompanying_period (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_social_issues ADD CONSTRAINT FK_CAFE078FA549916C FOREIGN KEY (socialissue_id) REFERENCES chill_person_social_issue (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP TABLE chill_person_accompanying_period_social_issues'); + } +}