Test Failed
Push — master ( afc6d5...e64a49 )
by Alexey
04:08
created

Materials::siteSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Materials module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Materials extends Module {
12
13
    public function viewsList() {
14
        $return = [
15
            'inherit' => 'Как у родителя',
16
            'default' => 'Стандартная страница',
17
            'main_page' => 'Главная страница',
18
            'materialWithCategorys' => 'Страница со списком категорий',
19
        ];
20
        $conf = App::$primary->view->template->config;
21
22
        if (!empty($conf['files']['modules']['Materials'])) {
23
24
            foreach ($conf['files']['modules']['Materials'] as $file) {
25
                if (!empty($file['type']) && $file['type'] == 'Material') {
26
                    $return[$file['file']] = $file['name'];
27
                }
28
            }
29
        }
30
        return $return;
31
    }
32
33 View Code Duplication
    public function templatesList() {
34
        $return = [
35
            'inherit' => 'Как у родителя',
36
            'current' => 'Текущая тема'
37
        ];
38
39
        $conf = App::$primary->view->template->config;
40
41
        if (!empty($conf['files']['aditionTemplateFiels'])) {
42
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
43
                $return[$file['file']] = '- ' . $file['name'];
44
            }
45
        }
46
        return $return;
47
    }
48
49
    public function viewsCategoryList() {
50
        $return = [
51
            'inherit' => 'Как у родителя',
52
            'category' => 'Стандартная категория',
53
        ];
54
        $conf = App::$primary->view->template->config;
55
56
        if (!empty($conf['files']['modules']['Materials'])) {
57
58
            foreach ($conf['files']['modules']['Materials'] as $file) {
59
                if ($file['type'] == 'Category') {
60
                    $return[$file['file']] = $file['name'];
61
                }
62
            }
63
        }
64
        return $return;
65
    }
66
67 View Code Duplication
    public function templatesCategoryList() {
68
        $return = [
69
            'inherit' => 'Как у родителя',
70
            'current' => 'Текущая тема'
71
        ];
72
73
        $conf = App::$primary->view->template->config;
74
75
        if (!empty($conf['files']['aditionTemplateFiels'])) {
76
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
77
                $return[$file['file']] = '- ' . $file['name'];
78
            }
79
        }
80
        return $return;
81
    }
82
83
    public function sitemap() {
84
        $map = [];
85
        $zeroMaterials = \Materials\Material::getList(['where' => ['category_id', 0]]);
86
        foreach ($zeroMaterials as $mat) {
87
            $map[] = [
88
                'name' => $mat->name,
89
                'url' => [
90
                    'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($mat->getHref())
91
                ],
92
            ];
93
        }
94
95
        $categorys = \Materials\Category::getList(['where' => ['parent_id', 0]]);
96
        $scan = function ($category, $scan) {
97
            $map = [];
98
99
            foreach ($category->items as $mat) {
100
                $map[] = [
101
                    'name' => $mat->name,
102
                    'url' => [
103
                        'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($mat->getHref())
104
                    ],
105
                ];
106
            }
107
            foreach ($category->childs as $child) {
108
                $map = array_merge($map, $scan($child, $scan));
109
            }
110
            return $map;
111
        };
112
        foreach ($categorys as $category) {
113
            $map = array_merge($map, $scan($category, $scan));
114
        }
115
        return $map;
116
    }
117
118
    function search($search) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
119
        $query = 'select material_id from ' . App::$cur->db->table_prefix . \Materials\Material::table() . ' where MATCH (material_name,material_text,material_keywords,material_description) AGAINST (\'' . $search . '\')';
120
        $ids = array_keys(App::$cur->db->query($query)->getArray('material_id'));
121
        $count = count($ids);
122
        $pages = new \Ui\Pages($_GET, ['count' => $count]);
123
        //items
124
        //items
125
        $items = \Materials\Material::getList([
126
            'where' => ['id', $ids, 'IN'],
127
            'start' => $pages->params['start'],
128
            'limit' => $pages->params['limit']
129
        ]);
130
        return ['count' => $count, 'items' => $items, 'pages' => $pages];
131
    }
132
133
    public function siteSearch($search) {
134
        $result = $this->search($search);
135
        $searchResult = [];
136
        foreach ($result['items'] as $item) {
137
            $details = '<div>';
138
            $shortdes = mb_substr(strip_tags($item->text), 0, 300);
139
            $shortdes = mb_substr($shortdes, 0, mb_strrpos($shortdes, ' '));
140
            $details .= $shortdes;
141
            if (mb_strlen($item->description) > $shortdes) {
142
                $details .= '...';
143
            }
144
            $details .= '</div>';
145
            $searchResult[] = [
146
                'title' => $item->name(),
147
                'details' => $details,
148
                'href' => '/materials/view/' . $item->id
149
            ];
150
        }
151
        return ['name' => 'Материалы', 'count' => $result['count'], 'result' => $searchResult, 'detailSearch' => ' / materials / detailSearch ? search = ' . $search];
152
    }
153
}
154