Files
animal_management/models/AnimalGalleryItem.php
2026-04-04 13:13:00 -04:00

76 lines
1.7 KiB
PHP

<?php
namespace humhub\modules\animal_management\models;
use humhub\components\ActiveRecord;
use humhub\modules\file\models\File;
class AnimalGalleryItem extends ActiveRecord
{
public static function tableName()
{
return 'rescue_animal_gallery_item';
}
public function rules()
{
return [
[['animal_id'], 'required'],
[['animal_id', 'file_id', 'source_post_id', 'created_by'], 'integer'],
[['caption'], 'string'],
[['source_type'], 'string', 'max' => 32],
[['file_path'], 'string', 'max' => 500],
];
}
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;
}
if (empty($this->source_type)) {
$this->source_type = 'upload';
}
$this->updated_at = $now;
return true;
}
public function getAnimal()
{
return $this->hasOne(Animal::class, ['id' => 'animal_id']);
}
public function getFile()
{
return $this->hasOne(File::class, ['id' => 'file_id']);
}
public function getImageUrl(): string
{
$path = trim((string)$this->file_path);
if ($path !== '') {
if (preg_match('/^https?:\/\//i', $path) || substr($path, 0, 1) === '/') {
return $path;
}
return '/' . ltrim($path, '/');
}
if ($this->file_id) {
$file = $this->file;
if ($file instanceof File) {
return (string)$file->getUrl([], false);
}
}
return '';
}
}