cerebrate/src/Model/Table/OrganisationsTable.php

84 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Model\Table;
use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Error\Debugger;
class OrganisationsTable extends AppTable
{
public function initialize(array $config): void
{
parent::initialize($config);
2021-11-24 23:59:34 +01:00
$this->addBehavior('UUID');
$this->addBehavior('Timestamp');
$this->addBehavior('Tags.Tag');
$this->addBehavior('AuditLog');
$this->addBehavior('NotifyAdmins', [
'fields' => ['uuid', 'name', 'url', 'nationality', 'sector', 'type', 'contacts', 'modified', 'meta_fields'],
]);
$this->hasMany(
'Alignments',
[
'dependent' => true,
'cascadeCallbacks' => true
]
);
$this->hasMany(
'EncryptionKeys',
[
'dependent' => true,
'foreignKey' => 'owner_id',
'conditions' => ['owner_model' => 'organisation']
]
);
$this->addBehavior('MetaFields');
$this->setDisplayField('name');
}
public function validationDefault(Validator $validator): Validator
{
$validator
->notEmptyString('name')
->notEmptyString('uuid')
->requirePresence(['name', 'uuid'], 'create');
return $validator;
}
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()
]);
} else {
2021-06-28 14:49:38 +02:00
$this->patchEntity($existingOrg, $org);
$entityToSave = $existingOrg;
}
$entityToSave->setDirty('modified', false);
2021-06-28 14:49:38 +02:00
$savedEntity = $this->save($entityToSave, ['associated' => false]);
if (!$savedEntity) {
return null;
}
2021-06-28 14:49:38 +02:00
$this->postCaptureActions($savedEntity->id, $org);
return $savedEntity->id;
}
public function postCaptureActions($id, $org)
{
if (!empty($org['metaFields'])) {
$this->saveMetaFields($id, $org);
}
}
}