CategoryForm   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 212
Duplicated Lines 8.49 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 18
loc 212
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
F __construct() 18 202 21

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 namespace Xoopsmodules\randomquote\form;
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)
48
    {
49
        global $helper;
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' );
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');
98
            //$this->addElement( new \XoopsFormEditor(AM_RANDOMQUOTE_CATEGORY_DESCRIPTION, 'description', $editorOptions), false  );
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);
180
        //$selectPerm = new \XoopsFormCheckBox('', 'groupsRead[]', $this->targetObject->getGroupsRead());
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);
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);
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