1 | <?php |
||
2 | |||
3 | namespace Widgets\Front\Newcomment; |
||
4 | |||
5 | use Apps\Model\Api\Comments\EntityCommentData; |
||
6 | use Ffcms\Core\App; |
||
7 | use Extend\Core\Arch\FrontWidget as AbstractWidget; |
||
8 | use Ffcms\Core\Traits\ClassTools; |
||
9 | use Apps\ActiveRecord\CommentPost; |
||
10 | |||
11 | /** |
||
12 | * Class Newcomment. New comments widget. Show new comments in system. |
||
13 | * @package Widgets\Front\Newcomment |
||
14 | */ |
||
15 | class Newcomment extends AbstractWidget |
||
16 | { |
||
17 | use ClassTools; |
||
18 | |||
19 | public $snippet; |
||
20 | public $count; |
||
21 | public $cache; |
||
22 | public $lang; |
||
23 | |||
24 | private $_cacheName; |
||
25 | |||
26 | /** |
||
27 | * Set default configs if not passed |
||
28 | * {@inheritDoc} |
||
29 | * @see \Ffcms\Core\Arch\Widget::init() |
||
30 | */ |
||
31 | public function init(): void |
||
32 | { |
||
33 | $cfg = $this->getConfigs(); |
||
34 | if (!$this->snippet) { |
||
35 | $this->snippet = $cfg['snippet']; |
||
36 | } |
||
37 | |||
38 | if (!$this->count) { |
||
39 | $this->count = $cfg['count']; |
||
40 | } |
||
41 | |||
42 | if (!$this->cache) { |
||
43 | $this->cache = (int)$cfg['cache']; |
||
44 | } |
||
45 | |||
46 | if (!$this->lang) { |
||
47 | $this->lang = App::$Request->getLanguage(); |
||
48 | } |
||
49 | |||
50 | |||
51 | $this->_cacheName = 'widget.newcomment.' . $this->createStringClassSnapshotHash(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Show latest comments |
||
56 | * @return string |
||
57 | * @throws \Ffcms\Core\Exception\JsonException |
||
58 | * @throws \Psr\Cache\InvalidArgumentException |
||
59 | */ |
||
60 | public function display(): ?string |
||
61 | { |
||
62 | // work with cache and make query |
||
63 | $records = null; |
||
64 | if ((int)$this->cache > 0) { |
||
65 | // process caching data |
||
66 | $cache = App::$Cache->getItem($this->_cacheName); |
||
67 | if (!$cache->isHit()) { |
||
68 | $cache->set($this->makeQuery()); |
||
69 | $cache->expiresAfter($this->cache); |
||
70 | App::$Cache->save($cache); |
||
71 | } |
||
72 | |||
73 | $records = $cache->get(); |
||
74 | } else { |
||
75 | $records = $this->makeQuery(); |
||
76 | } |
||
77 | |||
78 | // check if records is found |
||
79 | if (!$records) { |
||
80 | return __('Comments not yet found'); |
||
81 | } |
||
82 | |||
83 | $commentEntity = null; |
||
84 | foreach ($records as $record) { |
||
85 | $commentEntity[] = (new EntityCommentData($record, false))->make(); |
||
86 | } |
||
87 | |||
88 | // render view |
||
89 | return App::$View->render('widgets/newcomment/default', [ |
||
90 | 'comments' => $commentEntity, |
||
91 | 'snippet' => $this->snippet |
||
92 | ]); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Make database query and return results |
||
97 | * @return object |
||
98 | */ |
||
99 | private function makeQuery() |
||
100 | { |
||
101 | $records = CommentPost::with(['user', 'user.profile', 'user.role']) |
||
102 | ->where('lang', $this->lang) |
||
103 | ->where('moderate', 0); |
||
104 | |||
105 | if (!$records || $records->count() < 1) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
106 | return null; |
||
107 | } |
||
108 | |||
109 | return $records->orderBy('id', 'DESC') |
||
110 | ->take($this->count) |
||
111 | ->get(); |
||
112 | } |
||
113 | } |
||
114 |