src/Entity/Person.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Controller\Admin\ChangesController;
  4. use App\Helper\CompareHelper;
  5. use App\Repository\PersonRepository;
  6. use App\Repository\ReferenceRepository;
  7. use App\Service\UploaderHelper;
  8. use App\Validator\UncertainNumber;
  9. use App\Validator\PersonFormAge;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Gedmo\Timestampable\Traits\TimestampableEntity;
  16. use phpDocumentor\Reflection\Types\Boolean;
  17. use Psr\Log\LoggerInterface;
  18. //use Symfony\Component\Validator\Constraints\NotBlank as Assert;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  22. /**
  23.  * @ORM\Entity(repositoryClass=PersonRepository::class)
  24.  * @ORM\Table(name="person", indexes={@ORM\Index(name="IDX_34DCD176A54DED42", columns={"folk_id"})})
  25.  */
  26. class Person extends ChangeableEntity
  27. {
  28.     use TimestampableEntity;
  29.     /**
  30.      * @ORM\Id()
  31.      * @ORM\GeneratedValue(strategy="IDENTITY")
  32.      * @ORM\Column(type="integer")
  33.      * @Groups("participant")
  34.      */
  35.     protected $id;
  36.     /**
  37.      * @ORM\Column(type="string", length=255, nullable=false)
  38.      * @Assert\NotBlank(message="das muss schon sein....")
  39.      * @Groups({"main", "info", "participant"})
  40.      */
  41.     private $name;
  42.     /**
  43.      * @ORM\Column(type="integer", nullable=true)
  44.      * @Groups("info")
  45.      */
  46.     private $born;
  47.     /**
  48.      * @ORM\Column(type="integer", nullable=true)
  49.      * @Groups("info")
  50.      */
  51.     private $died;
  52.     /**
  53.      * @ORM\Column(type="integer", nullable=true)
  54.      * @Groups("info")
  55.      */
  56.     private $bornEstimated;
  57.     /**
  58.      * @ORM\Column(type="integer", nullable=true)
  59.      * @Groups("info")
  60.      */
  61.     private $diedEstimated;
  62.     /**
  63.      * @ORM\Column(type="string", length=5, nullable=true)
  64.      * @Groups("info")
  65.      */
  66.     private $gender;
  67.     /**
  68.      * @ORM\Column(type="string", length=255, nullable=true)
  69.      * @Groups({"info", "participant"})
  70.      */
  71.     private $alternateNames;
  72.     /**
  73.      * @ORM\Column(type="string", length=255, nullable=true)
  74.      */
  75.     private $url;
  76.     /**
  77.      * @ORM\Column(type="string", length=255, nullable=true)
  78.      * @Groups({"info", "participant"})
  79.      */
  80.     private $image;
  81.     /**
  82.      * @ORM\Column(type="boolean")
  83.      */
  84.     private $isAbstract false;
  85.     /**
  86.      * @ORM\Column(type="integer", nullable=true)
  87.      */
  88.     private $leafStart;
  89.     /**
  90.      * @ORM\Column(type="integer", nullable=true)
  91.      */
  92.     private $leafOut;
  93.     /**
  94.      * @ORM\Column(type="integer", nullable=true)
  95.      */
  96.     private $leafLevel;
  97.     /**
  98.      * @ORM\ManyToOne(targetEntity=Folk::class)
  99.      * @Groups("info")
  100.      */
  101.     private $folk;
  102.     /**
  103.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="getChangedPeoples")
  104.      */
  105.     private $owner null;
  106.     /**
  107.      * @ORM\ManyToOne(targetEntity=Person::class)
  108.      * @ORM\JoinColumn(onDelete="SET NULL")
  109.      */
  110.     private $livedAtTimeOfPerson;
  111.     /**
  112.      * @ORM\OneToMany(targetEntity=EventPerson::class, mappedBy="person", cascade={"persist"})
  113.      */
  114.     private $events;
  115.     /**
  116.      * @ORM\OneToMany(targetEntity=PersonReference::class, mappedBy="person", orphanRemoval=true)
  117.      */
  118.     private $personReferences;
  119.     /**
  120.      * @ORM\Column(type="boolean")
  121.      */
  122.     protected $approved false;
  123.     /**
  124.      * @ORM\Column(type="integer", nullable=true)
  125.      */
  126.     private $bornCalculated;
  127.     /**
  128.      * @ORM\Column(type="integer", nullable=true)
  129.      */
  130.     private $diedCalculated;
  131.     /**
  132.      * @PersonFormAge
  133.      * @UncertainNumber()
  134.      */
  135.     protected $uncertainBorn;
  136.     /**
  137.      * @UncertainNumber()
  138.      */
  139.     protected $uncertainDied;
  140.     /**
  141.      * @ORM\Column(type="text", nullable=true)
  142.      */
  143.     private $description;
  144.     /**
  145.      * @ORM\ManyToMany(targetEntity=Job::class, inversedBy="people")
  146.      */
  147.     private $job;
  148.     /**
  149.      * @ORM\ManyToMany(targetEntity=Folk::class, inversedBy="people")
  150.      */
  151.     private $progenitor;
  152.     /**
  153.      * @ORM\OneToMany(targetEntity=PersonLink::class, mappedBy="parent", cascade={"persist"})
  154.      */
  155.     private $children;
  156.     /**
  157.      * @ORM\OneToMany(targetEntity=PersonLink::class, mappedBy="child", cascade={"persist"})
  158.      */
  159.     private $parents;
  160.     /**
  161.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="taggedPersonsForDelete")
  162.      */
  163.     protected $taggedForDeleteBy;
  164.     /**
  165.      * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="updates")
  166.      */
  167.     protected $updateOf;
  168.     /**
  169.      * @ORM\OneToMany(targetEntity=Person::class, mappedBy="updateOf")
  170.      */
  171.     protected $updates;
  172.     /**
  173.      * @ORM\OneToMany(targetEntity=PersonVersion::class, mappedBy="person", orphanRemoval=true)
  174.      */
  175.     private $personVersions;
  176.     public function __construct()
  177.     {
  178.         $this->events = new ArrayCollection();
  179.         $this->personReferences = new ArrayCollection();
  180.         $this->job = new ArrayCollection();
  181.         $this->progenitor = new ArrayCollection();
  182.         $this->children = new ArrayCollection();
  183.         $this->parents = new ArrayCollection();
  184.        // $this->eventsParticipated = new ArrayCollection();
  185.         $this->personVersions = new ArrayCollection();
  186.     }
  187.     public function __clone()
  188.     {
  189.         $this->id null;
  190.     }
  191.     public function getId(): ?int
  192.     {
  193.         return $this->id;
  194.     }
  195.     public function getUniqueName(bool $unique true): ?string
  196.     {
  197.         $parentName '';
  198.         if($unique) {
  199.             if($this->getFather()) {
  200.                 $parentName ' (' $this->getFather()->getName(false) .')';
  201.             } else if ($this->getMother()) {
  202.                 $parentName ' ('.$this->getMother()->getName(false).')';
  203.             }
  204.         }
  205.         return $this->name $parentName;
  206.     }
  207.     public function getName(?User $user null): ?string
  208.     {
  209.         if($user && $user->getPreferedVersion()) {
  210.             $ret $this->name;
  211.             foreach($this->getPersonVersions() as $version){
  212.                 if($version->getId() == $user->getPreferedVersion()) {
  213.                     if($version->getName()) {
  214.                         $ret $version->getName();
  215.                     }
  216.                     break;
  217.                 }
  218.             }
  219.         }
  220.         return $this->name;
  221.     }
  222.     public function getNameWidthAddition(?User $user): ?string
  223.     {
  224.         return $this->getName($user) . $this->getAlternateNames() ? ' (' $this->getAlternateNames() .')' '';
  225.     }
  226.     public function getShortName(): ?string
  227.     {
  228.         if(strpos($this->name" ") === false)
  229.             return $this->name;
  230.         return strstr ($this->name" "true);
  231.     }
  232.     public function getNameWithBreak(): ?string
  233.     {
  234.         if($this->getAlternateNames() === null)
  235.             return $this->name;
  236.         return $this->name "\n" $this->getAlternateNames();
  237.     }
  238.     public function setName(string $name): self
  239.     {
  240.         $this->name $name;
  241.         return $this;
  242.     }
  243.     public function getBorn(bool $estimateIfNull falsebool $calculatedIfNull false$formatted false)
  244.     {
  245.         $ret $this->born;
  246.         $estimated '';
  247.         if(!$this->born && $estimateIfNull === true) {
  248.             $ret =  $this->getBornEstimated(1);
  249.             if($ret === null && $calculatedIfNull === true) {
  250.                 $ret $this->getBornCalculated();
  251.                 $estimated ' (c)';
  252.             } else {
  253.                 $estimated '?';
  254.             }
  255.         }
  256.         if($ret && $formatted) {
  257.             $ret $ret ? -$ret ' BC' $ret 'AD';
  258.             $ret.= $estimated;
  259.         }
  260.         return $ret;
  261.     }
  262.     public function getDied(bool $estimateIfNull falsebool $calculatedIfNull false$formatted false)
  263.     {
  264.         $ret $this->died;
  265.         $estimated '';
  266.         if(!$this->died && $estimateIfNull === true)
  267.         {
  268.             $ret $this->getDiedEstimated(1);
  269.             if($ret === null && $calculatedIfNull) {
  270.                 $ret $this->getDiedCalculated();
  271.                 $estimated ' (c)';
  272.             } else {
  273.                 $estimated '?';
  274.             }
  275.         }
  276.         if($ret && $formatted) {
  277.             $ret $ret ? -$ret ' BC' $ret ' AD';
  278.             $ret.= $estimated;
  279.         }
  280.         return $ret;
  281.     }
  282.     public function setBorn(?int $born): self
  283.     {
  284.         $this->born $born;
  285.         return $this;
  286.     }
  287.     public function setAge(?int $age) : self
  288.     {
  289.         return $this;
  290.     }
  291.     public function getAge(bool $estimateIfNull falsebool $calculatedIfNull false): ?int
  292.     {
  293.         $b $this->getBorn($estimateIfNull$calculatedIfNull);
  294.         $d $this->getDied($estimateIfNull$calculatedIfNull);
  295.         if($b && $d && $b <= $d){
  296.             return ($d $b);
  297.         }
  298.         return null;
  299.     }
  300.     public function setDied(?int $died): self
  301.     {
  302.         $this->died $died;
  303.         return $this;
  304.     }
  305.     public function getBornEstimated($recursion 10bool $calculationIfNull false$formatted false): ?int
  306.     {
  307.         if(!$this->bornEstimated && $recursion 0)
  308.         {
  309.             if($calculationIfNull && $this->getBornCalculated()) {
  310.                 return $this->getBornCalculated();
  311.             }
  312.             if( $this->livedAtTimeOfPerson) {
  313.                 if ($this->livedAtTimeOfPerson->getBorn() !== null) {
  314.                     return $this->livedAtTimeOfPerson->getBorn();
  315.                 }
  316.                 // beware, don't use recursion  (getBorn(true)  here!
  317.                 return $this->livedAtTimeOfPerson->getBornEstimated($recursion 1$calculationIfNull$formatted);
  318.             }
  319.         }
  320.         if ($formatted) {
  321.             return $this->bornEstimated ' ?';
  322.         }
  323.         return $this->bornEstimated;
  324.     }
  325.     public function setBornEstimated(?int $bornEstimated): self
  326.     {
  327.         $this->bornEstimated $bornEstimated;
  328.         return $this;
  329.     }
  330.     public function getDiedEstimated($recursion 10bool $calculationIfNull false$formatted false): ?int
  331.     {
  332.         if(!$this->diedEstimated && $recursion 0)
  333.         {
  334.             if($calculationIfNull && $this->getDiedCalculated()) {
  335.                 return $this->getDiedCalculated();
  336.             }
  337.             if($this->livedAtTimeOfPerson) {
  338.                 if( $this->livedAtTimeOfPerson->getDied() !== null)
  339.                     return $this->livedAtTimeOfPerson->getDied();
  340.                 // beware, don't use recursion  (getDied(true)  here!
  341.                 return $this->livedAtTimeOfPerson->getDiedEstimated($recursion 1);
  342.             }
  343.         }
  344.         if ($formatted) {
  345.             return $this->diedEstimated ' ?';
  346.         }
  347.         return $this->diedEstimated;
  348.     }
  349.     public function setDiedEstimated(?int $diedEstimated): self
  350.     {
  351.         $this->diedEstimated $diedEstimated;
  352.         return $this;
  353.     }
  354.     public function getGender(): ?string
  355.     {
  356.         return $this->gender;
  357.     }
  358.     public function setGender(?string $gender): self
  359.     {
  360.         $this->gender $gender;
  361.         return $this;
  362.     }
  363.     public function getAlternateNames(): ?string
  364.     {
  365.         return $this->alternateNames;
  366.     }
  367.     public function setAlternateNames(?string $alternateNames): self
  368.     {
  369.         $this->alternateNames $alternateNames;
  370.         return $this;
  371.     }
  372.     public function getUrl(): ?string
  373.     {
  374.         return $this->url;
  375.     }
  376.     public function setUrl(?string $url): self
  377.     {
  378.         $this->url $url;
  379.         return $this;
  380.     }
  381.     public function getImage($fullPath true): ?string
  382.     {
  383.         if(!$fullPath)
  384.         {
  385.             return $this->image;
  386.         }
  387.         if($this->image !== null) {
  388.             return UploaderHelper::PERSON_IMAGE.'/'$this->image;
  389.         } else {
  390.             if(!empty($this->getJob())){
  391.                 /* @var Job $job*/
  392.                 $job $this->getJob()[0];
  393.                 return UploaderHelper::PERSON_IMAGE.'/'$job->getName() .'.png';
  394.             }
  395.             return UploaderHelper::PERSON_IMAGE.'/''default_'$this->getGender() .'.png';
  396.         }
  397.         return null;
  398.     }
  399.     public function setImage(?string $image): self
  400.     {
  401.         $this->image $image;
  402.         return $this;
  403.     }
  404.     public function getIsAbstract(): ?bool
  405.     {
  406.         return $this->isAbstract;
  407.     }
  408.     public function setIsAbstract(bool $isAbstract): self
  409.     {
  410.         $this->isAbstract $isAbstract;
  411.         return $this;
  412.     }
  413.     public function getLeafStart(): ?int
  414.     {
  415.         return $this->leafStart;
  416.     }
  417.     public function setLeafStart(?int $leafStart): self
  418.     {
  419.         $this->leafStart $leafStart;
  420.         return $this;
  421.     }
  422.     public function getLeafOut(): ?int
  423.     {
  424.         return $this->leafOut;
  425.     }
  426.     public function setLeafOut(?int $leafOut): self
  427.     {
  428.         $this->leafOut $leafOut;
  429.         return $this;
  430.     }
  431.     public function getLeafLevel(): ?int
  432.     {
  433.         return $this->leafLevel;
  434.     }
  435.     public function setLeafLevel(?int $leafLevel): self
  436.     {
  437.         $this->leafLevel $leafLevel;
  438.         return $this;
  439.     }
  440.     function filter_callback(Person $personbool $isOrigin) : bool{
  441.         if ($person->getUpdateOf() == null) {
  442.             return $isOrigin;
  443.         }
  444.         return !$isOrigin;
  445.     }
  446.     public function getUpdate(?User $user) : self{
  447.         if ($user && !in_array('ROLE_ACCEPT_CHANGES'$user->getRoles())) {
  448.             $arr $this->updates->toArray();
  449.             $arr array_filter($arr, function(Person $person) use ($user){
  450.                 return ($person->getOwner() == $user);
  451.             });
  452.             if(!empty($arr)) {
  453.                 return array_slice($arr01)[0];;
  454.             }
  455.         }
  456.         return $this;
  457.     }
  458.     public function getFolk()
  459.     {
  460.         return $this->folk;
  461.     }
  462.     public function setFolk(?Folk $folk): self
  463.     {
  464.         $this->folk $folk;
  465.         return $this;
  466.     }
  467.     public function getOwner(): ?User
  468.     {
  469.         return $this->owner;
  470.     }
  471.     public function setOwner(?User $owner): self
  472.     {
  473.         $this->owner $owner;
  474.         return $this;
  475.     }
  476.     public function isPublished() : bool
  477.     {
  478.         return $this->owner === null;
  479.     }
  480.     public function __toString()
  481.     {
  482.         return $this->name;
  483.     }
  484.     /**
  485.      * @param ExecutionContextInterface $context
  486.      * @param $payload
  487.      * @Assert\Callback
  488.      */
  489.     public function validate(ExecutionContextInterface $context$payload)
  490.     {
  491.         if (stripos($this->getName(), 'Hans') !== false) {
  492.             $context->buildViolation('No Hans allowed!')
  493.                 ->atPath('name')
  494.                 ->addViolation();
  495.         };
  496.     }
  497.     public function getLivedAtTimeOfPerson(): ?self
  498.     {
  499.         return $this->livedAtTimeOfPerson;
  500.     }
  501.     public function setLivedAtTimeOfPerson(?self $livedAtTimeOfPerson): self
  502.     {
  503.         $this->livedAtTimeOfPerson $livedAtTimeOfPerson;
  504.         return $this;
  505.     }
  506.     public function getReferenceList(?User $user$type null): array
  507.     {
  508.         $list = [];
  509.         if($this->getUpdateOf()){
  510.             return $this->getUpdateOf()->getReferenceList($type);
  511.         }
  512.         if($type == null)
  513.         {
  514.             $list $this->getPersonReferences();
  515.         }
  516.         else {
  517.             /* @var  Reference $personReference */
  518.             foreach ($this->getPersonReferences() as $personReference) {
  519.                 if (!$type || $personReference->getType() == $type) {
  520.                     $list[] = $personReference;
  521.                 }
  522.             }
  523.         }
  524.         foreach ($list as $key => $personReference) {
  525.             if($personReference->getTaggedForDeleteBy() == $user){
  526.                 unset($list[$key]);
  527.             }
  528.             if($personReference->getApproved() == false && $personReference->getOwner() != $user){
  529.                 unset($list[$key]);
  530.             }
  531.         }
  532.         $list array_values($list);
  533.         /** @var Reference $ref*/
  534.        return $list;
  535.     }
  536.     /**
  537.      * @return Collection|Event[]
  538.      */
  539.     public function getEvents(): Collection
  540.     {
  541.         return $this->events;
  542.     }
  543.     public function addEvent(Event $event, ?User $user): self
  544.     {
  545.         $eventPerson = new EventPerson();
  546.         $eventPerson->setApproved($user->acceptsChanges() ? 0);
  547.         $eventPerson->setEvent($event);
  548.         $eventPerson->setOwner($user);
  549.         $eventPerson->setPerson($this);
  550.         //$event->addParticipant($eventPerson);
  551.         if (!$this->events->contains($eventPerson)) {
  552.             $this->events[] = $eventPerson;
  553.         }
  554.         return $this;
  555.     }
  556.     public function removeEvent(Event $event): self
  557.     {
  558.         if ($this->events->contains($event)) {
  559.             $this->events->removeElement($event);
  560.             $event->removeParticipant($this);
  561.         }
  562.         return $this;
  563.     }
  564.     /**
  565.      * //return Collection|PersonReference[]|array
  566.      */
  567.     public function getPersonReferences(bool $asName false)
  568.     {
  569.         $arr = [];
  570.         if($this->getUpdateOf()){
  571.             return $this->getUpdateOf()->getPersonReferences($asName);
  572.         };
  573.         /** @var PersonReference $personReference */
  574.         foreach($this->personReferences as $personReference){
  575.             if($asName) {
  576.                 $arr[] = $personReference->getReference()->generatUrl();
  577.             } else {
  578.                 $arr[] = $personReference;
  579.             }
  580.         }
  581.         return $arr;
  582.     }
  583.     public function getPersonReferencesAsName() {
  584.         return $this->getPersonReferences(true);
  585.     }
  586.     public function addPersonReference(PersonReference $personReference): self
  587.     {
  588.         if (!$this->personReferences->contains($personReference)) {
  589.             $this->personReferences[] = $personReference;
  590.             $personReference->setPerson($this);
  591.         }
  592.         return $this;
  593.     }
  594.     public function removePersonReference(PersonReference $personReference): self
  595.     {
  596.         if ($this->personReferences->contains($personReference)) {
  597.             $this->personReferences->removeElement($personReference);
  598.             // set the owning side to null (unless already changed)
  599.             if ($personReference->getPerson() === $this) {
  600.                 $personReference->setPerson(null);
  601.             }
  602.         }
  603.         return $this;
  604.     }
  605. /*
  606.     public function getDifference(Person $p){
  607.         $originSet = [];
  608.         foreach(get_object_vars($this) as $key => $value) {
  609.             $originSet[$key] = $value;
  610.         }
  611. dd($originSet);
  612.         $changes = [];
  613.         foreach($originSet as $key => $value)
  614.         {
  615.             $getFunctionName = 'get' . ucfirst($key);
  616.             $success = false;
  617.             try {
  618.                 if (method_exists($this, $getFunctionName)) {
  619.                     $function = call_user_func(
  620.                         array($this, $getFunctionName)
  621.                     );
  622.                     dump($key, $function, $value);
  623.                 }
  624.             }
  625.             catch (\Exception $ex){
  626.                 dd("Exception " . $ex);
  627.             }
  628.         }
  629.     }*/
  630.     /**
  631.      * @return mixed
  632.      * /
  633.     public function getBornUncertain()
  634.     {
  635.         return $this->bornUncertain;
  636.     }
  637.     /**
  638.      * @param mixed $bornUncertain
  639.      * /
  640.     public function setBornUncertain($bornUncertain): void
  641.     {
  642.         $this->bornUncertain = $bornUncertain;
  643.     }*/
  644.     /**
  645.      * @return mixed
  646.      */
  647.     public function getUncertainBorn()
  648.     {
  649.         return $this->uncertainBorn;
  650.     }
  651.     /**
  652.      * @param mixed $uncertainBorn
  653.      */
  654.     public function setUncertainBorn($uncertainBorn): void
  655.     {
  656.         $this->uncertainBorn $uncertainBorn;
  657.     }
  658.     public function getBornCalculated(): ?int
  659.     {
  660.         return $this->bornCalculated;
  661.     }
  662.     public function setBornCalculated(?int $bornCalculated): self
  663.     {
  664.         $this->bornCalculated $bornCalculated;
  665.         return $this;
  666.     }
  667.     public function getDiedCalculated(): ?int
  668.     {
  669.         return $this->diedCalculated;
  670.     }
  671.     public function setDiedCalculated(?int $diedCalculated): self
  672.     {
  673.         $this->diedCalculated $diedCalculated;
  674.         return $this;
  675.     }
  676.     /**
  677.      * @return mixed
  678.      */
  679.     public function getUncertainDied()
  680.     {
  681.         return $this->uncertainDied;
  682.     }
  683.     /**
  684.      * @param mixed $uncertainDied
  685.      */
  686.     public function setUncertainDied($uncertainDied): void
  687.     {
  688.         $this->uncertainDied $uncertainDied;
  689.     }
  690.     public function getDescription(): ?string
  691.     {
  692.         return $this->description;
  693.     }
  694.     public function setDescription(?string $description): self
  695.     {
  696.         $this->description $description;
  697.         return $this;
  698.     }
  699.     /**
  700.      * @return Collection|Job[]
  701.      */
  702.     public function getJob()//: Collection
  703.     {
  704.         //return $this->job;
  705.         $arr = [];
  706.         /** @var Job $job */
  707.         foreach ($this->job as $job){
  708.             $arr[] = $job;
  709.         }
  710.         return $arr;
  711.     }
  712.     public function getJobAsName(): array
  713.     {
  714.         $arr = [];
  715.         foreach($this->getJob() as $job){
  716.             $arr $job->getName();
  717.         }
  718.         return $arr;
  719.     }
  720.     public function addJob(Job $job): self
  721.     {
  722.         if (!$this->job->contains($job)) {
  723.             $this->job[] = $job;
  724.         }
  725.         return $this;
  726.     }
  727.     public function removeJob(Job $job): self
  728.     {
  729.         if ($this->job->contains($job)) {
  730.             $this->job->removeElement($job);
  731.         }
  732.         return $this;
  733.     }
  734.     /**
  735.      * @return Collection|Folk[]
  736.      */
  737.     public function getProgenitor(): Collection
  738.     {
  739.         return $this->progenitor;
  740.     }
  741.     public function addProgenitor(Folk $progenitor): self
  742.     {
  743.         if (!$this->progenitor->contains($progenitor)) {
  744.             $this->progenitor[] = $progenitor;
  745.         }
  746.         return $this;
  747.     }
  748.     public function removeProgenitor(Folk $progenitor): self
  749.     {
  750.         if ($this->progenitor->contains($progenitor)) {
  751.             $this->progenitor->removeElement($progenitor);
  752.         }
  753.         return $this;
  754.     }
  755.     public function showInList(User $user null) :bool{
  756.         $ret true;
  757.        /* if($user && in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
  758.             // dont show if is update of ...
  759.             $ret = $this->getUpdateOf(false) == null;
  760.         } else */{
  761.             $ret = !$this->hasUserUpdate($userfalse);
  762.         }
  763.         return $ret;
  764.     }
  765.     public function hasUserUpdate(User $user null): bool
  766.     {
  767.         $ret false;
  768.         if ($user && !in_array('ROLE_ACCEPT_CHANGES'$user->getRoles())) {
  769.             $arr array_filter($this->getUpdates()->toArray(), function(Person $update) use ($user) {
  770.                 return ($update->getOwner() == $user);
  771.             });
  772.             $ret = !empty($arr);
  773.         }
  774.         return $ret;
  775.     }
  776. /*
  777. public function getUpdated(User $user = null) : Person{
  778.     $ret = $this;
  779.     if($this->getUpdateOf() && $this->getUpdateOf()->getPerson() !==null) {
  780.         if (!in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
  781.             $ret = $this->getUpdateOf()->getPerson();
  782.         }
  783.     }
  784.     return $ret;
  785. }*/
  786.     public function getUserUpdates(User $user null): ?array {
  787.         $ret = [];
  788.         if(!$user)
  789.             return $ret;
  790.         if (!in_array('ROLE_ACCEPT_CHANGES'$user->getRoles())) {
  791.             foreach ($this->getUpdates() as $change) {
  792.                 if ($change->getApproved() == &&  $user !== $change->getOwner()) {
  793.                     continue;
  794.                 }
  795.                 $ret[] = $change;
  796.             }
  797.         } else {
  798.             foreach ($this->getUpdates() as $change) {
  799.                 $ret[] = $change;
  800.             }
  801.         }
  802.         return $ret;
  803.     }
  804.     public function hasUpdate(User $user nullbool $showForAdmin true): bool {
  805.         $ret false;
  806.         if($user)
  807.         {
  808.             if($showForAdmin && $user->acceptsChanges() && $this->getUpdates()->count() > 0)
  809.             {
  810.                 $ret true;
  811.             }
  812.             else
  813.             {
  814.                 foreach ($this->getUpdates() as $update) {
  815.                     if ($update->getOwner() == $user) {
  816.                         $ret true;
  817.                         break;
  818.                     }
  819.                 }
  820.             }
  821.         }
  822.         return $ret;
  823.     }
  824. /*
  825.     private function isChangeViewAllowed(EntityChange $change, User $user = null) : bool {
  826.         $ret = false;
  827.         if($user) {
  828.             if ($change->getChangedBy() == $user) {
  829.                 $ret = true;
  830.             } else {
  831.                 if (in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
  832.                     $ret = true;
  833.                 }
  834.             }
  835.         }
  836.         return $ret;
  837.     }
  838.     public function getVisibleChanges(User $user = null) : array  {
  839.         $ret = [];
  840.         foreach ($this->getChanges() as $change) {
  841.             if($this->isChangeViewAllowed($change, $user)) {
  842.                 if (in_array($change->getModificationType(), ['edit'])) {
  843.                     if ($change->getUpdatedPerson()) {
  844.                         $ret[] = $change;
  845.                     }
  846.                 }
  847.             }
  848.         }
  849.         return $ret;
  850.     }
  851.     public function updateFromChange(EntityManagerInterface $em, EntityChange $change){
  852.         $p= $change->getUpdatedPerson();
  853.         $em->refresh($p);
  854.         $this->setName($p->getName());
  855.         $this->setDescription($p->getDescription());
  856.         $this->setAlternateNames($p->getAlternateNames()?? "");
  857.         $this->setBorn($p->getBorn());
  858.         $this->setBornEstimated($p->getBornEstimated());
  859.         $this->setBornCalculated($p->getBornCalculated());
  860.         $this->setDied($p->getDied());
  861.         $this->setDiedEstimated($p->getDiedEstimated());
  862.         $this->setDiedCalculated($p->getDiedCalculated());
  863.         $this->setFather($p->getFather());
  864.         $this->setMother($p->getMother());
  865.     }*/
  866.     /**
  867.      * @return Collection|PersonLink[]
  868.      */
  869.     public function getParents(): Collection
  870.     {
  871.         return $this->parents;
  872.     }
  873.     public function getFather() : ?self {
  874.          $arr array_filter($this->getParents()->toArray(), function(PersonLink $personLink) {
  875.              return ($personLink->getParent()->getGender() == 'm');
  876.          });
  877.          if(empty($arr)){
  878.              return null;
  879.          }
  880.         return array_slice($arr01)[0]->getParent();
  881.     }
  882.     public function setFatherParent(EntityManagerInterface $em, ?Person $personUser $user) : ?self {
  883.         return $this->setParent($em,'m'$user$person);
  884.     }
  885.     public function setMotherParent(EntityManagerInterface $em, ?Person $personUser $user) : ?self {
  886.         return $this->setParent($em'w'$user$person);
  887.     }
  888.     private function setParent(EntityManagerInterface $emstring $genderUser $user, ?Person $person) : ?self {
  889.         foreach($this->getParents() as $parentLink) {
  890.             if ($parentLink->getParent()->getGender() == $gender) {
  891.                 $parentLink->setTaggedForDeleteBy(null);
  892.                 $parentLink->setChild($this);
  893.                 $parentLink->setParent($person);
  894.                 if($user->acceptsChanges()) {
  895.                     $parentLink->setApproved(1);
  896.                 } else {
  897.                     $parentLink->setApproved(0);
  898.                 }
  899.                 $this->addParent($parentLink);
  900.                 return $this;
  901.             }
  902.         }
  903.         if($person) {
  904.             $link = new PersonLink();
  905.             if($user->acceptsChanges()) {
  906.                 $link->setApproved(1);
  907.             } else {
  908.                 $link->setApproved(0);
  909.             }
  910.             $link->setOwner($user);
  911.             $link->setParent($person)->setChild($this);
  912.             $this->addParent($link);
  913.             $person->addChild($link);
  914.         }
  915.         return $this;
  916.     }
  917.     public function getMother() : ?self {
  918.         $arr array_filter($this->getParents()->toArray(), function(PersonLink $personLink) {
  919.             return ($personLink->getParent()->getGender() == 'w');
  920.         });
  921.         if(empty($arr)){
  922.             return null;
  923.         }
  924.         return array_slice($arr01)[0]->getParent();
  925.     }
  926.     public function addParent(PersonLink $parent): self
  927.     {
  928.         if (!$this->parents->contains($parent)) {
  929.             $this->parents[] = $parent;
  930.             $parent->setChild($this);
  931.         }
  932.         return $this;
  933.     }
  934.     public function removeParent(PersonLink $parent): self
  935.     {
  936.         if ($this->parents->contains($parent)) {
  937.             $this->parents->removeElement($parent);
  938.             // set the owning side to null (unless already changed)
  939.             if ($parent->getChild() === $this) {
  940.                 $parent->setChild(null);
  941.             }
  942.         }
  943.         return $this;
  944.     }
  945.     /**
  946.      * @param User|null $user
  947.      * @return Collection|PersonLinks[]
  948.      */
  949.     public function getChildLinks(?User $user null): Collection
  950.     {
  951.         if($user && $this->updateOf)
  952.         {
  953.             return new ArrayCollection(array_merge(
  954.                 $this->children->toArray(),
  955.                 $this->updateOf->getChildLinks($user)->toArray()
  956.             ));
  957.         }
  958.         return $this->children;
  959.     }
  960.     /**
  961.      * @param User|null $user
  962.      * @param bool $excludeTaggedForDelete
  963.      * @return Collection|Person[]
  964.      */
  965.     public function getChildren(bool $asName false/*?User $user = null, bool $excludeTaggedForDelete = true*/): Collection
  966.     {
  967.         /*if($user && $this->updateOf)
  968.         {
  969.             return new ArrayCollection(array_merge(
  970.                 $this->children->toArray(),
  971.                 $this->updateOf->getChildren($user)->toArray()
  972.             ));
  973.         }*/
  974.         $arr = [];
  975.         /** @var PersonLink $childLink */
  976.         foreach($this->children as $childLink){
  977.             if($asName)
  978.             {
  979.                 $arr[] = $childLink->getChild()->getName();
  980.             } else
  981.             //if(!$excludeTaggedForDelete || !$childLink->getTaggedForDeleteBy())
  982.             {
  983.                 $arr[] = $childLink->getChild();
  984.             }
  985.         }
  986.         return new ArrayCollection ($arr);
  987.     }
  988.     public function getChildrenAsName(): Collection{
  989.         return $this->getChildren(true);
  990.     }
  991.     public function addChild(PersonLink $child): self
  992.     {
  993.         if (!$this->children->contains($child)) {
  994.             $this->children[] = $child;
  995.             $child->setParent($this);
  996.         }
  997.         return $this;
  998.     }
  999.     public function removeChild(PersonLink $child): self
  1000.     {
  1001.         if ($this->children->contains($child)) {
  1002.             $this->children->removeElement($child);
  1003.             // set the owning side to null (unless already changed)
  1004.             if ($child->getParent() === $this) {
  1005.                 $child->setParent(null);
  1006.             }
  1007.         }
  1008.         return $this;
  1009.     }
  1010.     public function hasChild(?User $userPerson $person) : bool {
  1011.         $filtered array_filter(
  1012.             $this->getChildren()->toArray(),
  1013.             function ($key) use ($person$user) {
  1014.                 return in_array($key, [$person]);
  1015.             }
  1016.         );
  1017.         return sizeof($filtered) > 0;
  1018.     }
  1019.     /**
  1020.      * @return Collection|PersonVersion[]
  1021.      */
  1022.     public function getPersonVersions(): Collection
  1023.     {
  1024.         return $this->personVersions;
  1025.     }
  1026.     public function addPersonVersion(PersonVersion $personVersion): self
  1027.     {
  1028.         if (!$this->personVersions->contains($personVersion)) {
  1029.             $this->personVersions[] = $personVersion;
  1030.             $personVersion->setPerson($this);
  1031.         }
  1032.         return $this;
  1033.     }
  1034.     public function removePersonVersion(PersonVersion $personVersion): self
  1035.     {
  1036.         if ($this->personVersions->contains($personVersion)) {
  1037.             $this->personVersions->removeElement($personVersion);
  1038.             // set the owning side to null (unless already changed)
  1039.             if ($personVersion->getPerson() === $this) {
  1040.                 $personVersion->setPerson(null);
  1041.             }
  1042.         }
  1043.         return $this;
  1044.     }
  1045.     public function hasVersions() : bool {
  1046.         $v = [];
  1047.         foreach($this->getPersonVersions() as $version){
  1048.             $v[$version->getPerson()->getName()] = 1;
  1049.         }
  1050.         return (sizeof($v) > 1);
  1051.     }
  1052.     public function getOtherVersionNames() : array {
  1053.         $result = [];
  1054.         foreach($this->getPersonVersions() as $version){
  1055.             $result[$version->getPerson()->getName()][] = $version->getVersion()->getName();
  1056.         }
  1057.         return $result;
  1058.     }
  1059.     /*public function getChanges() : array
  1060.     {
  1061.         $compareHelper = new CompareHelper();
  1062.         $changes = [];
  1063.         foreach($this->getUpdates() as $update) {
  1064.             $compareHelper->cmp($this, $update, $changes);
  1065.         }
  1066.         //dd($changes);
  1067.         return $changes;
  1068.     }*/
  1069.     public function getChangesAsString(?self $otherPerson) : string {
  1070.         $s "";
  1071.         $compareHelper = new CompareHelper();
  1072.         if($otherPerson)
  1073.         {
  1074.             if($this->getName() == $otherPerson->getName()) {
  1075.                 $s $this->getName();
  1076.             } else {
  1077.                 $s '<br>Name: <s>'$this->getName() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherPerson->getName();
  1078.             }
  1079.             if($this->getAlternateNames() != $otherPerson->getAlternateNames()) {
  1080.                 $s.='<br>Alternate Name: <s>'$this->getAlternateNames() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherPerson->getAlternateNames();
  1081.             }
  1082.             if($this->getBorn() != $otherPerson->getBorn()) {
  1083.                 $s.='<br>Born: <s>'$this->getBorn() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherPerson->getBorn();
  1084.             }
  1085.             if($this->getDied() != $otherPerson->getDied()) {
  1086.                 $s.='<br>Died: <s>'$this->getDied() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherPerson->getDied();
  1087.             } if($this->getAge() != $otherPerson->getAge()) {
  1088.                 $s.='<br>Age: <s>'$this->getAge() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherPerson->getAge();
  1089.             }
  1090.             if($this->getLivedAtTimeOfPerson() != $otherPerson->getLivedAtTimeOfPerson()) {
  1091.                 $s.='<br>lived at times: <s>'. ($this->getLivedAtTimeOfPerson() ? $this->getLivedAtTimeOfPerson()->getName() :'')
  1092.                     .'</s><i class="fa fa-arrow-circle-right"></i>'. ($otherPerson->getLivedAtTimeOfPerson() ? $otherPerson->getLivedAtTimeOfPerson()->getName():'');
  1093.             }
  1094.             $referenceChanges $compareHelper->cmpStringArray($this->getPersonReferencesAsName(), $otherPerson->getPersonReferencesAsName());
  1095.         } else {
  1096.             $s.= '<br>Name: '$this->getName() . ' ' .  $this->getAlternateNames();
  1097.             $s.= '<br>Birth/Death: '$this->getBorn() . ' - ' .  $this->getDied() . ' (' $this->getAge() . ')';
  1098.             $s.='<br>Lived at times:  '$this->getLivedAtTimeOfPerson();
  1099.             $referenceChanges $compareHelper->cmpStringArray([], $this->getPersonReferencesAsName());
  1100.         }
  1101.         $s.= $compareHelper->generateDiffHtml("References"$referenceChanges);
  1102.         return $s;
  1103.     }
  1104. }