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;
|
2024-11-04 15:13:10 -08:00
|
|
|
use Illuminate\Notifications\Channels\SlackWebhookChannel;
|
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-10-28 14:30:34 -07:00
|
|
|
use Illuminate\Support\Str;
|
2024-01-30 12:38:17 -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-17 17:31:23 -08:00
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
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-03-07 18:10:10 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {
|
2024-01-30 12:38:17 -08:00
|
|
|
|
|
|
|
$notifyBy[] = GoogleChatChannel::class;
|
|
|
|
}
|
2024-01-24 10:38:06 -08:00
|
|
|
|
2024-03-07 18:10:10 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {
|
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-30 10:47:18 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('use webhook');
|
2024-11-04 15:13:10 -08:00
|
|
|
$notifyBy[] = SlackWebhookChannel::class;
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-28 14:30:34 -07:00
|
|
|
if(!Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {
|
|
|
|
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 ?: '');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-24 14:39:40 -07:00
|
|
|
$message = trans('mail.Asset_Checkin_Notification');
|
|
|
|
$details = [
|
|
|
|
trans('mail.asset') => htmlspecialchars_decode($item->present()->name),
|
|
|
|
trans('mail.checked_into') => $item->location->name ? $item->location->name : '',
|
|
|
|
trans('mail.Asset_Checkin_Notification')." by " => $admin->present()->fullName(),
|
|
|
|
trans('admin/hardware/form.status') => $item->assetstatus->name,
|
|
|
|
trans('mail.notes') => $note ?: '',
|
|
|
|
];
|
|
|
|
|
|
|
|
return array($message, $details);
|
2024-01-17 11:49:31 -08:00
|
|
|
}
|
2024-01-30 12:38:17 -08:00
|
|
|
public function toGoogleChat()
|
|
|
|
{
|
|
|
|
$target = $this->target;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
|
|
|
return GoogleChatMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->card(
|
|
|
|
Card::create()
|
|
|
|
->header(
|
|
|
|
'<strong>'.trans('mail.Asset_Checkin_Notification').'</strong>' ?: '',
|
|
|
|
htmlspecialchars_decode($item->present()->name) ?: '',
|
|
|
|
)
|
|
|
|
->section(
|
|
|
|
Section::create(
|
|
|
|
KeyValue::create(
|
|
|
|
trans('mail.checked_into') ?: '',
|
|
|
|
$item->location->name ? $item->location->name : '',
|
2024-01-30 12:57:45 -08:00
|
|
|
trans('admin/hardware/form.status').": ".$item->assetstatus->name,
|
2024-01-30 12:38:17 -08:00
|
|
|
)
|
|
|
|
->onClick(route('hardware.show', $item->id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
}
|