Initial import of animal_management module
This commit is contained in:
158
models/AnimalTransfer.php
Normal file
158
models/AnimalTransfer.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\animal_management\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
use humhub\modules\space\models\Space;
|
||||
use Yii;
|
||||
|
||||
class AnimalTransfer extends ActiveRecord
|
||||
{
|
||||
public const STATUS_REQUESTED = 'requested';
|
||||
public const STATUS_ACCEPTED = 'accepted';
|
||||
public const STATUS_DECLINED = 'declined';
|
||||
public const STATUS_COMPLETED = 'completed';
|
||||
public const STATUS_CANCELLED = 'cancelled';
|
||||
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_animal_transfer';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['animal_id', 'from_contentcontainer_id', 'to_contentcontainer_id', 'status'], 'required'],
|
||||
[['animal_id', 'from_contentcontainer_id', 'to_contentcontainer_id', 'requested_by'], 'integer'],
|
||||
[['request_message', 'conditions_text'], 'string'],
|
||||
[['status'], 'string', 'max' => 32],
|
||||
[['status'], 'in', 'range' => array_keys(self::statusOptions())],
|
||||
];
|
||||
}
|
||||
|
||||
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 static function statusOptions(): array
|
||||
{
|
||||
return [
|
||||
self::STATUS_REQUESTED => Yii::t('AnimalManagementModule.base', 'Requested'),
|
||||
self::STATUS_ACCEPTED => Yii::t('AnimalManagementModule.base', 'Accepted'),
|
||||
self::STATUS_DECLINED => Yii::t('AnimalManagementModule.base', 'Declined'),
|
||||
self::STATUS_COMPLETED => Yii::t('AnimalManagementModule.base', 'Completed'),
|
||||
self::STATUS_CANCELLED => Yii::t('AnimalManagementModule.base', 'Cancelled'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getAnimal()
|
||||
{
|
||||
return $this->hasOne(Animal::class, ['id' => 'animal_id']);
|
||||
}
|
||||
|
||||
public function getFromSpace(): ?Space
|
||||
{
|
||||
return Space::findOne(['contentcontainer_id' => $this->from_contentcontainer_id]);
|
||||
}
|
||||
|
||||
public function getToSpace(): ?Space
|
||||
{
|
||||
return Space::findOne(['contentcontainer_id' => $this->to_contentcontainer_id]);
|
||||
}
|
||||
|
||||
public function getAuditEvents()
|
||||
{
|
||||
return $this->hasMany(AnimalTransferEvent::class, ['transfer_id' => 'id']);
|
||||
}
|
||||
|
||||
public function markAccepted(): bool
|
||||
{
|
||||
$fromStatus = $this->status;
|
||||
$this->status = self::STATUS_ACCEPTED;
|
||||
$this->responded_at = date('Y-m-d H:i:s');
|
||||
if (!$this->save(false, ['status', 'responded_at', 'updated_at'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimalTransferEvent::log(
|
||||
$this,
|
||||
AnimalTransferEvent::EVENT_ACCEPTED,
|
||||
$fromStatus,
|
||||
$this->status,
|
||||
Yii::t('AnimalManagementModule.base', 'Transfer request accepted.')
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function markDeclined(): bool
|
||||
{
|
||||
$fromStatus = $this->status;
|
||||
$this->status = self::STATUS_DECLINED;
|
||||
$this->responded_at = date('Y-m-d H:i:s');
|
||||
if (!$this->save(false, ['status', 'responded_at', 'updated_at'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimalTransferEvent::log(
|
||||
$this,
|
||||
AnimalTransferEvent::EVENT_DECLINED,
|
||||
$fromStatus,
|
||||
$this->status,
|
||||
Yii::t('AnimalManagementModule.base', 'Transfer request declined.')
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function markCompleted(): bool
|
||||
{
|
||||
$fromStatus = $this->status;
|
||||
$this->status = self::STATUS_COMPLETED;
|
||||
$this->completed_at = date('Y-m-d H:i:s');
|
||||
if (!$this->save(false, ['status', 'completed_at', 'updated_at'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimalTransferEvent::log(
|
||||
$this,
|
||||
AnimalTransferEvent::EVENT_COMPLETED,
|
||||
$fromStatus,
|
||||
$this->status,
|
||||
Yii::t('AnimalManagementModule.base', 'Transfer completed.')
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function markCancelled(string $message = ''): bool
|
||||
{
|
||||
$fromStatus = $this->status;
|
||||
$this->status = self::STATUS_CANCELLED;
|
||||
|
||||
if (!$this->save(false, ['status', 'updated_at'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimalTransferEvent::log(
|
||||
$this,
|
||||
AnimalTransferEvent::EVENT_CANCELLED,
|
||||
$fromStatus,
|
||||
$this->status,
|
||||
$message !== '' ? $message : Yii::t('AnimalManagementModule.base', 'Transfer cancelled.')
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user