mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
various fixes
This commit is contained in:
parent
fdae6c106a
commit
d01eaa8065
@ -98,7 +98,7 @@ class SocialAction
|
||||
{
|
||||
if (!$this->children->contains($child)) {
|
||||
$this->children[] = $child;
|
||||
$child->setParent($this);
|
||||
$child->setParent($this)->setIssue($this->getIssue());
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -266,9 +266,16 @@ class SocialAction
|
||||
{
|
||||
$this->issue = $issue;
|
||||
|
||||
foreach ($this->getChildren() as $child) {
|
||||
$child->setIssue($issue);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal use $parent->addChild() instead (@see{self::addChild()})
|
||||
*/
|
||||
public function setParent(?self $parent): self
|
||||
{
|
||||
$this->parent = $parent;
|
||||
|
@ -305,6 +305,9 @@ class SocialIssue
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal use @see{SocialIssue::addChild()} instead
|
||||
*/
|
||||
public function setParent(?self $parent): self
|
||||
{
|
||||
$this->parent = $parent;
|
||||
|
@ -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\PersonBundle\Service\Import;
|
||||
@ -18,20 +25,21 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query\Expr\Comparison;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
use Exception;
|
||||
use function count;
|
||||
|
||||
final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
{
|
||||
private SocialIssueRepository $socialIssueRepository;
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private SocialActionRepository $socialActionRepository;
|
||||
private EvaluationRepository $evaluationRepository;
|
||||
|
||||
private GoalRepository $goalRepository;
|
||||
|
||||
private ResultRepository $resultRepository;
|
||||
|
||||
private EvaluationRepository $evaluationRepository;
|
||||
private SocialActionRepository $socialActionRepository;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
private SocialIssueRepository $socialIssueRepository;
|
||||
|
||||
public function __construct(
|
||||
SocialIssueRepository $socialIssueRepository,
|
||||
@ -69,9 +77,10 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
'eval' => null,
|
||||
];
|
||||
|
||||
foreach ($dataset as $row) {
|
||||
foreach ($dataset as $key => $row) {
|
||||
$result = $this
|
||||
->import1(
|
||||
$key,
|
||||
// Columns cleanup before importing data.
|
||||
array_map(
|
||||
static fn (string $column): ?string => '' === $column ? null : $column,
|
||||
@ -84,221 +93,6 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Row Structure:
|
||||
*
|
||||
* Index 0: Parent SocialIssue
|
||||
* Index 1: Child SocialIssue
|
||||
* Index 2: Parent SocialAction
|
||||
* Index 3: Child SocialAction
|
||||
* Index 4: Goal
|
||||
* Index 5: Result
|
||||
* Index 6: Evaluation
|
||||
*
|
||||
* @param array<int, ?string> $row
|
||||
* @param array<string, array<string, SocialIssue|null>|array<string, SocialAction|null>|Goal|Result|Evaluation> $previousRow
|
||||
*
|
||||
* @return array<string, array<string, SocialIssue|null>|array<string, SocialAction|null>|Goal|Result|Evaluation>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function import1(array $row, array $previousRow): array
|
||||
{
|
||||
$socialIssues = $this
|
||||
->handleSocialIssue(
|
||||
$row[0],
|
||||
$row[1],
|
||||
$previousRow['socialIssues']['socialIssue'] ?? null,
|
||||
$previousRow['socialIssues']['socialIssueChild'] ?? null
|
||||
);
|
||||
|
||||
$socialActions = $this
|
||||
->handleSocialAction(
|
||||
$row[2],
|
||||
$row[3],
|
||||
$socialIssues['socialIssue'],
|
||||
$previousRow['socialActions']['socialAction'] ?? null,
|
||||
$previousRow['socialActions']['socialActionChild'] ?? null
|
||||
);
|
||||
|
||||
$goal = $this->handleGoal($row[4], $socialActions['socialAction']);
|
||||
$result = $this->handleResult($row[5], $socialActions['socialAction'], $goal);
|
||||
$eval = $this->handleEvaluation($row[6], $socialActions['socialAction']);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return [
|
||||
'socialIssues' => $socialIssues,
|
||||
'socialActions' => $socialActions,
|
||||
'goal' => $goal,
|
||||
'result' => $result,
|
||||
'eval' => $eval,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SocialIssue|null>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function handleSocialIssue(
|
||||
?string $socialIssueTitle,
|
||||
?string $socialIssueChildTitle,
|
||||
?SocialIssue $previousSocialIssue,
|
||||
?SocialIssue $previousSocialIssueChild
|
||||
): array {
|
||||
if (null !== $previousSocialIssue && ($socialIssueTitle === $previousSocialIssue->getTitle())) {
|
||||
$return = [
|
||||
'socialIssue' => $previousSocialIssue,
|
||||
];
|
||||
|
||||
return $return + [
|
||||
'socialIssueChild' => (null !== $previousSocialIssueChild && ($socialIssueChildTitle === $previousSocialIssueChild->getTitle())) ? $previousSocialIssueChild : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialIssue = $this
|
||||
->getOrCreateEntity(
|
||||
$this->socialIssueRepository,
|
||||
'title',
|
||||
['fr' => $socialIssueTitle]
|
||||
)
|
||||
->setTitle(['fr' => $socialIssueTitle]);
|
||||
|
||||
if (null !== $socialIssueChildTitle) {
|
||||
/** @var SocialIssue $socialIssueChild */
|
||||
$socialIssueChild = $this
|
||||
->getOrCreateEntity(
|
||||
$this->socialIssueRepository,
|
||||
'title',
|
||||
['fr' => $socialIssueChildTitle]
|
||||
)
|
||||
->setTitle(['fr' => $socialIssueChildTitle]);
|
||||
|
||||
$this->entityManager->persist($socialIssueChild);
|
||||
|
||||
$socialIssue->addChild($socialIssueChild);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialIssue);
|
||||
|
||||
return [
|
||||
'socialIssue' => $socialIssue,
|
||||
'socialIssueChild' => $socialIssueChild ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SocialAction|null>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function handleSocialAction(
|
||||
?string $socialActionTitle,
|
||||
?string $socialActionChildTitle,
|
||||
SocialIssue $socialIssue,
|
||||
?SocialAction $previousSocialAction,
|
||||
?SocialAction $previousSocialActionChild
|
||||
): array {
|
||||
if (null !== $previousSocialAction && ($socialActionTitle === $previousSocialAction->getTitle())) {
|
||||
$return = [
|
||||
'socialAction' => $previousSocialAction,
|
||||
];
|
||||
|
||||
return $return + [
|
||||
'socialActionChild' => (null !== $previousSocialActionChild && ($socialActionChildTitle === $previousSocialActionChild->getTitle())) ? $previousSocialActionChild : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
||||
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
||||
|
||||
if (null !== $socialActionChildTitle) {
|
||||
/** @var SocialAction $socialActionChild */
|
||||
$socialActionChild = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildTitle]);
|
||||
$socialActionChild->setTitle(['fr' => $socialActionChildTitle]);
|
||||
|
||||
$this->entityManager->persist($socialActionChild);
|
||||
|
||||
$socialActionChild->setIssue($socialIssue);
|
||||
$this->entityManager->persist($socialActionChild);
|
||||
|
||||
$socialAction->addChild($socialActionChild);
|
||||
} else {
|
||||
$socialAction->setIssue($socialIssue);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
|
||||
return [
|
||||
'socialAction' => $socialAction,
|
||||
'socialActionChild' => $socialActionChild ?? null
|
||||
];
|
||||
}
|
||||
|
||||
private function handleGoal(?string $goalTitle, SocialAction $socialAction): ?Goal
|
||||
{
|
||||
if (null === $goalTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Goal $goal */
|
||||
$goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]);
|
||||
$goal->setTitle(['fr' => $goalTitle]);
|
||||
|
||||
$socialAction->addGoal($goal);
|
||||
$goal->addSocialAction($socialAction);
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
$this->entityManager->persist($goal);
|
||||
|
||||
return $goal;
|
||||
}
|
||||
|
||||
private function handleResult(?string $resultTitle, SocialAction $socialAction, ?Goal $goal): ?Result
|
||||
{
|
||||
if (null === $resultTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Result $result */
|
||||
$result = $this->getOrCreateEntity($this->resultRepository, 'title', ['fr' => $resultTitle]);
|
||||
$result->setTitle(['fr' => $resultTitle]);
|
||||
|
||||
if (null !== $goal) {
|
||||
$result->addGoal($goal);
|
||||
$goal->addResult($result);
|
||||
|
||||
$this->entityManager->persist($goal);
|
||||
}
|
||||
|
||||
$result->addSocialAction($socialAction);
|
||||
$socialAction->addResult($result);
|
||||
|
||||
$this->entityManager->persist($result);
|
||||
$this->entityManager->persist($socialAction);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation
|
||||
{
|
||||
if (null === $evaluationTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Evaluation $eval */
|
||||
$eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]);
|
||||
$eval->setTitle(['fr' => $evaluationTitle]);
|
||||
$eval->setSocialAction($socialAction);
|
||||
|
||||
$this->entityManager->persist($eval);
|
||||
|
||||
return $eval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, object>
|
||||
*/
|
||||
@ -320,8 +114,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
|
||||
$jsonParameters = array_reduce(
|
||||
$temporaryJsonCriterias,
|
||||
static function (array $carry, array $row): array
|
||||
{
|
||||
static function (array $carry, array $row): array {
|
||||
[,, $value, $placeholder] = $row;
|
||||
|
||||
return array_merge(
|
||||
@ -335,8 +128,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
);
|
||||
|
||||
$jsonPredicates = array_map(
|
||||
static function (array $row) use ($expr): Comparison
|
||||
{
|
||||
static function (array $row) use ($expr): Comparison {
|
||||
[$field, $key,, $placeholder] = $row;
|
||||
|
||||
$left = sprintf(
|
||||
@ -363,9 +155,9 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
||||
{
|
||||
@ -376,10 +168,10 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$jsonCriterias
|
||||
);
|
||||
|
||||
if ($results === []) {
|
||||
if ([] === $results) {
|
||||
$entity = $repository->getClassName();
|
||||
|
||||
return new $entity;
|
||||
return new $entity();
|
||||
}
|
||||
|
||||
if (count($results) === 1) {
|
||||
@ -387,11 +179,218 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
}
|
||||
|
||||
throw new Exception(
|
||||
sprintf(
|
||||
'Unable to create entity.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation
|
||||
{
|
||||
if (null === $evaluationTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Evaluation $eval */
|
||||
$eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]);
|
||||
$eval->setTitle(['fr' => $evaluationTitle]);
|
||||
$eval->setSocialAction($socialAction);
|
||||
|
||||
$this->entityManager->persist($eval);
|
||||
|
||||
return $eval;
|
||||
}
|
||||
|
||||
private function handleGoal(?string $goalTitle, SocialAction $socialAction): ?Goal
|
||||
{
|
||||
if (null === $goalTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Goal $goal */
|
||||
$goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]);
|
||||
$goal->setTitle(['fr' => $goalTitle]);
|
||||
|
||||
$socialAction->addGoal($goal);
|
||||
$goal->addSocialAction($socialAction);
|
||||
|
||||
//$this->entityManager->persist($socialAction);
|
||||
$this->entityManager->persist($goal);
|
||||
|
||||
return $goal;
|
||||
}
|
||||
|
||||
private function handleResult(?string $resultTitle, SocialAction $socialAction, ?Goal $goal): ?Result
|
||||
{
|
||||
if (null === $resultTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Result $result */
|
||||
$result = $this->getOrCreateEntity($this->resultRepository, 'title', ['fr' => $resultTitle]);
|
||||
$result->setTitle(['fr' => $resultTitle]);
|
||||
|
||||
if (null !== $goal) {
|
||||
$result->addGoal($goal);
|
||||
$goal->addResult($result);
|
||||
|
||||
$this->entityManager->persist($goal);
|
||||
} else {
|
||||
$result->addSocialAction($socialAction);
|
||||
$socialAction->addResult($result);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($result);
|
||||
//$this->entityManager->persist($socialAction);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array<string, SocialAction|null>
|
||||
*/
|
||||
private function handleSocialAction(
|
||||
?string $socialActionTitle,
|
||||
?string $socialActionChildTitle,
|
||||
SocialIssue $socialIssue,
|
||||
?SocialAction $previousSocialAction,
|
||||
?SocialAction $previousSocialActionChild
|
||||
): array {
|
||||
if (null === $socialActionTitle) {
|
||||
return [
|
||||
'socialAction' => null,
|
||||
'socialActionChild' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$return = [];
|
||||
|
||||
if (null !== $previousSocialAction && ($previousSocialAction->getTitle()['fr'] === $socialActionTitle)) {
|
||||
$return = [
|
||||
'socialAction' => $parent = $previousSocialAction,
|
||||
];
|
||||
$parentIsSame = true;
|
||||
} else {
|
||||
$return['socialAction'] = $parent = (new SocialAction())->setTitle(['fr' => $socialActionTitle]);
|
||||
$parent->setIssue($socialIssue);
|
||||
$this->entityManager->persist($parent);
|
||||
$parentIsSame = false;
|
||||
}
|
||||
|
||||
if (null === $socialActionChildTitle) {
|
||||
$return['socialActionChild'] = null;
|
||||
} elseif ($parentIsSame && null !== $previousSocialActionChild && $previousSocialActionChild->getTitle()['fr'] === $socialActionChildTitle) {
|
||||
$return['socialActionChild'] = $previousSocialActionChild;
|
||||
} else {
|
||||
$return['socialActionChild'] = $child = (new SocialAction())->setTitle(['fr' => $socialActionChildTitle]);
|
||||
$parent->addChild($child);
|
||||
$this->entityManager->persist($child);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array<string, SocialIssue|null>
|
||||
*/
|
||||
private function handleSocialIssue(
|
||||
?string $socialIssueTitle,
|
||||
?string $socialIssueChildTitle,
|
||||
?SocialIssue $previousSocialIssue,
|
||||
?SocialIssue $previousSocialIssueChild
|
||||
): array {
|
||||
$return = [];
|
||||
|
||||
if (null !== $previousSocialIssue && ($previousSocialIssue->getTitle()['fr'] === $socialIssueTitle)) {
|
||||
$return = [
|
||||
'socialIssue' => $parent = $previousSocialIssue,
|
||||
];
|
||||
$parentIsSame = true;
|
||||
} elseif (null !== $socialIssueTitle) {
|
||||
$return['socialIssue'] = $parent = (new SocialIssue())->setTitle(['fr' => $socialIssueTitle]);
|
||||
$this->entityManager->persist($parent);
|
||||
$parentIsSame = false;
|
||||
} else {
|
||||
return [
|
||||
'socialIssue' => null,
|
||||
'socialIssueChild' => null,
|
||||
];
|
||||
}
|
||||
|
||||
if ($parentIsSame && null !== $previousSocialIssueChild && ($previousSocialIssueChild->getTitle()['fr'] === $socialIssueChildTitle)) {
|
||||
$return['socialIssueChild'] = $previousSocialIssueChild;
|
||||
} elseif (null !== $socialIssueChildTitle) {
|
||||
$return['socialIssueChild'] = $child = (new SocialIssue())->setTitle(['fr' => $socialIssueChildTitle]);
|
||||
$parent->addChild($child);
|
||||
$this->entityManager->persist($child);
|
||||
} else {
|
||||
$return['socialIssueChild'] = null;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Row Structure:.
|
||||
*
|
||||
* Index 0: Parent SocialIssue
|
||||
* Index 1: Child SocialIssue
|
||||
* Index 2: Parent SocialAction
|
||||
* Index 3: Child SocialAction
|
||||
* Index 4: Goal
|
||||
* Index 5: Result
|
||||
* Index 6: Evaluation
|
||||
*
|
||||
* @param array<int, ?string> $row
|
||||
* @param array<string, array<string, <|array|null>|SocialIssue|string, SocialAction|null>|Evaluation|Goal|Result> $previousRow
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array<string, array<string, <|array|null>|SocialIssue|string, SocialAction|null>|Evaluation|Goal|Result>
|
||||
*/
|
||||
private function import1(int $key, array $row, array $previousRow): array
|
||||
{
|
||||
$socialIssues = $this
|
||||
->handleSocialIssue(
|
||||
$row[0],
|
||||
$row[1],
|
||||
$previousRow['socialIssues']['socialIssue'] ?? null,
|
||||
$previousRow['socialIssues']['socialIssueChild'] ?? null
|
||||
);
|
||||
|
||||
$socialIssue = $socialIssues['socialIssueChild'] ?? $socialIssues['socialIssue'];
|
||||
|
||||
if (null === $socialIssue) {
|
||||
throw new Exception(sprintf("no social issue on row {$key}, values: %s", implode(', ', $row)));
|
||||
}
|
||||
|
||||
$socialActions = $this
|
||||
->handleSocialAction(
|
||||
$row[2],
|
||||
$row[3],
|
||||
$socialIssues['socialIssue'] ?? $socialIssues,
|
||||
$previousRow['socialActions']['socialAction'] ?? null,
|
||||
$previousRow['socialActions']['socialActionChild'] ?? null
|
||||
);
|
||||
|
||||
$socialAction = $socialActions['socialActionChild'] ?? $socialActions['socialAction'];
|
||||
|
||||
if (null !== $socialAction) {
|
||||
$goal = $this->handleGoal($row[4], $socialAction);
|
||||
$result = $this->handleResult($row[5], $socialActions['socialAction'], $goal);
|
||||
$eval = $this->handleEvaluation($row[6], $socialActions['socialAction']);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return [
|
||||
'socialIssues' => $socialIssues,
|
||||
'socialActions' => $socialActions,
|
||||
'goal' => $goal ?? null,
|
||||
'result' => $result ?? null,
|
||||
'eval' => $eval ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user