Completed
Push — master ( 592643...f454c2 )
by Michael
03:05 queued 01:21
created

Import::getSearchForm()   F

Complexity

Conditions 21
Paths 1728

Size

Total Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
nc 1728
nop 4
dl 0
loc 111
rs 0
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
namespace XoopsModules\Xnewsletter;
4
5
/**
6
 * ****************************************************************************
7
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
8
 * ****************************************************************************
9
 *  XNEWSLETTER - MODULE FOR XOOPS
10
 *  Copyright (c) 2007 - 2012
11
 *  Goffy ( wedega.com )
12
 *
13
 *  You may not change or alter any portion of this comment or credits
14
 *  of supporting developers from this source code or any supporting
15
 *  source code which is considered copyrighted (c) material of the
16
 *  original comment or credit authors.
17
 *
18
 *  This program is distributed in the hope that it will be useful,
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 *  GNU General Public License for more details.
22
 *  ---------------------------------------------------------------------------
23
 * @copyright  Goffy ( wedega.com )
24
 * @license    GPL 2.0
25
 * @package    xnewsletter
26
 * @author     Goffy ( [email protected] )
27
 *
28
 * ****************************************************************************
29
 */
30
31
//use XoopsModules\Xnewsletter;
32
33
require_once dirname(__DIR__) . '/include/common.php';
34
35
/**
36
 * Class Import
37
 */
38
class Import extends \XoopsObject
39
{
40
    public $helper = null;
41
42
    //Constructor
43
44
    public function __construct()
45
    {
46
        $this->helper = Helper::getInstance();
47
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
48
        $this->initVar('import_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('import_email', XOBJ_DTYPE_TXTBOX, null, false, 100);
50
        $this->initVar('import_firstname', XOBJ_DTYPE_TXTBOX, null, false, 100);
51
        $this->initVar('import_lastname', XOBJ_DTYPE_TXTBOX, null, false, 100);
52
        $this->initVar('import_sex', XOBJ_DTYPE_TXTBOX, null, false, 100);
53
        $this->initVar('import_cat_id', XOBJ_DTYPE_INT, null, false);
54
        $this->initVar('import_subscr_id', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('import_catsubscr_id', XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('import_status', XOBJ_DTYPE_INT, false, false); // boolean
57
    }
58
59
    /**
60
     * @param      $plugin
61
     * @param int  $action_after_read
62
     * @param int  $limitcheck
63
     * @param bool $action
64
     *
65
     * @return \XoopsThemeForm
66
     */
67
    public function getSearchForm($plugin, $action_after_read = 1, $limitcheck = 0, $action = false)
68
    {
69
        global $xoopsDB;
70
71
        if (false === $action) {
72
            $action = $_SERVER['REQUEST_URI'];
73
        }
74
75
        $title = _AM_XNEWSLETTER_IMPORT_SEARCH;
76
77
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
78
        $form = new \XoopsThemeForm($title, 'form_select_import', $action, 'post', true);
79
        $form->setExtra('enctype="multipart/form-data"');
80
81
        $catCriteria = new \CriteriaCompo();
82
        $catCriteria->setSort('cat_id ASC, cat_name');
83
        $catCriteria->setOrder('ASC');
84
        $cat_select = new \XoopsFormSelect(_AM_XNEWSLETTER_IMPORT_PRESELECT_CAT, 'cat_id', '1');
85
        $cat_select->addOptionArray($this->helper->getHandler('Cat')->getList($catCriteria));
86
        $form->addElement($cat_select, false);
87
88
        $opt_import_type = new \XoopsFormRadio(_AM_XNEWSLETTER_IMPORT_PLUGINS_AVAIL, 'plugin', $plugin, '<br>');
89
        $opt_import_type->setExtra('onclick="document.forms.form_select_import.submit()"');
90
        $aFiles            = \XoopsLists::getFileListAsArray(XNEWSLETTER_ROOT_PATH . '/plugins/');
91
        $arrPlugin         = [];
0 ignored issues
show
Unused Code introduced by
$arrPlugin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        $currpluginhasform = 0;
93
        foreach ($aFiles as $file) {
94
            if ('.php' === mb_substr($file, mb_strlen($file) - 4, 4)) {
95
                $pluginName = str_replace('.php', '', $file);
96
                $pluginFile = XNEWSLETTER_ROOT_PATH . '/plugins/' . $pluginName . '.php';
97
                if (file_exists($pluginFile)) {
98
                    require_once $pluginFile;
99
                    $function    = 'xnewsletter_plugin_getinfo_' . $pluginName;
100
                    $arrPlugin   = $function();
101
                    $show_plugin = $this->tableExists($arrPlugin['tables'][0]);
102
                    if (true === $show_plugin && @is_array($arrPlugin['tables'][1])) {
103
                        $show_plugin = $this->tableExists($arrPlugin['tables'][1]);
104
                    }
105
106
                    if (true === $show_plugin) {
107
                        $label = "<img src='" . $arrPlugin['icon'] . "' title='" . $arrPlugin['descr'] . "' alt='" . $arrPlugin['descr'] . "' style='height:32px;margin-bottom:5px;margin-right:5px'>" . $arrPlugin['descr'];
108
                        $opt_import_type->addOption($arrPlugin['name'], $label);
109
                        $form->addElement(new \XoopsFormHidden('hasform_' . $pluginName, $arrPlugin['hasform']));
110
                        if ($plugin == $pluginName && 1 == $arrPlugin['hasform']) {
111
                            $currpluginhasform = 1;
112
                        }
113
                    }
114
                }
115
            }
116
        }
117
        $form->addElement($opt_import_type, false);
118
119
        //option, whether data should be shown for check or directly imported
120
        $check_after = new \XoopsFormRadio(_AM_XNEWSLETTER_IMPORT_AFTER_READ, 'action_after_read', $action_after_read, '<br>');
121
        $check_after->addOption(0, _AM_XNEWSLETTER_IMPORT_READ_IMPORT);
122
        $check_after->addOption(1, _AM_XNEWSLETTER_IMPORT_READ_CHECK);
123
        $check_after->setExtra('onclick="document.forms.form_select_import.submit()"');
124
        $form->addElement($check_after, false);
125
126
        //limit for import
127
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_IMPORT_CHECK_LIMIT, '100000'), false);
128
        if (0 == $action_after_read) {
129
            if ($limitcheck < 500 && $limitcheck > 0) {
130
                $limitcheck = 500;
131
            }
132
        } else {
133
            if ($limitcheck > 200) {
134
                $limitcheck = 200;
135
            }
136
        }
137
        $sel_limitcheck = new \XoopsFormSelect(_AM_XNEWSLETTER_IMPORT_CHECK_LIMIT_PACKAGE, 'limitcheck', $limitcheck);
138
        if (0 == $action_after_read) {
139
            $sel_limitcheck->addOption(0, _AM_XNEWSLETTER_IMPORT_NOLIMIT);
140
            $sel_limitcheck->addOption(500, 500);
141
            $sel_limitcheck->addOption(1000, 1000);
142
            $sel_limitcheck->addOption(10000, 10000);
143
            $sel_limitcheck->addOption(25000, 25000);
144
        } else {
145
            $limitOptions = [25, 50, 100, 200, 400];
146
            foreach ($limitOptions as $limitOption) {
147
                // check if limit options are compatible with php.ini 'max_input_vars' setting
148
                if ((0 == ini_get('max_input_vars')) || ((($limitOption * 7) + 4) < ini_get('max_input_vars'))) {
149
                    $sel_limitcheck->addOption($limitOption, $limitOption);
150
                }
151
            }
152
        }
153
        $form->addElement($sel_limitcheck, false);
154
155
        $skip               = 1 == $action_after_read ? 0 : 1;
156
        $skipcatsubscrexist = new \XoopsFormRadioYN(_AM_XNEWSLETTER_IMPORT_SKIP_EXISTING, 'skipcatsubscrexist', $skip);
157
        if (0 == $action_after_read) {
158
            $skipcatsubscrexist->setExtra('disabled="disabled"');
159
        }
160
        $form->addElement($skipcatsubscrexist, false);
161
162
        $form->addElement(new \XoopsFormHidden('op', 'default'));
163
        $buttonTray = new \XoopsFormElementTray('', '');
164
        if (1 == $currpluginhasform) {
165
            //show form for additional options
166
            $button1 = new \XoopsFormButton('', 'form_additional', _AM_XNEWSLETTER_IMPORT_CONTINUE, 'submit1');
167
            $button1->setExtra('onclick="document.getElementById(\'op\').value = \'form_additional\';document.forms.form_select_import.submit()"');
168
            $buttonTray->addElement($button1);
169
        } else {
170
            $button2 = new \XoopsFormButton('', 'searchdata', _AM_XNEWSLETTER_IMPORT_CONTINUE, 'submit2');
171
            $button2->setExtra('onclick="document.getElementById(\'op\').value = \'searchdata\';document.forms.form_select_import.submit()"');
172
            $buttonTray->addElement($button2);
173
        }
174
        $form->addElement($buttonTray);
175
176
        return $form;
177
    }
178
179
    /**
180
     * @param $tablename
181
     *
182
     * @return bool
183
     */
184
    private function tableExists($tablename)
185
    {
186
        if ('' == $tablename) {
187
            return true;
188
        }
189
        global $xoopsDB;
190
        $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
191
192
        return ($xoopsDB->getRowsNum($result) > 0);
193
    }
194
}
195