new: [Organisation] table capture function added

pull/38/head
iglocska 2021-01-13 14:19:45 +01:00
parent 7159034e9e
commit a47dd94011
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 48 additions and 0 deletions

View File

@ -5,11 +5,16 @@ namespace App\Model\Table;
use App\Model\Table\AppTable; use App\Model\Table\AppTable;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\Validation\Validator; use Cake\Validation\Validator;
use Cake\Error\Debugger;
class OrganisationsTable extends AppTable class OrganisationsTable extends AppTable
{ {
public $metaFields = 'organisation'; public $metaFields = 'organisation';
protected $_accessible = [
'id' => false
];
public function initialize(array $config): void public function initialize(array $config): void
{ {
parent::initialize($config); parent::initialize($config);
@ -47,4 +52,47 @@ class OrganisationsTable extends AppTable
->requirePresence(['name', 'uuid'], 'create'); ->requirePresence(['name', 'uuid'], 'create');
return $validator; return $validator;
} }
public function captureOrg($org): ?int
{
if (!empty($org['id'])) {
unset($org['id']);
}
if (!empty($org['uuid'])) {
$existingOrg = $this->find()->where([
'uuid' => $org['uuid']
])->first();
} else {
return null;
}
if (empty($existingOrg)) {
$data = $this->newEmptyEntity();
$data = $this->patchEntity($data, $org, ['associated' => []]);
if (!$this->save($data)) {
return null;
}
$savedOrg = $data;
} else {
$reserved = ['id', 'uuid', 'metaFields'];
foreach ($org as $field => $value) {
if (in_array($field, $reserved)) {
continue;
}
$existingOrg->$field = $value;
}
if (!$this->save($existingOrg)) {
return null;
}
$savedOrg = $existingOrg;
}
$this->postCaptureActions($savedOrg->id, $org);
return $savedOrg->id;
}
public function postCaptureActions($id, $org)
{
if (!empty($org['metaFields'])) {
$this->saveMetaFields($id, $org);
}
}
} }