Initial import of animal_management module
This commit is contained in:
82
models/AnimalTransferEvent.php
Normal file
82
models/AnimalTransferEvent.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\animal_management\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
use humhub\modules\user\models\User;
|
||||
use Yii;
|
||||
use yii\helpers\Json;
|
||||
|
||||
class AnimalTransferEvent extends ActiveRecord
|
||||
{
|
||||
public const EVENT_REQUESTED = 'requested';
|
||||
public const EVENT_ACCEPTED = 'accepted';
|
||||
public const EVENT_DECLINED = 'declined';
|
||||
public const EVENT_COMPLETED = 'completed';
|
||||
public const EVENT_CANCELLED = 'cancelled';
|
||||
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_animal_transfer_event';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['transfer_id', 'animal_id', 'event_type'], 'required'],
|
||||
[['transfer_id', 'animal_id', 'created_by'], 'integer'],
|
||||
[['message', 'metadata_json'], 'string'],
|
||||
[['event_type', 'from_status', 'to_status'], 'string', 'max' => 32],
|
||||
[['event_type'], 'in', 'range' => [
|
||||
self::EVENT_REQUESTED,
|
||||
self::EVENT_ACCEPTED,
|
||||
self::EVENT_DECLINED,
|
||||
self::EVENT_COMPLETED,
|
||||
self::EVENT_CANCELLED,
|
||||
]],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!$insert) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($this->created_at)) {
|
||||
$this->created_at = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function log(
|
||||
AnimalTransfer $transfer,
|
||||
string $eventType,
|
||||
?string $fromStatus,
|
||||
?string $toStatus,
|
||||
string $message = '',
|
||||
array $metadata = []
|
||||
): bool {
|
||||
$event = new self();
|
||||
$event->transfer_id = (int)$transfer->id;
|
||||
$event->animal_id = (int)$transfer->animal_id;
|
||||
$event->event_type = $eventType;
|
||||
$event->from_status = $fromStatus;
|
||||
$event->to_status = $toStatus;
|
||||
$event->message = $message;
|
||||
$event->metadata_json = empty($metadata) ? null : Json::encode($metadata);
|
||||
$event->created_by = Yii::$app->user->isGuest ? null : (int)Yii::$app->user->id;
|
||||
|
||||
return $event->save(false);
|
||||
}
|
||||
|
||||
public function getCreatedByUser()
|
||||
{
|
||||
return $this->hasOne(User::class, ['id' => 'created_by']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user