115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\donations\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 DonationRefundedNotification extends BaseNotification
|
|
{
|
|
public $moduleId = 'donations';
|
|
public $requireSource = false;
|
|
public $requireOriginator = false;
|
|
|
|
public string $spaceGuid = '';
|
|
public string $goalTitle = '';
|
|
public string $amountLabel = '';
|
|
|
|
public function category()
|
|
{
|
|
return new DonationNotificationCategory();
|
|
}
|
|
|
|
public function getUrl()
|
|
{
|
|
$spaceGuid = $this->payloadString('spaceGuid', $this->spaceGuid);
|
|
$space = Space::findOne(['guid' => $spaceGuid]);
|
|
|
|
if ($space instanceof Space) {
|
|
return $space->createUrl('/donations/settings/history');
|
|
}
|
|
|
|
return Url::to(['/donations/settings/history', 'sguid' => $spaceGuid]);
|
|
}
|
|
|
|
public function html()
|
|
{
|
|
$goalTitle = $this->payloadString('goalTitle', $this->goalTitle);
|
|
$amountLabel = $this->payloadString('amountLabel', $this->amountLabel);
|
|
|
|
if ($this->originator) {
|
|
return Yii::t('DonationsModule.base', '{displayName} processed a refund of {amount} for {goal}.', [
|
|
'displayName' => Html::tag('strong', Html::encode($this->originator->displayName)),
|
|
'amount' => Html::tag('strong', Html::encode($amountLabel)),
|
|
'goal' => Html::tag('strong', Html::encode($goalTitle)),
|
|
]);
|
|
}
|
|
|
|
return Yii::t('DonationsModule.base', 'A refund of {amount} was processed for {goal}.', [
|
|
'amount' => Html::tag('strong', Html::encode($amountLabel)),
|
|
'goal' => Html::tag('strong', Html::encode($goalTitle)),
|
|
]);
|
|
}
|
|
|
|
public function getMailSubject()
|
|
{
|
|
$goalTitle = $this->payloadString('goalTitle', $this->goalTitle);
|
|
$amountLabel = $this->payloadString('amountLabel', $this->amountLabel);
|
|
|
|
return Yii::t('DonationsModule.base', 'Donation refunded: {amount} for {goal}', [
|
|
'amount' => $amountLabel,
|
|
'goal' => $goalTitle,
|
|
]);
|
|
}
|
|
|
|
public function __serialize(): array
|
|
{
|
|
$data = parent::__serialize();
|
|
$data['spaceGuid'] = $this->spaceGuid;
|
|
$data['goalTitle'] = $this->goalTitle;
|
|
$data['amountLabel'] = $this->amountLabel;
|
|
$data['payload'] = $this->payload;
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function __unserialize($unserializedArr)
|
|
{
|
|
parent::__unserialize($unserializedArr);
|
|
|
|
$this->spaceGuid = (string)($unserializedArr['spaceGuid'] ?? '');
|
|
$this->goalTitle = (string)($unserializedArr['goalTitle'] ?? '');
|
|
$this->amountLabel = (string)($unserializedArr['amountLabel'] ?? '');
|
|
|
|
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) {
|
|
}
|
|
}
|
|
|
|
return trim($fallback);
|
|
}
|
|
}
|