1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rudolf\Modules\Articles\One; |
4
|
|
|
|
5
|
|
|
use Rudolf\Component\Forms\Validator; |
6
|
|
|
use Rudolf\Component\Hooks; |
7
|
|
|
use Rudolf\Component\Html\Text; |
8
|
|
|
use Rudolf\Component\Images\Image; |
9
|
|
|
|
10
|
|
|
class Article |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array Article data |
14
|
|
|
*/ |
15
|
|
|
protected $article; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor. |
19
|
|
|
* |
20
|
|
|
* @param array $article |
21
|
|
|
*/ |
22
|
|
|
public function __construct($article = []) |
23
|
|
|
{ |
24
|
|
|
$this->setData($article); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set article data. |
29
|
|
|
* |
30
|
|
|
* @param array $article |
31
|
|
|
*/ |
32
|
|
|
public function setData($article) |
33
|
|
|
{ |
34
|
|
|
$this->article = array_merge( |
35
|
|
|
[ |
36
|
|
|
'id' => 0, |
37
|
|
|
'category_ID' => 0, |
38
|
|
|
'title' => '', |
39
|
|
|
'keywords' => '', |
40
|
|
|
'description' => '', |
41
|
|
|
'content' => '', |
42
|
|
|
'author' => '', |
43
|
|
|
'date' => '', |
44
|
|
|
'added' => '', |
45
|
|
|
'modified' => '', |
46
|
|
|
'adder_ID' => 0, |
47
|
|
|
'adder_first_name' => '', |
48
|
|
|
'adder_surname' => '', |
49
|
|
|
'modifier_ID' => 0, |
50
|
|
|
'modifier_first_name' => '', |
51
|
|
|
'modifier_surname' => '', |
52
|
|
|
'views' => 0, |
53
|
|
|
'slug' => '', |
54
|
|
|
'url' => '', |
55
|
|
|
'album' => '', |
56
|
|
|
'thumb' => '', |
57
|
|
|
'thumbnail' => '', |
58
|
|
|
'photos' => '', |
59
|
|
|
'homepage_hidden' => false, |
60
|
|
|
'published' => false, |
61
|
|
|
'category' => '', |
62
|
|
|
'category_title' => '', |
63
|
|
|
'category_url' => '', |
64
|
|
|
], |
65
|
|
|
(array) $article |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns article ID. |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function id() |
75
|
|
|
{ |
76
|
|
|
return (int) $this->article['id']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns category ID. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
public function categoryID() |
85
|
|
|
{ |
86
|
|
|
return (int) $this->article['category_ID']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns article title. |
91
|
|
|
* |
92
|
|
|
* @param string $type null|raw |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function title($type = '') |
97
|
|
|
{ |
98
|
|
|
$title = $this->article['title']; |
99
|
|
|
if ('raw' === $type) { |
100
|
|
|
return $title; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return Text::escape($title); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the keywords. |
108
|
|
|
* |
109
|
|
|
* @param string $type null|raw |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function keywords($type = '') |
114
|
|
|
{ |
115
|
|
|
$keywords = $this->article['keywords']; |
116
|
|
|
if ('raw' === $type) { |
117
|
|
|
return $keywords; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return Text::escape($keywords); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the description. |
125
|
|
|
* |
126
|
|
|
* @param string $type |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function description($type = '') |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$description = $this->article['description']; |
133
|
|
|
if ('raw' === $type) { |
134
|
|
|
return $description; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return Text::escape($description); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns article content. |
142
|
|
|
* |
143
|
|
|
* @param bool|int $truncate |
144
|
|
|
* @param bool $stripTags |
145
|
|
|
* @param bool $escape |
146
|
|
|
* @param bool $raw |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function content($truncate = false, $stripTags = false, $escape = false, $raw = false) |
151
|
|
|
{ |
152
|
|
|
$content = $this->article['content']; |
153
|
|
|
|
154
|
|
|
if (true === $stripTags) { |
155
|
|
|
$content = strip_tags($content); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (false !== $truncate) { |
159
|
|
|
$content = Text::truncate($content, $truncate, '…', '<b><i><u><em><strong><a><span>'); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (true === $escape) { |
163
|
|
|
$content = Text::escape($content); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (false === $raw) { |
167
|
|
|
$content = Hooks\Filter::apply('content_filter', $content); |
168
|
|
|
|
169
|
|
|
return $content; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $content; |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* Returns the author. |
176
|
|
|
* |
177
|
|
|
* @param string $type null|raw |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function author($type = '', $adder = true) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$author = $this->article['author']; |
184
|
|
|
|
185
|
|
|
// if fields is empty and $adder is true |
186
|
|
|
if (empty($author) and true === $adder) { |
|
|
|
|
187
|
|
|
$author = $this->adderFullName(false); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if ('raw' === $type) { |
191
|
|
|
return $author; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return Text::escape($author); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns article date. |
199
|
|
|
* |
200
|
|
|
* @param bool|string $format |
201
|
|
|
* @param string $style normal|locale |
202
|
|
|
* |
203
|
|
|
* @return string If date field empty, return current date |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function date($format = false, $style = 'normal', $inflected = true) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$date = $this->article['date']; |
208
|
|
|
|
209
|
|
|
$validator = new Validator(); |
210
|
|
|
$validator->checkDatetime('date', $date, 'Y-m-d H:i:s'); |
211
|
|
|
if ($validator->isErrors() or empty($date)) { |
|
|
|
|
212
|
|
|
return $date; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
switch ($style) { |
216
|
|
|
case 'locale': // http://php.net/manual/en/function.strftime.php |
217
|
|
|
$format = ($format) ? $format : '%D'; |
218
|
|
|
$date = strftime($format, strtotime($date)); |
219
|
|
|
break; |
220
|
|
|
|
221
|
|
|
default: // http://php.net/manual/en/datetime.formats.date.php |
222
|
|
|
$format = ($format) ? $format : 'Y-m-d H:i:s'; |
223
|
|
|
$date = date_format(date_create($date), $format); |
224
|
|
|
break; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$date = Hooks\Filter::apply('date_format_filter', $date); |
228
|
|
|
|
229
|
|
|
if (true === $inflected) { |
230
|
|
|
$month = [ |
231
|
|
|
'styczeń' => 'stycznia', // 01 |
232
|
|
|
'luty' => 'lutego', // 02 |
233
|
|
|
'marzec' => 'marca', // 03 |
234
|
|
|
'kwiecień' => 'kwietnia', // 04 |
235
|
|
|
'maj' => 'maja', // 05 |
236
|
|
|
'czerwiec' => 'czerwca', // 06 |
237
|
|
|
'lipiec' => 'lipca', // 07 |
238
|
|
|
'sierpień' => 'sierpnia', // 08 |
239
|
|
|
'wrzesień' => 'września', // 09 |
240
|
|
|
'październik' => 'października', // 10 |
241
|
|
|
'listopad' => 'listopada', // 11 |
242
|
|
|
'grudzień' => 'grudnia', // 12 |
243
|
|
|
]; |
244
|
|
|
|
245
|
|
|
foreach ($month as $key => $value) { |
246
|
|
|
$date = str_replace($key, $value, $date); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $date; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns date of article added. |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
public function added() |
259
|
|
|
{ |
260
|
|
|
return $this->article['added']; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns date of last article modified. |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function modified() |
269
|
|
|
{ |
270
|
|
|
return $this->article['modified']; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Returns adder ID. |
275
|
|
|
* |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
public function adderID() |
279
|
|
|
{ |
280
|
|
|
return (int) $this->article['adder_ID']; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Returns first name and surname of adder. |
285
|
|
|
* |
286
|
|
|
* @param string $type |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
View Code Duplication |
public function adderFullName($type = '') |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$name = trim($this->article['adder_first_name'].' '.$this->article['adder_surname']); |
293
|
|
|
if ('raw' === $type) { |
294
|
|
|
return $name; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return Text::escape($name); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Returns modifier ID. |
302
|
|
|
* |
303
|
|
|
* @return int |
304
|
|
|
*/ |
305
|
|
|
public function modifierID() |
306
|
|
|
{ |
307
|
|
|
return (int) $this->article['modifier_ID']; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Returns modifier full name. |
312
|
|
|
* |
313
|
|
|
* @return int |
314
|
|
|
*/ |
315
|
|
View Code Duplication |
public function modifierFullName($type = '') |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
$name = $this->article['modifier_first_name'].' '.$this->article['modifier_surname']; |
318
|
|
|
if ('raw' === $type) { |
319
|
|
|
return $name; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return Text::escape($name); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Checks whether the article has modified. |
327
|
|
|
* |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
|
|
public function isModified() |
331
|
|
|
{ |
332
|
|
|
return (bool) $this->article['modified']; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Returns the number of views. |
337
|
|
|
* |
338
|
|
|
* @return int |
339
|
|
|
*/ |
340
|
|
|
public function views() |
341
|
|
|
{ |
342
|
|
|
return (int) $this->article['views']; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Returns article slug. |
347
|
|
|
* |
348
|
|
|
* @param string $type |
349
|
|
|
* |
350
|
|
|
* @return string |
351
|
|
|
*/ |
352
|
|
View Code Duplication |
public function slug($type = '') |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$slug = $this->article['slug']; |
355
|
|
|
if ('raw' === $type) { |
356
|
|
|
return $slug; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return Text::escape($slug); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Returns article url. |
364
|
|
|
* |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
public function url() |
368
|
|
|
{ |
369
|
|
|
return sprintf('%1$s/%2$s/%3$s/%4$s/%5$s', |
370
|
|
|
DIR, |
371
|
|
|
'artykuly', |
372
|
|
|
$this->date('Y'), |
373
|
|
|
$this->date('m'), |
374
|
|
|
$this->slug() |
375
|
|
|
); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Returns album path. |
380
|
|
|
* |
381
|
|
|
* @param string $type |
382
|
|
|
* |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
public function album($type = '') |
386
|
|
|
{ |
387
|
|
|
$album = $this->article['album']; |
388
|
|
|
if ('raw' === $type) { |
389
|
|
|
return $album; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return Text::escape($album); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Returns thumb path. |
397
|
|
|
* |
398
|
|
|
* @param string $type |
399
|
|
|
* |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
public function thumb($type = '') |
403
|
|
|
{ |
404
|
|
|
$thumb = $this->article['thumb']; |
405
|
|
|
if ('raw' === $type) { |
406
|
|
|
return $thumb; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return Text::escape($thumb); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Checks whether the article has a thumbnail. |
414
|
|
|
* |
415
|
|
|
* @return bool |
416
|
|
|
*/ |
417
|
|
|
public function hasThumbnail() |
418
|
|
|
{ |
419
|
|
|
return (bool) $this->article['thumb']; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Returns thumbnail code or only address. |
424
|
|
|
* |
425
|
|
|
* @param int $width Image width |
426
|
|
|
* @param int $height Image height |
427
|
|
|
* @param bool $album Add album address if exists |
428
|
|
|
* @param string $alt Set alternative text |
429
|
|
|
* @param string $default Default thumb path. It use when thumb path is empty |
430
|
|
|
* |
431
|
|
|
* @return string |
432
|
|
|
*/ |
433
|
|
View Code Duplication |
public function thumbnail($width = 100, $height = 100, $album = false, $alt = '', $default = '') |
|
|
|
|
434
|
|
|
{ |
435
|
|
|
$thumbUrl = $this->thumb(); |
436
|
|
|
$albumUrl = $this->album(); |
437
|
|
|
$alt = ($alt) ? $alt : $this->title(); |
438
|
|
|
|
439
|
|
|
if (!$this->hasThumbnail()) { |
440
|
|
|
if (!empty($default)) { |
441
|
|
|
$thumbUrl = $default; |
442
|
|
|
} else { |
443
|
|
|
return false; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
$thumbUrl = Image::resize($thumbUrl, $width, $height); |
448
|
|
|
|
449
|
|
|
$html = sprintf('<img src="%1$s" alt="%4$s" width="%2$s" height="%3$s">', |
450
|
|
|
$thumbUrl, $width, $height, $alt |
451
|
|
|
); |
452
|
|
|
|
453
|
|
|
if (true === $album and !empty($albumUrl)) { |
|
|
|
|
454
|
|
|
$html = sprintf('<a href="%1$s">%2$s</a>', $albumUrl, $html); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return $html; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Returns the number of photos. |
462
|
|
|
* |
463
|
|
|
* @return int |
464
|
|
|
*/ |
465
|
|
|
public function photos() |
466
|
|
|
{ |
467
|
|
|
return (int) $this->article['photos']; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Checks whether the article has a photos. |
472
|
|
|
* |
473
|
|
|
* @return bool |
474
|
|
|
*/ |
475
|
|
|
public function hasPhotos() |
476
|
|
|
{ |
477
|
|
|
return (bool) $this->article['photos']; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Check whether the article is hidden on homepage. |
482
|
|
|
* |
483
|
|
|
* @return bool |
484
|
|
|
*/ |
485
|
|
|
public function isHomepageHidden() |
486
|
|
|
{ |
487
|
|
|
return (bool) $this->article['homepage_hidden']; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Chcecks whether the article is published. |
492
|
|
|
* |
493
|
|
|
* @return bool |
494
|
|
|
*/ |
495
|
|
|
public function isPublished() |
496
|
|
|
{ |
497
|
|
|
return (bool) $this->article['published']; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Returns article category anchor. |
502
|
|
|
* |
503
|
|
|
* @return string |
504
|
|
|
*/ |
505
|
|
|
public function category() |
506
|
|
|
{ |
507
|
|
|
return sprintf('<a href="%1$s">%2$s</a>', |
508
|
|
|
$this->categoryUrl(), |
509
|
|
|
$this->categoryTitle() |
510
|
|
|
); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Returns category title. |
515
|
|
|
* |
516
|
|
|
* @param string $type |
517
|
|
|
* |
518
|
|
|
* @return string |
519
|
|
|
*/ |
520
|
|
|
public function categoryTitle($type = '') |
521
|
|
|
{ |
522
|
|
|
$title = $this->article['category_title']; |
523
|
|
|
|
524
|
|
|
if ('raw' === $type) { |
525
|
|
|
return $title; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
return Text::escape($title); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Returns category url. |
533
|
|
|
* |
534
|
|
|
* @return string |
535
|
|
|
*/ |
536
|
|
|
public function categoryUrl() |
537
|
|
|
{ |
538
|
|
|
return sprintf('%1$s/%2$s/%3$s', |
539
|
|
|
DIR, |
540
|
|
|
'artykuly/kategorie', |
541
|
|
|
Text::escape($this->article['category_url']) |
542
|
|
|
); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Checks whether the article has a category. |
547
|
|
|
* |
548
|
|
|
* @return bool |
549
|
|
|
*/ |
550
|
|
|
public function hasCategory() |
551
|
|
|
{ |
552
|
|
|
return (bool) $this->article['category_url']; |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.