2018-03-25 13:46:57 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications;
|
|
|
|
|
2018-07-21 05:02:56 -07:00
|
|
|
use App\Models\Accessory;
|
2018-03-25 13:46:57 -07:00
|
|
|
use App\Models\Setting;
|
2018-07-16 14:09:04 -07:00
|
|
|
use App\Models\User;
|
2018-03-25 13:46:57 -07:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
use Illuminate\Notifications\Messages\SlackMessage;
|
|
|
|
use Illuminate\Notifications\Notification;
|
2024-01-30 12:57:45 -08:00
|
|
|
use NotificationChannels\GoogleChat\Card;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatChannel;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatMessage;
|
|
|
|
use NotificationChannels\GoogleChat\Section;
|
|
|
|
use NotificationChannels\GoogleChat\Widgets\KeyValue;
|
2024-01-22 15:30:15 -08:00
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
class CheckinAccessoryNotification extends Notification
|
|
|
|
{
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @param $params
|
|
|
|
*/
|
2018-07-21 05:02:56 -07:00
|
|
|
public function __construct(Accessory $accessory, $checkedOutTo, User $checkedInby, $note)
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->item = $accessory;
|
|
|
|
$this->target = $checkedOutTo;
|
|
|
|
$this->admin = $checkedInby;
|
|
|
|
$this->note = $note;
|
2018-07-21 05:02:56 -07:00
|
|
|
$this->settings = Setting::getSettings();
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification's delivery channels.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function via()
|
|
|
|
{
|
2024-01-24 10:38:06 -08:00
|
|
|
$notifyBy = [];
|
2024-01-30 12:57:45 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'google'){
|
|
|
|
|
|
|
|
$notifyBy[] = GoogleChatChannel::class;
|
|
|
|
}
|
2024-01-24 10:38:06 -08:00
|
|
|
|
2024-01-22 15:30:15 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'microsoft'){
|
|
|
|
|
2024-01-24 10:38:06 -08:00
|
|
|
$notifyBy[] = MicrosoftTeamsChannel::class;
|
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
|
2024-01-30 10:47:18 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
|
2020-09-11 16:10:18 -07:00
|
|
|
$notifyBy[] = 'slack';
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:09:04 -07:00
|
|
|
/**
|
2020-09-11 16:10:18 -07:00
|
|
|
* Only send notifications to users that have email addresses
|
2018-07-16 14:09:04 -07:00
|
|
|
*/
|
2020-09-11 16:10:18 -07:00
|
|
|
if ($this->target instanceof User && $this->target->email != '') {
|
|
|
|
\Log::debug('The target is a user');
|
|
|
|
|
|
|
|
if ($this->item->checkin_email()) {
|
2024-02-12 16:22:59 -08:00
|
|
|
$notifyBy[] = 'mail';
|
2020-09-11 16:10:18 -07:00
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 16:10:18 -07:00
|
|
|
\Log::debug('checkin_email on this category is '.$this->item->checkin_email());
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
return $notifyBy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toSlack()
|
|
|
|
{
|
|
|
|
$target = $this->target;
|
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
2023-03-22 14:43:00 -07:00
|
|
|
$botname = ($this->settings->webhook_botname) ? $this->settings->webhook_botname : 'Snipe-Bot';
|
2023-06-02 02:59:47 -07:00
|
|
|
$channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'To' => '<'.$target->present()->viewUrl().'|'.$target->present()->fullName().'>',
|
|
|
|
'By' => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
|
|
|
|
];
|
|
|
|
|
|
|
|
return (new SlackMessage)
|
2020-11-12 15:05:57 -08:00
|
|
|
->content(':arrow_down: :keyboard: '.trans('mail.Accessory_Checkin_Notification'))
|
2018-03-25 13:46:57 -07:00
|
|
|
->from($botname)
|
2023-06-02 02:59:47 -07:00
|
|
|
->to($channel)
|
2018-03-25 13:46:57 -07:00
|
|
|
->attachment(function ($attachment) use ($item, $note, $admin, $fields) {
|
|
|
|
$attachment->title(htmlspecialchars_decode($item->present()->name), $item->present()->viewUrl())
|
|
|
|
->fields($fields)
|
|
|
|
->content($note);
|
|
|
|
});
|
|
|
|
}
|
2024-01-22 15:30:15 -08:00
|
|
|
public function toMicrosoftTeams()
|
|
|
|
{
|
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
|
|
|
return MicrosoftTeamsMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->type('success')
|
|
|
|
->addStartGroupToSection('activityTitle')
|
2024-01-24 10:38:06 -08:00
|
|
|
->title(trans('Accessory_Checkin_Notification'))
|
2024-01-22 15:30:15 -08:00
|
|
|
->addStartGroupToSection('activityText')
|
|
|
|
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
|
2024-01-24 10:38:06 -08:00
|
|
|
->fact(trans('mail.checked_into'), $item->location->name ? $item->location->name : '')
|
2024-01-22 15:30:15 -08:00
|
|
|
->fact(trans('mail.Accessory_Checkin_Notification')." by ", $admin->present()->fullName())
|
2024-01-24 10:38:06 -08:00
|
|
|
->fact(trans('admin/consumables/general.remaining'), $item->numRemaining())
|
2024-01-24 14:49:42 -08:00
|
|
|
->fact(trans('mail.notes'), $note ?: '');
|
2024-01-22 15:30:15 -08:00
|
|
|
}
|
2024-01-30 12:57:45 -08:00
|
|
|
public function toGoogleChat()
|
|
|
|
{
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
|
|
|
return GoogleChatMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->card(
|
|
|
|
Card::create()
|
|
|
|
->header(
|
|
|
|
'<strong>'.trans('mail.Accessory_Checkin_Notification').'</strong>' ?: '',
|
|
|
|
htmlspecialchars_decode($item->present()->name) ?: '',
|
|
|
|
)
|
|
|
|
->section(
|
|
|
|
Section::create(
|
|
|
|
KeyValue::create(
|
|
|
|
trans('mail.checked_into').': '.$item->location->name ? $item->location->name : '',
|
|
|
|
trans('admin/consumables/general.remaining').': '.$item->numRemaining(),
|
|
|
|
trans('admin/hardware/form.notes').": ".$note ?: '',
|
|
|
|
)
|
|
|
|
->onClick(route('accessories.show', $item->id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
/**
|
|
|
|
* Get the mail representation of the notification.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
|
|
*/
|
2020-05-23 11:56:56 -07:00
|
|
|
public function toMail()
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
2020-09-11 16:10:18 -07:00
|
|
|
\Log::debug('to email called');
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
return (new MailMessage)->markdown('notifications.markdown.checkin-accessory',
|
|
|
|
[
|
|
|
|
'item' => $this->item,
|
|
|
|
'admin' => $this->admin,
|
|
|
|
'note' => $this->note,
|
|
|
|
'target' => $this->target,
|
|
|
|
])
|
2020-11-12 15:05:57 -08:00
|
|
|
->subject(trans('mail.Accessory_Checkin_Notification'));
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
}
|