mod_search_count()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 4
nop 4
dl 25
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      efqdirectory
16
 * @since
17
 * @author       Martijn Hertog (aka wtravel)
18
 * @author       XOOPS Development Team,
19
 */
20
21
include __DIR__ . '/header.php';
22
$myts = MyTextSanitizer::getInstance(); // MyTextSanitizer object
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
require_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
require_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
25
$myts   = MyTextSanitizer::getInstance();
26
$mytree = new XoopsTree($xoopsDB->prefix('links_cat'), 'cid', 'pid');
0 ignored issues
show
Bug introduced by
The type XoopsTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
$eh     = new ErrorHandler;
0 ignored issues
show
Bug introduced by
The type ErrorHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
$moddir = $xoopsModule->getVar('dirname');
30
include XOOPS_ROOT_PATH . '/header.php';
31
32
if (isset($_GET['catid'])) {
33
    $get_cid = (int)$_GET['cid'];
34
} else {
35
    $get_cid = '1';
36
}
37
if (isset($_GET['dirid'])) {
38
    $get_dirid = (int)$_GET['dirid'];
39
} else {
40
    $get_dirid = '1';
41
}
42 View Code Duplication
if (isset($_GET['orderby'])) {
0 ignored issues
show
Duplication introduced by
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...
43
    $orderby = convertOrderByIn($_GET['orderby']);
44
} else {
45
    $orderby = 'title ASC';
46
}
47
if (isset($_GET['page'])) {
48
    $get_page = (int)$_GET['page'];
49
} else {
50
    $get_page = 1;
51
}
52
$GLOBALS['xoopsOption']['template_main'] = 'efqdiralpha1_search.tpl';
53
$xoopsTpl->assign('xoops_module_header', $xoops_module_header);
54
$lang_adv_search = sprintf(_MD_SEARCH_ADV, $get_dirid);
55
56
ob_start();
57
$searchform = '<form action="search.php" name="search" id="search" method="get">';
58
$searchform .= '<input type="hidden" name="dirid" value="' . $get_dirid . '"><input type="text" name="q" size="40" maxsize="150" value=""><input type="submit" id="submit" value="' . _MD_SEARCH . '">' . $lang_adv_search . '</form>';
59
echo $searchform;
60
if (!empty($_GET['q'])) {
61
    //get search results from query
62
    if (isset($_GET['q'])) {
63
        $querystring = $GLOBALS['xoopsDB']->escape($myts->stripSlashesGPC($_GET['q']));
64
        //echo $querystring."<br>";
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...
65
    } else {
66
        redirect_header(XOOPS_URL . "/modules/$moddir/search.php", 2, _MD_NO_SEARCH_STRING_SELECTED);
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        /** @scrutinizer ignore-call */ redirect_header(XOOPS_URL . "/modules/$moddir/search.php", 2, _MD_NO_SEARCH_STRING_SELECTED);
Loading history...
Bug introduced by
The constant _MD_NO_SEARCH_STRING_SELECTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
    }
68
    $poscount   = substr_count($querystring, '"') / 2;
69
    $specialarr = array();
70 View Code Duplication
    for ($i = 0; $i < $poscount; ++$i) {
0 ignored issues
show
Duplication introduced by
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...
71
        $start = strpos($querystring, '"', 0);
72
        $end   = strpos($querystring, '"', $start + 1);
73
        if ($end !== false) {
74
            $specialstring = ltrim(substr($querystring, $start, $end - $start), '"');
75
            $specialarr[]  = $specialstring;
76
            $querystring   = ltrim(substr_replace($querystring, '', $start, $end - $start + 1));
77
        } else {
78
            $querystring = ltrim(substr_replace($querystring, '', $start, 1));
79
        }
80
    }
81
    $queryarr   = explode(' ', $querystring);
82
    $queryarr   = array_merge($specialarr, $queryarr);
83
    $emptyarr[] = '';
84
    $querydiff  = array_diff($queryarr, $emptyarr);
85
86
    $limit  = $xoopsModuleConfig['searchresults_perpage'];
87
    $offset = ($get_page - 1) * $limit;
88
89
    $andor         = 'AND';
90
    $searchresults = mod_search($querydiff, $andor, $limit, $offset);
91
    $maxpages      = 10;
92
    $maxcount      = 30;
93
94
    $count_results = mod_search_count($querydiff, $andor, $maxcount, 0);
95
    $count_pages   = 0;
96
    //Calculate the number of result pages.
97
    if ($count_results > $limit) {
98
        $count_pages = ceil($count_results / $limit);
99
    }
100
    $pages_text = '';
101
    $pages_text .= $count_results . ' ' . _MD_LISTINGS_FOUND . '<br>';
102
103
    if ($count_pages >= 2) {
104
        $pages_text .= '<a href="search.php?q=' . $querystring . '&page=1">1</a>';
105
    }
106
    for ($i = 1; $i < $count_pages; ++$i) {
107
        $page       = $i + 1;
108
        $pages_text .= ' - <a href="search.php?q=' . $querystring . '&page=' . $page . '">' . $page . '</a>';
109
    }
110
111
    echo '<div class="itemTitleLarge">' . _MD_SEARCHRESULTS_TITLE . '</div><br>';
112
    if ($searchresults == 0) {
113
        echo '<div class="itemTitle">' . _MD_NORESULTS . '</div>';
114
    } else {
115 View Code Duplication
        foreach ($searchresults as $result) {
0 ignored issues
show
Duplication introduced by
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...
116
            echo '<div class="itemTitle"><a href="' . $result['link'] . '">' . $result['title'] . '</a></div><div class="itemText">' . $result['description'] . '</div><hr>';
117
        }
118
    }
119
    echo '<br>';
120
    echo $pages_text;
121
}
122
$xoopsTpl->assign('search_page', ob_get_contents());
123
ob_end_clean();
124
125
include XOOPS_ROOT_PATH . '/footer.php';
126
127
/**
128
 * @param $queryarray
129
 * @param $andor
130
 * @param $limit
131
 * @param $offset
132
 * @return array|int
133
 */
134
function mod_search($queryarray, $andor, $limit, $offset)
135
{
136
    global $xoopsDB, $eh;
137
    $sql = 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
138
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
139
           . ' d RIGHT JOIN '
140
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
141
           . ' i ON (d.itemid=i.itemid) LEFT JOIN '
142
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
143
           . " t ON (i.itemid=t.itemid) WHERE i.status='2'";
144
    // because count() returns 1 even if a supplied variable
145
    // is not an array, we must check if $queryarray is really an array
146
    if (is_array($queryarray) && $count = count($queryarray)) {
147
        $sql .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
148
        for ($i = 1; $i < $count; ++$i) {
149
            $sql .= " $andor ";
150
            $sql .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
151
        }
152
        $sql .= ') ';
153
    }
154
    $sql .= 'ORDER BY i.created DESC';
155
156
    $result      = $xoopsDB->query($sql, $limit, $offset) or $eh->show('0013');
157
    $num_results = $xoopsDB->getRowsNum($result);
158
    if (!$result) {
159
        return 0;
160
    } elseif ($num_results == 0) {
161
        return 0;
162
    } else {
163
        $ret = array();
164
        $i   = 0;
165
        while ($myrow = $xoopsDB->fetchArray($result)) {
166
            $ret[$i]['image']       = 'images/home.gif';
167
            $ret[$i]['link']        = 'listing.php?item=' . $myrow['itemid'] . '';
168
            $ret[$i]['title']       = $myrow['title'];
169
            $ret[$i]['description'] = $myrow['description'];
170
            $ret[$i]['time']        = $myrow['created'];
171
            $ret[$i]['uid']         = $myrow['uid'];
172
            ++$i;
173
        }
174
175
        return $ret;
176
    }
177
}
178
179
/**
180
 * @param     $queryarray
181
 * @param     $andor
182
 * @param     $limit
183
 * @param int $offset
184
 * @return int|void
185
 */
186 View Code Duplication
function mod_search_count($queryarray, $andor, $limit, $offset = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

186
function mod_search_count($queryarray, $andor, $limit, /** @scrutinizer ignore-unused */ $offset = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

186
function mod_search_count($queryarray, $andor, /** @scrutinizer ignore-unused */ $limit, $offset = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This function seems to be duplicated in 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...
187
{
188
    global $xoopsDB, $eh;
189
    $count = 0;
190
    $sql   = 'SELECT COUNT(DISTINCT i.itemid) FROM '
191
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
192
             . ' d, '
193
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
194
             . ' i LEFT JOIN '
195
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
196
             . " t ON (i.itemid=t.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
197
    // because count() returns 1 even if a supplied variable
198
    // is not an array, we must check if $queryarray is really an array
199
    if (is_array($queryarray) && $count = count($queryarray)) {
200
        $sql .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
201
        for ($i = 1; $i < $count; ++$i) {
202
            $sql .= " $andor ";
203
            $sql .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
204
        }
205
        $sql .= ') ';
206
    }
207
    $result = $xoopsDB->query($sql) or $eh->show('0013');
208
    list($count) = $xoopsDB->fetchRow($result);
209
210
    return $count;
211
}
212