EquipmentEquipmentForm   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 167
Duplicated Lines 2.99 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 5
loc 167
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F __construct() 5 157 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
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
/**
14
 * Module: Equipment
15
 *
16
 * @category        Module
17
 * @package         equipment
18
 * @author          XOOPS Development Team <[email protected]> - <http://xoops.org>
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GPL 2.0 or later
21
 * @link            https://xoops.org/
22
 * @since           1.0.0
23
 */
24
25
use Xmf\Request;
26
use Xmf\Module\Helper;
27
use Xmf\Module\Helper\Permission;
28
29
require_once __DIR__ . '/../../include/config.php';
30
31
$moduleDirName = basename(dirname(dirname(__DIR__)));
32
$moduleHelper  = Xmf\Module\Helper::getHelper($moduleDirName);
33
$permHelper    = new Permission($moduleDirName);
34
35
xoops_load('XoopsFormLoader');
36
37
/**
38
 * Class EquipmentEquipmentForm
39
 */
40
class EquipmentEquipmentForm extends XoopsThemeForm
41
{
42
    public $targetObject;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param $target
48
     */
49
    public function __construct($target)
50
    {
51
        global $moduleHelper;
52
        $this->targetObject = $target;
53
54
        $title = $this->targetObject->isNew() ? sprintf(AM_EQUIPMENT_EQUIPMENT_ADD) : sprintf(AM_EQUIPMENT_EQUIPMENT_EDIT);
55
        parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true);
56
        $this->setExtra('enctype="multipart/form-data"');
57
58
        //include ID field, it's needed so the module knows if it is a new form or an edited form
59
60
        $hidden = new XoopsFormHidden('id', $this->targetObject->getVar('id'));
61
        $this->addElement($hidden);
62
        unset($hidden);
63
64
        // Id
65
        $this->addElement(new XoopsFormLabel(AM_EQUIPMENT_EQUIPMENT_ID, $this->targetObject->getVar('id'), 'id'));
66
        // Owner
67
        $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_OWNER, 'owner', 50, 255, $this->targetObject->getVar('owner')), false);
68
        // Name
69
        $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_NAME, 'name', 50, 255, $this->targetObject->getVar('name')), false);
70
        // Amount
71
        $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_AMOUNT, 'amount', 50, 255, $this->targetObject->getVar('amount')), false);
72
        // Total
73
        $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_TOTAL, 'total', 50, 255, $this->targetObject->getVar('total')), false);
74
        // Image
75
        $image = $this->targetObject->getVar('image') ?: 'blank.png';
76
77
        $uploadDir   = '/uploads/equipment/images/';
78
        $imgtray     = new XoopsFormElementTray(AM_EQUIPMENT_EQUIPMENT_IMAGE, '<br>');
79
        $imgpath     = sprintf(AM_EQUIPMENT_FORMIMAGE_PATH, $uploadDir);
80
        $imageselect = new XoopsFormSelect($imgpath, 'image', $image);
81
        $imageArray  = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
82
        foreach ($imageArray as $image) {
83
            $imageselect->addOption("$image", $image);
84
        }
85
        $imageselect->setExtra("onchange='showImgSelected(\"image_image\", \"image\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
86
        $imgtray->addElement($imageselect);
87
        $imgtray->addElement(new XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $image . "' name='image_image' id='image_image' alt='' />"));
88
        $fileseltray = new XoopsFormElementTray('', '<br>');
89
        $fileseltray->addElement(new XoopsFormFile(AM_EQUIPMENT_FORMUPLOAD, 'image', xoops_getModuleOption('maxsize')));
90
        $fileseltray->addElement(new XoopsFormLabel(''));
91
        $imgtray->addElement($fileseltray);
92
        $this->addElement($imgtray);
93
94
        //permissions
95
        /** @var XoopsMemberHandler $memberHandler */
96
        $memberHandler = xoops_getHandler('member');
97
        $groupList     = $memberHandler->getGroupList();
98
        /** @var XoopsGroupPermHandler $gpermHandler */
99
        $gpermHandler = xoops_getHandler('groupperm');
100
        $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...
101
102
        //========================================================================
103
104
        $mid            = $GLOBALS['xoopsModule']->mid();
105
        $groupIdAdmin   = 0;
106
        $groupNameAdmin = '';
107
108
        // create admin checkbox
109
        foreach ($groupList as $groupId => $groupName) {
110
            if ($groupId == XOOPS_GROUP_ADMIN) {
111
                $groupIdAdmin   = $groupId;
112
                $groupNameAdmin = $groupName;
113
            }
114
        }
115
116
        $selectPermAdmin = new XoopsFormCheckBox('', 'admin', XOOPS_GROUP_ADMIN);
117
        $selectPermAdmin->addOption($groupIdAdmin, $groupNameAdmin);
118
        $selectPermAdmin->setExtra("disabled='disabled'"); //comment it out, if you want to allow to remove permissions for the admin 
119
120
        // ********************************************************
121
        // permission view items
122
        $cat_gperms_read     = $gpermHandler->getGroupIds('equipment_view', $this->targetObject->getVar('id'), $mid);
123
        $arr_cat_gperms_read = $this->targetObject->isNew() ? '0' : $cat_gperms_read;
124
125
        $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_VIEW, '');
126
127
        $selectAllReadCheckbox = new XoopsFormCheckBox('', 'adminbox1', 1);
128
        $selectAllReadCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
129
        $selectAllReadCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox1\" , \"groupsRead[]\");' ");
130
        $selectAllReadCheckbox->setClass('xo-checkall');
131
        $permsTray->addElement($selectAllReadCheckbox);
132
133
        // checkbox webmaster
134
        $permsTray->addElement($selectPermAdmin, false);
135
        // checkboxes other groups
136
        //$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...
137
        //$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...
138
        $selectPerm = new XoopsFormCheckBox('', 'groupsRead[]', $arr_cat_gperms_read);
139
        foreach ($groupList as $groupId => $groupName) {
140
            if ($groupId != XOOPS_GROUP_ADMIN) {
141
                $selectPerm->addOption($groupId, $groupName);
142
            }
143
        }
144
        $permsTray->addElement($selectPerm, false);
145
        $this->addElement($permsTray, false);
146
        unset($permsTray, $selectPerm);
147
148
        // ********************************************************
149
        // permission submit item
150
        $cat_gperms_create     = $gpermHandler->getGroupIds('equipment_submit', $this->targetObject->getVar('id'), $mid);
151
        $arr_cat_gperms_create = $this->targetObject->isNew() ? '0' : $cat_gperms_create;
152
153
        $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_SUBMIT, '');
154
155
        $selectAllSubmitCheckbox = new XoopsFormCheckBox('', 'adminbox2', 1);
156
        $selectAllSubmitCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
157
        $selectAllSubmitCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox2\" , \"groupsSubmit[]\");' ");
158
        $selectAllSubmitCheckbox->setClass('xo-checkall');
159
        $permsTray->addElement($selectAllSubmitCheckbox);
160
161
        // checkbox webmaster
162
        $permsTray->addElement($selectPermAdmin, false);
163
        // checkboxes other groups
164
        //$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...
165
        $selectPerm = new XoopsFormCheckBox('', 'groupsSubmit[]', $arr_cat_gperms_create);
166
        foreach ($groupList as $groupId => $groupName) {
167
            if ($groupId != XOOPS_GROUP_ADMIN) {
168
                $selectPerm->addOption($groupId, $groupName);
169
            }
170
        }
171
        $permsTray->addElement($selectPerm, false);
172
        $this->addElement($permsTray, false);
173
        unset($permsTray, $selectPerm);
174
175
        // ********************************************************
176
        // permission approve items
177
        $cat_gperms_admin     = $gpermHandler->getGroupIds('equipment_approve', $this->targetObject->getVar('id'), $mid);
178
        $arr_cat_gperms_admin = $this->targetObject->isNew() ? '0' : $cat_gperms_admin;
179
180
        $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_APPROVE, '');
181
182
        $selectAllModerateCheckbox = new XoopsFormCheckBox('', 'adminbox3', 1);
183
        $selectAllModerateCheckbox->addOption('allbox', _AM_SYSTEM_ALL);
184
        $selectAllModerateCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox3\" , \"groupsModeration[]\");' ");
185
        $selectAllModerateCheckbox->setClass('xo-checkall');
186
        $permsTray->addElement($selectAllModerateCheckbox);
187
188
        // checkbox webmaster
189
        $permsTray->addElement($selectPermAdmin, false);
190
        // checkboxes other groups
191
        //$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...
192
        $selectPerm = new XoopsFormCheckBox('', 'groupsModeration[]', $arr_cat_gperms_admin);
193 View Code Duplication
        foreach ($groupList as $groupId => $groupName) {
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...
194
            if ($groupId != XOOPS_GROUP_ADMIN && $groupId != XOOPS_GROUP_ANONYMOUS) {
195
                $selectPerm->addOption($groupId, $groupName);
196
            }
197
        }
198
        $permsTray->addElement($selectPerm, false);
199
        $this->addElement($permsTray, false);
200
        unset($permsTray, $selectPerm);
201
202
        //=========================================================================
203
        $this->addElement(new XoopsFormHidden('op', 'save'));
204
        $this->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
205
    }
206
}
207