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;
|
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;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$notifyBy = [];
|
|
|
|
|
2023-03-22 14:43:00 -07:00
|
|
|
if ((Setting::getSettings()) && (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
|
|
|
/**
|
|
|
|
* 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';
|
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)
|
|
|
|
->attachment(function ($attachment) use ($item, $note, $admin, $fields) {
|
|
|
|
$attachment->title(htmlspecialchars_decode($item->present()->name), $item->present()->viewUrl())
|
|
|
|
->fields($fields)
|
|
|
|
->content($note);
|
|
|
|
});
|
|
|
|
}
|
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()
|
|
|
|
{
|
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,
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|