Issues (1149)

advancedsearch.php (8 issues)

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
//Include XOOPS classes
23
require_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
24
require_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
25
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
26
//Include module classes
27
require_once __DIR__ . '/class/class.formimage.php';
28
require_once __DIR__ . '/class/class.image.php';
29
require_once __DIR__ . '/class/class.efqtree.php';
30
require_once __DIR__ . '/class/class.datafieldmanager.php';
31
32
$myts             = MyTextSanitizer::getInstance();
33
$eh               = new ErrorHandler;
34
$datafieldmanager = new efqDataFieldManager();
35
36
$moddir = $xoopsModule->getVar('dirname');
37
38
if (isset($_GET['catid'])) {
39
    $get_cid = (int)$_GET['cid'];
40
} else {
41
    $get_cid = '1';
42
}
43 View Code Duplication
if (isset($_GET['dirid'])) {
44
    $get_dirid = (int)$_GET['dirid'];
45
} elseif (isset($_POST['dirid'])) {
46
    $get_dirid = (int)$_POST['dirid'];
47
}
48 View Code Duplication
if (isset($_GET['orderby'])) {
49
    $orderby = convertOrderByIn($_GET['orderby']);
50
} else {
51
    $orderby = 'title ASC';
52
}
53
if (isset($_GET['page'])) {
54
    $get_page = (int)$_GET['page'];
55
} else {
56
    $get_page = 1;
57
}
58
59
$GLOBALS['xoopsOption']['template_main'] = 'efqdiralpha1_search.tpl';
60
include XOOPS_ROOT_PATH . '/header.php';
61
$xoopsTpl->assign('xoops_module_header', $xoops_module_header);
62
//The advanced search is within a directory
63
//First get an array of all datatypes within a directory that has listings linked to them
64
if (isset($_POST['submit'])) {
65
    $sql         = 'SELECT DISTINCT dt.dtypeid, dt.title, dt.options, ft.fieldtype FROM '
66
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_fieldtypes')
67
                   . ' ft, '
68
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_dtypes')
69
                   . ' dt, '
70
                   . $xoopsDB->prefix($module->getVar('dirname', 'n')
71
                                      . '_dtypes_x_cat')
72
                   . ' dxc, '
73
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_cat')
74
                   . " c WHERE c.cid=dxc.cid AND dt.dtypeid=dxc.dtypeid AND dt.fieldtypeid=ft.typeid AND c.dirid='$get_dirid' AND dt.activeyn='1'";
75
    $result      = $xoopsDB->query($sql) or $eh->show('0013');
76
    $num_results = $xoopsDB->getRowsNum($result);
77
    $filter_arr  = array();
78
    while (list($dtypeid, $dtypetitle, $dtypeoptions, $fieldtype) = $xoopsDB->fetchRow($result)) {
79
        switch ($fieldtype) {
80
            case 'textbox':
81
                $filter_arr[] = getPostedValue_text($dtypeid);
82
                break;
83
            case 'textarea':
84
                $filter_arr[] = getPostedValue_text($dtypeid);
85
                break;
86
            case 'yesno':
87
                $filter_arr[] = getPostedValue_text($dtypeid);
88
                break;
89
            case 'radio':
90
                $filter_arr[] = getPostedValue_text($dtypeid);
91
                break;
92 View Code Duplication
            case 'select':
93
                $selectarr = getPostedValue_array($dtypeid);
94
                $count_arr = count($selectarr);
95
96
                if ($count_arr >= 1) {
97
                    foreach ($selectarr as $arr) {
98
                        $selectarray[] = $arr['postvalue'];
99
                    }
100
                    $filter_arr[] = array('dtypeid' => $dtypeid, 'constr' => 'equal', 'postvalue' => $selectarr['postvalue'], 'selectfields' => $selectarray, 'addressfields' => 0);
101
                }
102
                break;
103
            case 'dhtml':
104
                $filter_arr[] = getPostedValue_text($dtypeid);
105
                break;
106
            case 'address':
107
                $addressarr   = getPostedValue_address($dtypeid);
108
                $addressarray = array();
109
                //$countarr = count($address
110
                if (is_array($addressarray) && count($addressarray) >= 1) {
111
                    foreach ($addressarr as $arr) {
112
                        $addressarray[] = array($arr['field'] => $arr['postvalue']);
113
                    }
114
                    $filter_arr[] = array('dtypeid' => $dtypeid, 'constr' => 'equal', 'postvalue' => $arr['postvalue'], 'selectfields' => 0, 'addressfields' => $addressarray);
115
                }
116
                break;
117 View Code Duplication
            case 'rating':
118
                $selectarr = getPostedValue_array($dtypeid);
119
                //$selectarr = getPostedValue_text($dtypeid);
120
                $count_arr = count($selectarr);
121
122
                if ($count_arr >= 1) {
123
                    foreach ($selectarr as $arr) {
124
                        $selectarray[] = $arr;
125
                    }
126
                    //echo $selectarr['postvalue'];
127
                    $filter_arr[] = array('dtypeid' => $dtypeid, 'constr' => $selectarr['constr'], 'postvalue' => $selectarr['postvalue'], 'selectfields' => '', 'addressfields' => 0);
128
                }
129
                break;
130
            case 'date':
131
                $filter_arr[] = getPostedValue_text($dtypeid);
132
                break;
133
            default:
134
                break;
135
        }
136
    }
137
    if (isset($_POST['q'])) {
138
        $querystring = $GLOBALS['xoopsDB']->escape($myts->stripSlashesGPC($_POST['q']));
139
    } else {
140
        $querystring = '';
141
    }
142
    $poscount   = substr_count($querystring, '"') / 2;
143
    $specialarr = array();
144 View Code Duplication
    for ($i = 0; $i < $poscount; ++$i) {
145
        $start = strpos($querystring, '"', 0);
146
        $end   = strpos($querystring, '"', $start + 1);
147
        if ($end !== false) {
148
            $specialstring = ltrim(substr($querystring, $start, $end - $start), '"');
149
            $specialarr[]  = $specialstring;
150
            $querystring   = ltrim(substr_replace($querystring, '', $start, $end - $start + 1));
151
        } else {
152
            $querystring = ltrim(substr_replace($querystring, '', $start, 1));
153
        }
154
    }
155
    $queryarr   = explode(' ', $querystring);
156
    $queryarr   = array_merge($specialarr, $queryarr);
157
    $emptyarr[] = '';
158
    $querydiff  = array_diff($queryarr, $emptyarr);
159
160
    $limit  = $xoopsModuleConfig['searchresults_perpage'];
161
    $offset = ($get_page - 1) * $limit;
162
163
    $andor         = 'AND';
164
    $searchresults = mod_search($querydiff, $andor, $limit, $offset, $filter_arr);
165
166
    $maxpages      = 10;
167
    $maxcount      = 30;
168
    $count_results = count($searchresults);
169
    $count_pages   = 0;
170
171
    $items_arr_text = '';
172
    //Calculate the number of result pages.
173
    if ($count_results > $limit) {
174
        $count_pages = ceil($count_results / $limit);
175
    } else {
176
        $count_pages = 1;
177
    }
178
    $pages_text = sprintf(_MD_LISTINGS_FOUND_ADV, $count_results);
179
180
    if ($count_pages >= 2) {
181
        $searchnum  = uniqid(mt_rand(), 1);
182
        $pages_text .= '' . _MD_PAGES . '<a href="advancedsearch.php?ref=' . $searchnum . '&page=1&dirid=' . $get_dirid . '">1</a>';
183
    }
184 View Code Duplication
    for ($i = 1; $i < $count_pages; ++$i) {
185
        $page       = $i + 1;
186
        $pages_text .= ' - <a href="advancedsearch.php?ref=' . $searchnum . '&page=' . $page . '&dirid=' . $get_dirid . '">' . $page . '</a>';
187
    }
188
    $pages_text     .= sprintf(_MD_NEW_SEARCH_ADV, $get_dirid);
189
    $pages_top_text = sprintf(_MD_PAGE_OF_PAGES, $get_page, $count_pages) . '<br><br>';
190
191
    ob_start();
192
    echo '<div class="itemTitleLarge">' . _MD_SEARCHRESULTS_TITLE . '</div><br>';
193
    echo $pages_top_text;
194
    if ($searchresults == 0) {
195
        echo '<div class="itemTitle">' . _MD_NORESULTS . '</div>';
196
    } else {
197
        $y    = 0;
198
        $page = 1;
199
        foreach ($searchresults as $result) {
200
            if ($y < $limit && $page == 1) {
201
                echo '<div class="itemTitle"><a href="' . $result['link'] . '">' . $result['title'] . '</a></div><div class="itemText">' . $result['description'] . '</div><hr>';
202 View Code Duplication
                if ($y < $limit && $y > 0) {
203
                    $items_arr_text .= ',' . $result['itemid'] . '';
204
                } else {
205
                    $items_arr_text = $result['itemid'];
206
                }
207
                ++$y;
208 View Code Duplication
            } elseif ($y < $limit && $y > 0) {
209
                $items_arr_text .= ',' . $result['itemid'] . '';
210
                ++$y;
211
            } elseif ($y < $limit && $y == 0) {
212
                $items_arr_text = $result['itemid'];
213
                ++$y;
214
            } elseif ($y == $limit) {
215
                //Save $items_arr_text and $page into DB
216
                $newid = $xoopsDB->genId($xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults') . '_searchid_seq');
217
                $sql   = sprintf("INSERT INTO %s (searchid, searchnum, created, PAGE, items, dirid, catid) VALUES (%u, '%s', '%s', %u, '%s', %u, %u)", $xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults'), $newid, $searchnum, time(), $page, $items_arr_text, $get_dirid, $get_cid);
218
                $xoopsDB->query($sql) or $eh->show('0013');
219
                if ($newid == 0) {
220
                    $itemid = $xoopsDB->getInsertId();
221
                }
222
                $items_arr_text = $result['itemid'];
223
                $y              = 1;
224
                ++$page;
225
            }
226
        }
227
        if ($y != 0 && $page > 1) {
228
            $newid = $xoopsDB->genId($xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults') . '_searchid_seq');
229
            $sql   = sprintf("INSERT INTO %s (searchid, searchnum, created, PAGE, items, dirid, catid) VALUES (%u, '%s', '%s', %u, '%s', %u, %u)", $xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults'), $newid, $searchnum, time(), $page, $items_arr_text, $get_dirid, $get_cid);
230
            $xoopsDB->query($sql) or $eh->show('0013');
231
            if ($newid == 0) {
232
                $itemid = $xoopsDB->getInsertId();
233
            }
234
        }
235
    }
236
    echo '<br>';
237
    echo $pages_text;
238
239
    $xoopsTpl->assign('search_page', ob_get_contents());
240
    ob_end_clean();
241
} elseif (isset($_GET['ref']) && isset($_GET['page'])) {
242
    $get_searchnum = $GLOBALS['xoopsDB']->escape($myts->stripSlashesGPC($_GET['ref']));
243
    $get_page      = (int)$_GET['page'];
244
245
    //Query the saved results from the DB.
246
    $sql = 'SELECT searchid, searchnum, created, page, items, dirid, catid FROM ' . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults') . " WHERE searchnum='$get_searchnum' AND page='$get_page'";
247
248
    $result      = $xoopsDB->query($sql) or $eh->show('0013');
249
    $num_results = $xoopsDB->getRowsNum($result);
250
    while (list($searchid, $searchnum, $created, $page, $items, $dirid, $catid) = $xoopsDB->fetchRow($result)) {
251
        //Split items and for each item, get item data.
252
        if ($items !== '') {
253
            $searchresults = get_search_results($items);
254
        } else {
255
            $searchresults = 0;
256
        }
257
    }
258
    $maxpages      = 10;
259
    $maxcount      = 30;
260
    $limit         = $xoopsModuleConfig['searchresults_perpage'];
261
    $count_results = getNumberOfResults($get_searchnum, $limit);
262
    $count_pages   = 0;
263
264
    $items_arr_text = '';
265
266
    $offset = ($get_page - 1) * $limit;
267
    //Calculate the number of result pages.
268
    if ($count_results > $limit) {
269
        $count_pages = ceil($count_results / $limit);
270
    }
271
272
    ob_start();
273
    printf(_MD_LISTINGS_FOUND_ADV, $count_results);
274
    $pages_text = ob_get_contents();
275
    ob_end_clean();
276
277
    ob_start();
278
    printf(_MD_PAGE_OF_PAGES, $get_page, $count_pages) . '<br><br>';
279
    $pages_top_text = ob_get_contents();
280
    ob_end_clean();
281
282
    if ($count_pages >= 2) {
283
        $pages_text .= '' . _MD_PAGES . '<a href="advancedsearch.php?ref=' . $get_searchnum . '&page=1&dirid=' . $get_dirid . '">1</a>';
284
    }
285 View Code Duplication
    for ($i = 1; $i < $count_pages; ++$i) {
286
        $page       = $i + 1;
287
        $pages_text .= ' - <a href="advancedsearch.php?ref=' . $get_searchnum . '&page=' . $page . '&dirid=' . $get_dirid . '">' . $page . '</a><br>';
288
    }
289
    ob_start();
290
    printf(_MD_NEW_SEARCH_ADV, $get_dirid);
291
    $pages_text .= ob_get_contents();
292
    ob_end_clean();
293
294
    ob_start();
295
    echo '<div class="itemTitleLarge">' . _MD_SEARCHRESULTS_TITLE . '</div><br>';
296
    echo $pages_top_text;
297
    if ($searchresults == 0) {
298
        echo '<div class="itemTitle">' . _MD_NORESULTS . '</div>';
299
    } else {
300
        $y    = 0;
301
        $page = 1;
302 View Code Duplication
        foreach ($searchresults as $result) {
303
            echo '<div class="itemTitle"><a href="' . $result['link'] . '">' . $result['title'] . '</a></div><div class="itemText">' . $result['description'] . '</div><hr>';
304
        }
305
    }
306
    echo $pages_text;
307
    $xoopsTpl->assign('search_page', ob_get_contents());
308
    ob_end_clean();
309
} else {
310
    //No search was posted nor was a search page requested.
311
    //A search form is generated from the datatype fields in the chosen directory.
312
    $sql         = 'SELECT DISTINCT dt.dtypeid, dt.title, dt.options, ft.fieldtype FROM '
313
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_fieldtypes')
314
                   . ' ft, '
315
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_dtypes')
316
                   . ' dt, '
317
                   . $xoopsDB->prefix($module->getVar('dirname', 'n')
318
                                      . '_dtypes_x_cat')
319
                   . ' dxc, '
320
                   . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_cat')
321
                   . " c WHERE c.cid=dxc.cid AND dt.dtypeid=dxc.dtypeid AND dt.fieldtypeid=ft.typeid AND c.dirid='$get_dirid' AND dt.activeyn='1'";
322
    $result      = $xoopsDB->query($sql) or $eh->show('0013');
323
    $num_results = $xoopsDB->getRowsNum($result);
324
325
    $xoopsTpl->assign('xoops_module_header', $xoops_module_header);
326
    ob_start();
327
    $form = new XoopsThemeForm(_MD_ADVSEARCH_FORM, 'advsearchform', 'advancedsearch.php');
328
    $form->setExtra('enctype="multipart/form-data"');
329
    while (list($dtypeid, $dtypetitle, $dtypeoptions, $fieldtype) = $xoopsDB->fetchRow($result)) {
330
        $field = $datafieldmanager->createSearchField($dtypetitle, $dtypeid, $fieldtype, '', $dtypeoptions);
331
    }
332
    $form->addElement(new XoopsFormText(_MD_SEARCHSTRING, 'q', 50, 250, ''));
333
    $form->addElement(new XoopsFormButton('', 'submit', _MD_SEARCH, 'submit'));
334
    $form->addElement(new XoopsFormHidden('op', 'search'));
335
    $form->addElement(new XoopsFormHidden('dirid', $get_dirid));
336
    $form->display();
337
    $xoopsTpl->assign('search_page', ob_get_contents());
338
    ob_end_clean();
339
}
340
include XOOPS_ROOT_PATH . '/footer.php';
341
342
/**
343
 * @param $queryarray
344
 * @param $andor
345
 * @param $limit
346
 * @param $offset
347
 * @param $filter_arr
348
 * @return array
349
 */
350
function mod_search($queryarray, $andor, $limit, $offset, $filter_arr)
351
{
352
    global $xoopsDB, $eh;
353
    // because count() returns 1 even if a supplied variable
354
    // is not an array, we must check if $queryarray is really an array
355
    $n            = 0;
356
    ${'sql' . $n} = '';
357
    $items        = '';
358
    if (is_array($queryarray) && count($queryarray) >= 1) {
359
        $count        = count($queryarray);
360
        ${'sql' . $n} = 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
361
                        . $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...
362
                        . ' d RIGHT JOIN '
363
                        . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
364
                        . ' i ON (d.itemid=i.itemid) LEFT JOIN '
365
                        . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
366
                        . " t ON (t.itemid=i.itemid) WHERE i.status='2'";
367
        ${'sql' . $n} .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
368
        for ($i = 1; $i < $count; ++$i) {
369
            ${'sql' . $n} .= " $andor ";
370
            ${'sql' . $n} .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
371
        }
372
        ${'sql' . $n} .= ') ';
373
        ${'sql' . $n} .= ' ORDER BY i.created DESC';
374
        ++$n;
375
    }
376
377
    $andor = ' AND';
378
    foreach ($filter_arr as $filter) {
379
        ${'sql' . $n}  = '';
380
        $dtypeid       = $filter['dtypeid'];
381
        $constr        = $filter['constr'];
382
        $postvalue     = $filter['postvalue'];
383
        $selectfields  = $filter['selectfields'];
384
        $addressfields = $filter['addressfields'];
385
        if (is_array($selectfields)) {
386
            if (count($selectfields) >= 1) {
387
                ${'sql' . $n}       .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
388
                                       . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
389
                                       . ' d, '
390
                                       . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
391
                                       . ' i LEFT JOIN '
392
                                       . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
393
                                       . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
394
                $select             = '';
395
                ${'sql' . $n}       .= ' ' . $andor . " (d.dtypeid = $dtypeid AND d.value IN (";
396
                $count_selectfields = count($selectfields);
397
                $i                  = 1;
398
                foreach ($selectfields as $selectfield) {
399
                    ${'sql' . $n} .= '\'' . $selectfield . '\'';
400
                    if ($i != $count_selectfields) {
401
                        ${'sql' . $n} .= ', ';
402
                    }
403
                    ++$i;
404
                }
405
                ${'sql' . $n} .= '))';
406
                ++$n;
407
                $postvalue = '';
408
            }
409
        } else {
410
            $select = '0';
411
        }
412
413
        if ($postvalue !== '') {
414
            ${'sql' . $n} = '';
415
            //$zero_allowed = '1';
416
            switch ($constr) {
417 View Code Duplication
                case 'equal':
418
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
419
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
420
                                     . ' d, '
421
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
422
                                     . ' i LEFT JOIN '
423
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
424
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
425
                    $constraint   = " = '" . $postvalue . '\'';
426
                    break;
427 View Code Duplication
                case 'notequal':
428
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
429
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
430
                                     . ' d, '
431
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
432
                                     . ' i LEFT JOIN '
433
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
434
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
435
                    $constraint   = " <> '" . $postvalue . '\'';
436
                    break;
437 View Code Duplication
                case 'contains':
438
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
439
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
440
                                     . ' d, '
441
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
442
                                     . ' i LEFT JOIN '
443
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
444
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
445
                    $constraint   = " LIKE '%" . $postvalue . "%'";
446
                    break;
447 View Code Duplication
                case 'notcontain':
448
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
449
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
450
                                     . ' d, '
451
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
452
                                     . ' i LEFT JOIN '
453
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
454
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
455
                    $constraint   = " NOT LIKE '%" . $postvalue . "%'";
456
                    break;
457 View Code Duplication
                case 'begins':
458
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
459
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
460
                                     . ' d, '
461
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
462
                                     . ' i LEFT JOIN '
463
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
464
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
465
                    $constraint   = " LIKE '" . $postvalue . "%'";
466
                    break;
467 View Code Duplication
                case 'ends':
468
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
469
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
470
                                     . ' d, '
471
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
472
                                     . ' i LEFT JOIN '
473
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
474
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
475
                    $constraint   = " LIKE '%" . $postvalue . '\'';
476
                    break;
477 View Code Duplication
                case 'bigger':
478
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
479
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
480
                                     . ' d, '
481
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
482
                                     . ' i LEFT JOIN '
483
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
484
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
485
                    $constraint   = ' > ' . (int)$postvalue . '';
486
                    break;
487 View Code Duplication
                case 'smaller':
488
                    ${'sql' . $n} .= 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
489
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
490
                                     . ' d, '
491
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
492
                                     . ' i LEFT JOIN '
493
                                     . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
494
                                     . " t ON (t.itemid=i.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
495
                    $constraint   = ' < ' . (int)$postvalue . '';
496
                    break;
497
                default:
498
                    break;
499
            }
500
            if ($postvalue != '0') {
501
                ${'sql' . $n} .= $andor . ' (d.dtypeid = ' . $dtypeid . ' AND d.value ' . $constraint . ')';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $constraint does not seem to be defined for all execution paths leading up to this point.
Loading history...
502
            }
503
            ${'sql' . $n} .= ' ORDER BY i.created DESC';
504
            ++$n;
505
        }
506
    }
507
508
    //Getting the results into an array.
509
    $z = 0;
510
511
    $ret          = array();
512
    $items        = array();
513
    $intersection = array();
514
    for ($i = 0; $i < $n; ++$i) {
515
        $result      = $xoopsDB->query(${'sql' . $i}) or $eh->show('0013');
516
        $num_results = $xoopsDB->getRowsNum($result);
517
518
        if (!$result) {
519
            $num_results = 0;
520
        } elseif ($num_results == 0) {
521
            $num_results = 0;
522
        } else {
523
            while ($myrow = $xoopsDB->fetchArray($result)) {
524
                $items[] = $myrow['itemid'];
525
                if ($i == 0) {
526
                    $ret[$z]['itemid']      = $myrow['itemid'];
527
                    $ret[$z]['image']       = 'images/home.gif';
528
                    $ret[$z]['link']        = 'listing.php?item=' . $myrow['itemid'] . '';
529
                    $ret[$z]['title']       = $myrow['title'];
530
                    $ret[$z]['description'] = $myrow['description'];
531
                    $ret[$z]['time']        = $myrow['created'];
532
                    $ret[$z]['uid']         = $myrow['uid'];
533
                    $intersection           = $items;
534
                }
535
                ++$z;
536
            }
537
            $intersection = array_intersect($intersection, $items);
538
        }
539
    }
540
541
    $i        = 0;
542
    $item_arr = array();
543
    foreach ($intersection as $value) {
544
        $item_arr[$i] = $ret['' . findKeyValuePair($ret, 'itemid', $value, false) . ''];
545
        ++$i;
546
    }
547
548
    return $item_arr;
549
}
550
551
/**
552
 * @param string $items
553
 * @return array|bool
554
 */
555
function get_search_results($items = '')
556
{
557
    global $xoopsDB, $eh;
558
    if ($items !== '') {
559
        $z           = 0;
560
        $ret         = array();
561
        $split_items = explode(',', $items);
562
        foreach ($split_items as $item) {
563
            $sql         = 'SELECT i.itemid, i.title, i.uid, i.created, t.description FROM ' . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items') . ' i, ' . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text') . " t WHERE i.itemid=t.itemid AND i.itemid='$item'";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
564
            $result      = $xoopsDB->query($sql) or $eh->show('0013');
565
            $num_results = $xoopsDB->getRowsNum($result);
566
            while ($myrow = $xoopsDB->fetchArray($result)) {
567
                $ret[$z]['itemid']      = $myrow['itemid'];
568
                $ret[$z]['image']       = 'images/home.gif';
569
                $ret[$z]['link']        = 'listing.php?item=' . $myrow['itemid'] . '';
570
                $ret[$z]['title']       = $myrow['title'];
571
                $ret[$z]['description'] = $myrow['description'];
572
                $ret[$z]['time']        = $myrow['created'];
573
                $ret[$z]['uid']         = $myrow['uid'];
574
                ++$z;
575
            }
576
        }
577
578
        return $ret;
579
    } else {
580
        return false;
581
    }
582
}
583
584
/**
585
 * @param int $searchnum
586
 * @param int $limit
587
 * @return int
588
 */
589
function getNumberOfResults($searchnum = 0, $limit = 10)
590
{
591
    global $xoopsDB, $eh;
592
    $block       = array();
593
    $sql         = 'SELECT MAX(page), items FROM ' . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_searchresults') . " WHERE searchnum = '" . $searchnum . '\' GROUP BY page';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
594
    $result      = $xoopsDB->query($sql) or $eh->show('0013');
595
    $num_results = $xoopsDB->getRowsNum($result);
596
    if (!$result) {
597
        return 0;
598
    }
599
    for ($i = 0; $i < $num_results; ++$i) {
600
        $row   = $GLOBALS['xoopsDB']->fetchBoth($result);
601
        $page  = $row['MAX(page)'];
602
        $items = $row['items'];
603
    }
604
605
    $count_lastpage = substr_count($items, ',') + 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $items does not seem to be defined for all execution paths leading up to this point.
Loading history...
606
    $count          = ($page * $limit) - ($limit - $count_lastpage);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $page does not seem to be defined for all execution paths leading up to this point.
Loading history...
607
608
    return $count;
609
}
610
611
/**
612
 * @param     $queryarray
613
 * @param     $andor
614
 * @param     $limit
615
 * @param int $offset
616
 * @param     $filter_arr
617
 * @return int|void
618
 */
619 View Code Duplication
function mod_search_count($queryarray, $andor, $limit, $offset = 0, $filter_arr)
620
{
621
    global $xoopsDB, $eh;
622
    $count = 0;
623
624
    $sql = 'SELECT COUNT(DISTINCT i.itemid) FROM '
625
           . $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...
626
           . ' d, '
627
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
628
           . ' i LEFT JOIN '
629
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
630
           . " t ON (i.itemid=t.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
631
    // because count() returns 1 even if a supplied variable
632
    // is not an array, we must check if $queryarray is really an array
633
    if (is_array($queryarray) && $count = count($queryarray)) {
634
        $sql .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
635
        for ($i = 1; $i < $count; ++$i) {
636
            $sql .= " $andor ";
637
            $sql .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
638
        }
639
        $sql .= ') ';
640
    }
641
    $result = $xoopsDB->query($sql) or $eh->show('0013');
642
    list($count) = $xoopsDB->fetchRow($result);
643
644
    return $count;
645
}
646
647
/**
648
 * @param int $dtypeid
649
 * @return array
650
 */
651 View Code Duplication
function getPostedValue_text($dtypeid = 0)
652
{
653
    global $_POST;
654
    $postedvalues = array();
655
    if (isset($_POST["$dtypeid"])) {
656
        if (isset($_POST['' . $dtypeid . 'constr'])) {
657
            $constrvalue = $_POST['' . $dtypeid . 'constr'];
658
        } else {
659
            $constrvalue = '';
660
        }
661
        $postvalue    = $_POST["$dtypeid"];
662
        $postedvalues = array('dtypeid' => $dtypeid, 'constr' => $constrvalue, 'postvalue' => $postvalue, 'selectfields' => 0, 'addressfields' => 0);
663
    }
664
665
    return $postedvalues;
666
}
667
668
/**
669
 * @param int $dtypeid
670
 * @return array
671
 */
672
function getPostedValue_address($dtypeid = 0)
673
{
674
    global $_POST;
675
    $addressfields = getAddressFields('0');
676
    $postedvalues  = array();
677
    foreach ($addressfields['addressfields'] as $field => $fieldvalue) {
678
        if (isset($_POST["$dtypeid$field"])) {
679
            $addressfield = $_POST["$dtypeid$field"];
680
            if ($addressfield !== '') {
681
                $postedvalues[] = array('field' => $field, 'postvalue' => $addressfield);
682
            }
683
        }
684
    }
685
686
    return $postedvalues;
687
}
688
689
/**
690
 * @param int $dtypeid
691
 * @return array
692
 */
693 View Code Duplication
function getPostedValue_array($dtypeid = 0)
694
{
695
    global $_POST;
696
    $postvalues_arr = array();
697
    if (isset($_POST["$dtypeid"])) {
698
        if (isset($_POST['' . $dtypeid . 'constr'])) {
699
            $constrvalue = $_POST['' . $dtypeid . 'constr'];
700
        } else {
701
            $constrvalue = '';
702
        }
703
        $postvalue    = $_POST["$dtypeid"];
704
        $postedvalues = array('dtypeid' => $dtypeid, 'constr' => $constrvalue, 'postvalue' => $postvalue, 'selectfields' => 0, 'addressfields' => 0);
705
    }
706
707
    //print_r($postedvalues);
708
    return $postedvalues;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $postedvalues does not seem to be defined for all execution paths leading up to this point.
Loading history...
709
710
    /* if ( isset($_POST["$dtypeid"]) ) {
711
    $postvalues_arr[] = $_POST["$dtypeid"];
712
    } else {
713
    $postvalues_arr[] = "";
714
    }
715
716
    return $postvalues_arr; */
717
}
718
719
/**
720
 * Search for a key and value pair in the second typelevel of a multi-dimensional array.
721
 *
722
 * @param array   multi-dimensional array to search
723
 * @param string  key name for which to search
724
 * @param mixed   value for which to search
725
 * @param boolean preform strict comparison
726
 * @return boolean found
727
 * @access public
728
 */
729
function findKeyValuePair($multiArray, $keyName, $value, $strict = false)
730
{
731
    /* Doing this test here makes for a bit of redundant code, but
732
    * improves the speed greatly, as it is not being preformed on every
733
    * iteration of the loop.
734
    */
735
    if (!$strict) {
736 View Code Duplication
        foreach ($multiArray as $multiArrayKey => $childArray) {
737
            if (array_key_exists($keyName, $childArray) && $childArray[$keyName] == $value) {
738
                return $multiArrayKey;
739
            }
740
        }
741
    } else {
742 View Code Duplication
        foreach ($multiArray as $multiArrayKey => $childArray) {
743
            if (array_key_exists($keyName, $childArray) && $childArray[$keyName] === $value) {
744
                return $multiArrayKey;
745
            }
746
        }
747
    }
748
749
    return false;
750
}
751