|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Widgets\Front\Newcomment; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
|
6
|
|
|
use Extend\Core\Arch\FrontWidget as AbstractWidget; |
|
7
|
|
|
use Ffcms\Core\Traits\ClassTools; |
|
8
|
|
|
use Apps\ActiveRecord\CommentPost; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Newcomment. New comments widget. Show new comments in system. |
|
12
|
|
|
* @package Widgets\Front\Newcomment |
|
13
|
|
|
*/ |
|
14
|
|
|
class Newcomment extends AbstractWidget |
|
15
|
|
|
{ |
|
16
|
|
|
use ClassTools; |
|
17
|
|
|
|
|
18
|
|
|
public $snippet; |
|
19
|
|
|
public $count; |
|
20
|
|
|
public $cache; |
|
21
|
|
|
public $lang; |
|
22
|
|
|
|
|
23
|
|
|
private $_cacheName; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set default configs if not passed |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
* @see \Ffcms\Core\Arch\Widget::init() |
|
29
|
|
|
*/ |
|
30
|
|
|
public function init() |
|
31
|
|
|
{ |
|
32
|
|
|
$cfg = $this->getConfigs(); |
|
33
|
|
|
if ($this->snippet === null) { |
|
34
|
|
|
$this->snippet = $cfg['snippet']; |
|
35
|
|
|
} |
|
36
|
|
|
if ($this->count === null) { |
|
37
|
|
|
$this->count = $cfg['count']; |
|
38
|
|
|
} |
|
39
|
|
|
if ($this->cache === null) { |
|
40
|
|
|
$this->cache = $cfg['cache']; |
|
41
|
|
|
} |
|
42
|
|
|
if ($this->lang === null) { |
|
43
|
|
|
$this->lang = App::$Request->getLanguage(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->_cacheName = 'widget.newcomment.' . $this->createStringClassSnapshotHash(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Show latest comments |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
* @see \Ffcms\Core\Arch\Widget::display() |
|
53
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
|
54
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function display() |
|
57
|
|
|
{ |
|
58
|
|
|
// work with cache and make query |
|
59
|
|
|
$records = null; |
|
|
|
|
|
|
60
|
|
|
if ((int)$this->cache > 0) { |
|
61
|
|
|
if (App::$Cache->get($this->_cacheName) !== null) { |
|
62
|
|
|
$records = App::$Cache->get($this->_cacheName); |
|
63
|
|
|
} else { |
|
64
|
|
|
$records = $this->makeQuery(); |
|
65
|
|
|
App::$Cache->set($this->_cacheName, $records, $this->cache); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
$records = $this->makeQuery(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// check if records is found |
|
72
|
|
|
if ($records === null) { |
|
73
|
|
|
return __('Comments not yet found'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// render view |
|
77
|
|
|
return App::$View->render('widgets/newcomment/default', [ |
|
78
|
|
|
'records' => $records, |
|
79
|
|
|
'snippet' => $this->snippet |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Make database query and return results |
|
85
|
|
|
* @return object |
|
86
|
|
|
*/ |
|
87
|
|
|
private function makeQuery() |
|
88
|
|
|
{ |
|
89
|
|
|
$records = CommentPost::where('lang', $this->lang) |
|
90
|
|
|
->where('moderate', 0); |
|
91
|
|
|
|
|
92
|
|
|
if ($records === null || $records->count() < 1) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $records->orderBy('id', 'DESC') |
|
97
|
|
|
->take($this->count) |
|
98
|
|
|
->get(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.