95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\animal_management\models;
|
|
|
|
use humhub\components\ActiveRecord;
|
|
use Yii;
|
|
|
|
class AnimalMedicalVisit extends ActiveRecord
|
|
{
|
|
public static function tableName()
|
|
{
|
|
return 'rescue_animal_medical_visit';
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
$rules = [
|
|
[['animal_id'], 'required'],
|
|
[['animal_id', 'created_by'], 'integer'],
|
|
[['visit_at'], 'safe'],
|
|
[['notes', 'recommendations'], 'string'],
|
|
[['provider_name'], 'string', 'max' => 190],
|
|
];
|
|
|
|
$table = Yii::$app->db->schema->getTableSchema(static::tableName(), true);
|
|
if ($table !== null && isset($table->columns['post_to_space_feed']) && isset($table->columns['post_to_animal_feed'])) {
|
|
$rules[] = [['post_to_space_feed', 'post_to_animal_feed'], 'integer'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
public function beforeSave($insert)
|
|
{
|
|
if (!parent::beforeSave($insert)) {
|
|
return false;
|
|
}
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
if ($insert && empty($this->created_at)) {
|
|
$this->created_at = $now;
|
|
}
|
|
|
|
$this->updated_at = $now;
|
|
return true;
|
|
}
|
|
|
|
public function getFieldValues()
|
|
{
|
|
return $this->hasMany(AnimalMedicalVisitFieldValue::class, ['medical_visit_id' => 'id']);
|
|
}
|
|
|
|
public function getCustomFieldDisplayValues(bool $includeRestricted = false): array
|
|
{
|
|
$values = [];
|
|
|
|
foreach ($this->getFieldValues()->with('fieldDefinition')->all() as $fieldValue) {
|
|
$definition = $fieldValue->fieldDefinition;
|
|
if ($definition === null || (string)$definition->module_id !== 'animal_management') {
|
|
continue;
|
|
}
|
|
|
|
if ((int)$definition->is_active !== 1 || (string)$definition->group_key !== 'animal_medical_visit') {
|
|
continue;
|
|
}
|
|
|
|
$visibility = (string)$definition->visibility;
|
|
if (!$includeRestricted && $visibility !== 'public') {
|
|
continue;
|
|
}
|
|
|
|
$raw = trim((string)$fieldValue->value_text);
|
|
if ($raw === '') {
|
|
continue;
|
|
}
|
|
|
|
$display = $raw;
|
|
$inputType = (string)$definition->input_type;
|
|
if ($inputType === 'boolean') {
|
|
$display = in_array(strtolower($raw), ['1', 'true', 'yes', 'on'], true)
|
|
? Yii::t('AnimalManagementModule.base', 'Yes')
|
|
: Yii::t('AnimalManagementModule.base', 'No');
|
|
}
|
|
|
|
$values[] = [
|
|
'field_key' => (string)$definition->field_key,
|
|
'label' => trim((string)$definition->label) !== '' ? (string)$definition->label : (string)$definition->field_key,
|
|
'value' => $display,
|
|
];
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
}
|