Files
animal_management/notifications/TransferStatusNotification.php
2026-04-04 13:13:00 -04:00

163 lines
5.6 KiB
PHP

<?php
namespace humhub\modules\animal_management\notifications;
use humhub\libs\Html;
use humhub\modules\notification\components\BaseNotification;
use humhub\modules\space\models\Space;
use Yii;
use yii\helpers\Json;
use yii\helpers\Url;
class TransferStatusNotification extends BaseNotification
{
public $moduleId = 'animal_management';
public $requireSource = false;
public $requireOriginator = false;
public string $animalName = '';
public string $fromSpaceName = '';
public string $toSpaceName = '';
public string $spaceGuid = '';
public string $eventType = '';
public string $details = '';
public function category()
{
return new TransferNotificationCategory();
}
public function getUrl()
{
$eventType = $this->payloadString('eventType', $this->eventType);
$spaceGuid = $this->payloadString('spaceGuid', $this->spaceGuid);
$anchor = static::eventAnchor($eventType);
$space = Space::findOne(['guid' => $spaceGuid]);
if ($space instanceof Space) {
return $space->createUrl('/animal_management/animals/index') . '#' . $anchor;
}
return Url::to(['/animal_management/animals/index', 'sguid' => $spaceGuid]) . '#' . $anchor;
}
public function html()
{
$animalName = $this->payloadString('animalName', $this->animalName);
$eventType = $this->payloadString('eventType', $this->eventType);
$details = $this->payloadString('details', $this->details);
$params = [
'animalName' => Html::tag('strong', Html::encode($animalName)),
'status' => Html::encode(static::statusLabel($eventType)),
];
if ($this->originator) {
$params['displayName'] = Html::tag('strong', Html::encode($this->originator->displayName));
$message = Yii::t('AnimalManagementModule.base', '{displayName} {status} the transfer of {animalName}.', $params);
} else {
$message = Yii::t('AnimalManagementModule.base', '{status} the transfer of {animalName}.', $params);
}
if ($details !== '') {
$message .= ' ' . Html::encode($details);
}
return $message;
}
public function getMailSubject()
{
$animalName = $this->payloadString('animalName', $this->animalName);
$eventType = $this->payloadString('eventType', $this->eventType);
return Yii::t('AnimalManagementModule.base', 'Animal transfer update: {animalName} ({status})', [
'animalName' => $animalName,
'status' => static::statusLabel($eventType),
]);
}
public function __serialize(): array
{
$data = parent::__serialize();
$data['animalName'] = $this->animalName;
$data['fromSpaceName'] = $this->fromSpaceName;
$data['toSpaceName'] = $this->toSpaceName;
$data['spaceGuid'] = $this->spaceGuid;
$data['eventType'] = $this->eventType;
$data['details'] = $this->details;
$data['payload'] = $this->payload;
return $data;
}
public function __unserialize($unserializedArr)
{
parent::__unserialize($unserializedArr);
$this->animalName = (string)($unserializedArr['animalName'] ?? '');
$this->fromSpaceName = (string)($unserializedArr['fromSpaceName'] ?? '');
$this->toSpaceName = (string)($unserializedArr['toSpaceName'] ?? '');
$this->spaceGuid = (string)($unserializedArr['spaceGuid'] ?? '');
$this->eventType = (string)($unserializedArr['eventType'] ?? '');
$this->details = (string)($unserializedArr['details'] ?? '');
if (isset($unserializedArr['payload']) && is_array($unserializedArr['payload'])) {
$this->payload = $unserializedArr['payload'];
}
}
public static function statusLabel(string $eventType): string
{
switch ($eventType) {
case 'accepted':
return Yii::t('AnimalManagementModule.base', 'Accepted');
case 'declined':
return Yii::t('AnimalManagementModule.base', 'Declined');
case 'completed':
return Yii::t('AnimalManagementModule.base', 'Completed');
case 'cancelled':
return Yii::t('AnimalManagementModule.base', 'Cancelled');
default:
return Yii::t('AnimalManagementModule.base', 'Updated');
}
}
private static function eventAnchor(string $eventType): string
{
switch ($eventType) {
case 'requested':
case 'cancelled':
return 'incoming-transfers';
case 'accepted':
case 'declined':
case 'completed':
return 'outgoing-transfers';
default:
return 'outgoing-transfers';
}
}
private function payloadString(string $key, string $fallback = ''): string
{
if (is_array($this->payload) && array_key_exists($key, $this->payload)) {
return trim((string)$this->payload[$key]);
}
if ($this->record !== null && !empty($this->record->payload)) {
try {
$decoded = Json::decode((string)$this->record->payload);
if (is_array($decoded)) {
$this->payload = $decoded;
if (array_key_exists($key, $decoded)) {
return trim((string)$decoded[$key]);
}
}
} catch (\Throwable $e) {
// Fall back to explicit property values when payload is unavailable.
}
}
return trim($fallback);
}
}