2018-03-25 13:46:57 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications;
|
|
|
|
|
2021-10-07 12:32:57 -07:00
|
|
|
use App\Helpers\Helper;
|
2018-07-27 15:27:19 -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;
|
2024-01-23 13:05:39 -08:00
|
|
|
use Exception;
|
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:38:17 -08:00
|
|
|
use NotificationChannels\GoogleChat\Card;
|
|
|
|
use NotificationChannels\GoogleChat\Enums\Icon;
|
|
|
|
use NotificationChannels\GoogleChat\Enums\ImageStyle;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatChannel;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatMessage;
|
|
|
|
use NotificationChannels\GoogleChat\Section;
|
|
|
|
use NotificationChannels\GoogleChat\Widgets\KeyValue;
|
2024-01-22 10:57:22 -08:00
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-10-24 14:24:00 -07:00
|
|
|
use Osama\LaravelTeamsNotification\Logging\TeamsLoggingChannel;
|
2024-10-24 12:21:51 -07:00
|
|
|
use Osama\LaravelTeamsNotification\TeamsNotification;
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
class CheckoutAssetNotification extends Notification
|
|
|
|
{
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @param $params
|
|
|
|
*/
|
2018-07-28 03:45:33 -07:00
|
|
|
public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
2018-07-27 15:27:19 -07:00
|
|
|
$this->item = $asset;
|
|
|
|
$this->admin = $checkedOutBy;
|
|
|
|
$this->note = $note;
|
|
|
|
$this->target = $checkedOutTo;
|
2018-07-28 03:45:33 -07:00
|
|
|
$this->acceptance = $acceptance;
|
2018-07-27 15:27:19 -07:00
|
|
|
|
|
|
|
$this->settings = Setting::getSettings();
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
$this->last_checkout = '';
|
|
|
|
$this->expected_checkin = '';
|
|
|
|
|
|
|
|
if ($this->item->last_checkout) {
|
2021-09-28 19:44:55 -07:00
|
|
|
$this->last_checkout = Helper::getFormattedDateObject($this->item->last_checkout, 'date',
|
2018-03-25 13:46:57 -07:00
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 12:38:17 -08:00
|
|
|
$notifyBy = [];
|
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-03-07 18:10:10 -08:00
|
|
|
if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {
|
2024-01-22 10:57:22 -08:00
|
|
|
|
2024-10-24 12:21:51 -07:00
|
|
|
$notifyBy[] = TeamsNotification::class;
|
2024-01-22 10:57:22 -08:00
|
|
|
}
|
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' ) {
|
2024-02-08 14:59:25 -08:00
|
|
|
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('use webhook');
|
2018-03-25 13:46:57 -07:00
|
|
|
$notifyBy[] = 'slack';
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:09:04 -07:00
|
|
|
/**
|
|
|
|
* Only send notifications to users that have email addresses
|
|
|
|
*/
|
|
|
|
if ($this->target instanceof User && $this->target->email != '') {
|
|
|
|
|
|
|
|
/**
|
2020-05-23 11:56:56 -07:00
|
|
|
* Send an email if the asset requires acceptance,
|
2018-07-16 14:09:04 -07:00
|
|
|
* so the user can accept or decline the asset
|
|
|
|
*/
|
|
|
|
if ($this->item->requireAcceptance()) {
|
|
|
|
$notifyBy[1] = 'mail';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email if the item has a EULA, since the user should always receive it
|
|
|
|
*/
|
|
|
|
if ($this->item->getEula()) {
|
|
|
|
$notifyBy[1] = 'mail';
|
2020-05-23 11:56:56 -07:00
|
|
|
}
|
2018-07-16 14:09:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email if an email should be sent at checkin/checkout
|
|
|
|
*/
|
|
|
|
if ($this->item->checkin_email()) {
|
|
|
|
$notifyBy[1] = 'mail';
|
2020-05-23 11:56:56 -07:00
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
2018-07-16 14:09:04 -07:00
|
|
|
|
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().'>',
|
|
|
|
];
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($this->expected_checkin) && ($this->expected_checkin != '')) {
|
2018-03-25 13:46:57 -07:00
|
|
|
$fields['Expected Checkin'] = $this->expected_checkin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new SlackMessage)
|
2023-05-15 09:01:30 -07:00
|
|
|
->content(':arrow_up: :computer: '.trans('mail.Asset_Checkout_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-10-24 14:39:40 -07:00
|
|
|
public function toMicrosoftTeams() : array
|
2024-01-22 10:57:22 -08:00
|
|
|
{
|
2024-01-24 10:38:06 -08:00
|
|
|
$target = $this->target;
|
2024-01-22 10:57:22 -08:00
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
2024-10-24 14:24:00 -07:00
|
|
|
$message = trans('mail.Asset_Checkout_Notification');
|
|
|
|
$details = [
|
|
|
|
trans('mail.assigned_to') => $target->present()->name,
|
2024-10-24 14:39:40 -07:00
|
|
|
trans('mail.asset') => htmlspecialchars_decode($item->present()->name),
|
2024-10-24 14:24:00 -07:00
|
|
|
trans('mail.Asset_Checkout_Notification'). ' by' => $admin->present()->fullName(),
|
|
|
|
trans('mail.notes') => $note ?: '',
|
|
|
|
];
|
|
|
|
return array($message, $details);
|
2024-01-22 10:57:22 -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_Checkout_Notification').'</strong>' ?: '',
|
|
|
|
htmlspecialchars_decode($item->present()->name) ?: '',
|
|
|
|
)
|
|
|
|
->section(
|
|
|
|
Section::create(
|
|
|
|
KeyValue::create(
|
|
|
|
trans('mail.assigned_to') ?: '',
|
|
|
|
$target->present()->name ?: '',
|
|
|
|
$note ?: '',
|
|
|
|
)
|
|
|
|
->onClick(route('users.show', $target->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
|
|
|
|
*/
|
|
|
|
public function toMail()
|
2024-09-19 12:23:07 -07:00
|
|
|
{ $this->item->load('assetstatus');
|
2021-06-10 13:15:52 -07:00
|
|
|
$eula = method_exists($this->item, 'getEula') ? $this->item->getEula() : '';
|
2018-03-25 13:46:57 -07:00
|
|
|
$req_accept = method_exists($this->item, 'requireAcceptance') ? $this->item->requireAcceptance() : 0;
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
$message = (new MailMessage)->markdown('notifications.markdown.checkout-asset',
|
|
|
|
[
|
|
|
|
'item' => $this->item,
|
|
|
|
'admin' => $this->admin,
|
2024-08-01 11:13:20 -07:00
|
|
|
'status' => $this->item->assetstatus?->name,
|
2018-03-25 13:46:57 -07:00
|
|
|
'note' => $this->note,
|
|
|
|
'target' => $this->target,
|
|
|
|
'fields' => $fields,
|
|
|
|
'eula' => $eula,
|
|
|
|
'req_accept' => $req_accept,
|
2018-07-28 03:45:33 -07:00
|
|
|
'accept_url' => $accept_url,
|
2018-03-25 13:46:57 -07:00
|
|
|
'last_checkout' => $this->last_checkout,
|
|
|
|
'expected_checkin' => $this->expected_checkin,
|
|
|
|
])
|
|
|
|
->subject(trans('mail.Confirm_asset_delivery'));
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
}
|