1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaResourceDynamicExport\Export; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Laravel\Nova\Notifications\NovaNotification; |
8
|
|
|
use Laravel\Nova\URL; |
9
|
|
|
use NovaResourceDynamicExport\Models\ExportStoredFile; |
10
|
|
|
|
11
|
|
|
trait WithNotification |
12
|
|
|
{ |
13
|
|
|
use HasDownLoadLink; |
14
|
|
|
|
15
|
|
|
protected ?string $notificationUserClass = null; |
16
|
|
|
protected ?int $notificationUserId = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set user data to end notifications for. |
20
|
|
|
* |
21
|
|
|
* @param Model|null $notificationUser |
22
|
|
|
* @return $this |
23
|
|
|
*/ |
24
|
1 |
|
public function setNotificationUser(?Model $notificationUser = null): static |
25
|
|
|
{ |
26
|
1 |
|
$this->notificationUserId = $notificationUser?->getKey(); |
27
|
1 |
|
$this->notificationUserClass = $notificationUser?->getMorphClass(); |
28
|
|
|
|
29
|
1 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Find user to send notification. |
34
|
|
|
* |
35
|
|
|
* @return Model|null |
36
|
|
|
*/ |
37
|
1 |
|
public function notificationUser(): ?Model |
38
|
|
|
{ |
39
|
1 |
|
if ($this->notificationUserClass && $this->notificationUserId) { |
|
|
|
|
40
|
1 |
|
$class = Relation::getMorphedModel($this->notificationUserClass)?:$this->notificationUserClass; |
41
|
1 |
|
if ($class && is_a($class, Model::class, true)) { |
42
|
1 |
|
return $class::query()->find($this->notificationUserId); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Send notification to user. |
52
|
|
|
* |
53
|
|
|
* @param ExportStoredFile $fileModel |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
1 |
|
public function notifyUser(ExportStoredFile $fileModel): void |
57
|
|
|
{ |
58
|
1 |
|
$user = $this->notificationUser(); |
59
|
1 |
|
if ($user && $fileModel?->exists) { |
60
|
1 |
|
$user->notify( |
61
|
1 |
|
NovaNotification::make() |
62
|
1 |
|
->message("File {$fileModel->name} exported") |
63
|
1 |
|
->action('Download', URL::remote($this->downloadLink($fileModel->download_link))) |
64
|
1 |
|
->icon('download') |
65
|
1 |
|
->type('info') |
66
|
1 |
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: