Completed
Push — master ( 171474...8d265e )
by Michael
01:31
created

CategoryForm::__construct()   F

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 202
Code Lines 125

Duplication

Lines 18
Ratio 8.91 %

Importance

Changes 0
Metric Value
cc 21
eloc 125
nc 93312
nop 1
dl 18
loc 202
rs 2
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 namespace Xoopsmodules\randomquote\form;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 38 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
/**
13
 * Module: randomquote
14
 *
15
 * @category        Module
16
 * @package         randomquote
17
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
18
 * @copyright       {@link https://xoops.org/ XOOPS Project}
19
 * @license         GPL 2.0 or later
20
 * @link            https://xoops.org/
21
 * @since           1.0.0
22
 */
23
24
use Xmf\Request;
25
use Xoopsmodules\randomquote;
26
27
require_once __DIR__ . '/../../include/common.php';
28
29
$moduleDirName = basename(dirname(dirname(__DIR__)));
30
$helper        = randomquote\Helper::getInstance();
31
$permHelper    = new \Xmf\Module\Helper\Permission();
32
33
xoops_load('XoopsFormLoader');
34
35
/**
36
 * Class CategoryForm
37
 */
38
class CategoryForm extends \XoopsThemeForm
39
{
40
    public $targetObject;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param $target
46
     */
47
    public function __construct($target)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49
        global $helper;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
50
        $this->targetObject = $target;
51
52
        $title = $this->targetObject->isNew() ? sprintf(AM_RANDOMQUOTE_CATEGORY_ADD) : sprintf(AM_RANDOMQUOTE_CATEGORY_EDIT);
53
        parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true);
54
        $this->setExtra('enctype="multipart/form-data"');
55
56
        //include ID field, it's needed so the module knows if it is a new form or an edited form
57
58
        $hidden = new \XoopsFormHidden('id', $this->targetObject->getVar('id'));
59
        $this->addElement($hidden);
60
        unset($hidden);
61
62
        // Id
63
        $this->addElement(new \XoopsFormLabel(AM_RANDOMQUOTE_CATEGORY_ID, $this->targetObject->getVar('id'), 'id'));
64
        // Pid
65
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
66
        //$categoryHandler = xoops_getModuleHandler('category', 'randomquote' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
68
        /** @var \XoopsPersistableObjectHandler $Handler */
69
        $categoryHandler = new randomquote\CategoryHandler($db);
70
71
        $criteria      = new \CriteriaCompo();
72
        $categoryArray = $categoryHandler->getObjects($criteria);
73
        if ($categoryArray) {
74
75
            $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid');
76
77
            if (randomquote\Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) {
78
                $categoryPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pid'), true, 0, '', AM_RANDOMQUOTE_CATEGORY_PID);
79
                $this->addElement($categoryPid);
80
            } else {
81
                $categoryPid = $categoryTree->makeSelBox('pid', 'title', '--', $this->targetObject->getVar('pid', 'e'), true);
82
                $this->addElement(new \XoopsFormLabel (AM_RANDOMQUOTE_CATEGORY_PID, $categoryPid));
83
            }
84
85
        }
86
        // Title
87
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_CATEGORY_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')), false);
88
        // Description
89 View Code Duplication
        if (class_exists('XoopsFormEditor')) {
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...
90
            $editorOptions           = [];
91
            $editorOptions['name']   = 'description';
92
            $editorOptions['value']  = $this->targetObject->getVar('description', 'e');
93
            $editorOptions['rows']   = 5;
94
            $editorOptions['cols']   = 40;
95
            $editorOptions['width']  = '100%';
96
            $editorOptions['height'] = '400px';
97
            //$editorOptions['editor'] = xoops_getModuleOption('randomquote_editor', 'randomquote');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
            //$this->addElement( new \XoopsFormEditor(AM_RANDOMQUOTE_CATEGORY_DESCRIPTION, 'description', $editorOptions), false  );
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
            if ($helper->isUserAdmin()) {
100
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_CATEGORY_DESCRIPTION, $helper->getConfig('randomquoteEditorAdmin'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
101
            } else {
102
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_CATEGORY_DESCRIPTION, $helper->getConfig('randomquoteEditorUser'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
103
            }
104
        } else {
105
            $descEditor = new \XoopsFormDhtmlTextArea(AM_RANDOMQUOTE_CATEGORY_DESCRIPTION, 'description', $this->targetObject->getVar('description', 'e'), '100%', '100%');
106
        }
107
        $this->addElement($descEditor);
108
        // Image
109
        $image = $this->targetObject->getVar('image') ?: 'blank.png';
110
111
        $uploadDir   = '/uploads/randomquote/images/';
112
        $imgtray     = new \XoopsFormElementTray(AM_RANDOMQUOTE_CATEGORY_IMAGE, '<br>');
113
        $imgpath     = sprintf(AM_RANDOMQUOTE_FORMIMAGE_PATH, $uploadDir);
114
        $imageselect = new \XoopsFormSelect($imgpath, 'image', $image);
115
        $imageArray  = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
116
        foreach ($imageArray as $image) {
117
            $imageselect->addOption("$image", $image);
118
        }
119
        $imageselect->setExtra("onchange='showImgSelected(\"image_image\", \"image\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
120
        $imgtray->addElement($imageselect);
121
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $image . "' name='image_image' id='image_image' alt='' />"));
122
        $fileseltray = new \XoopsFormElementTray('', '<br>');
123
        $fileseltray->addElement(new \XoopsFormFile(AM_RANDOMQUOTE_FORMUPLOAD, 'image', xoops_getModuleOption('maxsize')));
124
        $fileseltray->addElement(new \XoopsFormLabel(''));
125
        $imgtray->addElement($fileseltray);
126
        $this->addElement($imgtray);
127
        // Weight
128
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_CATEGORY_WEIGHT, 'weight', 50, 255, $this->targetObject->getVar('weight')), false);
129
        // Color
130
        $this->addElement(new \XoopsFormColorPicker(AM_RANDOMQUOTE_CATEGORY_COLOR, 'color', $this->targetObject->getVar('color')), false);
131
        // Online
132
        $online       = $this->targetObject->isNew() ? 0 : $this->targetObject->getVar('online');
133
        $check_online = new \XoopsFormCheckBox(AM_RANDOMQUOTE_CATEGORY_ONLINE, 'online', $online);
134
        $check_online->addOption(1, ' ');
135
        $this->addElement($check_online);
136
137
        //permissions
138
        /** @var \XoopsMemberHandler $memberHandler */
139
        $memberHandler = xoops_getHandler('member');
140
        $groupList     = $memberHandler->getGroupList();
141
        /** @var \XoopsGroupPermHandler $gpermHandler */
142
        $gpermHandler = xoops_getHandler('groupperm');
143
        $fullList     = array_keys($groupList);
0 ignored issues
show
Unused Code introduced by
$fullList 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...
144
145
        //========================================================================
146
147
        $mid            = $GLOBALS['xoopsModule']->mid();
148
        $groupIdAdmin   = 0;
149
        $groupNameAdmin = '';
150
151
        // create admin checkbox
152
        foreach ($groupList as $groupId => $groupName) {
153
            if (XOOPS_GROUP_ADMIN == $groupId) {
154
                $groupIdAdmin   = $groupId;
155
                $groupNameAdmin = $groupName;
156
            }
157
        }
158
159
        $selectPermAdmin = new \XoopsFormCheckBox('', 'admin', XOOPS_GROUP_ADMIN);
160
        $selectPermAdmin->addOption($groupIdAdmin, $groupNameAdmin);
161
        $selectPermAdmin->setExtra("disabled='disabled'"); //comment it out, if you want to allow to remove permissions for the admin 
162
163
        // ********************************************************
164
        // permission view items
165
        $cat_gperms_read     = $gpermHandler->getGroupIds('randomquote_view', $this->targetObject->getVar('id'), $mid);
166
        $arr_cat_gperms_read = $this->targetObject->isNew() ? '0' : $cat_gperms_read;
167
168
        $permsTray = new \XoopsFormElementTray(AM_RANDOMQUOTE_PERMISSIONS_VIEW, '');
169
170
        $selectAllReadCheckbox = new \XoopsFormCheckBox('', 'adminbox1', 1);
171
        $selectAllReadCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
172
        $selectAllReadCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox1\" , \"groupsRead[]\");' ");
173
        $selectAllReadCheckbox->setClass('xo-checkall');
174
        $permsTray->addElement($selectAllReadCheckbox);
175
176
        // checkbox webmaster
177
        $permsTray->addElement($selectPermAdmin, false);
178
        // checkboxes other groups
179
        //$selectPerm = new \XoopsFormCheckBox('', 'cat_gperms_read', $arr_cat_gperms_read);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
180
        //$selectPerm = new \XoopsFormCheckBox('', 'groupsRead[]', $this->targetObject->getGroupsRead());
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
181
        $selectPerm = new \XoopsFormCheckBox('', 'groupsRead[]', $arr_cat_gperms_read);
182
        foreach ($groupList as $groupId => $groupName) {
183
            if (XOOPS_GROUP_ADMIN != $groupId) {
184
                $selectPerm->addOption($groupId, $groupName);
185
            }
186
        }
187
        $permsTray->addElement($selectPerm, false);
188
        $this->addElement($permsTray, false);
189
        unset($permsTray, $selectPerm);
190
191
        // ********************************************************
192
        // permission submit item
193
        $cat_gperms_create     = $gpermHandler->getGroupIds('randomquote_submit', $this->targetObject->getVar('id'), $mid);
194
        $arr_cat_gperms_create = $this->targetObject->isNew() ? '0' : $cat_gperms_create;
195
196
        $permsTray = new \XoopsFormElementTray(AM_RANDOMQUOTE_PERMISSIONS_SUBMIT, '');
197
198
        $selectAllSubmitCheckbox = new \XoopsFormCheckBox('', 'adminbox2', 1);
199
        $selectAllSubmitCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
200
        $selectAllSubmitCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox2\" , \"groupsSubmit[]\");' ");
201
        $selectAllSubmitCheckbox->setClass('xo-checkall');
202
        $permsTray->addElement($selectAllSubmitCheckbox);
203
204
        // checkbox webmaster
205
        $permsTray->addElement($selectPermAdmin, false);
206
        // checkboxes other groups
207
        //$selectPerm = new \XoopsFormCheckBox('', 'cat_gperms_create', $arr_cat_gperms_create);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
        $selectPerm = new \XoopsFormCheckBox('', 'groupsSubmit[]', $arr_cat_gperms_create);
209
        foreach ($groupList as $groupId => $groupName) {
210
            if (XOOPS_GROUP_ADMIN != $groupId) {
211
                $selectPerm->addOption($groupId, $groupName);
212
            }
213
        }
214
        $permsTray->addElement($selectPerm, false);
215
        $this->addElement($permsTray, false);
216
        unset($permsTray, $selectPerm);
217
218
        // ********************************************************
219
        // permission approve items
220
        $cat_gperms_admin     = $gpermHandler->getGroupIds('randomquote_approve', $this->targetObject->getVar('id'), $mid);
221
        $arr_cat_gperms_admin = $this->targetObject->isNew() ? '0' : $cat_gperms_admin;
222
223
        $permsTray = new \XoopsFormElementTray(AM_RANDOMQUOTE_PERMISSIONS_APPROVE, '');
224
225
        $selectAllModerateCheckbox = new \XoopsFormCheckBox('', 'adminbox3', 1);
226
        $selectAllModerateCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
227
        $selectAllModerateCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox3\" , \"groupsModeration[]\");' ");
228
        $selectAllModerateCheckbox->setClass('xo-checkall');
229
        $permsTray->addElement($selectAllModerateCheckbox);
230
231
        // checkbox webmaster
232
        $permsTray->addElement($selectPermAdmin, false);
233
        // checkboxes other groups
234
        //$selectPerm = new \XoopsFormCheckBox('', 'cat_gperms_admin', $arr_cat_gperms_admin);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
235
        $selectPerm = new \XoopsFormCheckBox('', 'groupsModeration[]', $arr_cat_gperms_admin);
236
        foreach ($groupList as $groupId => $groupName) {
237
            if (XOOPS_GROUP_ADMIN != $groupId && XOOPS_GROUP_ANONYMOUS != $groupId) {
238
                $selectPerm->addOption($groupId, $groupName);
239
            }
240
        }
241
        $permsTray->addElement($selectPerm, false);
242
        $this->addElement($permsTray, false);
243
        unset($permsTray, $selectPerm);
244
245
        //=========================================================================
246
        $this->addElement(new \XoopsFormHidden('op', 'save'));
247
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
248
    }
249
}
250