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