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;
|
2021-01-13 14:19:45 +01:00
|
|
|
use Cake\Error\Debugger;
|
2020-05-29 13:41:58 +02:00
|
|
|
|
|
|
|
class OrganisationsTable extends AppTable
|
|
|
|
{
|
2020-09-28 01:25:07 +02:00
|
|
|
public $metaFields = 'organisation';
|
|
|
|
|
2021-01-13 14:19:45 +01:00
|
|
|
protected $_accessible = [
|
|
|
|
'id' => false
|
|
|
|
];
|
|
|
|
|
2020-05-29 13:41:58 +02:00
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
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',
|
|
|
|
[
|
2020-11-30 13:54:36 +01:00
|
|
|
'dependent' => true,
|
2020-05-29 13:41:58 +02:00
|
|
|
'foreignKey' => 'owner_id',
|
2021-03-15 22:47:13 +01:00
|
|
|
'conditions' => ['owner_model' => 'organisation']
|
2020-05-29 13:41:58 +02:00
|
|
|
]
|
|
|
|
);
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->hasMany(
|
|
|
|
'MetaFields',
|
|
|
|
[
|
2020-11-30 13:54:36 +01:00
|
|
|
'dependent' => true,
|
2020-09-28 01:25:07 +02:00
|
|
|
'foreignKey' => 'parent_id',
|
2021-02-26 10:36:06 +01:00
|
|
|
'conditions' => ['MetaFields.scope' => 'organisation']
|
2020-09-28 01:25:07 +02:00
|
|
|
]
|
|
|
|
);
|
2020-06-04 10:05:45 +02:00
|
|
|
$this->setDisplayField('name');
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
|
|
|
->notEmptyString('name')
|
|
|
|
->notEmptyString('uuid')
|
|
|
|
->requirePresence(['name', 'uuid'], 'create');
|
|
|
|
return $validator;
|
|
|
|
}
|
2021-01-13 14:19:45 +01:00
|
|
|
|
|
|
|
public function captureOrg($org): ?int
|
|
|
|
{
|
|
|
|
if (!empty($org['uuid'])) {
|
|
|
|
$existingOrg = $this->find()->where([
|
|
|
|
'uuid' => $org['uuid']
|
|
|
|
])->first();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (empty($existingOrg)) {
|
2021-06-28 14:49:38 +02:00
|
|
|
$entityToSave = $this->newEmptyEntity();
|
|
|
|
$this->patchEntity($entityToSave, $org, [
|
|
|
|
'accessibleFields' => $entityToSave->getAccessibleFieldForNew()
|
|
|
|
]);
|
2021-01-13 14:19:45 +01:00
|
|
|
} else {
|
2021-06-28 14:49:38 +02:00
|
|
|
$this->patchEntity($existingOrg, $org);
|
|
|
|
$entityToSave = $existingOrg;
|
|
|
|
}
|
|
|
|
$savedEntity = $this->save($entityToSave, ['associated' => false]);
|
|
|
|
if (!$savedEntity) {
|
|
|
|
return null;
|
2021-01-13 14:19:45 +01:00
|
|
|
}
|
2021-06-28 14:49:38 +02:00
|
|
|
$this->postCaptureActions($savedEntity->id, $org);
|
|
|
|
return $savedEntity->id;
|
2021-01-13 14:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postCaptureActions($id, $org)
|
|
|
|
{
|
|
|
|
if (!empty($org['metaFields'])) {
|
|
|
|
$this->saveMetaFields($id, $org);
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|