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

Cat::getForm()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 156

Duplication

Lines 15
Ratio 9.62 %

Importance

Changes 0
Metric Value
cc 24
nc 124416
nop 1
dl 15
loc 156
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 Cat
37
 */
38
class Cat extends \XoopsObject
39
{
40
    /**
41
     * @access public
42
     */
43
    public $helper = null;
44
45
    //Constructor
46
47
    public function __construct()
48
    {
49
        $this->helper = Helper::getInstance();
50
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
51
        $this->initVar('cat_id', XOBJ_DTYPE_INT, null, false);
52
        $this->initVar('cat_name', XOBJ_DTYPE_TXTBOX, '', false, 100);
53
        $this->initVar('cat_info', XOBJ_DTYPE_TXTAREA, '', false);
54
        $this->initVar('cat_mailinglist', XOBJ_DTYPE_INT, 0, false);
55
        $this->initVar('cat_submitter', XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('cat_created', XOBJ_DTYPE_INT, time(), false);
57
        $this->initVar('dohtml', XOBJ_DTYPE_INT, false); // boolean
58
        $this->initVar('dosmiley', XOBJ_DTYPE_INT, true); // boolean
59
        $this->initVar('doxcode', XOBJ_DTYPE_INT, true); // boolean
60
        $this->initVar('doimage', XOBJ_DTYPE_INT, true); // boolean
61
        $this->initVar('dobr', XOBJ_DTYPE_INT, true); // boolean
62
    }
63
64
    /**
65
     * @param bool $action
66
     *
67
     * @return \XoopsThemeForm
68
     */
69
    public function getForm($action = false)
70
    {
71
        global $xoopsDB;
72
73
        $grouppermHandler = xoops_getHandler('groupperm');
74
75
        if (false === $action) {
76
            $action = $_SERVER['REQUEST_URI'];
77
        }
78
79
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_CAT_ADD) : sprintf(_AM_XNEWSLETTER_CAT_EDIT);
80
81
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
82
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
83
        $form->setExtra('enctype="multipart/form-data"');
84
85
        // cat_name
86
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_CAT_NAME, 'cat_name', 50, 255, $this->getVar('cat_name', 'e')), true);
87
88
        // cat_info
89
        $cat_info_dhtemtextarea = new \XoopsFormDhtmlTextArea(_AM_XNEWSLETTER_CAT_INFO, 'cat_info', $this->getVar('cat_info', 'e'), 10, 50);
90
        $cat_info_dhtemtextarea->setDescription(_AM_XNEWSLETTER_CAT_INFO_DESC);
91
        $form->addElement($cat_info_dhtemtextarea, false);
92
93
        // category: dohtml, dosmiley, doxcode, doimage, dobr
94
        $options_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_TEXTOPTIONS, ' ');
95
        $options_tray->setDescription(_AM_XNEWSLETTER_TEXTOPTIONS_DESC);
96
        $html_checkbox = new \XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml'));
97
        $html_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWHTML);
98
        $options_tray->addElement($html_checkbox);
99
        $smiley_checkbox = new \XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley'));
100
        $smiley_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWSMILEY);
101
        $options_tray->addElement($smiley_checkbox);
102
        $xcodes_checkbox = new \XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode'));
103
        $xcodes_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWXCODE);
104
        $options_tray->addElement($xcodes_checkbox);
105
        $noimages_checkbox = new \XoopsFormCheckBox('', 'doimage', $this->getVar('doimage'));
106
        $noimages_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWIMAGES);
107
        $options_tray->addElement($noimages_checkbox);
108
        $breaks_checkbox = new \XoopsFormCheckBox('', 'dobr', $this->getVar('dobr'));
109
        $breaks_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWBREAK);
110
        $options_tray->addElement($breaks_checkbox);
111
        $form->addElement($options_tray);
112
113
        // cat_gperms...
114
        $memberHandler = xoops_getHandler('member');
115
        $userGroups    = $memberHandler->getGroupList();
116
        // create admin checkbox
117
        foreach ($userGroups as $group_id => $group_name) {
118
            if (XOOPS_GROUP_ADMIN == $group_id) {
119
                $group_id_admin   = $group_id;
120
                $group_name_admin = $group_name;
121
            }
122
        }
123
        $select_perm_admin = new \XoopsFormCheckBox('', 'admin', XOOPS_GROUP_ADMIN);
124
        $select_perm_admin->addOption($group_id_admin, $group_name_admin);
0 ignored issues
show
Bug introduced by
The variable $group_id_admin 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...
Bug introduced by
The variable $group_name_admin 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...
125
        $select_perm_admin->setExtra("disabled='disabled'");
126
127
        // permission read cat
128
        $cat_gperms_read     = $grouppermHandler->getGroupIds('newsletter_read_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid());
129
        $arr_cat_gperms_read = $this->isNew() ? '0' : $cat_gperms_read;
130
        $perms_tray          = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_READ, '');
131
        // checkbox webmaster
132
        $perms_tray->addElement($select_perm_admin, false);
133
        // checkboxes other groups
134
        $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_read', $arr_cat_gperms_read);
135
        foreach ($userGroups as $group_id => $group_name) {
136
            if (XOOPS_GROUP_ADMIN != $group_id) {
137
                $select_perm->addOption($group_id, $group_name);
138
            }
139
        }
140
        $perms_tray->addElement($select_perm, false);
141
        $form->addElement($perms_tray, false);
142
        unset($perms_tray);
143
        unset($select_perm);
144
145
        // permission create cat
146
        $cat_gperms_create     = $grouppermHandler->getGroupIds('newsletter_create_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid());
147
        $arr_cat_gperms_create = $this->isNew() ? '0' : $cat_gperms_create;
148
        $perms_tray            = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_CREATE . _AM_XNEWSLETTER_CAT_GPERMS_CREATE_DESC, '');
149
        // checkbox webmaster
150
        $perms_tray->addElement($select_perm_admin, false);
151
        // checkboxes other groups
152
        $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_create', $arr_cat_gperms_create);
153 View Code Duplication
        foreach ($userGroups as $group_id => $group_name) {
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...
154
            if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) {
155
                $select_perm->addOption($group_id, $group_name);
156
            }
157
        }
158
        $perms_tray->addElement($select_perm, false);
159
        $form->addElement($perms_tray, false);
160
        unset($perms_tray);
161
        unset($select_perm);
162
163
        // permission admin cat
164
        $cat_gperms_admin     = $grouppermHandler->getGroupIds('newsletter_admin_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid());
165
        $arr_cat_gperms_admin = $this->isNew() ? '0' : $cat_gperms_admin;
166
        $perms_tray           = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_ADMIN . _AM_XNEWSLETTER_CAT_GPERMS_ADMIN_DESC, '');
167
        // checkbox webmaster
168
        $perms_tray->addElement($select_perm_admin, false);
169
        // checkboxes other groups
170
        $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_admin', $arr_cat_gperms_admin);
171 View Code Duplication
        foreach ($userGroups as $group_id => $group_name) {
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...
172
            if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) {
173
                $select_perm->addOption($group_id, $group_name);
174
            }
175
        }
176
        $perms_tray->addElement($select_perm, false);
177
        $form->addElement($perms_tray, false);
178
        unset($perms_tray);
179
        unset($select_perm);
180
181
        // permission list subscriber of this cat
182
        $cat_gperms_list      = $grouppermHandler->getGroupIds('newsletter_list_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid());
183
        $arr_cat_gperms_admin = $this->isNew() ? '0' : $cat_gperms_list;
184
185
        $perms_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_LIST, '');
186
        // checkbox webmaster
187
        $perms_tray->addElement($select_perm_admin, false);
188
        // checkboxes other groups
189
        $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_list', $arr_cat_gperms_admin);
190 View Code Duplication
        foreach ($userGroups as $group_id => $group_name) {
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...
191
            if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) {
192
                $select_perm->addOption($group_id, $group_name);
193
            }
194
        }
195
        $perms_tray->addElement($select_perm, false);
196
        $form->addElement($perms_tray, false);
197
        unset($perms_tray);
198
        unset($select_perm);
199
200
        // cat_mailinglist
201
        $cat_mailinglist     = $this->isNew() ? '0' : $this->getVar('cat_mailinglist');
202
        $mailinglistCriteria = new \CriteriaCompo();
203
        $mailinglistCriteria->setSort('mailinglist_id');
204
        $mailinglistCriteria->setOrder('ASC');
205
        $numrows_mailinglist = $this->helper->getHandler('Mailinglist')->getCount();
206
        if ($numrows_mailinglist > 0) {
207
            $opt_mailinglist = new \XoopsFormRadio(_AM_XNEWSLETTER_LETTER_MAILINGLIST, 'cat_mailinglist', $cat_mailinglist);
208
            $opt_mailinglist->addOption('0', _AM_XNEWSLETTER_LETTER_MAILINGLIST_NO);
209
            $mailinglistObjs = $this->helper->getHandler('Mailinglist')->getAll($mailinglistCriteria);
210
            foreach ($mailinglistObjs as $mailinglist_id => $mailinglistObj) {
211
                $opt_mailinglist->addOption($mailinglist_id, $mailinglistObj->getVar('mailinglist_name'));
212
            }
213
            $form->addElement($opt_mailinglist);
214
        }
215
216
        $time = $this->isNew() ? time() : $this->getVar('cat_created');
217
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
218
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_CREATED, formatTimestamp($time, 's')));
219
220
        $form->addElement(new \XoopsFormHidden('op', 'save_cat'));
221
        $form->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
222
223
        return $form;
224
    }
225
}
226