2020-09-28 01:25:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use App\Model\Table\AppTable;
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\Validation\Validator;
|
|
|
|
|
2022-03-01 09:49:33 +01:00
|
|
|
use MetaFieldsTypes\TextType;
|
|
|
|
use MetaFieldsTypes\IPv4Type;
|
|
|
|
use MetaFieldsTypes\IPv6Type;
|
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
class MetaTemplateFieldsTable extends AppTable
|
|
|
|
{
|
2022-03-01 09:49:33 +01:00
|
|
|
private $typeHandlers = [];
|
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
2021-11-04 08:10:32 +01:00
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->BelongsTo(
|
|
|
|
'MetaTemplates'
|
|
|
|
);
|
2020-12-08 09:07:48 +01:00
|
|
|
$this->hasMany('MetaFields');
|
2021-11-04 08:10:32 +01:00
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->setDisplayField('field');
|
2022-03-01 09:49:33 +01:00
|
|
|
$this->loadTypeHandlers();
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:11:46 +01:00
|
|
|
public function beforeSave($event, $entity, $options)
|
|
|
|
{
|
|
|
|
if (empty($entity->meta_template_id)) {
|
|
|
|
$event->stopPropagation();
|
|
|
|
$event->setResult(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
|
|
|
->notEmptyString('field')
|
|
|
|
->notEmptyString('type')
|
2021-12-08 11:11:46 +01:00
|
|
|
->requirePresence(['field', 'type'], 'create');
|
2020-09-28 01:25:07 +02:00
|
|
|
return $validator;
|
|
|
|
}
|
2022-03-01 09:49:33 +01:00
|
|
|
|
|
|
|
public function loadTypeHandlers(): void
|
|
|
|
{
|
|
|
|
if (empty($this->typeHandlers)) {
|
|
|
|
$typeHandlers = [
|
|
|
|
new TextType(),
|
|
|
|
new IPv4Type(),
|
|
|
|
new IPv6Type(),
|
|
|
|
];
|
|
|
|
foreach ($typeHandlers as $handler) {
|
|
|
|
$this->typeHandlers[$handler::TYPE] = $handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTypeHandlers(): array
|
|
|
|
{
|
|
|
|
return $this->typeHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTypeHandler($type)
|
|
|
|
{
|
|
|
|
return $this->typeHandlers[$type] ?? false;
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|