new: [individuals] table capture functions added

pull/38/head
iglocska 2021-01-13 14:20:06 +01:00
parent a47dd94011
commit af3d2dd6d9
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 47 additions and 0 deletions

View File

@ -44,4 +44,51 @@ class IndividualsTable extends AppTable
->requirePresence(['email'], 'create');
return $validator;
}
public function captureIndividual($individual): ?int
{
if (!empty($individual['uuid'])) {
$existingIndividual = $this->find()->where([
'uuid' => $individual['uuid']
])->first();
} else {
return null;
}
if (empty($existingIndividual)) {
$entity = $this->newEntity($individual, ['associated' => []]);
if (!$this->save($entity)) {
return null;
}
$individual = $entity;
} else {
$reserved = ['id', 'uuid', 'metaFields'];
foreach ($individual as $field => $value) {
if (in_array($field, $reserved)) {
continue;
}
$existingIndividual->$field = $value;
}
if (!$this->save($existingIndividual, ['associated' => false])) {
return null;
}
$individual = $existingIndividua;
}
$this->postCaptureActions($individual);
return $individual->id;
}
public function postCaptureActions($individual): void
{
if (!empty($individual['metaFields'])) {
$this->saveMetaFields($id, $individual);
}
if (!empty($individual['alignments'])) {
foreach ($individual['alignments'] as $alignment) {
$org_id = $this->Organisation->captureOrg($alignment['organisation']);
if ($org_id) {
$this->Alignments->setAlignment($org_id, $individual->id, $alignment['type']);
}
}
}
}
}