2018-03-25 13:46:57 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications;
|
|
|
|
|
2023-11-07 16:03:28 -08:00
|
|
|
use App\Helpers\Helper;
|
2018-07-21 05:02:56 -07:00
|
|
|
use App\Models\Asset;
|
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-07-21 05:02:56 -07:00
|
|
|
use Illuminate\Bus\Queueable;
|
2018-03-25 13:46:57 -07:00
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
use Illuminate\Notifications\Messages\SlackMessage;
|
2018-07-21 05:02:56 -07:00
|
|
|
use Illuminate\Notifications\Notification;
|
2024-01-17 17:31:23 -08:00
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
class CheckinAssetNotification extends Notification
|
|
|
|
{
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @param $params
|
|
|
|
*/
|
2018-07-21 05:02:56 -07:00
|
|
|
public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $note)
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
2018-07-21 05:02:56 -07:00
|
|
|
$this->target = $checkedOutTo;
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->item = $asset;
|
|
|
|
$this->admin = $checkedInBy;
|
|
|
|
$this->note = $note;
|
2018-03-25 13:46:57 -07:00
|
|
|
|
2018-07-21 05:02:56 -07:00
|
|
|
$this->settings = Setting::getSettings();
|
|
|
|
$this->expected_checkin = '';
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
if ($this->item->expected_checkin) {
|
2021-09-28 19:44:55 -07:00
|
|
|
$this->expected_checkin = Helper::getFormattedDateObject($this->item->expected_checkin, 'date',
|
2018-03-25 13:46:57 -07:00
|
|
|
false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification's delivery channels.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function via()
|
2024-01-17 17:31:23 -08:00
|
|
|
{ if (Setting::getSettings()->webhook_selected == 'microsoft'){
|
|
|
|
|
|
|
|
return [MicrosoftTeamsChannel::class];
|
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
$notifyBy = [];
|
2023-03-22 14:43:00 -07:00
|
|
|
if (Setting::getSettings()->webhook_endpoint != '') {
|
|
|
|
\Log::debug('use webhook');
|
2018-03-25 13:46:57 -07:00
|
|
|
$notifyBy[] = 'slack';
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:09:04 -07:00
|
|
|
/**
|
2020-05-23 11:56:56 -07:00
|
|
|
* Only send checkin notifications to users if the category
|
2018-07-16 14:09:04 -07:00
|
|
|
* has the corresponding checkbox checked.
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($this->item->checkin_email() && $this->target instanceof User && $this->target->email != '') {
|
2018-03-25 13:46:57 -07:00
|
|
|
$notifyBy[] = 'mail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notifyBy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toSlack()
|
|
|
|
{
|
|
|
|
$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 = [
|
|
|
|
trans('general.administrator') => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
|
|
|
|
trans('general.status') => $item->assetstatus->name,
|
2018-05-04 21:01:25 -07:00
|
|
|
trans('general.location') => ($item->location) ? $item->location->name : '',
|
2018-03-25 13:46:57 -07:00
|
|
|
];
|
2020-05-23 11:56:56 -07:00
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
return (new SlackMessage)
|
2020-11-12 15:05:57 -08:00
|
|
|
->content(':arrow_down: :computer: '.trans('mail.Asset_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-17 17:31:23 -08:00
|
|
|
public function toMicrosoftTeams()
|
2024-01-17 11:49:31 -08:00
|
|
|
{
|
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
2024-01-17 17:31:23 -08:00
|
|
|
$button = [
|
2024-01-17 11:49:31 -08:00
|
|
|
trans('general.administrator') => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
|
|
|
|
trans('general.status') => $item->assetstatus->name,
|
|
|
|
trans('general.location') => ($item->location) ? $item->location->name : '',
|
|
|
|
];
|
|
|
|
|
2024-01-17 17:31:23 -08:00
|
|
|
return MicrosoftTeamsMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->type('success')
|
2024-01-17 17:59:20 -08:00
|
|
|
->addStartGroupToSection('header')
|
2024-01-17 17:31:23 -08:00
|
|
|
->title("Asset Checked in")
|
2024-01-17 17:59:20 -08:00
|
|
|
->fact(htmlspecialchars_decode($item->present()->name), '', 'header')
|
2024-01-17 17:31:23 -08:00
|
|
|
->fact('Checked into ', $item->location->name)
|
|
|
|
->fact(trans('mail.Asset_Checkin_Notification')." by ", $admin->present()->fullName())
|
2024-01-17 17:59:20 -08:00
|
|
|
->fact('Asset Status', $item->assetstatus->name)
|
|
|
|
->fact('Notes', $note ?: 'No notes');
|
|
|
|
// ->image($item->getImageUrl(), $item->model()->name, 'header');
|
|
|
|
|
2024-01-17 17:31:23 -08:00
|
|
|
|
|
|
|
|
2024-01-17 11:49:31 -08:00
|
|
|
|
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mail representation of the notification.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
|
|
*/
|
|
|
|
public function toMail()
|
|
|
|
{
|
|
|
|
$fields = [];
|
|
|
|
|
|
|
|
// Check if the item has custom fields associated with it
|
|
|
|
if (($this->item->model) && ($this->item->model->fieldset)) {
|
|
|
|
$fields = $this->item->model->fieldset->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = (new MailMessage)->markdown('notifications.markdown.checkin-asset',
|
|
|
|
[
|
|
|
|
'item' => $this->item,
|
|
|
|
'admin' => $this->admin,
|
|
|
|
'note' => $this->note,
|
|
|
|
'target' => $this->target,
|
|
|
|
'fields' => $fields,
|
|
|
|
'expected_checkin' => $this->expected_checkin,
|
|
|
|
])
|
2020-11-12 15:05:57 -08:00
|
|
|
->subject(trans('mail.Asset_Checkin_Notification'));
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
}
|