akaunting/app/Abstracts/Notification.php

164 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Abstracts;
2021-04-16 00:59:43 +03:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
2019-11-16 10:21:14 +03:00
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification as BaseNotification;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Str;
2019-11-16 10:21:14 +03:00
2021-04-16 00:59:43 +03:00
abstract class Notification extends BaseNotification implements ShouldQueue
2019-11-16 10:21:14 +03:00
{
2021-04-16 00:59:43 +03:00
use Queueable;
2022-06-01 10:15:55 +03:00
/**
* Custom mail subject, body, etc.
*
* @var array
*/
public $custom_mail;
2019-11-16 10:21:14 +03:00
/**
* Create a notification instance.
*/
public function __construct()
{
2021-04-16 00:59:43 +03:00
$this->onQueue('notifications');
2019-11-16 10:21:14 +03:00
}
/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Initialise the mail representation of the notification.
*
*/
2022-06-01 10:15:55 +03:00
public function initMailMessage(): MailMessage
2019-11-16 10:21:14 +03:00
{
2022-07-14 18:58:13 +03:00
app('url')->defaults(['company_id' => company_id()]);
2020-01-09 14:55:57 +03:00
2019-11-16 10:21:14 +03:00
$message = (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
2021-04-16 00:59:43 +03:00
->subject($this->getSubject())
2022-06-01 10:15:55 +03:00
->view('components.email.body', ['body' => $this->getBody()]);
if (!empty($this->custom_mail['cc'])) {
$message->cc($this->custom_mail['cc']);
}
2019-11-16 10:21:14 +03:00
return $message;
}
/**
* Initialise the array representation of the notification.
*
*/
public function initArrayMessage(): void
{
2022-07-14 18:58:13 +03:00
app('url')->defaults(['company_id' => company_id()]);
}
2022-06-01 10:15:55 +03:00
public function getSubject(): string
2019-11-16 10:21:14 +03:00
{
2022-06-01 10:15:55 +03:00
return !empty($this->custom_mail['subject'])
? $this->custom_mail['subject']
: $this->replaceTags($this->template->subject);
2019-11-16 10:21:14 +03:00
}
2021-04-16 00:59:43 +03:00
public function getBody()
2019-11-16 10:21:14 +03:00
{
2022-06-01 10:15:55 +03:00
$body = !empty($this->custom_mail['body']) ? $this->custom_mail['body'] : $this->replaceTags($this->template->body);
return $body . $this->getFooter();
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function replaceTags(string $content): string
2019-11-16 10:21:14 +03:00
{
2020-02-27 09:56:08 +03:00
$pattern = $this->getTagsPattern();
$replacement = $this->applyQuote($this->getTagsReplacement());
return $this->revertQuote(preg_replace($pattern, $replacement, $content));
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function getFooter()
{
2022-11-09 16:34:03 +03:00
$url = 'https://akaunting.com/accounting-software?utm_source=email&utm_medium=footer&utm_campaign=plg&utm_content=' . $this->template->alias;
2022-06-01 10:15:55 +03:00
$get_started = '<a href="' . $url . '" style="color: #676ba2; text-decoration: none;">' . trans('footer.get_started') . '</a>';
return view('components.email.footer', compact('url', 'get_started'));
}
public function getTagsPattern(): array
2019-11-16 10:21:14 +03:00
{
$pattern = [];
foreach($this->getTags() as $tag) {
$pattern[] = "/" . $tag . "/";
}
return $pattern;
}
2020-02-27 09:56:08 +03:00
2022-06-01 10:15:55 +03:00
public function getTags(): array
2020-02-27 09:56:08 +03:00
{
return [];
}
2022-06-01 10:15:55 +03:00
public function getTagsReplacement(): array
2020-02-27 09:56:08 +03:00
{
return [];
}
2022-06-01 10:15:55 +03:00
public function getTagsBinding(): array
{
$bindings = [];
$tags = $this->getTags();
$replacements = $this->getTagsReplacement();
$wrappers = ['{', '}'];
foreach ($tags as $index => $tag) {
$key = Str::replace($wrappers, '', $tag);
$bindings[$key] = $replacements[$index];
}
return $bindings;
}
public function applyQuote(array $vars): array
2020-02-27 09:56:08 +03:00
{
$new_vars = [];
foreach ($vars as $var) {
$new_vars[] = preg_quote($var);
}
return $new_vars;
}
2022-06-01 10:15:55 +03:00
public function revertQuote(string $content): string
2020-02-27 09:56:08 +03:00
{
return str_replace('\\', '', $content);
}
2022-06-01 10:15:55 +03:00
/**
* @deprecated 3.0
*/
public function initMessage()
{
return $this->initMailMessage();
}
2019-11-16 10:21:14 +03:00
}