search.inc.php ➔ references_search()   C
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 32
nop 5
dl 0
loc 65
rs 6.6169
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * ****************************************************************************
4
 * references - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright       Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
15
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package         references
17
 * @author          Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
18
 *
19
 * ****************************************************************************
20
 */
21
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
22
23
function references_search($queryarray, $andor, $limit, $offset, $userid)
24
{
25
    global $xoopsDB;
26
    include XOOPS_ROOT_PATH . '/modules/references/include/common.php';
27
28
    // Recherche dans les articles
29
    $sql = 'SELECT article_id, article_title, article_text, article_timestamp, article_author FROM ' . $xoopsDB->prefix('references_articles') . ' WHERE (article_online = ' . REFERENCES_STATUS_ONLINE . ')';
30
    // Permissions
31
    $handlers    = references_handler::getInstance();
32
    $permissions = $handlers->h_references_articles->getPermissionsCategoriesCriteria()->renderWhere();
33
    $sql .= ' AND (' . trim(str_replace('WHERE ', '', $permissions)) . ')';
34
    // *****
35
36
    // Auteur
37
    if ($userid != 0) {
38
        $sql .= ' AND (article_author = ' . $userid . ') ';
39
    }
40
41
    $tmpObject = new references_articles();
42
    $datas     = $tmpObject->getVars();
43
    $tblFields = array();
44
    $cnt       = 0;
45
    foreach ($datas as $key => $value) {
46
        if ($value['data_type'] == XOBJ_DTYPE_TXTBOX || $value['data_type'] == XOBJ_DTYPE_TXTAREA) {
47
            if ($cnt == 0) {
48
                $tblFields[] = $key;
49
            } else {
50
                $tblFields[] = ' OR ' . $key;
51
            }
52
            ++$cnt;
53
        }
54
    }
55
56
    $count = count($queryarray);
57
    $more  = '';
58
    if (is_array($queryarray) && $count > 0) {
59
        $cnt = 0;
60
        $sql .= ' AND (';
61
        $more = ')';
62
        foreach ($queryarray as $oneQuery) {
63
            $sql .= '(';
64
            $cond = " LIKE '%" . $oneQuery . "%' ";
65
            $sql .= implode($cond, $tblFields) . $cond . ')';
66
            ++$cnt;
67
            if ($cnt != $count) {
68
                $sql .= ' ' . $andor . ' ';
69
            }
70
        }
71
    }
72
    $sql .= $more . ' ORDER BY article_timestamp DESC';
73
    $i      = 0;
74
    $ret    = array();
75
    $myts   = MyTextSanitizer::getInstance();
76
    $result = $xoopsDB->query($sql, $limit, $offset);
77
    while ($myrow = $xoopsDB->fetchArray($result)) {
78
        $ret[$i]['image'] = 'assets/images/newspaper.png';
79
        $ret[$i]['link']  = REFERENCES_URL . 'reference.php?article_id=' . $myrow['article_id'];
80
        $ret[$i]['title'] = $myts->htmlSpecialChars($myrow['article_title']);
81
        $ret[$i]['time']  = $myrow['article_timestamp'];
82
        $ret[$i]['uid']   = $myrow['article_author'];
83
        ++$i;
84
    }
85
86
    return $ret;
87
}
88