102 lines
3.0 KiB
PHP
102 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\animal_management\models;
|
|
|
|
use humhub\modules\animal_management\widgets\stream\AnimalStreamEntryWallEntry;
|
|
use humhub\modules\content\components\ContentActiveRecord;
|
|
use Yii;
|
|
|
|
class AnimalStreamEntry extends ContentActiveRecord
|
|
{
|
|
public const TYPE_MEDICAL = 'medical';
|
|
public const TYPE_PROGRESS = 'progress';
|
|
|
|
public $wallEntryClass = AnimalStreamEntryWallEntry::class;
|
|
public $moduleId = 'animal_management';
|
|
|
|
public static function tableName()
|
|
{
|
|
return 'rescue_animal_stream_entry';
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['animal_id', 'entry_type'], 'required'],
|
|
[['animal_id', 'medical_visit_id', 'progress_update_id'], 'integer'],
|
|
[['entry_type'], 'string', 'max' => 32],
|
|
[['entry_type'], 'in', 'range' => [self::TYPE_MEDICAL, self::TYPE_PROGRESS]],
|
|
[['medical_visit_id', 'progress_update_id'], 'validateLinkedRecord'],
|
|
];
|
|
}
|
|
|
|
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 validateLinkedRecord(string $attribute, $params): void
|
|
{
|
|
if ((string)$this->entry_type === self::TYPE_MEDICAL) {
|
|
if ((int)$this->medical_visit_id <= 0) {
|
|
$this->addError('medical_visit_id', Yii::t('AnimalManagementModule.base', 'Medical visit reference is required.'));
|
|
}
|
|
return;
|
|
}
|
|
|
|
if ((string)$this->entry_type === self::TYPE_PROGRESS) {
|
|
if ((int)$this->progress_update_id <= 0) {
|
|
$this->addError('progress_update_id', Yii::t('AnimalManagementModule.base', 'Progress update reference is required.'));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function getAnimal()
|
|
{
|
|
return $this->hasOne(Animal::class, ['id' => 'animal_id']);
|
|
}
|
|
|
|
public function getMedicalVisit()
|
|
{
|
|
return $this->hasOne(AnimalMedicalVisit::class, ['id' => 'medical_visit_id']);
|
|
}
|
|
|
|
public function getProgressUpdate()
|
|
{
|
|
return $this->hasOne(AnimalProgressUpdate::class, ['id' => 'progress_update_id']);
|
|
}
|
|
|
|
public function getIcon()
|
|
{
|
|
return 'fa-paw';
|
|
}
|
|
|
|
public function getContentName()
|
|
{
|
|
return Yii::t('AnimalManagementModule.base', 'Animal stream update');
|
|
}
|
|
|
|
public function getContentDescription()
|
|
{
|
|
$animal = $this->animal;
|
|
$animalName = $animal instanceof Animal ? $animal->getDisplayName() : Yii::t('AnimalManagementModule.base', 'Animal');
|
|
|
|
if ((string)$this->entry_type === self::TYPE_MEDICAL) {
|
|
return Yii::t('AnimalManagementModule.base', 'Medical visit for {animal}', ['{animal}' => $animalName]);
|
|
}
|
|
|
|
return Yii::t('AnimalManagementModule.base', 'Progress update for {animal}', ['{animal}' => $animalName]);
|
|
}
|
|
}
|