Passed
Push — master ( fd2af9...93b609 )
by Michael
16:23 queued 02:10
created

b_tdmdownloads_search_show()   C

Complexity

Conditions 12
Paths 37

Size

Total Lines 113

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 113
rs 5.5733
cc 12
nc 37

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');
0 ignored issues
show
Unused Code introduced by
The assignment to $downloadsfield_Handler is dead and can be removed.
Loading history...
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($mytree->makeSelectElement('cat', 'cat_title', '--', '', true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), 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;
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
            if ($downloads_field[$i]->getVar('fid') == 2) {
72
                //version
73
                $title_sup = _AM_TDMDOWNLOADS_FORMVERSION;
74
                $criteria->setSort('version');
75
                $nom_champ_base = 'version';
76
            }
77
            if ($downloads_field[$i]->getVar('fid') == 3) {
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'));
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

86
                $platform_array = explode('|',/** @scrutinizer ignore-deprecated */ xoops_getModuleOption('platform', 'TDMDownloads'));
Loading history...
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
                foreach (array_keys($tdmdownloads_arr) as $j) {
94
                    $contenu_arr[$tdmdownloads_arr[$j]->getVar($nom_champ_base)] = $tdmdownloads_arr[$j]->getVar($nom_champ_base);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nom_champ_base does not seem to be defined for all execution paths leading up to this point.
Loading history...
95
                }
96
            }
97
        } else {
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')]));
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
Comprehensibility Best Practice introduced by
The variable $select_sup does not seem to be defined for all execution paths leading up to this point.
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