2020-05-29 13:41:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use App\Model\Table\AppTable;
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\Validation\Validator;
|
|
|
|
|
|
|
|
class IndividualsTable extends AppTable
|
|
|
|
{
|
2020-11-20 11:49:31 +01:00
|
|
|
public $metaFields = 'individual';
|
|
|
|
|
2020-05-29 13:41:58 +02:00
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
2020-06-19 00:39:29 +02:00
|
|
|
$this->addBehavior('UUID');
|
2021-09-28 13:32:51 +02:00
|
|
|
$this->addBehavior('Timestamp');
|
2021-09-02 11:30:09 +02:00
|
|
|
$this->addBehavior('Tags.Tag');
|
2020-06-04 10:05:45 +02:00
|
|
|
$this->hasMany(
|
|
|
|
'Alignments',
|
|
|
|
[
|
|
|
|
'dependent' => true,
|
|
|
|
'cascadeCallbacks' => true
|
|
|
|
]
|
|
|
|
);
|
2020-05-29 13:41:58 +02:00
|
|
|
$this->hasMany(
|
|
|
|
'EncryptionKeys',
|
|
|
|
[
|
|
|
|
'foreignKey' => 'owner_id',
|
2021-03-15 22:47:13 +01:00
|
|
|
'conditions' => ['owner_model' => 'individual']
|
2020-05-29 13:41:58 +02:00
|
|
|
]
|
|
|
|
);
|
2020-06-19 00:39:29 +02:00
|
|
|
$this->hasOne(
|
|
|
|
'Users'
|
|
|
|
);
|
|
|
|
$this->belongsToMany('Organisations', [
|
|
|
|
'through' => 'Alignments',
|
|
|
|
]);
|
2020-06-04 10:05:45 +02:00
|
|
|
$this->setDisplayField('email');
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
|
|
|
->notEmptyString('email')
|
2020-06-19 00:39:29 +02:00
|
|
|
->requirePresence(['email'], 'create');
|
2020-05-29 13:41:58 +02:00
|
|
|
return $validator;
|
|
|
|
}
|
2021-01-13 14:20:06 +01:00
|
|
|
|
|
|
|
public function captureIndividual($individual): ?int
|
|
|
|
{
|
|
|
|
if (!empty($individual['uuid'])) {
|
|
|
|
$existingIndividual = $this->find()->where([
|
|
|
|
'uuid' => $individual['uuid']
|
|
|
|
])->first();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (empty($existingIndividual)) {
|
2021-06-28 14:49:38 +02:00
|
|
|
$entityToSave = $this->newEmptyEntity();
|
|
|
|
$this->patchEntity($entityToSave, $individual, [
|
|
|
|
'accessibleFields' => $entityToSave->getAccessibleFieldForNew()
|
2021-06-28 14:04:01 +02:00
|
|
|
]);
|
2021-01-13 14:20:06 +01:00
|
|
|
} else {
|
2021-06-28 14:04:01 +02:00
|
|
|
$this->patchEntity($existingIndividual, $individual);
|
|
|
|
$entityToSave = $existingIndividual;
|
|
|
|
}
|
|
|
|
$savedEntity = $this->save($entityToSave, ['associated' => false]);
|
|
|
|
if (!$savedEntity) {
|
|
|
|
return null;
|
2021-01-13 14:20:06 +01:00
|
|
|
}
|
2021-06-28 14:04:01 +02:00
|
|
|
$this->postCaptureActions($savedEntity);
|
|
|
|
return $savedEntity->id;
|
2021-01-13 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postCaptureActions($individual): void
|
|
|
|
{
|
|
|
|
if (!empty($individual['metaFields'])) {
|
|
|
|
$this->saveMetaFields($id, $individual);
|
|
|
|
}
|
|
|
|
if (!empty($individual['alignments'])) {
|
2021-06-29 13:13:41 +02:00
|
|
|
$Organisation = \Cake\ORM\TableRegistry::getTableLocator()->get('Organisations');
|
2021-01-13 14:20:06 +01:00
|
|
|
foreach ($individual['alignments'] as $alignment) {
|
2021-06-29 13:13:41 +02:00
|
|
|
$org_id = $Organisation->captureOrg($alignment['organisation']);
|
2021-01-13 14:20:06 +01:00
|
|
|
if ($org_id) {
|
|
|
|
$this->Alignments->setAlignment($org_id, $individual->id, $alignment['type']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|