|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
|
6
|
|
|
use Ffcms\Core\Arch\Model; |
|
7
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
|
8
|
|
|
use Ffcms\Core\Helper\Serialize; |
|
9
|
|
|
use Ffcms\Core\Helper\Text; |
|
10
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
11
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
12
|
|
|
use Apps\ActiveRecord\Content as ContentEntity; |
|
13
|
|
|
|
|
14
|
|
|
class EntityContentSearch extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
const MAX_ITEMS = 5; |
|
17
|
|
|
const MIN_QUERY_LENGTH = 2; |
|
18
|
|
|
|
|
19
|
|
|
public $items; |
|
20
|
|
|
|
|
21
|
|
|
private $_terms; |
|
22
|
|
|
private $_skip = [0]; |
|
23
|
|
|
private $_records; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* EntityContentSearch constructor. Pass search terms (query string) to model and used items to skip it by id. |
|
27
|
|
|
* @param $terms |
|
28
|
|
|
* @param int|array $skipIds |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($terms, $skipIds = 0) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->_terms = App::$Security->strip_tags(trim($terms, ' ')); |
|
33
|
|
|
if (Obj::isLikeInt($skipIds)) { |
|
34
|
|
|
$this->_skip = [$skipIds]; |
|
35
|
|
|
} elseif (Obj::isArray($skipIds)) { |
|
36
|
|
|
$this->_skip = $skipIds; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Prepare conditions to build content list |
|
43
|
|
|
* @throws NotFoundException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function before() |
|
46
|
|
|
{ |
|
47
|
|
|
// check length of passed terms |
|
48
|
|
|
if (!Obj::isString($this->_terms) || Str::length($this->_terms) < self::MIN_QUERY_LENGTH) { |
|
|
|
|
|
|
49
|
|
|
throw new NotFoundException(__('Search terms is too short')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// lets make active record building |
|
53
|
|
|
$this->_records = ContentEntity::whereNotIn('id', $this->_skip) |
|
54
|
|
|
->search($this->_terms) |
|
55
|
|
|
->take(self::MAX_ITEMS) |
|
56
|
|
|
->get(); |
|
57
|
|
|
$this->buildContent(); |
|
58
|
|
|
parent::before(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Build content items as array |
|
63
|
|
|
*/ |
|
64
|
|
|
private function buildContent() |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->_records->count() < 1) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($this->_records as $item) { |
|
71
|
|
|
/** @var \Apps\ActiveRecord\Content $item */ |
|
72
|
|
|
// full text |
|
73
|
|
|
$text = Serialize::getDecodeLocale($item->text); |
|
74
|
|
|
// remove html |
|
75
|
|
|
$text = App::$Security->strip_tags($text); |
|
|
|
|
|
|
76
|
|
|
// build items |
|
77
|
|
|
$this->items[] = [ |
|
78
|
|
|
'title' => $item->getLocaled('title'), |
|
79
|
|
|
'snippet' => Text::snippet($text), |
|
80
|
|
|
'uri' => '/content/read/' . $item->getPath(), |
|
81
|
|
|
'thumb' => $item->getPosterThumbUri() |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..