|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
6
|
|
|
use Cocur\Slugify\Slugify; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Profile Entity |
|
10
|
|
|
* |
|
11
|
|
|
* @ORM\Table(name="profiles") |
|
12
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\ProfileRepository") |
|
13
|
|
|
* @ORM\HasLifecycleCallbacks() |
|
14
|
|
|
* |
|
15
|
|
|
* @author Borut Balažek <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ProfileEntity |
|
18
|
|
|
extends AbstractImageUpload |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var integer |
|
22
|
|
|
* |
|
23
|
|
|
* @ORM\Column(name="id", type="integer") |
|
24
|
|
|
* @ORM\Id |
|
25
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $id; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Mr., Mrs., Ms., Ing., ... |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
* |
|
34
|
|
|
* @ORM\Column(name="title", type="string", length=8, nullable=true) |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $title; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
* |
|
41
|
|
|
* @ORM\Column(name="first_name", type="string", length=32, nullable=true) |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $firstName; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
* |
|
48
|
|
|
* @ORM\Column(name="middle_name", type="string", length=32, nullable=true) |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $middleName; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
* |
|
55
|
|
|
* @ORM\Column(name="last_name", type="string", length=32, nullable=true) |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $lastName; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* male or female? |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
* |
|
64
|
|
|
* @ORM\Column(name="gender", type="string", length=8, nullable=true) |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $gender; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var \DateTime |
|
70
|
|
|
* |
|
71
|
|
|
* @ORM\Column(name="birthdate", type="datetime", nullable=true) |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $birthdate; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var string |
|
77
|
|
|
* |
|
78
|
|
|
* @ORM\Column(name="image_url", type="text", nullable=true) |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $imageUrl; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @ORM\OneToOne(targetEntity="Application\Entity\UserEntity", inversedBy="profile") |
|
84
|
|
|
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $user; |
|
87
|
|
|
|
|
88
|
|
|
/*** Id ***/ |
|
89
|
|
|
/** |
|
90
|
|
|
* @return integer |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getId() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->id; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $id |
|
99
|
|
|
* |
|
100
|
|
|
* @return ProfileEntity |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setId($id) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->id = $id; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/*** Title ***/ |
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getTitle() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->title; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $title |
|
120
|
|
|
* |
|
121
|
|
|
* @return ProfileEntity |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setTitle($title) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->title = $title; |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/*** Name ***/ |
|
131
|
|
|
/** |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getName() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->getFirstName().' '.$this->getLastName(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/*** First name ***/ |
|
140
|
|
|
/** |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getFirstName() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->firstName; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param $firstName |
|
150
|
|
|
* |
|
151
|
|
|
* @return ProfileEntity |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setFirstName($firstName) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->firstName = $firstName; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/*** Middle name ***/ |
|
161
|
|
|
/** |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getMiddleName() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->middleName; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param $middleName |
|
171
|
|
|
* |
|
172
|
|
|
* @return ProfileEntity |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setMiddleName($middleName) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->middleName = $middleName; |
|
177
|
|
|
|
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/*** Last name ***/ |
|
182
|
|
|
/** |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getLastName() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->lastName; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param $lastName |
|
192
|
|
|
* |
|
193
|
|
|
* @return ProfileEntity |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setLastName($lastName) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->lastName = $lastName; |
|
198
|
|
|
|
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/*** Full name ***/ |
|
203
|
|
|
/** |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getFullName() |
|
207
|
|
|
{ |
|
208
|
|
|
return trim( |
|
209
|
|
|
$this->getTitle().' '. |
|
210
|
|
|
$this->getFirstName().' '. |
|
211
|
|
|
$this->getMiddleName().' '. |
|
212
|
|
|
$this->getLastName() |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/*** Gender ***/ |
|
217
|
|
|
/** |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getGender() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->gender; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param $gender |
|
227
|
|
|
* |
|
228
|
|
|
* @return ProfileEntity |
|
229
|
|
|
*/ |
|
230
|
|
|
public function setGender($gender) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->gender = $gender; |
|
233
|
|
|
|
|
234
|
|
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/*** Birthdate ***/ |
|
238
|
|
|
/** |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getBirthdate() |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->birthdate; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @param mixed $birthdate |
|
248
|
|
|
* |
|
249
|
|
|
* @return ProfileEntity |
|
250
|
|
|
*/ |
|
251
|
|
|
public function setBirthdate($birthdate = null) |
|
252
|
|
|
{ |
|
253
|
|
|
if ($birthdate === null) { |
|
254
|
|
|
$this->birthdate = null; |
|
255
|
|
|
} elseif ($birthdate instanceof \DateTime) { |
|
256
|
|
|
$this->birthdate = $birthdate; |
|
257
|
|
|
} else { |
|
258
|
|
|
$this->birthdate = new \DateTime($birthdate); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/*** Age ***/ |
|
265
|
|
|
/** |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getAge($format = '%y') |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->getBirthdate() |
|
271
|
|
|
? $this->getBirthdate()->diff(new \DateTime())->format($format) |
|
272
|
|
|
: null |
|
273
|
|
|
; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/*** Image upload ***/ |
|
277
|
|
|
/** |
|
278
|
|
|
* @return ProfileEntity |
|
279
|
|
|
* |
|
280
|
|
|
* @throws \Exception If upload dir and path are not set |
|
281
|
|
|
*/ |
|
282
|
|
View Code Duplication |
public function imageUpload() |
|
|
|
|
|
|
283
|
|
|
{ |
|
284
|
|
|
if (null !== $this->getImage()) { |
|
285
|
|
|
$uploadDir = $this->getImageUploadDir(); |
|
286
|
|
|
$uploadPath = $this->getImageUploadPath(); |
|
287
|
|
|
|
|
288
|
|
|
if (!($uploadDir && $uploadPath)) { |
|
289
|
|
|
throw new \Exception('You must define the image upload dir and path!'); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
$slugify = new Slugify(); |
|
293
|
|
|
|
|
294
|
|
|
$filename = $slugify->slugify( |
|
295
|
|
|
$this->getImage()->getClientOriginalName() |
|
|
|
|
|
|
296
|
|
|
); |
|
297
|
|
|
|
|
298
|
|
|
$filename .= '_'.sha1(uniqid(mt_rand(), true)).'.'. |
|
299
|
|
|
$this->getImage()->guessExtension() |
|
300
|
|
|
; |
|
301
|
|
|
|
|
302
|
|
|
$this->getImage()->move( |
|
303
|
|
|
$uploadDir, |
|
304
|
|
|
$filename |
|
305
|
|
|
); |
|
306
|
|
|
|
|
307
|
|
|
$this->setImageUrl($uploadPath.$filename); |
|
308
|
|
|
|
|
309
|
|
|
$this->setImage(null); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return $this; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/*** User ***/ |
|
316
|
|
|
/** |
|
317
|
|
|
* @return UserEntity |
|
318
|
|
|
*/ |
|
319
|
|
|
public function getUser() |
|
320
|
|
|
{ |
|
321
|
|
|
return $this->user; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param UserEntity $user |
|
326
|
|
|
* |
|
327
|
|
|
* @return ProfileEntity |
|
328
|
|
|
*/ |
|
329
|
|
|
public function setUser(UserEntity $user) |
|
330
|
|
|
{ |
|
331
|
|
|
$this->user = $user; |
|
332
|
|
|
|
|
333
|
|
|
return $this; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* @return array |
|
338
|
|
|
*/ |
|
339
|
|
|
public function toArray() |
|
340
|
|
|
{ |
|
341
|
|
|
return array( |
|
342
|
|
|
'id' => $this->getId(), |
|
343
|
|
|
'title' => $this->getTitle(), |
|
344
|
|
|
'full_name' => $this->getFullName(), |
|
345
|
|
|
'first_name' => $this->getFirstName(), |
|
346
|
|
|
'middle_name' => $this->getMiddleName(), |
|
347
|
|
|
'last_name' => $this->getLastName(), |
|
348
|
|
|
'gender' => $this->getGender(), |
|
349
|
|
|
'birthdate' => $this->getBirthdate() |
|
350
|
|
|
? $this->getBirthdate()->format(DATE_ATOM) |
|
351
|
|
|
: null, |
|
352
|
|
|
'image_url' => $this->getImageUrl(), |
|
353
|
|
|
); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|