Completed
Branch master (00e474)
by Michael
05:07
created

search.inc.php ➔ oledrion_search()   C

Complexity

Conditions 13
Paths 128

Size

Total Lines 66
Code Lines 49

Duplication

Lines 10
Ratio 15.15 %

Importance

Changes 0
Metric Value
cc 13
eloc 49
nc 128
nop 5
dl 10
loc 66
rs 5.3966
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
 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
 * oledrion
14
 *
15
 * @copyright   The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 * @version     $Id: search.inc.php 12290 2014-02-07 11:05:17Z beckmi $
19
 */
20
21
function oledrion_search($queryarray, $andor, $limit, $offset, $userid)
22
{
23
    global $xoopsDB;
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...
24
    require XOOPS_ROOT_PATH . '/modules/oledrion/include/common.php';
25
    require_once XOOPS_ROOT_PATH . '/modules/oledrion/class/oledrion_products.php';
26
27
    // Recherche dans les produits
28
    $sql = 'SELECT product_id, product_title, product_submitted, product_submitter FROM ' . $xoopsDB->prefix('oledrion_products') . ' WHERE (product_online = 1';
29
    if (oledrion_utils::getModuleOption('show_unpublished') == 0) { // Ne pas afficher les produits qui ne sont pas publiés
30
        $sql .= ' AND product_submitted <= ' . time();
31
    }
32
    if (oledrion_utils::getModuleOption('nostock_display') == 0) { // Se limiter aux seuls produits encore en stock
33
        $sql .= ' AND product_stock > 0';
34
    }
35
    if ($userid != 0) {
36
        $sql .= '  AND product_submitter = ' . $userid;
37
    }
38
    $sql .= ') ';
39
40
    $tmpObject = new oledrion_products();
41
    $datas = $tmpObject->getVars();
42
    $tblFields = array();
43
    $cnt = 0;
44 View Code Duplication
    foreach ($datas as $key => $value) {
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...
45
        if ($value['data_type'] == XOBJ_DTYPE_TXTBOX || $value['data_type'] == XOBJ_DTYPE_TXTAREA) {
46
            if ($cnt == 0) {
47
                $tblFields[] = $key;
48
            } else {
49
                $tblFields[] = ' OR ' . $key;
50
            }
51
            $cnt++;
52
        }
53
    }
54
55
    $count = count($queryarray);
56
    $more = '';
57
    if (is_array($queryarray) && $count > 0) {
58
        $cnt = 0;
59
        $sql .= ' AND (';
60
        $more = ')';
61
        foreach ($queryarray as $oneQuery) {
62
            $sql .= '(';
63
            $cond = " LIKE '%" . $oneQuery . "%' ";
64
            $sql .= implode($cond, $tblFields) . $cond . ')';
65
            $cnt++;
66
            if ($cnt != $count) {
67
                $sql .= ' ' . $andor . ' ';
68
            }
69
        }
70
    }
71
    $sql .= $more . ' ORDER BY product_submitted DESC';
72
    $i = 0;
73
    $ret = array();
74
    $myts = MyTextSanitizer::getInstance();
75
    $result = $xoopsDB->query($sql, $limit, $offset);
76
    while ($myrow = $xoopsDB->fetchArray($result)) {
77
        $ret[$i]['image'] = 'assets/images/product.png';
78
        $ret[$i]['link'] = "product.php?product_id=" . $myrow['product_id'];
79
        $ret[$i]['title'] = $myts->htmlSpecialChars($myrow['product_title']);
80
        $ret[$i]['time'] = $myrow['product_submitted'];
81
        $ret[$i]['uid'] = $myrow['product_submitter'];
82
        $i++;
83
    }
84
85
    return $ret;
86
}
87