Completed
Push — master ( c39b68...a1acf8 )
by Michael
03:03
created

tdmdownloads_search.php ➔ b_tdmdownloads_search_show()   C

Complexity

Conditions 12
Paths 37

Size

Total Lines 114
Code Lines 85

Duplication

Lines 35
Ratio 30.7 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 85
c 3
b 0
f 0
nc 37
nop 0
dl 35
loc 114
rs 5.034

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
 * TDMDownload
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   Gregory Mage (Aka Mage)
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Gregory Mage (Aka Mage)
15
 */
16
17
function b_tdmdownloads_search_show()
18
{
19
    require_once XOOPS_ROOT_PATH."/modules/TDMDownloads/include/functions.php";
20
    include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php";
21
    include_once XOOPS_ROOT_PATH."/class/tree.php";
22
    //appel des class
23
    $downloadscat_Handler =& xoops_getModuleHandler('tdmdownloads_cat', 'TDMDownloads');
24
    $downloads_Handler =& xoops_getModuleHandler('tdmdownloads_downloads', 'TDMDownloads');
25
    $downloadsfield_Handler =& xoops_getModuleHandler('tdmdownloads_field', 'TDMDownloads');
26
    $downloadsfielddata_Handler =& xoops_getModuleHandler('tdmdownloads_fielddata', 'TDMDownloads');
27
    //appel des fichiers de langues
28
    xoops_loadLanguage('main', 'TDMDownloads');
29
    xoops_loadLanguage('admin', 'TDMDownloads');
30
31
    $categories = TDMDownloads_MygetItemIds('tdmdownloads_view', 'TDMDownloads');
32
33
    $block = array();
34
35
    //formulaire de recherche
36
    $form = new XoopsThemeForm(_MD_TDMDOWNLOADS_SEARCH, "search", XOOPS_URL . '/modules/TDMDownloads/search.php', 'post');
37
    $form->setExtra('enctype="multipart/form-data"');
38
    //recherche par titre
39
    $form->addElement(new XoopsFormText(_MD_TDMDOWNLOADS_SEARCH_TITLE, 'title', 25, 255, ''));
40
    //recherche par cat�gorie
41
    $criteria = new CriteriaCompo();
42
    $criteria->setSort('cat_weight ASC, cat_title');
43
    $criteria->setOrder('ASC');
44
    $criteria->add(new Criteria('cat_cid', '(' . implode(',', $categories) . ')','IN'));
45
    $downloadscat_arr = $downloadscat_Handler->getall($criteria);
46
    $mytree = new XoopsObjectTree($downloadscat_arr, 'cat_cid', 'cat_pid');
47
    $form->addElement(new XoopsFormLabel(_AM_TDMDOWNLOADS_FORMINCAT, $mytree->makeSelBox('cat', 'cat_title','--','', true)));
48
    //recherche champ sup.
49
    $downloadsfield_Handler =& xoops_getModuleHandler('tdmdownloads_field', 'TDMDownloads');
50
    $criteria = new CriteriaCompo();
51
    $criteria->add(new Criteria('search', 1));
52
    $criteria->add(new Criteria('status', 1));
53
    $criteria->setSort('weight ASC, title');
54
    $criteria->setOrder('ASC');
55
    $downloads_field = $downloadsfield_Handler->getall($criteria);
56
    foreach (array_keys($downloads_field) as $i) {
57
        $title_sup = '';
58
        $contenu_arr = array();
59
        $lid_arr = array();
60
        $nom_champ = 'champ' . $downloads_field[$i]->getVar('fid');
61
        $criteria = new CriteriaCompo();
62
        $champ_contenu[$downloads_field[$i]->getVar('fid')] = 999;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$champ_contenu was never initialized. Although not strictly required by PHP, it is generally a good practice to add $champ_contenu = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
63
        if ($downloads_field[$i]->getVar('status_def') == 1) {
64
            $criteria->add(new Criteria('status', 0, '!='));
65
            if ($downloads_field[$i]->getVar('fid') == 1) {
66
                //page d'accueil
67
                $title_sup = _AM_TDMDOWNLOADS_FORMHOMEPAGE;
68
                $criteria->setSort('homepage');
69
                $nom_champ_base = 'homepage';
70
            }
71 View Code Duplication
            if ($downloads_field[$i]->getVar('fid') == 2) {
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...
72
                //version
73
                $title_sup = _AM_TDMDOWNLOADS_FORMVERSION;
74
                $criteria->setSort('version');
75
                $nom_champ_base = 'version';
76
            }
77 View Code Duplication
            if ($downloads_field[$i]->getVar('fid') == 3) {
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...
78
                //taille du fichier
79
                $title_sup = _AM_TDMDOWNLOADS_FORMSIZE;
80
                $criteria->setSort('size');
81
                $nom_champ_base = 'size';
82
            }
83
            if ($downloads_field[$i]->getVar('fid') == 4) {
84
                //platform
85
                $title_sup = _AM_TDMDOWNLOADS_FORMPLATFORM;
86
                $platform_array = explode('|',xoops_getModuleOption('platform', 'TDMDownloads'));
87
                foreach ($platform_array as $platform) {
88
                    $contenu_arr[$platform] = $platform;
89
                }
90
            } else {
91
                $criteria->setOrder('ASC');
92
                $tdmdownloads_arr = $downloads_Handler->getall( $criteria );
93 View Code Duplication
                foreach (array_keys($tdmdownloads_arr) as $j) {
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...
94
                    $contenu_arr[$tdmdownloads_arr[$j]->getVar($nom_champ_base)] = $tdmdownloads_arr[$j]->getVar($nom_champ_base);
0 ignored issues
show
Bug introduced by
The variable $nom_champ_base does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
95
                }
96
            }
97 View Code Duplication
        } else {
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...
98
            $title_sup = $downloads_field[$i]->getVar('title');
99
            $criteria->add(new Criteria('fid', $downloads_field[$i]->getVar('fid')));
100
            $criteria->setSort('data');
101
            $criteria->setOrder('ASC');
102
            $tdmdownloads_arr = $downloadsfielddata_Handler->getall( $criteria );
103
            foreach (array_keys($tdmdownloads_arr) as $j) {
104
                $contenu_arr[$tdmdownloads_arr[$j]->getVar('data', 'n')] = $tdmdownloads_arr[$j]->getVar('data');
105
            }
106
            if ($champ_contenu[$downloads_field[$i]->getVar('fid')] != '') {
107
                $criteria_1 = new CriteriaCompo();
108
                $criteria_1->add(new Criteria('data', $champ_contenu[$downloads_field[$i]->getVar('fid')]));
0 ignored issues
show
Bug introduced by
The variable $champ_contenu does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
109
                $data_arr = $downloadsfielddata_Handler->getall($criteria_1);
110
                foreach (array_keys($data_arr) as $k) {
111
                    $lid_arr[] = $data_arr[$k]->getVar('lid');
112
                }
113
114
            }
115
            $form->addElement($select_sup);
0 ignored issues
show
Bug introduced by
The variable $select_sup does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
116
        }
117
        $select_sup = new XoopsFormSelect($title_sup, $nom_champ, $champ_contenu[$downloads_field[$i]->getVar('fid')]);
118
        $select_sup->addOption(999,_MD_TDMDOWNLOADS_SEARCH_ALL1);
119
        $select_sup->addOptionArray($contenu_arr);
120
        $form->addElement($select_sup);
121
        unset ($select_sup);
122
    }
123
    //bouton validation
124
    $button_tray = new XoopsFormElementTray('' ,'');
125
    $button_tray->addElement(new XoopsFormButton('', 'submit', _MD_TDMDOWNLOADS_SEARCH_BT, 'submit'));
126
    $form->addElement($button_tray);
127
    $block['form'] = $form->render();
128
129
    return $block;
130
}
131