1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Blog\Widget; |
6
|
|
|
|
7
|
|
|
use App\Blog\Entity\Post; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
10
|
|
|
use Yiisoft\Yii\Bootstrap5\Widget; |
11
|
|
|
|
12
|
|
|
class PostCard extends Widget |
13
|
|
|
{ |
14
|
|
|
private ?Post $post = null; |
15
|
|
|
|
16
|
|
|
private array $options = []; |
17
|
|
|
|
18
|
|
|
private UrlGeneratorInterface $urlGenerator; |
19
|
|
|
|
20
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
21
|
|
|
{ |
22
|
|
|
$this->urlGenerator = $urlGenerator; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function run(): string |
26
|
|
|
{ |
27
|
|
|
if (!isset($this->options['id'])) { |
28
|
|
|
$this->options['id'] = "{$this->getId()}-post-card"; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->initOptions(); |
32
|
|
|
|
33
|
|
|
return implode("\n", [ |
34
|
|
|
Html::openTag('div', $this->options), |
35
|
|
|
Html::openTag('div', ['class' => 'card-body d-flex flex-column align-items-start']), |
36
|
|
|
$this->renderHead(), |
37
|
|
|
$this->renderBody(), |
38
|
|
|
$this->renderTags(), |
39
|
|
|
Html::closeTag('div'), |
40
|
|
|
Html::closeTag('div'), |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function renderHead(): string |
45
|
|
|
{ |
46
|
|
|
return Html::a( |
47
|
|
|
Html::encode($this->post->getTitle()), |
48
|
|
|
$this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]), |
49
|
|
|
['class' => 'mb-0 h4 text-decoration-none'] // stretched-link |
50
|
|
|
) |
51
|
|
|
->render(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function renderBody(): string |
55
|
|
|
{ |
56
|
|
|
$return = Html::openTag('div', ['class' => card-text mb-auto']); |
|
|
|
|
57
|
|
|
$return .= $this->post->getPublishedAt()->format('M, d'); |
58
|
|
|
$return .= ' by '; |
59
|
|
|
$return .= Html::a( |
60
|
|
|
$this->post->getUser()->getLogin(), |
61
|
|
|
$this->urlGenerator->generate('user/profile', ['login' => $this->post->getUser()->getLogin()]) |
62
|
|
|
)->class('mb-1 text-muted'); |
63
|
|
|
|
64
|
|
|
$return .= Html::p( |
65
|
|
|
mb_substr($this->post->getContent(), 0, 400) |
66
|
|
|
. (mb_strlen($this->post->getContent()) > 400 ? '…' : '') |
67
|
|
|
); |
68
|
|
|
return $return . Html::closeTag('div'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function renderTags(): string |
72
|
|
|
{ |
73
|
|
|
$return = Html::openTag('div', ['class' => 'mt-3']); |
74
|
|
|
foreach ($this->post->getTags() as $tag) { |
75
|
|
|
$return .= Html::a( |
76
|
|
|
Html::encode($tag->getLabel()), |
77
|
|
|
$this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]), |
78
|
|
|
['class' => 'btn btn-outline-secondary btn-sm me-2 mt-1'] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
return $return . Html::closeTag('div'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function initOptions(): void |
85
|
|
|
{ |
86
|
|
|
Html::addCssClass($this->options, ['widget' => 'card mb-4']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function post(?Post $post): self |
90
|
|
|
{ |
91
|
|
|
$this->post = $post; |
92
|
|
|
|
93
|
|
|
if ($post !== null) { |
94
|
|
|
$this->options['data']['post-slug'] = $post->getSlug(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
102
|
|
|
* |
103
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
104
|
|
|
*/ |
105
|
|
|
public function options(array $value): self |
106
|
|
|
{ |
107
|
|
|
$this->options = $value; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|