Completed
Push — master ( c1777d...d193e2 )
by Michael
13:22
created

search.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?PHP
2
/**
3
 *
4
 * Module: Lexikon -  glossary module
5
 * Version: v 1.00
6
 * Release Date: 18 Dec 2011
7
 * Author: hsalazar
8
 * Licence: GNU
9
 */
10
#$xoopsOption['pagetype'] = "search";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
11
12
include __DIR__ . '/header.php';
13
$GLOBALS['xoopsOption']['template_main'] = 'lx_search.tpl';
14
include XOOPS_ROOT_PATH . '/header.php';
15
16
global $xoTheme, $xoopsDB, $xoopsModule, $xoopsModuleConfig, $searchtype;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
17
$myts = MyTextSanitizer::getInstance();
18
// -- options
19
include_once XOOPS_ROOT_PATH . '/modules/lexikon/include/common.inc.php';
20
$highlight = false;
21
$highlight = ($xoopsModuleConfig['config_highlighter'] = 1) ? 1 : 0;
22
//$highlight = LexikonUtility::getModuleOption('config_highlighter');
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
$hightlight_key = '';
24
25
include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
26
27
// Check if search is enabled site-wide
28
$configHandler     = xoops_getHandler('config');
29
$xoopsConfigSearch = $configHandler->getConfigsByCat(XOOPS_CONF_SEARCH);
30
if ($xoopsConfigSearch['enable_search'] != 1) {
31
    header('location: ' . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/index.php');
32
    exit();
33
}
34
35
// permissions
36
$gpermHandler = xoops_getHandler('groupperm');
37
$groups       = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
38
$module_id    = $xoopsModule->getVar('mid');
39
$allowed_cats = $gpermHandler->getItemIds('lexikon_view', $groups, $module_id);
40
$catids       = implode(',', $allowed_cats);
41
42
extract($_GET);
43
extract($_POST, EXTR_OVERWRITE);
44
45
$action     = isset($action) ? trim($action) : 'search';
46
$query      = isset($term) ? trim($term) : '';
47
$start      = isset($start) ? (int)$start : 0;
48
$categoryID = isset($categoryID) ? (int)$categoryID : 0;
49
$type       = isset($type) ? (int)$type : 3;
50
$queries    = array();
51
52
if ($xoopsModuleConfig['multicats'] == 1) {
53
    $xoopsTpl->assign('multicats', 1);
54
    $totalcats = LexikonUtility::countCats();
55
    $xoopsTpl->assign('totalcats', $totalcats);
56
} else {
57
    $xoopsTpl->assign('multicats', 0);
58
}
59
60
// Configure search parameters according to selector
61
$query = stripslashes($query);
62
if ($type == '1') {
63
    $searchtype = "( w.term LIKE '%$query%' )";
64
}
65
if ($type == '2') {
66
    $searchtype = "( definition LIKE '%$query%' )";
67
}
68
if ($type == '3') {
69
    $searchtype = "(( term LIKE '%$query%' OR definition LIKE '%$query%' OR ref LIKE '%$query%' ))";
70
}
71
72 View Code Duplication
if ($xoopsModuleConfig['multicats'] == 1) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    // If the search is in a particular category
74
    if ($categoryID > 0) {
75
        $andcatid = "AND categoryID = '$categoryID' ";
76
    } else {
77
        $andcatid = '';
78
    }
79
} else {
80
    $andcatid = '';
81
}
82
83
// Counter
84
$publishedwords = LexikonUtility::countWords();
85
$xoopsTpl->assign('publishedwords', $publishedwords);
86
87
// If there's no term here (calling directly search page)
88
if (!$query) {
89
    // Display message saying there's no term and explaining how to search
90
    $xoopsTpl->assign('intro', _MD_LEXIKON_NOSEARCHTERM);
91
    // Display search form
92
    $searchform = LexikonUtility::showSearchForm();
93
    $xoopsTpl->assign('searchform', $searchform);
94
} else {
95
    // IF results, count number
96
    $catrestrict = " categoryID IN ($catids) ";
97
    $searchquery = $xoopsDB->query('SELECT COUNT(*) as nrows FROM '
98
                                   . $xoopsDB->prefix('lxentries')
99
                                   . " w WHERE offline='0' AND "
100
                                   . $catrestrict
101
                                   . ' '
102
                                   . $andcatid
103
                                   . " AND $searchtype   ORDER BY term DESC");
104
    list($results) = $xoopsDB->fetchRow($searchquery);
105
106
    if ($results == 0) {
107
        // There's been no correspondences with the searched terms
108
        $xoopsTpl->assign('intro', _MD_LEXIKON_NORESULTS);
109
110
        // Display search form
111
        $searchform = LexikonUtility::showSearchForm();
112
        $xoopsTpl->assign('searchform', $searchform);
113
        // $results > 0 -> there were search results
114
    } else {
115
        // Show paginated list of results
116
        // We'll put the results in an array
117
        $resultset = array();
118
119
        // -- highlighter
120
        if (is_array($resultset)) {
121
            if ($highlight) {
122
                $xoopsTpl->assign('highlight', true);
123
                $hightlight_key = '&amp;keywords=' . urlencode(trim($query));
124
                //$hightlight_key = '&keywords='.urlencode(trim(implode(' ',$query)));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
            } else {
126
                $xoopsTpl->assign('highlight', false);
127
            }
128
        }
129
130
        // How many results will we show in this page?
131 View Code Duplication
        if ($xoopsModuleConfig['multicats'] == 1) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
            // If the search is in a particular category
133
            if ($categoryID > 0) {
134
                $andcatid2 = "AND w.categoryID = '$categoryID' ";
135
            } else {
136
                $andcatid2 = '';
137
            }
138
        } else {
139
            $andcatid2 = '';
140
        }
141
        $catsallow = " w.categoryID IN ($catids) ";
142
        $queryA    = 'SELECT w.entryID, w.categoryID, w.term, w.init, w.definition, w.datesub, w.ref, c.name AS catname FROM '
143
                     . $xoopsDB->prefix('lxentries')
144
                     . ' w LEFT JOIN '
145
                     . $xoopsDB->prefix('lxcategories')
146
                     . " c ON w.categoryID = c.categoryID WHERE w.offline = '0' AND "
147
                     . $catsallow
148
                     . ' '
149
                     . $andcatid2
150
                     . ' AND '
151
                     . $searchtype
152
                     . ' ';
153
        $queryA    .= '  ORDER BY w.term ASC';
154
        $resultA   = $xoopsDB->query($queryA, $xoopsModuleConfig['indexperpage'], $start);
155
156
        while (list($entryID, $categoryID, $term, $init, $definition, $datesub, $ref, $catname) = $xoopsDB->fetchRow($resultA)) {
157
            $eachresult               = array();
158
            $xoopsModule              = XoopsModule::getByDirname('lexikon');
159
            $eachresult['dir']        = $xoopsModule->dirname();
160
            $eachresult['id']         = $entryID;
161
            $eachresult['categoryID'] = $categoryID;
162
            $eachresult['term']       = ucfirst($myts->htmlSpecialChars($term));
163
            $eachresult['date']       = formatTimestamp($datesub, $xoopsModuleConfig['dateformat']);
164
            $eachresult['ref']        = LexikonUtility::getHTMLHighlight($query, $myts->htmlSpecialChars($ref), '<b style="background-color: #FFFF80; ">', '</b>');
165
            $eachresult['catname']    = $myts->htmlSpecialChars($catname);
166
            $tempdef                  = $myts->displayTarea($definition, 1, 1, 1, 1, 1);
167
            $eachresult['definition'] = LexikonUtility::getHTMLHighlight($query, $tempdef, '<b style="background-color: #FFFF80; ">', '</b>');
168
            if ($highlight) {
169
                $eachresult['keywords'] = $hightlight_key;
170
            }
171
            // Functional links
172
            $microlinks               = LexikonUtility::getServiceLinks($eachresult);
173
            $eachresult['microlinks'] = $microlinks;
174
            $resultset['match'][]     = $eachresult;
175
        }
176
177
        // Msg: there's # results
178
        $xoopsTpl->assign('intro', sprintf(_MD_LEXIKON_THEREWERE, $results, $query));
179
180
        $linkstring          = 'term=' . $query . '&start';
181
        $pagenav             = new XoopsPageNav($results, $xoopsModuleConfig['indexperpage'], $start, $linkstring);
182
        $resultset['navbar'] = '<div style="text-align:right;">' . $pagenav->renderNav(6) . '</div>';
183
184
        $xoopsTpl->assign('resultset', $resultset);
185
186
        // Display search form
187
        $searchform = LexikonUtility::showSearchForm();
188
        $xoopsTpl->assign('searchform', $searchform);
189
    }
190
}
191
// Assign variables and close
192
$xoopsTpl->assign('lang_modulename', $xoopsModule->name());
193
$xoopsTpl->assign('lang_moduledirname', $xoopsModule->getVar('dirname'));
194
195
$xoopsTpl->assign('xoops_module_header', '<link rel="stylesheet" type="text/css" href="assets/css/style.css" />');
196
$xoopsTpl->assign('xoops_pagetitle', _MD_LEXIKON_SEARCHENTRY . ' - ' . $myts->htmlSpecialChars($xoopsModule->name()));
197
198
// Meta data
199
$meta_description = _MD_LEXIKON_SEARCHENTRY . ' - ' . $myts->htmlSpecialChars($xoopsModule->name());
200
if (isset($xoTheme) && is_object($xoTheme)) {
201
    $xoTheme->addMeta('meta', 'description', $meta_description);
202
} else {
203
    $xoopsTpl->assign('xoops_meta_description', $meta_description);
204
}
205
206
include XOOPS_ROOT_PATH . '/footer.php';
207