ComponentSearchTrait   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 54
c 4
b 0
f 0
dl 0
loc 102
rs 10
wmc 23

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isSearchable() 0 6 1
D search() 0 75 22
1
<?php
2
3
namespace Larrock\Core\ComponentTraits;
4
5
use Larrock\Core\Helpers\FormBuilder\FormTags;
6
7
trait ComponentSearchTrait
8
{
9
    /** @var null|bool Осуществляется ли поиск по компоненту */
10
    public $searchable;
11
12
    /** @var string Какое поле модели показывать для поиска */
13
    public $search_title;
14
15
    /**
16
     * Разрешить поиск по материалам компонента.
17
     *
18
     * @param string $search_title Какое поле модели показывать для поиска
19
     * @return $this
20
     */
21
    public function isSearchable($title = 'title')
22
    {
23
        $this->searchable = true;
24
        $this->search_title = $title;
25
26
        return $this;
27
    }
28
29
    /**
30
     * Данные для поиска по материалам компонента.
31
     * @param null|bool $admin Если TRUE - для поиска будут доступны вообще все элементы (не только опубликованные)
32
     * @return null
33
     */
34
    public function search($admin = null)
35
    {
36
        if ($this->searchable !== true) {
37
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type null.
Loading history...
38
        }
39
40
        return \Cache::rememberForever('search'.$this->name.$admin, function () use ($admin) {
41
            $data = [];
42
43
            if ($this->name !== 'feed' && $this->name !== 'catalog') {
44
                return [];
45
            }
46
47
            $model = new $this->model;
48
49
            $search_rows = ['id', $this->search_title];
50
51
            if (isset($this->rows['url'])) {
52
                $search_rows[] = 'url';
53
            }
54
55
            if (isset($this->rows['category']) && ! $this->rows['category'] instanceof FormTags) {
56
                $search_rows[] = 'category';
57
            }
58
59
            if ($admin) {
60
                if (isset($this->rows['category'])) {
61
                    $model = $model::with(['getCategory']);
62
                }
63
            } else {
64
                if (isset($this->rows['category'])) {
65
                    $model = $model::with(['getCategoryActive']);
66
                }
67
            }
68
69
            $items = $model->get($search_rows);
70
71
            foreach ($items as $item) {
72
                if (empty($item->{$this->search_title})) {
73
                    unset($data[$item->id]);
74
                } else {
75
                    $data[$item->id]['id'] = $item->id;
76
                    $data[$item->id]['title'] = $item->{$this->search_title};
77
                    $data[$item->id]['full_url'] = $item->full_url;
78
                    $data[$item->id]['component'] = $this->name;
79
                    $data[$item->id]['category'] = null;
80
                    $data[$item->id]['admin_url'] = $item->admin_url;
81
                    if ($admin) {
82
                        if ($item->getCategory) {
83
                            if (!isset($item->getCategory->id) && \count($item->getCategory) > 0) {
84
                                $data[$item->id]['category'] = $item->getCategory->first()->title;
85
                            } elseif (isset($item->getCategory->title)) {
86
                                $data[$item->id]['category'] = $item->getCategory->title;
87
                            } else {
88
                                unset($data[$item->id]);
89
                            }
90
                        }
91
                    } else {
92
                        if ($item->getCategoryActive) {
93
                            if (!isset($item->getCategoryActive->id) && \count($item->getCategoryActive) > 0) {
94
                                $data[$item->id]['category'] = $item->getCategoryActive->first()->title;
95
                            } elseif (isset($item->getCategoryActive->title)) {
96
                                $data[$item->id]['category'] = $item->getCategoryActive->title;
97
                            } else {
98
                                unset($data[$item->id]);
99
                            }
100
                        }
101
                    }
102
                }
103
            }
104
            if (\count($data) === 0) {
105
                return null;
106
            }
107
108
            return $data;
109
        });
110
    }
111
}
112