1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Widgets\Front\Contenttag; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Extend\Core\Arch\FrontWidget as AbstractWidget; |
7
|
|
|
use Ffcms\Core\Traits\OopTools; |
8
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
9
|
|
|
use Apps\ActiveRecord\ContentTag as TagRecord; |
10
|
|
|
|
11
|
|
|
class Contenttag extends AbstractWidget |
12
|
|
|
{ |
13
|
|
|
use OopTools; |
14
|
|
|
|
15
|
|
|
public $count; |
16
|
|
|
public $cache; |
17
|
|
|
|
18
|
|
|
public $tpl = 'widgets/contenttag/default'; |
19
|
|
|
|
20
|
|
|
public function init() |
21
|
|
|
{ |
22
|
|
|
$cfg = $this->getConfigs(); |
23
|
|
|
// check cache is defined |
24
|
|
View Code Duplication |
if ($this->cache === null || !Obj::isLikeInt($this->cache)) { |
|
|
|
|
25
|
|
|
$this->cache = (int)$cfg['cache']; |
26
|
|
|
} |
27
|
|
|
// check tag count is defined |
28
|
|
View Code Duplication |
if ($this->count === null || !Obj::isLikeInt($this->count)) { |
|
|
|
|
29
|
|
|
$this->count = (int)$cfg['count']; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function display() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
// get special properties hash |
36
|
|
|
$classHash = $this->createStringClassSnapshotHash(); |
37
|
|
|
|
38
|
|
|
// get records rows from cache or directly from db |
39
|
|
|
$records = null; |
40
|
|
|
if ($this->cache === 0) { |
41
|
|
|
$records = $this->makeQuery(); |
42
|
|
|
} else { |
43
|
|
|
$records === App::$Cache->get('widget.contenttag.' . $classHash); |
44
|
|
|
if ($records === null) { |
45
|
|
|
$records = $this->makeQuery(); |
46
|
|
|
App::$Cache->set('widget.contenttag' . $classHash, $records, $this->cache); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// check if result is not empty |
51
|
|
|
if ($records === null || $records->count() < 1) { |
52
|
|
|
return __('Content tags is not found'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// render view |
56
|
|
|
return App::$View->render($this->tpl, [ |
57
|
|
|
'records' => $records |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function makeQuery() |
62
|
|
|
{ |
63
|
|
|
return TagRecord::select([ |
64
|
|
|
App::$Database->getConnection()->raw('SQL_CALC_FOUND_ROWS tag'), |
65
|
|
|
App::$Database->getConnection()->raw('COUNT(*) AS count') |
66
|
|
|
])->groupBy('tag') |
67
|
|
|
->orderBy('count', 'DESC') |
68
|
|
|
->take($this->count) |
69
|
|
|
->get(); |
70
|
|
|
} |
71
|
|
|
} |
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.