110 lines
3.6 KiB
PHP
110 lines
3.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 TransferRequestedNotification extends BaseNotification
|
|
{
|
|
public $moduleId = 'animal_management';
|
|
public $requireSource = false;
|
|
public $requireOriginator = false;
|
|
|
|
public string $animalName = '';
|
|
public string $fromSpaceName = '';
|
|
public string $toSpaceGuid = '';
|
|
|
|
public function category()
|
|
{
|
|
return new TransferNotificationCategory();
|
|
}
|
|
|
|
public function getUrl()
|
|
{
|
|
$toSpaceGuid = $this->payloadString('toSpaceGuid', $this->toSpaceGuid);
|
|
$space = Space::findOne(['guid' => $toSpaceGuid]);
|
|
if ($space instanceof Space) {
|
|
return $space->createUrl('/animal_management/animals/index') . '#incoming-transfers';
|
|
}
|
|
|
|
return Url::to(['/animal_management/animals/index', 'sguid' => $toSpaceGuid]) . '#incoming-transfers';
|
|
}
|
|
|
|
public function html()
|
|
{
|
|
$animalName = $this->payloadString('animalName', $this->animalName);
|
|
|
|
if ($this->originator) {
|
|
return Yii::t('AnimalManagementModule.base', '{displayName} requested to transfer {animalName}.', [
|
|
'displayName' => Html::tag('strong', Html::encode($this->originator->displayName)),
|
|
'animalName' => Html::tag('strong', Html::encode($animalName)),
|
|
]);
|
|
}
|
|
|
|
return Yii::t('AnimalManagementModule.base', 'A transfer was requested for {animalName}.', [
|
|
'animalName' => Html::tag('strong', Html::encode($animalName)),
|
|
]);
|
|
}
|
|
|
|
public function getMailSubject()
|
|
{
|
|
$animalName = $this->payloadString('animalName', $this->animalName);
|
|
|
|
return Yii::t('AnimalManagementModule.base', 'Animal transfer request: {animalName}', [
|
|
'animalName' => $animalName,
|
|
]);
|
|
}
|
|
|
|
public function __serialize(): array
|
|
{
|
|
$data = parent::__serialize();
|
|
$data['animalName'] = $this->animalName;
|
|
$data['fromSpaceName'] = $this->fromSpaceName;
|
|
$data['toSpaceGuid'] = $this->toSpaceGuid;
|
|
$data['payload'] = $this->payload;
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function __unserialize($unserializedArr)
|
|
{
|
|
parent::__unserialize($unserializedArr);
|
|
|
|
$this->animalName = (string)($unserializedArr['animalName'] ?? '');
|
|
$this->fromSpaceName = (string)($unserializedArr['fromSpaceName'] ?? '');
|
|
$this->toSpaceGuid = (string)($unserializedArr['toSpaceGuid'] ?? '');
|
|
|
|
if (isset($unserializedArr['payload']) && is_array($unserializedArr['payload'])) {
|
|
$this->payload = $unserializedArr['payload'];
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|