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-22 10:57:22 -08:00
|
|
|
{
|
2024-01-24 10:38:06 -08:00
|
|
|
$notifyBy = [];
|
|
|
|
|
2024-01-22 10:57:22 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'microsoft'){
|
2024-01-17 17:31:23 -08:00
|
|
|
|
2024-01-24 10:38:06 -08:00
|
|
|
$notifyBy[] = MicrosoftTeamsChannel::class;
|
2024-01-17 17:31:23 -08:00
|
|
|
}
|
2024-01-24 14:49:42 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'slack') {
|
2023-03-22 14:43:00 -07:00
|
|
|
\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
|
|
|
return MicrosoftTeamsMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->type('success')
|
2024-01-24 10:38:06 -08:00
|
|
|
->title(trans('mail.Asset_Checkin_Notification'))
|
2024-01-22 10:57:22 -08:00
|
|
|
->addStartGroupToSection('activityText')
|
2024-01-23 13:05:39 -08:00
|
|
|
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
|
2024-01-24 10:38:06 -08:00
|
|
|
->fact(trans('mail.checked_into'), $item->location->name ? $item->location->name : '')
|
2024-01-17 17:31:23 -08:00
|
|
|
->fact(trans('mail.Asset_Checkin_Notification')." by ", $admin->present()->fullName())
|
2024-01-24 10:38:06 -08:00
|
|
|
->fact(trans('admin/hardware/form.status'), $item->assetstatus->name)
|
2024-01-24 14:49:42 -08:00
|
|
|
->fact(trans('mail.notes'), $note ?: '');
|
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;
|
|
|
|
}
|
|
|
|
}
|