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
|
|
|
|
{
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
2021-11-24 23:59:34 +01: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');
|
2021-11-17 15:43:52 +01:00
|
|
|
$this->addBehavior('AuditLog');
|
2022-10-27 10:14:57 +02:00
|
|
|
$this->addBehavior('NotifyAdmins', [
|
|
|
|
'fields' => ['uuid', 'name', 'url', 'nationality', 'sector', 'type', 'contacts', 'modified', 'meta_fields'],
|
|
|
|
]);
|
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
|
|
|
]
|
|
|
|
);
|
2021-11-11 14:50:25 +01:00
|
|
|
$this->addBehavior('MetaFields');
|
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;
|
|
|
|
}
|
2022-01-25 17:01:27 +01:00
|
|
|
$entityToSave->setDirty('modified', false);
|
2021-06-28 14:49:38 +02:00
|
|
|
$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
|
|
|
}
|