Skip to content

A free open source IT asset/license management system

2.9 KB
Raw Blame
<?php

namespace App\Mail;

use App\Helpers\Helper;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class CheckinAssetMail extends BaseMailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    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, 'datetime',
                false);
        }
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        $from = new Address(config('mail.from.address'), config('mail.from.name'));

        return new Envelope(
            from: $from,
            subject: trans('mail.Asset_Checkin_Notification', ['tag' => $this->item->asset_tag]),
        );
    }

    /**
     * Get the mail representation of the notification.
     */
    public function content(): Content
    {
        $this->item->load('status');
        $fields = [];
        $customFields = [];

        // Check if the item has custom fields associated with it
        if (($this->item->model) && ($this->item->model->fieldset)) {
            $fields = $this->item->model->fieldset->fields;

            foreach ($fields as $field) {
                if (! $field->show_in_email || $field->field_encrypted == '1') {
                    continue;
                }

                $value = $this->item->{$field->db_column_name()};

                if (! is_null($value) && $value !== '') {
                    $customFields[] = [
                        'label' => $field->name,
                        'value' => $value,
                    ];
                }
            }
        }

        return new Content(
            markdown: 'mail.markdown.checkin-asset',
            with: [
                'item' => $this->item,
                'status' => $this->item->status?->name,
                'admin' => $this->admin,
                'note' => $this->note,
                'target' => $this->target,
                'fields' => $fields,
                'custom_fields' => $customFields,
                'expected_checkin' => $this->expected_checkin,
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}