Passed
Pull Request — master (#19)
by Michael
08:29 queued 03:40
created

search.php (1 issue)

Severity
1
<?php
2
/**
3
 * Module: Lexikon -  glossary module
4
 * Author: hsalazar
5
 * Licence: GNU
6
 */
7
8
#$xoopsOption['pagetype'] = "search";
9
10
use Xmf\Request;
11
use XoopsModules\Lexikon\{
12
    Helper,
13
    Utility
14
};
15
/** @var Helper $helper */
16
17
$GLOBALS['xoopsOption']['template_main'] = 'lx_search.tpl';
18
require __DIR__ . '/header.php';
19
require XOOPS_ROOT_PATH . '/header.php';
20
21
22
$helper = Helper::getInstance();
23
24
global $xoTheme, $xoopsDB, $xoopsModule, $xoopsModuleConfig, $searchtype;
25
$myts = \MyTextSanitizer::getInstance();
26
// -- options
27
require_once XOOPS_ROOT_PATH . '/modules/lexikon/include/common.inc.php';
28
$highlight      = false;
29
$highlight      = (1 == $helper->getConfig('config_highlighter')) ? 1 : 0;
30
$hightlight_key = '';
31
32
require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
33
34
// Check if search is enabled site-wide
35
/** @var \XoopsConfigHandler $configHandler */
36
$configHandler     = xoops_getHandler('config');
37
$xoopsConfigSearch = $configHandler->getConfigsByCat(XOOPS_CONF_SEARCH);
38
if (1 != $xoopsConfigSearch['enable_search']) {
39
    header('location: ' . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/index.php');
40
    exit();
41
}
42
43
// permissions
44
/** @var \XoopsGroupPermHandler $grouppermHandler */
45
$grouppermHandler = xoops_getHandler('groupperm');
46
$groups           = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
47
$module_id        = $xoopsModule->getVar('mid');
48
$allowed_cats     = $grouppermHandler->getItemIds('lexikon_view', $groups, $module_id);
49
$catids           = implode(',', $allowed_cats);
50
51
//extract($_GET);
52
//extract($_POST, EXTR_OVERWRITE);
53
54
$action     = Request::getCmd('action', 'search', 'GET'); //isset($action) ? trim($action) : 'search';
55
$query      = Request::getString('term', '', 'GET'); //isset($term) ? trim($term) : '';
56
$start      = Request::getInt('start', 0, 'GET'); //isset($start) ? (int)$start : 0;
57
$categoryID = Request::getInt('categoryID', 0, 'GET'); //isset($categoryID) ? (int)$categoryID : 0;
58
$type       = Request::getInt('type', 3, 'GET'); //isset($type) ? (int)$type : 3;
59
$queries    = [];
60
61
if (1 == $helper->getConfig('multicats')) {
62
    $xoopsTpl->assign('multicats', 1);
63
    $totalcats = $utility::countCats();
64
    $xoopsTpl->assign('totalcats', $totalcats);
65
} else {
66
    $xoopsTpl->assign('multicats', 0);
67
}
68
69
// Configure search parameters according to selector
70
$query = stripslashes($query);
71
if ('1' == $type) {
72
    $searchtype = "( w.term LIKE '%$query%' )";
73
}
74
if ('2' == $type) {
75
    $searchtype = "( definition LIKE '%$query%' )";
76
}
77
if ('3' == $type) {
78
    $searchtype = "(( term LIKE '%$query%' OR definition LIKE '%$query%' OR ref LIKE '%$query%' ))";
79
}
80
81
if (1 == $helper->getConfig('multicats')) {
82
    // If the search is in a particular category
83
    if ($categoryID > 0) {
84
        $andcatid = "AND categoryID = '$categoryID' ";
85
    } else {
86
        $andcatid = '';
87
    }
88
} else {
89
    $andcatid = '';
90
}
91
92
// Counter
93
$publishedwords = $utility::countWords();
94
$xoopsTpl->assign('publishedwords', $publishedwords);
95
96
// If there's no term here (calling directly search page)
97
if (!$query) {
98
    // Display message saying there's no term and explaining how to search
99
    $xoopsTpl->assign('intro', _MD_LEXIKON_NOSEARCHTERM);
100
    // Display search form
101
    $searchform = $utility::showSearchForm();
102
    $xoopsTpl->assign('searchform', $searchform);
103
} else {
104
    // IF results, count number
105
    $catrestrict = " categoryID IN ($catids) ";
106
    $searchquery = $xoopsDB->query('SELECT COUNT(*) as nrows FROM ' . $xoopsDB->prefix('lxentries') . " w WHERE offline='0' AND " . $catrestrict . ' ' . $andcatid . " AND $searchtype   ORDER BY term DESC");
107
    [$results] = $xoopsDB->fetchRow($searchquery);
108
109
    if (0 == $results) {
110
        // There's been no correspondences with the searched terms
111
        $xoopsTpl->assign('intro', _MD_LEXIKON_NORESULTS);
112
113
        // Display search form
114
        $searchform = $utility::showSearchForm();
115
        $xoopsTpl->assign('searchform', $searchform);
116
        // $results > 0 -> there were search results
117
    } else {
118
        // Show paginated list of results
119
        // We'll put the results in an array
120
        $resultset = [];
121
122
        // -- highlighter
123
        if (is_array($resultset)) {
0 ignored issues
show
The condition is_array($resultset) is always true.
Loading history...
124
            if ($highlight) {
125
                $xoopsTpl->assign('highlight', true);
126
                $hightlight_key = '&amp;keywords=' . urlencode(trim($query));
127
            } else {
128
                $xoopsTpl->assign('highlight', false);
129
            }
130
        }
131
132
        // How many results will we show in this page?
133
        if (1 == $helper->getConfig('multicats')) {
134
            // If the search is in a particular category
135
            if ($categoryID > 0) {
136
                $andcatid2 = "AND w.categoryID = '$categoryID' ";
137
            } else {
138
                $andcatid2 = '';
139
            }
140
        } else {
141
            $andcatid2 = '';
142
        }
143
        $catsallow = " w.categoryID IN ($catids) ";
144
        $queryA    = 'SELECT w.entryID, w.categoryID, w.term, w.init, w.definition, w.datesub, w.ref, c.name AS catname FROM '
145
                     . $xoopsDB->prefix('lxentries')
146
                     . ' w LEFT JOIN '
147
                     . $xoopsDB->prefix('lxcategories')
148
                     . " c ON w.categoryID = c.categoryID WHERE w.offline = '0' AND "
149
                     . $catsallow
150
                     . ' '
151
                     . $andcatid2
152
                     . ' AND '
153
                     . $searchtype
154
                     . ' ';
155
        $queryA    .= '  ORDER BY w.term ASC';
156
        $resultA   = $xoopsDB->query($queryA, $helper->getConfig('indexperpage'), $start);
157
158
        while (list($entryID, $categoryID, $term, $init, $definition, $datesub, $ref, $catname) = $xoopsDB->fetchRow($resultA)) {
159
            $eachresult               = [];
160
            $xoopsModule              = XoopsModule::getByDirname('lexikon');
161
            $eachresult['dir']        = $xoopsModule->dirname();
162
            $eachresult['id']         = $entryID;
163
            $eachresult['categoryID'] = $categoryID;
164
            $eachresult['term']       = ucfirst(htmlspecialchars($term, ENT_QUOTES | ENT_HTML5));
165
            $eachresult['date']       = formatTimestamp($datesub, $helper->getConfig('dateformat'));
166
            $eachresult['ref']        = $utility::getHTMLHighlight($query, htmlspecialchars($ref, ENT_QUOTES | ENT_HTML5), '<b style="background-color: #FFFF80; ">', '</b>');
167
            $eachresult['catname']    = htmlspecialchars($catname, ENT_QUOTES | ENT_HTML5);
168
            $tempdef                  = $myts->displayTarea($definition, 1, 1, 1, 1, 1);
169
            $eachresult['definition'] = $utility::getHTMLHighlight($query, $tempdef, '<b style="background-color: #FFFF80; ">', '</b>');
170
            if ($highlight) {
171
                $eachresult['keywords'] = $hightlight_key;
172
            }
173
            // Functional links
174
            $microlinks               = $utility::getServiceLinks($eachresult);
175
            $eachresult['microlinks'] = $microlinks;
176
            $resultset['match'][]     = $eachresult;
177
        }
178
179
        // Msg: there's # results
180
        $xoopsTpl->assign('intro', sprintf(_MD_LEXIKON_THEREWERE, $results, $query));
181
182
        $linkstring          = 'term=' . $query . '&start';
183
        $pagenav             = new \XoopsPageNav($results, $helper->getConfig('indexperpage'), $start, $linkstring);
184
        $resultset['navbar'] = '<div style="text-align:right;">' . $pagenav->renderNav(6) . '</div>';
185
186
        $xoopsTpl->assign('resultset', $resultset);
187
188
        // Display search form
189
        $searchform = $utility::showSearchForm();
190
        $xoopsTpl->assign('searchform', $searchform);
191
    }
192
}
193
// Assign variables and close
194
$xoopsTpl->assign('lang_modulename', $xoopsModule->name());
195
$xoopsTpl->assign('lang_moduledirname', $xoopsModule->getVar('dirname'));
196
197
$xoopsTpl->assign('xoops_module_header', '<link rel="stylesheet" type="text/css" href="assets/css/style.css">');
198
$xoopsTpl->assign('xoops_pagetitle', _MD_LEXIKON_SEARCHENTRY . ' - ' . htmlspecialchars($xoopsModule->name(), ENT_QUOTES | ENT_HTML5));
199
200
// Meta data
201
$meta_description = _MD_LEXIKON_SEARCHENTRY . ' - ' . htmlspecialchars($xoopsModule->name(), ENT_QUOTES | ENT_HTML5);
202
if (isset($xoTheme) && is_object($xoTheme)) {
203
    $xoTheme->addMeta('meta', 'description', $meta_description);
204
} else {
205
    $xoopsTpl->assign('xoops_meta_description', $meta_description);
206
}
207
208
require XOOPS_ROOT_PATH . '/footer.php';
209