mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-10 07:34:06 -08:00
140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Notifications;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\Asset;
|
|
use App\Models\Setting;
|
|
use App\Models\User;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
use Illuminate\Notifications\Messages\SlackMessage;
|
|
use Illuminate\Notifications\Notification;
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
|
|
|
class CheckinAssetNotification extends Notification
|
|
{
|
|
use Queueable;
|
|
|
|
/**
|
|
* Create a new notification instance.
|
|
*
|
|
* @param $params
|
|
*/
|
|
public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $note)
|
|
{
|
|
$this->target = $checkedOutTo;
|
|
$this->item = $asset;
|
|
$this->admin = $checkedInBy;
|
|
$this->note = $note;
|
|
|
|
$this->settings = Setting::getSettings();
|
|
$this->expected_checkin = '';
|
|
|
|
if ($this->item->expected_checkin) {
|
|
$this->expected_checkin = Helper::getFormattedDateObject($this->item->expected_checkin, 'date',
|
|
false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the notification's delivery channels.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function via()
|
|
{
|
|
$notifyBy = [];
|
|
|
|
if (Setting::getSettings()->webhook_selected == 'microsoft'){
|
|
|
|
$notifyBy[] = MicrosoftTeamsChannel::class;
|
|
}
|
|
if (Setting::getSettings()->webhook_selected == 'slack') {
|
|
\Log::debug('use webhook');
|
|
$notifyBy[] = 'slack';
|
|
}
|
|
|
|
/**
|
|
* Only send checkin notifications to users if the category
|
|
* has the corresponding checkbox checked.
|
|
*/
|
|
if ($this->item->checkin_email() && $this->target instanceof User && $this->target->email != '') {
|
|
$notifyBy[] = 'mail';
|
|
}
|
|
|
|
return $notifyBy;
|
|
}
|
|
|
|
public function toSlack()
|
|
{
|
|
$admin = $this->admin;
|
|
$item = $this->item;
|
|
$note = $this->note;
|
|
$botname = ($this->settings->webhook_botname != '') ? $this->settings->webhook_botname : 'Snipe-Bot';
|
|
$channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
|
|
|
|
$fields = [
|
|
trans('general.administrator') => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
|
|
trans('general.status') => $item->assetstatus->name,
|
|
trans('general.location') => ($item->location) ? $item->location->name : '',
|
|
];
|
|
|
|
return (new SlackMessage)
|
|
->content(':arrow_down: :computer: '.trans('mail.Asset_Checkin_Notification'))
|
|
->from($botname)
|
|
->to($channel)
|
|
->attachment(function ($attachment) use ($item, $note, $admin, $fields) {
|
|
$attachment->title(htmlspecialchars_decode($item->present()->name), $item->present()->viewUrl())
|
|
->fields($fields)
|
|
->content($note);
|
|
});
|
|
}
|
|
public function toMicrosoftTeams()
|
|
{
|
|
$admin = $this->admin;
|
|
$item = $this->item;
|
|
$note = $this->note;
|
|
|
|
return MicrosoftTeamsMessage::create()
|
|
->to($this->settings->webhook_endpoint)
|
|
->type('success')
|
|
->title(trans('mail.Asset_Checkin_Notification'))
|
|
->addStartGroupToSection('activityText')
|
|
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
|
|
->fact(trans('mail.checked_into'), $item->location->name ? $item->location->name : '')
|
|
->fact(trans('mail.Asset_Checkin_Notification')." by ", $admin->present()->fullName())
|
|
->fact(trans('admin/hardware/form.status'), $item->assetstatus->name)
|
|
->fact(trans('mail.notes'), $note ?: '');
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
])
|
|
->subject(trans('mail.Asset_Checkin_Notification'));
|
|
|
|
return $message;
|
|
}
|
|
}
|