b_tdmdownloads_search_show()   C
last analyzed

Complexity

Conditions 12
Paths 29

Size

Total Lines 123
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 89
nc 29
nop 0
dl 0
loc 123
rs 5.8133
c 3
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
declare(strict_types=1);
4
5
/**
6
 * TDMDownload
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright   Gregory Mage (Aka Mage)
16
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @author      Gregory Mage (Aka Mage)
18
 */
19
20
use XoopsModules\Tdmdownloads\{
21
    Helper,
22
    Tree
23
};
24
25
/**
26
 * @return array
27
 */
28
function b_tdmdownloads_search_show()
29
{
30
    require dirname(__DIR__) . '/include/common.php';
31
    $moduleDirName = basename(dirname(__DIR__));
32
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
33
    require_once XOOPS_ROOT_PATH . '/class/tree.php';
34
    $db = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
35
    $helper = Helper::getInstance();
36
    //appel des class
37
    //appel des fichiers de langues
38
    $helper->loadLanguage('admin');
39
    $helper->loadLanguage('main');
40
    $helper->loadLanguage('common');
41
    $utility    = new \XoopsModules\Tdmdownloads\Utility();
42
    $categories = $utility->getItemIds('tdmdownloads_view', $moduleDirName);
43
    /** @var \XoopsModules\Tdmdownloads\CategoryHandler $categoryHandler */
44
    $categoryHandler = Helper::getInstance()->getHandler('Category');
45
    $block           = [];
46
    //formulaire de recherche
47
    $form = new \XoopsThemeForm(_MD_TDMDOWNLOADS_SEARCH, 'search', XOOPS_URL . '/modules/' . $moduleDirName . '/search.php', 'post');
48
    $form->setExtra('enctype="multipart/form-data"');
49
    //recherche par titre
50
    $form->addElement(new \XoopsFormText(_MD_TDMDOWNLOADS_SEARCH_TITLE, 'title', 25, 255, ''));
51
    //recherche par catégorie
52
    $criteria = new \CriteriaCompo();
53
    $criteria->setSort('cat_weight ASC, cat_title');
54
    $criteria->setOrder('ASC');
55
    $criteria->add(new \Criteria('cat_cid', '(' . implode(',', $categories) . ')', 'IN'));
56
    $downloadscatArray = $categoryHandler->getAll($criteria);
57
    $mytree            = new Tree($downloadscatArray, 'cat_cid', 'cat_pid');
58
    $form->addElement($mytree->makeSelectElement('cat', 'cat_title', '--', '', true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
59
    //recherche champ sup.
60
    /** @var \XoopsModules\Tdmdownloads\FieldHandler $fieldHandler */
61
    $fieldHandler = Helper::getInstance()->getHandler('Field');
62
    $criteria     = new \CriteriaCompo();
63
    $criteria->add(new \Criteria('search', 1));
64
    $criteria->add(new \Criteria('status', 1));
65
    $criteria->setSort('weight ASC, title');
66
    $criteria->setOrder('ASC');
67
    $downloads_field = $fieldHandler->getAll($criteria);
68
    $fieldNameBase   = '';
69
    foreach (array_keys($downloads_field) as $i) {
70
        /** @var \XoopsModules\Tdmdownloads\Field[] $downloads_field */
71
        $title_sup                                         = '';
72
        $contentArray                                      = [];
73
        $lid_arr                                           = [];
74
        $fieldName                                         = 'champ' . $downloads_field[$i]->getVar('fid');
75
        $criteria                                          = new \CriteriaCompo();
76
        $fieldContent[$downloads_field[$i]->getVar('fid')] = 999;
77
        if (1 == $downloads_field[$i]->getVar('status_def')) {
78
            $criteria->add(new \Criteria('status', 0, '!='));
79
            if (1 == $downloads_field[$i]->getVar('fid')) {
80
                //page d'accueil
81
                $title_sup = _AM_TDMDOWNLOADS_FORMHOMEPAGE;
82
                $criteria->setSort('homepage');
83
                $fieldNameBase = 'homepage';
84
            }
85
            if (2 == $downloads_field[$i]->getVar('fid')) {
86
                //version
87
                $title_sup = _AM_TDMDOWNLOADS_FORMVERSION;
88
                $criteria->setSort('version');
89
                $fieldNameBase = 'version';
90
            }
91
            if (3 == $downloads_field[$i]->getVar('fid')) {
92
                //taille du fichier
93
                $title_sup = _AM_TDMDOWNLOADS_FORMSIZE;
94
                $criteria->setSort('size');
95
                $fieldNameBase = 'size';
96
            }
97
            if (4 == $downloads_field[$i]->getVar('fid')) {
98
                //platform
99
                $title_sup     = _AM_TDMDOWNLOADS_FORMPLATFORM;
100
                $platformArray = explode('|', xoops_getModuleOption('platform', $moduleDirName));
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

100
                $platformArray = explode('|', /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('platform', $moduleDirName));
Loading history...
Bug introduced by
xoops_getModuleOption('platform', $moduleDirName) of type boolean is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

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

100
                $platformArray = explode('|', /** @scrutinizer ignore-type */ xoops_getModuleOption('platform', $moduleDirName));
Loading history...
101
                foreach ($platformArray as $platform) {
102
                    $contentArray[$platform] = $platform;
103
                }
104
            } else {
105
                $criteria->setOrder('ASC');
106
                /** @var \XoopsModules\Tdmdownloads\DownloadsHandler $downloadsHandler */
107
                $downloadsHandler  = Helper::getInstance()->getHandler('Downloads');
108
                $tdmdownloadsArray = $downloadsHandler->getAll($criteria);
109
                foreach (array_keys($tdmdownloadsArray) as $j) {
110
                    /** @var \XoopsModules\Tdmdownloads\Downloads[] $tdmdownloadsArray */
111
                    $temp                = $tdmdownloadsArray[$j]->getVar($fieldNameBase);
112
                    $contentArray[$temp] = $temp;
113
                }
114
            }
115
        } else {
116
            $title_sup = $downloads_field[$i]->getVar('title');
117
            $criteria->add(new \Criteria('fid', $downloads_field[$i]->getVar('fid')));
0 ignored issues
show
Bug introduced by
It seems like $downloads_field[$i]->getVar('fid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

117
            $criteria->add(new \Criteria('fid', /** @scrutinizer ignore-type */ $downloads_field[$i]->getVar('fid')));
Loading history...
118
            $criteria->setSort('data');
119
            $criteria->setOrder('ASC');
120
            /** @var \XoopsModules\Tdmdownloads\FielddataHandler $fielddataHandler */
121
            $fielddataHandler  = Helper::getInstance()->getHandler('Fielddata');
122
            $tdmdownloadsArray = $fielddataHandler->getAll($criteria);
123
            foreach (array_keys($tdmdownloadsArray) as $j) {
124
                /** @var \XoopsModules\Tdmdownloads\Downloads[] $tdmdownloadsArray */
125
                $contentArray[$tdmdownloadsArray[$j]->getVar('data', 'n')] = $tdmdownloadsArray[$j]->getVar('data');
126
            }
127
            if ('' != $fieldContent[$downloads_field[$i]->getVar('fid')]) {
128
                $criteria_1 = new \CriteriaCompo();
129
                $criteria_1->add(new \Criteria('data', $fieldContent[$downloads_field[$i]->getVar('fid')]));
130
                /** @var \XoopsModules\Tdmdownloads\Fielddata $dataArray */
131
                $dataArray = $fielddataHandler->getAll($criteria_1);
132
                foreach (array_keys($dataArray) as $k) {
0 ignored issues
show
Bug introduced by
$dataArray of type XoopsModules\Tdmdownloads\Fielddata is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

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

132
                foreach (array_keys(/** @scrutinizer ignore-type */ $dataArray) as $k) {
Loading history...
133
                    /** @var \XoopsModules\Tdmdownloads\Fielddata[] $dataArray */
134
                    $lid_arr[] = $dataArray[$k]->getVar('lid');
135
                }
136
            }
137
            //            $form->addElement($select_sup);
138
        }
139
        $select_sup = new \XoopsFormSelect($title_sup, $fieldName, $fieldContent[$downloads_field[$i]->getVar('fid')]);
140
        $select_sup->addOption(999, _MD_TDMDOWNLOADS_SEARCH_ALL1);
141
        $select_sup->addOptionArray($contentArray);
142
        $form->addElement($select_sup);
143
        unset($select_sup);
144
    }
145
    //bouton validation
146
    $buttonTray = new \XoopsFormElementTray('', '');
147
    $buttonTray->addElement(new \XoopsFormButton('', 'submit', _MD_TDMDOWNLOADS_SEARCH_BT, 'submit'));
148
    $form->addElement($buttonTray);
149
    $block['form'] = $form->render();
150
    return $block;
151
}
152