1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Date; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
11
|
|
|
use GraphQL\Doctrine\Annotation as API; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* An item that can be booked by a user. |
16
|
|
|
* |
17
|
|
|
* @ORM\Entity(repositoryClass="\Application\Repository\ProductRepository") |
18
|
|
|
* @API\Sorting({ |
19
|
|
|
* "Application\Api\Input\Sorting\Illustration" |
20
|
|
|
* }) |
21
|
|
|
*/ |
22
|
|
|
class Product extends AbstractProduct |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @ORM\Column(type="smallint", nullable=true, options={"unsigned" = true}) |
26
|
|
|
*/ |
27
|
|
|
private ?int $readingDuration = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @ORM\Column(type="date", nullable=true) |
31
|
|
|
*/ |
32
|
|
|
private ?\Cake\Chronos\Date $releaseDate = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @ORM\Column(type="smallint", nullable=true, unique=true, options={"unsigned" = true}) |
36
|
|
|
*/ |
37
|
|
|
private ?int $reviewNumber = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\OneToOne(targetEntity="File", orphanRemoval=true) |
41
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
42
|
|
|
*/ |
43
|
|
|
private ?\Application\Model\File $file = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @ORM\ManyToOne(targetEntity="Product") |
47
|
|
|
* @ORM\JoinColumns({ |
48
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
49
|
|
|
* }) |
50
|
|
|
*/ |
51
|
|
|
private ?\Application\Model\Product $review = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Collection<ProductTag> |
55
|
|
|
* |
56
|
|
|
* @ORM\ManyToMany(targetEntity="ProductTag", mappedBy="products") |
57
|
|
|
*/ |
58
|
|
|
private Collection $productTags; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var Collection<Product> |
62
|
|
|
* |
63
|
|
|
* @ORM\ManyToMany(targetEntity="Product") |
64
|
|
|
*/ |
65
|
|
|
private Collection $relatedProducts; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @ORM\Column(type="boolean", options={"default" = 0}) |
69
|
|
|
*/ |
70
|
|
|
private bool $isHighlighted = false; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @ORM\Column(type="text", length=65535) |
74
|
|
|
*/ |
75
|
|
|
private string $content = ''; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @ORM\Column(type="smallint", options={"default" = 0}) |
79
|
|
|
*/ |
80
|
|
|
private int $sorting = 0; |
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Constructor. |
84
|
|
|
*/ |
85
|
20 |
|
public function __construct(string $name = '') |
86
|
|
|
{ |
87
|
20 |
|
parent::__construct($name); |
88
|
20 |
|
$this->productTags = new ArrayCollection(); |
89
|
20 |
|
$this->relatedProducts = new ArrayCollection(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getProductTags(): Collection |
93
|
|
|
{ |
94
|
|
|
return $this->productTags; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Notify the user that it has a new productTag. |
99
|
|
|
* This should only be called by ProductTag::addUser(). |
100
|
|
|
*/ |
101
|
|
|
public function productTagAdded(ProductTag $productTag): void |
102
|
|
|
{ |
103
|
|
|
$this->productTags->add($productTag); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Notify the user that it a productTag was removed. |
108
|
|
|
* This should only be called by ProductTag::removeUser(). |
109
|
|
|
*/ |
110
|
|
|
public function productTagRemoved(ProductTag $productTag): void |
111
|
|
|
{ |
112
|
|
|
$this->productTags->removeElement($productTag); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Reading duration in minutes. |
117
|
|
|
*/ |
118
|
|
|
public function getReadingDuration(): ?int |
119
|
|
|
{ |
120
|
|
|
return $this->readingDuration; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Reading duration in minutes. |
125
|
|
|
*/ |
126
|
|
|
public function setReadingDuration(?int $readingDuration): void |
127
|
|
|
{ |
128
|
|
|
$this->readingDuration = $readingDuration; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getReleaseDate(): ?Date |
132
|
|
|
{ |
133
|
|
|
return $this->releaseDate; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function setReleaseDate(?Date $releaseDate): void |
137
|
|
|
{ |
138
|
1 |
|
$this->releaseDate = $releaseDate; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getReviewNumber(): ?int |
142
|
|
|
{ |
143
|
|
|
return $this->reviewNumber; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function setReviewNumber(?int $reviewNumber): void |
147
|
|
|
{ |
148
|
1 |
|
$this->reviewNumber = $reviewNumber; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getFile(): ?File |
152
|
|
|
{ |
153
|
|
|
return $this->file; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
public function setFile(?File $file): void |
157
|
|
|
{ |
158
|
|
|
// We must trigger lazy loading, otherwise Doctrine will seriously |
159
|
|
|
// mess up lifecycle callbacks and delete unrelated image on disk |
160
|
1 |
|
if ($this->file) { |
161
|
|
|
$this->file->getFilename(); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$this->file = $file; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get related products. |
169
|
|
|
* |
170
|
|
|
* @API\Field(type="Product[]") |
171
|
|
|
*/ |
172
|
1 |
|
public function getRelatedProducts(): Collection |
173
|
|
|
{ |
174
|
1 |
|
return $this->relatedProducts; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add related product. |
179
|
|
|
*/ |
180
|
2 |
|
public function addRelatedProduct(self $product): void |
181
|
|
|
{ |
182
|
2 |
|
if ($product === $this) { |
183
|
1 |
|
throw new InvalidArgumentException('A product cannot be related to itself'); |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
if (!$this->relatedProducts->contains($product)) { |
187
|
1 |
|
$this->relatedProducts[] = $product; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Remove related product. |
193
|
|
|
*/ |
194
|
1 |
|
public function removeRelatedProduct(self $product): void |
195
|
|
|
{ |
196
|
1 |
|
$this->relatedProducts->removeElement($product); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return null|Product |
201
|
|
|
*/ |
202
|
|
|
public function getReview(): ?self |
203
|
|
|
{ |
204
|
|
|
return $this->review; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param null|Product $review |
209
|
|
|
*/ |
210
|
|
|
public function setReview(?self $review): void |
211
|
|
|
{ |
212
|
|
|
$this->review = $review; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Whether this product has more visibility. |
217
|
|
|
*/ |
218
|
|
|
public function isHighlighted(): bool |
219
|
|
|
{ |
220
|
|
|
return $this->isHighlighted; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
*Whether this product has more visibility. |
225
|
|
|
*/ |
226
|
|
|
public function setIsHighlighted(bool $isHighlighted): void |
227
|
|
|
{ |
228
|
|
|
$this->isHighlighted = $isHighlighted; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set content. |
233
|
|
|
*/ |
234
|
|
|
public function setContent(string $content): void |
235
|
|
|
{ |
236
|
|
|
$content = strip_tags($content); |
237
|
|
|
$this->content = $content; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get content. |
242
|
|
|
*/ |
243
|
|
|
public function getContent(): string |
244
|
|
|
{ |
245
|
|
|
// Only administrator is allowed to see a product content |
246
|
|
|
if (!User::getCurrent() || User::getCurrent()->getRole() !== User::ROLE_ADMINISTRATOR) { |
247
|
|
|
return ''; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->content; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|