Completed
Pull Request — master (#27)
by Michael
01:42
created

Accounts::getForm()   F

Complexity

Conditions 14
Paths 512

Size

Total Lines 140

Duplication

Lines 52
Ratio 37.14 %

Importance

Changes 0
Metric Value
cc 14
nc 512
nop 1
dl 52
loc 140
rs 2.2222
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 Accounts
37
 */
38
class Accounts 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('accounts_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('accounts_type', XOBJ_DTYPE_INT, _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_MAIL, false);
50
        $this->initVar('accounts_name', XOBJ_DTYPE_TXTBOX, null, false, 100);
51
        $this->initVar('accounts_yourname', XOBJ_DTYPE_TXTBOX, null, false, 100);
52
        $this->initVar('accounts_yourmail', XOBJ_DTYPE_TXTBOX, null, false, 100);
53
        $this->initVar('accounts_username', XOBJ_DTYPE_TXTBOX, null, false, 100);
54
        $this->initVar('accounts_password', XOBJ_DTYPE_TXTBOX, null, false, 100);
55
        $this->initVar('accounts_server_in', XOBJ_DTYPE_TXTBOX, null, false, 100);
56
        $this->initVar('accounts_port_in', XOBJ_DTYPE_TXTBOX, null, false, 100);
57
        $this->initVar('accounts_securetype_in', XOBJ_DTYPE_TXTBOX, null, false, 20);
58
        $this->initVar('accounts_server_out', XOBJ_DTYPE_TXTBOX, null, false, 100);
59
        $this->initVar('accounts_port_out', XOBJ_DTYPE_TXTBOX, null, false, 100);
60
        $this->initVar('accounts_securetype_out', XOBJ_DTYPE_TXTAREA, null, false);
61
        $this->initVar('accounts_use_bmh', XOBJ_DTYPE_INT, null, false);
62
        $this->initVar('accounts_inbox', XOBJ_DTYPE_TXTBOX, null, false, 100);
63
        $this->initVar('accounts_hardbox', XOBJ_DTYPE_TXTBOX, null, false, 100);
64
        $this->initVar('accounts_movehard', XOBJ_DTYPE_INT, null, false);
65
        $this->initVar('accounts_softbox', XOBJ_DTYPE_TXTBOX, null, false, 100);
66
        $this->initVar('accounts_movesoft', XOBJ_DTYPE_INT, null, false);
67
        $this->initVar('accounts_default', XOBJ_DTYPE_INT, null, false); // boolean
68
        $this->initVar('accounts_submitter', XOBJ_DTYPE_INT, null, false);
69
        $this->initVar('accounts_created', XOBJ_DTYPE_INT, time(), false);
70
    }
71
72
    /**
73
     * @param bool $action
74
     *
75
     * @return \XoopsThemeForm
76
     */
77
    public function getForm($action = false)
78
    {
79
        global $xoopsDB;
80
81
        if (false === $action) {
82
            $action = $_SERVER['REQUEST_URI'];
83
        }
84
85
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_ACCOUNTS_ADD) : sprintf(_AM_XNEWSLETTER_ACCOUNTS_EDIT);
86
87
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
88
        $form = new \XoopsThemeForm($title, $this->helper->getModule()->getVar('dirname') . '_form', $action, 'post', true);
89
        $form->setExtra('enctype="multipart/form-data"');
90
91
        $default = $this->getVar('accounts_type');
92
93
        switch ($default) {
94
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_MAIL:
95 View Code Duplication
            default:
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...
96
                $dis_accounts_userpass     = true;
97
                $dis_accounts_server_in    = true;
98
                $dis_accounts_server_out   = true;
99
                $dis_accounts_use_bmh      = true;
100
                $dis_accounts_button_check = true;
101
                break;
102 View Code Duplication
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL:
0 ignored issues
show
Unused Code introduced by
case _XNEWSLETTER_ACCOUN...heck = true; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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...
103
                $dis_accounts_userpass     = false;
104
                $dis_accounts_server_in    = true;
105
                $dis_accounts_server_out   = false;
106
                $dis_accounts_use_bmh      = true;
107
                $dis_accounts_button_check = true;
108
                break;
109 View Code Duplication
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3:
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...
110
                $dis_accounts_userpass     = false;
111
                $dis_accounts_server_in    = false;
112
                $dis_accounts_server_out   = false;
113
                $dis_accounts_use_bmh      = true;
114
                $dis_accounts_button_check = false;
115
                break;
116
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP:
117 View Code Duplication
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL:
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...
118
                $dis_accounts_userpass     = false;
119
                $dis_accounts_server_in    = false;
120
                $dis_accounts_server_out   = false;
121
                $dis_accounts_use_bmh      = false;
122
                $dis_accounts_button_check = false;
123
                break;
124
        }
125
126
        $accstd_select = new \XoopsFormSelect(_AM_XNEWSLETTER_ACCOUNTS_TYPE, 'accounts_type', $this->getVar('accounts_type'));
127
        $accstd_select->setExtra('onchange="document.forms.' . $this->helper->getModule()->getVar('dirname') . '_form.submit()"');
128
        $accstd_select->addOption(_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_MAIL, _AM_XNEWSLETTER_ACCOUNTS_TYPE_PHPMAIL);
129
        $accstd_select->addOption(_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL, _AM_XNEWSLETTER_ACCOUNTS_TYPE_PHPSENDMAIL);
130
        $accstd_select->addOption(_XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3, _AM_XNEWSLETTER_ACCOUNTS_TYPE_POP3);
131
        $accstd_select->addOption(_XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP, _AM_XNEWSLETTER_ACCOUNTS_TYPE_SMTP);
132
        $accstd_select->addOption(_XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL, _AM_XNEWSLETTER_ACCOUNTS_TYPE_GMAIL);
133
        $form->addElement($accstd_select, true);
134
135
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_NAME, 'accounts_name', 50, 255, $this->getVar('accounts_name')), true);
136
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_YOURNAME, 'accounts_yourname', 50, 255, $this->getVar('accounts_yourname')), true);
137
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_YOURMAIL, 'accounts_yourmail', 50, 255, $this->getVar('accounts_yourmail')), true);
138
139
        $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_ACCOUNTS_DEFAULT, 'accounts_default', $this->getVar('accounts_default'), _YES, _NO), false);
140
141
        if (false === $dis_accounts_userpass) {
142
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_USERNAME, 'accounts_username', 50, 255, $this->getVar('accounts_username')), true);
143
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_PASSWORD, 'accounts_password', 50, 255, $this->getVar('accounts_password')), true);
144
        }
145
146 View Code Duplication
        if (false === $dis_accounts_server_in) {
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...
147
            $incomming_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_ACCOUNTS_INCOMING, '');
148
            $incomming_tray->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_SERVER_IN, 'accounts_server_in', 50, 255, $this->getVar('accounts_server_in')));
149
            $incomming_tray->addElement(new \XoopsFormText('<br>' . _AM_XNEWSLETTER_ACCOUNTS_PORT_IN, 'accounts_port_in', 50, 255, $this->getVar('accounts_port_in')));
150
            $formfield_securetype_in = new \XoopsFormSelect('<br>' . _AM_XNEWSLETTER_ACCOUNTS_SECURETYPE_IN, 'accounts_securetype_in', $this->getVar('accounts_securetype_in'));
151
            $formfield_securetype_in->addOption('', '');
152
            $formfield_securetype_in->addOption('notls', 'NOTLS / STARTTLS');
153
            $formfield_securetype_in->addOption('ssl', 'SSL');
154
            $formfield_securetype_in->addOption('tls', 'TLS');
155
            $incomming_tray->addElement($formfield_securetype_in);
156
            $form->addElement($incomming_tray);
157
        }
158
159 View Code Duplication
        if (false === $dis_accounts_server_out) {
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...
160
            $outcomming_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_ACCOUNTS_OUTGOING, '');
161
            $outcomming_tray->addElement(new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_SERVER_OUT, 'accounts_server_out', 50, 255, $this->getVar('accounts_server_out')));
162
            $outcomming_tray->addElement(new \XoopsFormText('<br>' . _AM_XNEWSLETTER_ACCOUNTS_PORT_OUT, 'accounts_port_out', 50, 255, $this->getVar('accounts_port_out')));
163
            $formfield_securetype_out = new \XoopsFormSelect('<br>' . _AM_XNEWSLETTER_ACCOUNTS_SECURETYPE_OUT, 'accounts_securetype_out', $this->getVar('accounts_securetype_out'));
164
            $formfield_securetype_out->addOption('', '');
165
            $formfield_securetype_out->addOption('notls', 'NOTLS / STARTTLS');
166
            $formfield_securetype_out->addOption('ssl', 'SSL');
167
            $formfield_securetype_out->addOption('tls', 'TLS');
168
            $outcomming_tray->addElement($formfield_securetype_out);
169
            $form->addElement($outcomming_tray);
170
        }
171
172
        if (false === $dis_accounts_use_bmh) {
173
            $form->addElement(new \XoopsFormLabel('', _AM_XNEWSLETTER_ACCOUNTS_BOUNCE_INFO));
174
175
            $formfield_use_bmh = new \XoopsFormRadioYN(_AM_XNEWSLETTER_ACCOUNTS_USE_BMH, 'accounts_use_bmh', $this->getVar('accounts_use_bmh'), _YES, _NO);
176
            $form->addElement($formfield_use_bmh, false);
177
178
            $formfield_inbox = new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_INBOX, 'accounts_inbox', 50, 255, $this->getVar('accounts_inbox'));
179
            $form->addElement($formfield_inbox, false);
180
181
            //Hardbox
182
            $hard_tray          = new \XoopsFormElementTray(_AM_XNEWSLETTER_BOUNCETYPE . ' ' . _XNEWSLETTER_BOUNCETYPE_HARD, '<br>');
183
            $formfield_movehard = new \XoopsFormRadioYN(_AM_XNEWSLETTER_ACCOUNTS_MOVEHARD, 'accounts_movehard', $this->getVar('accounts_movehard'), _YES, _NO);
184
            $hard_tray->addElement($formfield_movehard, false);
185
            $formfield_hardbox = new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_HARDBOX, 'accounts_hardbox', 50, 255, $this->getVar('accounts_hardbox'));
186
            $hard_tray->addElement($formfield_hardbox, false);
187
            $hard_tray->addElement(new \XoopsFormLabel('', _AM_XNEWSLETTER_ACCOUNTS_HARDBOX_DESC), false);
188
            $form->addElement($hard_tray, false);
189
190
            //Softbox
191
            $soft_tray          = new \XoopsFormElementTray(_AM_XNEWSLETTER_BOUNCETYPE . ' ' . _XNEWSLETTER_BOUNCETYPE_SOFT, '<br>');
192
            $formfield_movesoft = new \XoopsFormRadioYN(_AM_XNEWSLETTER_ACCOUNTS_MOVESOFT, 'accounts_movesoft', $this->getVar('accounts_movesoft'), _YES, _NO);
193
            $soft_tray->addElement($formfield_movesoft, false);
194
            $formfield_softbox = new \XoopsFormText(_AM_XNEWSLETTER_ACCOUNTS_SOFTBOX, 'accounts_softbox', 50, 255, $this->getVar('accounts_softbox'));
195
            $soft_tray->addElement($formfield_softbox, false);
196
            $soft_tray->addElement(new \XoopsFormLabel('', _AM_XNEWSLETTER_ACCOUNTS_HARDBOX_DESC), false);
197
            $form->addElement($soft_tray, false);
198
        }
199
        $time = $this->isNew() ? time() : $this->getVar('accounts_created');
200
        $form->addElement(new \XoopsFormHidden('accounts_submitter', $GLOBALS['xoopsUser']->uid()));
201
        $form->addElement(new \XoopsFormHidden('accounts_created', $time));
202
203
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
204
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_CREATED, formatTimestamp($time, 's')));
205
206
        $buttonTray = new \XoopsFormElementTray(' ', '&nbsp;&nbsp;');
207
        $buttonTray->addElement(new \XoopsFormHidden('op', 'save_accounts'));
208
        $buttonTray->addElement(new \XoopsFormButton('', 'post', _SUBMIT, 'submit'));
209
        if (false === $dis_accounts_button_check) {
210
            $button_check = new \XoopsFormButton('', 'save_and_check', _AM_XNEWSLETTER_SAVE_AND_CHECK, 'submit');
211
            $buttonTray->addElement($button_check);
212
        }
213
        $form->addElement($buttonTray, false);
214
215
        return $form;
216
    }
217
}
218