<?php
namespace App\Entity;
use App\Controller\Admin\ChangesController;
use App\Helper\CompareHelper;
use App\Repository\PersonRepository;
use App\Repository\ReferenceRepository;
use App\Service\UploaderHelper;
use App\Validator\UncertainNumber;
use App\Validator\PersonFormAge;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use phpDocumentor\Reflection\Types\Boolean;
use Psr\Log\LoggerInterface;
//use Symfony\Component\Validator\Constraints\NotBlank as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass=PersonRepository::class)
* @ORM\Table(name="person", indexes={@ORM\Index(name="IDX_34DCD176A54DED42", columns={"folk_id"})})
*/
class Person extends ChangeableEntity
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Column(type="integer")
* @Groups("participant")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @Assert\NotBlank(message="das muss schon sein....")
* @Groups({"main", "info", "participant"})
*/
private $name;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups("info")
*/
private $born;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups("info")
*/
private $died;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups("info")
*/
private $bornEstimated;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups("info")
*/
private $diedEstimated;
/**
* @ORM\Column(type="string", length=5, nullable=true)
* @Groups("info")
*/
private $gender;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"info", "participant"})
*/
private $alternateNames;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $url;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"info", "participant"})
*/
private $image;
/**
* @ORM\Column(type="boolean")
*/
private $isAbstract = false;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $leafStart;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $leafOut;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $leafLevel;
/**
* @ORM\ManyToOne(targetEntity=Folk::class)
* @Groups("info")
*/
private $folk;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="getChangedPeoples")
*/
private $owner = null;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(onDelete="SET NULL")
*/
private $livedAtTimeOfPerson;
/**
* @ORM\OneToMany(targetEntity=EventPerson::class, mappedBy="person", cascade={"persist"})
*/
private $events;
/**
* @ORM\OneToMany(targetEntity=PersonReference::class, mappedBy="person", orphanRemoval=true)
*/
private $personReferences;
/**
* @ORM\Column(type="boolean")
*/
protected $approved = false;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $bornCalculated;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $diedCalculated;
/**
* @PersonFormAge
* @UncertainNumber()
*/
protected $uncertainBorn;
/**
* @UncertainNumber()
*/
protected $uncertainDied;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\ManyToMany(targetEntity=Job::class, inversedBy="people")
*/
private $job;
/**
* @ORM\ManyToMany(targetEntity=Folk::class, inversedBy="people")
*/
private $progenitor;
/**
* @ORM\OneToMany(targetEntity=PersonLink::class, mappedBy="parent", cascade={"persist"})
*/
private $children;
/**
* @ORM\OneToMany(targetEntity=PersonLink::class, mappedBy="child", cascade={"persist"})
*/
private $parents;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="taggedPersonsForDelete")
*/
protected $taggedForDeleteBy;
/**
* @ORM\ManyToOne(targetEntity=Person::class, inversedBy="updates")
*/
protected $updateOf;
/**
* @ORM\OneToMany(targetEntity=Person::class, mappedBy="updateOf")
*/
protected $updates;
/**
* @ORM\OneToMany(targetEntity=PersonVersion::class, mappedBy="person", orphanRemoval=true)
*/
private $personVersions;
public function __construct()
{
$this->events = new ArrayCollection();
$this->personReferences = new ArrayCollection();
$this->job = new ArrayCollection();
$this->progenitor = new ArrayCollection();
$this->children = new ArrayCollection();
$this->parents = new ArrayCollection();
// $this->eventsParticipated = new ArrayCollection();
$this->personVersions = new ArrayCollection();
}
public function __clone()
{
$this->id = null;
}
public function getId(): ?int
{
return $this->id;
}
public function getUniqueName(bool $unique = true): ?string
{
$parentName = '';
if($unique) {
if($this->getFather()) {
$parentName = ' (' . $this->getFather()->getName(false) .')';
} else if ($this->getMother()) {
$parentName = ' ('.$this->getMother()->getName(false).')';
}
}
return $this->name . $parentName;
}
public function getName(?User $user = null): ?string
{
if($user && $user->getPreferedVersion()) {
$ret = $this->name;
foreach($this->getPersonVersions() as $version){
if($version->getId() == $user->getPreferedVersion()) {
if($version->getName()) {
$ret = $version->getName();
}
break;
}
}
}
return $this->name;
}
public function getNameWidthAddition(?User $user): ?string
{
return $this->getName($user) . $this->getAlternateNames() ? ' (' . $this->getAlternateNames() .')' : '';
}
public function getShortName(): ?string
{
if(strpos($this->name, " ") === false)
return $this->name;
return strstr ($this->name, " ", true);
}
public function getNameWithBreak(): ?string
{
if($this->getAlternateNames() === null)
return $this->name;
return $this->name . "\n" . $this->getAlternateNames();
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getBorn(bool $estimateIfNull = false, bool $calculatedIfNull = false, $formatted = false)
{
$ret = $this->born;
$estimated = '';
if(!$this->born && $estimateIfNull === true) {
$ret = $this->getBornEstimated(1);
if($ret === null && $calculatedIfNull === true) {
$ret = $this->getBornCalculated();
$estimated = ' (c)';
} else {
$estimated = '?';
}
}
if($ret && $formatted) {
$ret = $ret < 0 ? -$ret . ' BC' : $ret . 'AD';
$ret.= $estimated;
}
return $ret;
}
public function getDied(bool $estimateIfNull = false, bool $calculatedIfNull = false, $formatted = false)
{
$ret = $this->died;
$estimated = '';
if(!$this->died && $estimateIfNull === true)
{
$ret = $this->getDiedEstimated(1);
if($ret === null && $calculatedIfNull) {
$ret = $this->getDiedCalculated();
$estimated = ' (c)';
} else {
$estimated = '?';
}
}
if($ret && $formatted) {
$ret = $ret < 0 ? -$ret . ' BC' : $ret . ' AD';
$ret.= $estimated;
}
return $ret;
}
public function setBorn(?int $born): self
{
$this->born = $born;
return $this;
}
public function setAge(?int $age) : self
{
return $this;
}
public function getAge(bool $estimateIfNull = false, bool $calculatedIfNull = false): ?int
{
$b = $this->getBorn($estimateIfNull, $calculatedIfNull);
$d = $this->getDied($estimateIfNull, $calculatedIfNull);
if($b && $d && $b <= $d){
return ($d - $b);
}
return null;
}
public function setDied(?int $died): self
{
$this->died = $died;
return $this;
}
public function getBornEstimated($recursion = 10, bool $calculationIfNull = false, $formatted = false): ?int
{
if(!$this->bornEstimated && $recursion > 0)
{
if($calculationIfNull && $this->getBornCalculated()) {
return $this->getBornCalculated();
}
if( $this->livedAtTimeOfPerson) {
if ($this->livedAtTimeOfPerson->getBorn() !== null) {
return $this->livedAtTimeOfPerson->getBorn();
}
// beware, don't use recursion (getBorn(true) here!
return $this->livedAtTimeOfPerson->getBornEstimated($recursion - 1, $calculationIfNull, $formatted);
}
}
if ($formatted) {
return $this->bornEstimated . ' ?';
}
return $this->bornEstimated;
}
public function setBornEstimated(?int $bornEstimated): self
{
$this->bornEstimated = $bornEstimated;
return $this;
}
public function getDiedEstimated($recursion = 10, bool $calculationIfNull = false, $formatted = false): ?int
{
if(!$this->diedEstimated && $recursion > 0)
{
if($calculationIfNull && $this->getDiedCalculated()) {
return $this->getDiedCalculated();
}
if($this->livedAtTimeOfPerson) {
if( $this->livedAtTimeOfPerson->getDied() !== null)
return $this->livedAtTimeOfPerson->getDied();
// beware, don't use recursion (getDied(true) here!
return $this->livedAtTimeOfPerson->getDiedEstimated($recursion - 1);
}
}
if ($formatted) {
return $this->diedEstimated . ' ?';
}
return $this->diedEstimated;
}
public function setDiedEstimated(?int $diedEstimated): self
{
$this->diedEstimated = $diedEstimated;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): self
{
$this->gender = $gender;
return $this;
}
public function getAlternateNames(): ?string
{
return $this->alternateNames;
}
public function setAlternateNames(?string $alternateNames): self
{
$this->alternateNames = $alternateNames;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function getImage($fullPath = true): ?string
{
if(!$fullPath)
{
return $this->image;
}
if($this->image !== null) {
return UploaderHelper::PERSON_IMAGE.'/'. $this->image;
} else {
if(!empty($this->getJob())){
/* @var Job $job*/
$job = $this->getJob()[0];
return UploaderHelper::PERSON_IMAGE.'/'. $job->getName() .'.png';
}
return UploaderHelper::PERSON_IMAGE.'/'. 'default_'. $this->getGender() .'.png';
}
return null;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getIsAbstract(): ?bool
{
return $this->isAbstract;
}
public function setIsAbstract(bool $isAbstract): self
{
$this->isAbstract = $isAbstract;
return $this;
}
public function getLeafStart(): ?int
{
return $this->leafStart;
}
public function setLeafStart(?int $leafStart): self
{
$this->leafStart = $leafStart;
return $this;
}
public function getLeafOut(): ?int
{
return $this->leafOut;
}
public function setLeafOut(?int $leafOut): self
{
$this->leafOut = $leafOut;
return $this;
}
public function getLeafLevel(): ?int
{
return $this->leafLevel;
}
public function setLeafLevel(?int $leafLevel): self
{
$this->leafLevel = $leafLevel;
return $this;
}
function filter_callback(Person $person, bool $isOrigin) : bool{
if ($person->getUpdateOf() == null) {
return $isOrigin;
}
return !$isOrigin;
}
public function getUpdate(?User $user) : self{
if ($user && !in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
$arr = $this->updates->toArray();
$arr = array_filter($arr, function(Person $person) use ($user){
return ($person->getOwner() == $user);
});
if(!empty($arr)) {
return array_slice($arr, 0, 1)[0];;
}
}
return $this;
}
public function getFolk()
{
return $this->folk;
}
public function setFolk(?Folk $folk): self
{
$this->folk = $folk;
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
public function isPublished() : bool
{
return $this->owner === null;
}
public function __toString()
{
return $this->name;
}
/**
* @param ExecutionContextInterface $context
* @param $payload
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
if (stripos($this->getName(), 'Hans') !== false) {
$context->buildViolation('No Hans allowed!')
->atPath('name')
->addViolation();
};
}
public function getLivedAtTimeOfPerson(): ?self
{
return $this->livedAtTimeOfPerson;
}
public function setLivedAtTimeOfPerson(?self $livedAtTimeOfPerson): self
{
$this->livedAtTimeOfPerson = $livedAtTimeOfPerson;
return $this;
}
public function getReferenceList(?User $user, $type = null): array
{
$list = [];
if($this->getUpdateOf()){
return $this->getUpdateOf()->getReferenceList($type);
}
if($type == null)
{
$list = $this->getPersonReferences();
}
else {
/* @var Reference $personReference */
foreach ($this->getPersonReferences() as $personReference) {
if (!$type || $personReference->getType() == $type) {
$list[] = $personReference;
}
}
}
foreach ($list as $key => $personReference) {
if($personReference->getTaggedForDeleteBy() == $user){
unset($list[$key]);
}
if($personReference->getApproved() == false && $personReference->getOwner() != $user){
unset($list[$key]);
}
}
$list = array_values($list);
/** @var Reference $ref*/
return $list;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event, ?User $user): self
{
$eventPerson = new EventPerson();
$eventPerson->setApproved($user->acceptsChanges() ? 1 : 0);
$eventPerson->setEvent($event);
$eventPerson->setOwner($user);
$eventPerson->setPerson($this);
//$event->addParticipant($eventPerson);
if (!$this->events->contains($eventPerson)) {
$this->events[] = $eventPerson;
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
$event->removeParticipant($this);
}
return $this;
}
/**
* //return Collection|PersonReference[]|array
*/
public function getPersonReferences(bool $asName = false)
{
$arr = [];
if($this->getUpdateOf()){
return $this->getUpdateOf()->getPersonReferences($asName);
};
/** @var PersonReference $personReference */
foreach($this->personReferences as $personReference){
if($asName) {
$arr[] = $personReference->getReference()->generatUrl();
} else {
$arr[] = $personReference;
}
}
return $arr;
}
public function getPersonReferencesAsName() {
return $this->getPersonReferences(true);
}
public function addPersonReference(PersonReference $personReference): self
{
if (!$this->personReferences->contains($personReference)) {
$this->personReferences[] = $personReference;
$personReference->setPerson($this);
}
return $this;
}
public function removePersonReference(PersonReference $personReference): self
{
if ($this->personReferences->contains($personReference)) {
$this->personReferences->removeElement($personReference);
// set the owning side to null (unless already changed)
if ($personReference->getPerson() === $this) {
$personReference->setPerson(null);
}
}
return $this;
}
/*
public function getDifference(Person $p){
$originSet = [];
foreach(get_object_vars($this) as $key => $value) {
$originSet[$key] = $value;
}
dd($originSet);
$changes = [];
foreach($originSet as $key => $value)
{
$getFunctionName = 'get' . ucfirst($key);
$success = false;
try {
if (method_exists($this, $getFunctionName)) {
$function = call_user_func(
array($this, $getFunctionName)
);
dump($key, $function, $value);
}
}
catch (\Exception $ex){
dd("Exception " . $ex);
}
}
}*/
/**
* @return mixed
* /
public function getBornUncertain()
{
return $this->bornUncertain;
}
/**
* @param mixed $bornUncertain
* /
public function setBornUncertain($bornUncertain): void
{
$this->bornUncertain = $bornUncertain;
}*/
/**
* @return mixed
*/
public function getUncertainBorn()
{
return $this->uncertainBorn;
}
/**
* @param mixed $uncertainBorn
*/
public function setUncertainBorn($uncertainBorn): void
{
$this->uncertainBorn = $uncertainBorn;
}
public function getBornCalculated(): ?int
{
return $this->bornCalculated;
}
public function setBornCalculated(?int $bornCalculated): self
{
$this->bornCalculated = $bornCalculated;
return $this;
}
public function getDiedCalculated(): ?int
{
return $this->diedCalculated;
}
public function setDiedCalculated(?int $diedCalculated): self
{
$this->diedCalculated = $diedCalculated;
return $this;
}
/**
* @return mixed
*/
public function getUncertainDied()
{
return $this->uncertainDied;
}
/**
* @param mixed $uncertainDied
*/
public function setUncertainDied($uncertainDied): void
{
$this->uncertainDied = $uncertainDied;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|Job[]
*/
public function getJob()//: Collection
{
//return $this->job;
$arr = [];
/** @var Job $job */
foreach ($this->job as $job){
$arr[] = $job;
}
return $arr;
}
public function getJobAsName(): array
{
$arr = [];
foreach($this->getJob() as $job){
$arr = $job->getName();
}
return $arr;
}
public function addJob(Job $job): self
{
if (!$this->job->contains($job)) {
$this->job[] = $job;
}
return $this;
}
public function removeJob(Job $job): self
{
if ($this->job->contains($job)) {
$this->job->removeElement($job);
}
return $this;
}
/**
* @return Collection|Folk[]
*/
public function getProgenitor(): Collection
{
return $this->progenitor;
}
public function addProgenitor(Folk $progenitor): self
{
if (!$this->progenitor->contains($progenitor)) {
$this->progenitor[] = $progenitor;
}
return $this;
}
public function removeProgenitor(Folk $progenitor): self
{
if ($this->progenitor->contains($progenitor)) {
$this->progenitor->removeElement($progenitor);
}
return $this;
}
public function showInList(User $user = null) :bool{
$ret = true;
/* if($user && in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
// dont show if is update of ...
$ret = $this->getUpdateOf(false) == null;
} else */{
$ret = !$this->hasUserUpdate($user, false);
}
return $ret;
}
public function hasUserUpdate(User $user = null): bool
{
$ret = false;
if ($user && !in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
$arr = array_filter($this->getUpdates()->toArray(), function(Person $update) use ($user) {
return ($update->getOwner() == $user);
});
$ret = !empty($arr);
}
return $ret;
}
/*
public function getUpdated(User $user = null) : Person{
$ret = $this;
if($this->getUpdateOf() && $this->getUpdateOf()->getPerson() !==null) {
if (!in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
$ret = $this->getUpdateOf()->getPerson();
}
}
return $ret;
}*/
public function getUserUpdates(User $user = null): ?array {
$ret = [];
if(!$user)
return $ret;
if (!in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
foreach ($this->getUpdates() as $change) {
if ($change->getApproved() == 0 && $user !== $change->getOwner()) {
continue;
}
$ret[] = $change;
}
} else {
foreach ($this->getUpdates() as $change) {
$ret[] = $change;
}
}
return $ret;
}
public function hasUpdate(User $user = null, bool $showForAdmin = true): bool {
$ret = false;
if($user)
{
if($showForAdmin && $user->acceptsChanges() && $this->getUpdates()->count() > 0)
{
$ret = true;
}
else
{
foreach ($this->getUpdates() as $update) {
if ($update->getOwner() == $user) {
$ret = true;
break;
}
}
}
}
return $ret;
}
/*
private function isChangeViewAllowed(EntityChange $change, User $user = null) : bool {
$ret = false;
if($user) {
if ($change->getChangedBy() == $user) {
$ret = true;
} else {
if (in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
$ret = true;
}
}
}
return $ret;
}
public function getVisibleChanges(User $user = null) : array {
$ret = [];
foreach ($this->getChanges() as $change) {
if($this->isChangeViewAllowed($change, $user)) {
if (in_array($change->getModificationType(), ['edit'])) {
if ($change->getUpdatedPerson()) {
$ret[] = $change;
}
}
}
}
return $ret;
}
public function updateFromChange(EntityManagerInterface $em, EntityChange $change){
$p= $change->getUpdatedPerson();
$em->refresh($p);
$this->setName($p->getName());
$this->setDescription($p->getDescription());
$this->setAlternateNames($p->getAlternateNames()?? "");
$this->setBorn($p->getBorn());
$this->setBornEstimated($p->getBornEstimated());
$this->setBornCalculated($p->getBornCalculated());
$this->setDied($p->getDied());
$this->setDiedEstimated($p->getDiedEstimated());
$this->setDiedCalculated($p->getDiedCalculated());
$this->setFather($p->getFather());
$this->setMother($p->getMother());
}*/
/**
* @return Collection|PersonLink[]
*/
public function getParents(): Collection
{
return $this->parents;
}
public function getFather() : ?self {
$arr = array_filter($this->getParents()->toArray(), function(PersonLink $personLink) {
return ($personLink->getParent()->getGender() == 'm');
});
if(empty($arr)){
return null;
}
return array_slice($arr, 0, 1)[0]->getParent();
}
public function setFatherParent(EntityManagerInterface $em, ?Person $person, User $user) : ?self {
return $this->setParent($em,'m', $user, $person);
}
public function setMotherParent(EntityManagerInterface $em, ?Person $person, User $user) : ?self {
return $this->setParent($em, 'w', $user, $person);
}
private function setParent(EntityManagerInterface $em, string $gender, User $user, ?Person $person) : ?self {
foreach($this->getParents() as $parentLink) {
if ($parentLink->getParent()->getGender() == $gender) {
$parentLink->setTaggedForDeleteBy(null);
$parentLink->setChild($this);
$parentLink->setParent($person);
if($user->acceptsChanges()) {
$parentLink->setApproved(1);
} else {
$parentLink->setApproved(0);
}
$this->addParent($parentLink);
return $this;
}
}
if($person) {
$link = new PersonLink();
if($user->acceptsChanges()) {
$link->setApproved(1);
} else {
$link->setApproved(0);
}
$link->setOwner($user);
$link->setParent($person)->setChild($this);
$this->addParent($link);
$person->addChild($link);
}
return $this;
}
public function getMother() : ?self {
$arr = array_filter($this->getParents()->toArray(), function(PersonLink $personLink) {
return ($personLink->getParent()->getGender() == 'w');
});
if(empty($arr)){
return null;
}
return array_slice($arr, 0, 1)[0]->getParent();
}
public function addParent(PersonLink $parent): self
{
if (!$this->parents->contains($parent)) {
$this->parents[] = $parent;
$parent->setChild($this);
}
return $this;
}
public function removeParent(PersonLink $parent): self
{
if ($this->parents->contains($parent)) {
$this->parents->removeElement($parent);
// set the owning side to null (unless already changed)
if ($parent->getChild() === $this) {
$parent->setChild(null);
}
}
return $this;
}
/**
* @param User|null $user
* @return Collection|PersonLinks[]
*/
public function getChildLinks(?User $user = null): Collection
{
if($user && $this->updateOf)
{
return new ArrayCollection(array_merge(
$this->children->toArray(),
$this->updateOf->getChildLinks($user)->toArray()
));
}
return $this->children;
}
/**
* @param User|null $user
* @param bool $excludeTaggedForDelete
* @return Collection|Person[]
*/
public function getChildren(bool $asName = false/*?User $user = null, bool $excludeTaggedForDelete = true*/): Collection
{
/*if($user && $this->updateOf)
{
return new ArrayCollection(array_merge(
$this->children->toArray(),
$this->updateOf->getChildren($user)->toArray()
));
}*/
$arr = [];
/** @var PersonLink $childLink */
foreach($this->children as $childLink){
if($asName)
{
$arr[] = $childLink->getChild()->getName();
} else
//if(!$excludeTaggedForDelete || !$childLink->getTaggedForDeleteBy())
{
$arr[] = $childLink->getChild();
}
}
return new ArrayCollection ($arr);
}
public function getChildrenAsName(): Collection{
return $this->getChildren(true);
}
public function addChild(PersonLink $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(PersonLink $child): self
{
if ($this->children->contains($child)) {
$this->children->removeElement($child);
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function hasChild(?User $user, Person $person) : bool {
$filtered = array_filter(
$this->getChildren()->toArray(),
function ($key) use ($person, $user) {
return in_array($key, [$person]);
}
);
return sizeof($filtered) > 0;
}
/**
* @return Collection|PersonVersion[]
*/
public function getPersonVersions(): Collection
{
return $this->personVersions;
}
public function addPersonVersion(PersonVersion $personVersion): self
{
if (!$this->personVersions->contains($personVersion)) {
$this->personVersions[] = $personVersion;
$personVersion->setPerson($this);
}
return $this;
}
public function removePersonVersion(PersonVersion $personVersion): self
{
if ($this->personVersions->contains($personVersion)) {
$this->personVersions->removeElement($personVersion);
// set the owning side to null (unless already changed)
if ($personVersion->getPerson() === $this) {
$personVersion->setPerson(null);
}
}
return $this;
}
public function hasVersions() : bool {
$v = [];
foreach($this->getPersonVersions() as $version){
$v[$version->getPerson()->getName()] = 1;
}
return (sizeof($v) > 1);
}
public function getOtherVersionNames() : array {
$result = [];
foreach($this->getPersonVersions() as $version){
$result[$version->getPerson()->getName()][] = $version->getVersion()->getName();
}
return $result;
}
/*public function getChanges() : array
{
$compareHelper = new CompareHelper();
$changes = [];
foreach($this->getUpdates() as $update) {
$compareHelper->cmp($this, $update, $changes);
}
//dd($changes);
return $changes;
}*/
public function getChangesAsString(?self $otherPerson) : string {
$s = "";
$compareHelper = new CompareHelper();
if($otherPerson)
{
if($this->getName() == $otherPerson->getName()) {
$s = $this->getName();
} else {
$s = '<br>Name: <s>'. $this->getName() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherPerson->getName();
}
if($this->getAlternateNames() != $otherPerson->getAlternateNames()) {
$s.='<br>Alternate Name: <s>'. $this->getAlternateNames() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherPerson->getAlternateNames();
}
if($this->getBorn() != $otherPerson->getBorn()) {
$s.='<br>Born: <s>'. $this->getBorn() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherPerson->getBorn();
}
if($this->getDied() != $otherPerson->getDied()) {
$s.='<br>Died: <s>'. $this->getDied() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherPerson->getDied();
} if($this->getAge() != $otherPerson->getAge()) {
$s.='<br>Age: <s>'. $this->getAge() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherPerson->getAge();
}
if($this->getLivedAtTimeOfPerson() != $otherPerson->getLivedAtTimeOfPerson()) {
$s.='<br>lived at times: <s>'. ($this->getLivedAtTimeOfPerson() ? $this->getLivedAtTimeOfPerson()->getName() :'')
.'</s><i class="fa fa-arrow-circle-right"></i>'. ($otherPerson->getLivedAtTimeOfPerson() ? $otherPerson->getLivedAtTimeOfPerson()->getName():'');
}
$referenceChanges = $compareHelper->cmpStringArray($this->getPersonReferencesAsName(), $otherPerson->getPersonReferencesAsName());
} else {
$s.= '<br>Name: '. $this->getName() . ' ' . $this->getAlternateNames();
$s.= '<br>Birth/Death: '. $this->getBorn() . ' - ' . $this->getDied() . ' (' . $this->getAge() . ')';
$s.='<br>Lived at times: '. $this->getLivedAtTimeOfPerson();
$referenceChanges = $compareHelper->cmpStringArray([], $this->getPersonReferencesAsName());
}
$s.= $compareHelper->generateDiffHtml("References", $referenceChanges);
return $s;
}
}