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\Serialize; |
7
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
8
|
|
|
use Ffcms\Core\Traits\OopTools; |
9
|
|
|
use Apps\ActiveRecord\Content; |
10
|
|
|
|
11
|
|
|
class Newcontent extends Widget |
12
|
|
|
{ |
13
|
|
|
use OopTools; |
14
|
|
|
|
15
|
|
|
public $categories; |
16
|
|
|
public $cache; |
17
|
|
|
public $count; |
18
|
|
|
|
19
|
|
|
public $tpl = 'widgets/newcontent/default'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepare widget. Set default configs if not defined on initialization |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
* @see \Ffcms\Core\Arch\Widget::init() |
25
|
|
|
*/ |
26
|
|
|
public function init() |
27
|
|
|
{ |
28
|
|
|
$cfg = $this->getConfigs(); |
29
|
|
|
// check if categories is empty |
30
|
|
|
if ($this->categories === null) { |
31
|
|
|
$this->categories = Serialize::decode($cfg['categories']); |
32
|
|
|
} |
33
|
|
|
// check cache is defined |
34
|
|
View Code Duplication |
if ($this->cache === null || !Obj::isLikeInt($this->cache)) { |
|
|
|
|
35
|
|
|
$this->cache = (int)$cfg['cache']; |
36
|
|
|
} |
37
|
|
|
// check item count is defined |
38
|
|
View Code Duplication |
if ($this->count === null || !Obj::isLikeInt($this->count)) { |
|
|
|
|
39
|
|
|
$this->count = (int)$cfg['count']; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Display new content widget logic |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
* @see \Ffcms\Core\Arch\Widget::display() |
47
|
|
|
*/ |
48
|
|
|
public function display() |
49
|
|
|
{ |
50
|
|
|
// get unique instance hash with current params |
51
|
|
|
$cacheHash = $this->createStringClassSnapshotHash(); |
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
|
|
|
$query = App::$Cache->get('widget.newcontent.' . $cacheHash); |
60
|
|
|
if ($query === null) { |
61
|
|
|
// if query is not cached make it |
62
|
|
|
$query = $this->makeQuery(); |
63
|
|
|
// and save result to cache |
64
|
|
|
App::$Cache->set('widget.newcontent.' . $cacheHash, $query, $this->cache); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// check if response is not empty |
69
|
|
|
if ($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
|
|
|
->join('content_categories', 'content_categories.id', '=', 'contents.category_id', 'left outer') |
88
|
|
|
->orderBy('contents.created_at', 'DESC') |
89
|
|
|
->take($this->count)->get(); |
90
|
|
|
} |
91
|
|
|
} |
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.