1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace App\Services\Attachments; |
24
|
|
|
|
25
|
|
|
use App\Entity\Attachments\Attachment; |
26
|
|
|
use App\Entity\Parts\Part; |
27
|
|
|
|
28
|
|
|
class PartPreviewGenerator |
29
|
|
|
{ |
30
|
|
|
protected AttachmentManager $attachmentHelper; |
31
|
|
|
|
32
|
|
|
public function __construct(AttachmentManager $attachmentHelper) |
33
|
|
|
{ |
34
|
|
|
$this->attachmentHelper = $attachmentHelper; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns a list of attachments that can be used for previewing the part ordered by priority. |
39
|
|
|
* The priority is: Part MasterAttachment -> Footprint MasterAttachment -> Category MasterAttachment |
40
|
|
|
* -> Storelocation Attachment -> MeasurementUnit Attachment -> ManufacturerAttachment. |
41
|
|
|
* |
42
|
|
|
* @param Part $part the part for which the attachments should be determined |
43
|
|
|
* |
44
|
|
|
* @return (Attachment|null)[] |
45
|
|
|
* |
46
|
|
|
* @psalm-return list<Attachment|null> |
47
|
|
|
*/ |
48
|
|
|
public function getPreviewAttachments(Part $part): array |
49
|
|
|
{ |
50
|
|
|
$list = []; |
51
|
|
|
|
52
|
|
|
//Master attachment has top priority |
53
|
|
|
$attachment = $part->getMasterPictureAttachment(); |
54
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
55
|
|
|
$list[] = $attachment; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (null !== $part->getFootprint()) { |
59
|
|
|
$attachment = $part->getFootprint()->getMasterPictureAttachment(); |
60
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
61
|
|
|
$list[] = $attachment; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null !== $part->getBuiltProject()) { |
66
|
|
|
$attachment = $part->getBuiltProject()->getMasterPictureAttachment(); |
67
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
68
|
|
|
$list[] = $attachment; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (null !== $part->getCategory()) { |
73
|
|
|
$attachment = $part->getCategory()->getMasterPictureAttachment(); |
74
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
75
|
|
|
$list[] = $attachment; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($part->getPartLots() as $lot) { |
80
|
|
|
if (null !== $lot->getStorageLocation()) { |
81
|
|
|
$attachment = $lot->getStorageLocation()->getMasterPictureAttachment(); |
82
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
83
|
|
|
$list[] = $attachment; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (null !== $part->getPartUnit()) { |
89
|
|
|
$attachment = $part->getPartUnit()->getMasterPictureAttachment(); |
90
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
91
|
|
|
$list[] = $attachment; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (null !== $part->getManufacturer()) { |
96
|
|
|
$attachment = $part->getManufacturer()->getMasterPictureAttachment(); |
97
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
98
|
|
|
$list[] = $attachment; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $list; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determines what attachment should be used for previewing a part (especially in part table). |
107
|
|
|
* The returned attachment is guaranteed to be existing and be a picture. |
108
|
|
|
* |
109
|
|
|
* @param Part $part The part for which the attachment should be determined |
110
|
|
|
*/ |
111
|
|
|
public function getTablePreviewAttachment(Part $part): ?Attachment |
112
|
|
|
{ |
113
|
|
|
//First of all we check if the master attachment of the part is set (and a picture) |
114
|
|
|
$attachment = $part->getMasterPictureAttachment(); |
115
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
116
|
|
|
return $attachment; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//Otherwise check if the part has a footprint with a valid master attachment |
120
|
|
|
if (null !== $part->getFootprint()) { |
121
|
|
|
$attachment = $part->getFootprint()->getMasterPictureAttachment(); |
122
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
123
|
|
|
return $attachment; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//With lowest priority use the master attachment of the project this part represents (when existing) |
128
|
|
|
if (null !== $part->getBuiltProject()) { |
129
|
|
|
$attachment = $part->getBuiltProject()->getMasterPictureAttachment(); |
130
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
131
|
|
|
return $attachment; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//If nothing is available return null |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks if a attachment is exising and a valid picture. |
141
|
|
|
* |
142
|
|
|
* @param Attachment|null $attachment the attachment that should be checked |
143
|
|
|
* |
144
|
|
|
* @return bool true if the attachment is valid |
145
|
|
|
*/ |
146
|
|
|
protected function isAttachmentValidPicture(?Attachment $attachment): bool |
147
|
|
|
{ |
148
|
|
|
return null !== $attachment |
149
|
|
|
&& $attachment->isPicture() |
150
|
|
|
&& $this->attachmentHelper->isFileExisting($attachment); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|