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 AlignmentsTable extends AppTable
|
|
|
|
{
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
|
|
|
$this->belongsTo('Individuals');
|
2021-11-17 15:43:52 +01:00
|
|
|
$this->addBehavior('AuditLog');
|
2020-05-29 13:41:58 +02:00
|
|
|
$this->belongsTo('Organisations');
|
2021-09-28 13:32:51 +02:00
|
|
|
$this->addBehavior('Timestamp');
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
|
|
|
->notEmptyString('individual_id')
|
|
|
|
->notEmptyString('organisation_id')
|
|
|
|
->requirePresence(['individual_id', 'organisation_id'], 'create');
|
|
|
|
return $validator;
|
2021-01-13 14:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setAlignment($organisation_id, $individual_id, $type): void
|
|
|
|
{
|
|
|
|
$query = $this->find();
|
|
|
|
$query->where([
|
|
|
|
'organisation_id' => $organisation_id,
|
|
|
|
'individual_id' => $individual_id
|
|
|
|
]);
|
|
|
|
$existingAlignment = $query->first();
|
|
|
|
if (empty($existingAlignment)) {
|
|
|
|
$alignment = $this->newEmptyEntity();
|
|
|
|
$data = [
|
|
|
|
'organisation_id' => $organisation_id,
|
|
|
|
'individual_id' => $individual_id,
|
|
|
|
'type' => $type
|
|
|
|
];
|
|
|
|
$this->patchEntity($alignment, $data);
|
|
|
|
}
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
}
|