1
|
|
|
<?php |
2
|
|
|
namespace Widgets\Front\Newcontent; |
3
|
|
|
|
4
|
|
|
use Ffcms\Core\App; |
5
|
|
|
use Extend\Core\Arch\FrontWidget as Widget; |
6
|
|
|
use Ffcms\Core\Helper\Type\Any; |
7
|
|
|
use Ffcms\Core\Traits\ClassTools; |
8
|
|
|
use Apps\ActiveRecord\Content; |
9
|
|
|
|
10
|
|
|
class Newcontent extends Widget |
11
|
|
|
{ |
12
|
|
|
use ClassTools; |
13
|
|
|
|
14
|
|
|
public $categories; |
15
|
|
|
public $cache; |
16
|
|
|
public $count; |
17
|
|
|
|
18
|
|
|
public $tpl = 'widgets/newcontent/default'; |
19
|
|
|
|
20
|
|
|
private $_cacheName; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prepare widget. Set default configs if not defined on initialization |
24
|
|
|
* @see \Ffcms\Core\Arch\Widget::init() |
25
|
|
|
*/ |
26
|
|
|
public function init(): void |
27
|
|
|
{ |
28
|
|
|
$cfg = $this->getConfigs(); |
29
|
|
|
// check if categories is empty |
30
|
|
|
if (!$this->categories) { |
31
|
|
|
$this->categories = $cfg['categories']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// check cache is defined |
35
|
|
|
if (!$this->cache || !Any::isInt($this->cache)) { |
36
|
|
|
$this->cache = (int)$cfg['cache']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// check item count is defined |
40
|
|
|
if (!$this->count|| !Any::isInt($this->count)) { |
41
|
|
|
$this->count = (int)$cfg['count']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->_cacheName = 'widget.newcontent.' . $this->createStringClassSnapshotHash(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Display new content widget logic |
49
|
|
|
* @see \Ffcms\Core\Arch\Widget::display() |
50
|
|
|
*/ |
51
|
|
|
public function display(): ?string |
52
|
|
|
{ |
53
|
|
|
$query = null; |
54
|
|
|
// cache is disabled, get result directly |
55
|
|
|
if ($this->cache === 0) { |
56
|
|
|
$query = $this->makeQuery(); |
57
|
|
|
} else { |
58
|
|
|
// try get query result from cache |
59
|
|
|
$cache = App::$Cache->getItem($this->_cacheName); |
60
|
|
|
if (!$cache->isHit()) { |
61
|
|
|
$cache->set($this->makeQuery())->expiresAfter((int)$this->cache); |
62
|
|
|
App::$Cache->save($cache); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$query = $cache->get(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// check if response is not empty |
69
|
|
|
if (!$query || $query->count() < 1) { |
70
|
|
|
return __('Content is not founded'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// render view |
74
|
|
|
return App::$View->render($this->tpl, [ |
75
|
|
|
'records' => $query |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Make query to database |
81
|
|
|
* @return object |
82
|
|
|
*/ |
83
|
|
|
private function makeQuery() |
84
|
|
|
{ |
85
|
|
|
return Content::select(['contents.*', 'content_categories.path as cpath', 'content_categories.title as ctitle']) |
86
|
|
|
->whereIn('contents.category_id', $this->categories) |
87
|
|
|
->where('contents.display', true) |
88
|
|
|
->join('content_categories', 'content_categories.id', '=', 'contents.category_id', 'left outer') |
89
|
|
|
->orderBy('contents.created_at', 'DESC') |
90
|
|
|
->take($this->count)->get(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|