|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
|
6
|
|
|
use Ffcms\Core\Arch\View; |
|
7
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
10
|
|
|
use Ffcms\Core\Network\Request; |
|
11
|
|
|
use Ffcms\Core\Network\Response; |
|
12
|
|
|
use Apps\ActiveRecord\Content as ContentRecord; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait ActionTag |
|
16
|
|
|
* @package Apps\Controller\Front\Content |
|
17
|
|
|
* @property View $view |
|
18
|
|
|
* @property Request $request |
|
19
|
|
|
* @property Response $response |
|
20
|
|
|
* @method array getConfigs |
|
21
|
|
|
*/ |
|
22
|
|
|
trait ActionTag |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* List latest by created_at content items contains tag name |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @return null|string |
|
28
|
|
|
* @throws NotFoundException |
|
29
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function tag($name) |
|
32
|
|
|
{ |
|
33
|
|
|
// remove spaces and other sh@ts |
|
34
|
|
|
$name = App::$Security->strip_tags(trim($name)); |
|
35
|
|
|
|
|
36
|
|
|
// check if tag is not empty |
|
37
|
|
|
if (!Any::isStr($name) || Str::length($name) < 2) { |
|
|
|
|
|
|
38
|
|
|
throw new NotFoundException(__('Tag is empty or is too short!')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// get equal rows order by creation date |
|
42
|
|
|
$records = ContentRecord::where('meta_keywords', 'like', '%' . $name . '%') |
|
43
|
|
|
->orderBy('created_at', 'DESC') |
|
44
|
|
|
->take(self::TAG_PER_PAGE); |
|
45
|
|
|
// check if result is not empty |
|
46
|
|
|
if ($records->count() < 1) { |
|
47
|
|
|
throw new NotFoundException(__('Nothing founded')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// define tag list event |
|
51
|
|
|
App::$Event->run(static::EVENT_TAG_LIST, [ |
|
52
|
|
|
'records' => $records |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
// render response |
|
56
|
|
|
return $this->view->render('tag', [ |
|
57
|
|
|
'records' => $records->get(), |
|
58
|
|
|
'tag' => $name |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.