156 lines
5.1 KiB
PHP
156 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\animal_management\models\forms;
|
|
|
|
use humhub\modules\content\components\ContentContainerActiveRecord;
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use yii\helpers\Json;
|
|
|
|
class DisplaySettingsForm extends Model
|
|
{
|
|
public const DEFAULT_SEARCH_BLOCK_HEADING = 'Search Animal Profiles';
|
|
|
|
public const DEFAULT_TILE_FIELDS = [
|
|
'name',
|
|
'species',
|
|
'status',
|
|
];
|
|
|
|
public const DEFAULT_DETAIL_FIELDS = [
|
|
'species',
|
|
'breed',
|
|
'sex',
|
|
'status',
|
|
'location_name',
|
|
];
|
|
|
|
public ?ContentContainerActiveRecord $contentContainer = null;
|
|
|
|
public string $search_block_heading = self::DEFAULT_SEARCH_BLOCK_HEADING;
|
|
public array $tile_fields = self::DEFAULT_TILE_FIELDS;
|
|
public array $detail_fields = self::DEFAULT_DETAIL_FIELDS;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['search_block_heading'], 'string', 'max' => 190],
|
|
[['tile_fields', 'detail_fields'], 'each', 'rule' => ['in', 'range' => array_keys(self::fieldOptions())]],
|
|
[['tile_fields', 'detail_fields'], 'default', 'value' => []],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'search_block_heading' => Yii::t('AnimalManagementModule.base', 'Search Block Heading'),
|
|
'tile_fields' => Yii::t('AnimalManagementModule.base', 'Animal Tile Fields'),
|
|
'detail_fields' => Yii::t('AnimalManagementModule.base', 'Animal Detail Hero Fields'),
|
|
];
|
|
}
|
|
|
|
public function loadValues(): void
|
|
{
|
|
if ($this->contentContainer === null) {
|
|
return;
|
|
}
|
|
|
|
$settings = Yii::$app->getModule('animal_management')->settings->contentContainer($this->contentContainer);
|
|
|
|
$heading = trim((string)$settings->get('searchBlockHeading', self::DEFAULT_SEARCH_BLOCK_HEADING));
|
|
$this->search_block_heading = $heading !== '' ? $heading : self::DEFAULT_SEARCH_BLOCK_HEADING;
|
|
|
|
$this->tile_fields = $this->normalizeFields(
|
|
$settings->get('tileFields', Json::encode(self::DEFAULT_TILE_FIELDS)),
|
|
self::DEFAULT_TILE_FIELDS
|
|
);
|
|
|
|
$this->detail_fields = $this->normalizeFields(
|
|
$settings->get('detailHeroFields', Json::encode(self::DEFAULT_DETAIL_FIELDS)),
|
|
self::DEFAULT_DETAIL_FIELDS
|
|
);
|
|
}
|
|
|
|
public function save(): bool
|
|
{
|
|
if ($this->contentContainer === null) {
|
|
$this->addError('search_block_heading', Yii::t('AnimalManagementModule.base', 'Missing content container.'));
|
|
return false;
|
|
}
|
|
|
|
if (!$this->validate()) {
|
|
return false;
|
|
}
|
|
|
|
$settings = Yii::$app->getModule('animal_management')->settings->contentContainer($this->contentContainer);
|
|
|
|
$heading = trim($this->search_block_heading);
|
|
$settings->set('searchBlockHeading', $heading !== '' ? $heading : self::DEFAULT_SEARCH_BLOCK_HEADING);
|
|
$settings->set('tileFields', Json::encode($this->normalizeFieldArray($this->tile_fields, self::DEFAULT_TILE_FIELDS)));
|
|
$settings->set('detailHeroFields', Json::encode($this->normalizeFieldArray($this->detail_fields, self::DEFAULT_DETAIL_FIELDS)));
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function fieldOptions(): array
|
|
{
|
|
return [
|
|
'name' => Yii::t('AnimalManagementModule.base', 'Name'),
|
|
'species' => Yii::t('AnimalManagementModule.base', 'Species'),
|
|
'breed' => Yii::t('AnimalManagementModule.base', 'Breed'),
|
|
'sex' => Yii::t('AnimalManagementModule.base', 'Sex'),
|
|
'status' => Yii::t('AnimalManagementModule.base', 'Status'),
|
|
'location_name' => Yii::t('AnimalManagementModule.base', 'Location'),
|
|
'animal_uid' => Yii::t('AnimalManagementModule.base', 'ID'),
|
|
'last_medical' => Yii::t('AnimalManagementModule.base', 'Last Medical Visit'),
|
|
'public_summary' => Yii::t('AnimalManagementModule.base', 'Public Summary'),
|
|
];
|
|
}
|
|
|
|
private function normalizeFields($raw, array $default): array
|
|
{
|
|
$decoded = [];
|
|
if (is_string($raw)) {
|
|
$raw = trim($raw);
|
|
if ($raw !== '') {
|
|
try {
|
|
$decoded = Json::decode($raw);
|
|
} catch (\Throwable $e) {
|
|
$decoded = [];
|
|
}
|
|
}
|
|
} elseif (is_array($raw)) {
|
|
$decoded = $raw;
|
|
}
|
|
|
|
if (!is_array($decoded)) {
|
|
return $default;
|
|
}
|
|
|
|
return $this->normalizeFieldArray($decoded, $default);
|
|
}
|
|
|
|
private function normalizeFieldArray(array $fields, array $default): array
|
|
{
|
|
$normalized = [];
|
|
$options = self::fieldOptions();
|
|
|
|
foreach ($fields as $field) {
|
|
$field = trim((string)$field);
|
|
if ($field === '' || !isset($options[$field])) {
|
|
continue;
|
|
}
|
|
|
|
if (!in_array($field, $normalized, true)) {
|
|
$normalized[] = $field;
|
|
}
|
|
}
|
|
|
|
if (empty($normalized)) {
|
|
return $default;
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
}
|