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\Entity\Attachments; |
24
|
|
|
|
25
|
|
|
use App\Entity\Base\AbstractNamedDBElement; |
26
|
|
|
use App\Entity\Base\MasterAttachmentTrait; |
27
|
|
|
use App\Entity\Contracts\HasAttachmentsInterface; |
28
|
|
|
use App\Entity\Contracts\HasMasterAttachmentInterface; |
29
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
30
|
|
|
use Doctrine\Common\Collections\Collection; |
31
|
|
|
use Doctrine\ORM\Mapping as ORM; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @ORM\MappedSuperclass() |
35
|
|
|
*/ |
36
|
|
|
abstract class AttachmentContainingDBElement extends AbstractNamedDBElement implements HasMasterAttachmentInterface, HasAttachmentsInterface |
37
|
|
|
{ |
38
|
|
|
use MasterAttachmentTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Attachment[]|Collection |
42
|
|
|
* //TODO |
43
|
|
|
* //@ORM\OneToMany(targetEntity="Attachment", mappedBy="element") |
44
|
|
|
* |
45
|
|
|
* Mapping is done in sub classes like part |
46
|
|
|
*/ |
47
|
|
|
protected $attachments; |
48
|
|
|
|
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->attachments = new ArrayCollection(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __clone() |
55
|
|
|
{ |
56
|
|
|
if ($this->id) { |
|
|
|
|
57
|
|
|
$attachments = $this->attachments; |
58
|
|
|
$this->attachments = new ArrayCollection(); |
59
|
|
|
//Set master attachment is needed |
60
|
|
|
foreach ($attachments as $attachment) { |
61
|
|
|
$clone = clone $attachment; |
62
|
|
|
if ($attachment === $this->master_picture_attachment) { |
63
|
|
|
$this->setMasterPictureAttachment($clone); |
64
|
|
|
} |
65
|
|
|
$this->addAttachment($clone); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//Parent has to be last call, as it resets the ID |
70
|
|
|
parent::__clone(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/******************************************************************************** |
74
|
|
|
* |
75
|
|
|
* Getters |
76
|
|
|
* |
77
|
|
|
*********************************************************************************/ |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets all attachments associated with this element. |
81
|
|
|
* |
82
|
|
|
* @return Attachment[]|Collection |
83
|
|
|
*/ |
84
|
|
|
public function getAttachments(): Collection |
85
|
|
|
{ |
86
|
|
|
return $this->attachments; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Adds an attachment to this element. |
91
|
|
|
* |
92
|
|
|
* @param Attachment $attachment Attachment |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function addAttachment(Attachment $attachment): self |
97
|
|
|
{ |
98
|
|
|
//Attachment must be associated with this element |
99
|
|
|
$attachment->setElement($this); |
100
|
|
|
$this->attachments->add($attachment); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Removes the given attachment from this element. |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function removeAttachment(Attachment $attachment): self |
111
|
|
|
{ |
112
|
|
|
$this->attachments->removeElement($attachment); |
113
|
|
|
|
114
|
|
|
//Check if this is the master attachment -> remove it from master attachment too, or it can not be deleted from DB... |
115
|
|
|
if ($attachment === $this->getMasterPictureAttachment()) { |
116
|
|
|
$this->setMasterPictureAttachment(null); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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: