|
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\Helper\Type\Any; |
|
8
|
|
|
use Ffcms\Core\Traits\ClassTools; |
|
9
|
|
|
use Apps\ActiveRecord\ContentTag as TagRecord; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Contenttag. Display most popular tags in block |
|
13
|
|
|
* @package Widgets\Front\Contenttag |
|
14
|
|
|
*/ |
|
15
|
|
|
class Contenttag extends AbstractWidget |
|
16
|
|
|
{ |
|
17
|
|
|
use ClassTools; |
|
18
|
|
|
|
|
19
|
|
|
public $count; |
|
20
|
|
|
public $cache; |
|
21
|
|
|
|
|
22
|
|
|
public $tpl = 'widgets/contenttag/default'; |
|
23
|
|
|
|
|
24
|
|
|
private $_lang; |
|
25
|
|
|
private $_cacheName; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set default configurations if not defined |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
* @see \Ffcms\Core\Arch\Widget::init() |
|
31
|
|
|
*/ |
|
32
|
|
|
public function init(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$cfg = $this->getConfigs(); |
|
35
|
|
|
// check cache is defined |
|
36
|
|
|
if (!$this->cache|| !Any::isInt($this->cache)) { |
|
37
|
|
|
$this->cache = (int)$cfg['cache']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// check tag count is defined |
|
41
|
|
|
if (!$this->count || !Any::isInt($this->count)) { |
|
42
|
|
|
$this->count = (int)$cfg['count']; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->_lang = App::$Request->getLanguage(); |
|
46
|
|
|
$this->_cacheName = 'widget.contenttag.' . $this->createStringClassSnapshotHash(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Display widget info |
|
51
|
|
|
* @return string|null |
|
52
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function display(): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
// get records rows from cache or directly from db |
|
57
|
|
|
$records = null; |
|
58
|
|
|
if ($this->cache === 0) { |
|
59
|
|
|
$records = $this->makeQuery(); |
|
60
|
|
|
} else { |
|
61
|
|
|
$cache = App::$Cache->getItem($this->_cacheName); |
|
62
|
|
|
if (!$cache->isHit()) { |
|
63
|
|
|
$cache->set($this->makeQuery())->expiresAfter((int)$this->cache); |
|
64
|
|
|
App::$Cache->save($cache); |
|
65
|
|
|
} |
|
66
|
|
|
$records = $cache->get(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// check if result is not empty |
|
70
|
|
|
if (!$records || $records->count() < 1) { |
|
71
|
|
|
return __('Content tags is not found'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// render view |
|
75
|
|
|
return App::$View->render($this->tpl, [ |
|
76
|
|
|
'records' => $records |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Make query to database via active record |
|
82
|
|
|
* @return object |
|
83
|
|
|
*/ |
|
84
|
|
|
private function makeQuery() |
|
85
|
|
|
{ |
|
86
|
|
|
return TagRecord::select([ |
|
87
|
|
|
'tag', |
|
88
|
|
|
App::$Database->getConnection()->raw('COUNT(tag) AS count') |
|
89
|
|
|
])->where('lang', $this->_lang) |
|
90
|
|
|
->groupBy('tag') |
|
91
|
|
|
->orderBy('count', 'DESC') |
|
92
|
|
|
->take($this->count) |
|
93
|
|
|
->get(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|