editCategory()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 54
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 43
nc 16
nop 1
dl 0
loc 54
rs 8.9208
c 0
b 0
f 0

How to fix   Long Method   

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 declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project (https://xoops.org)/
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, https://xoops.org/, http://jp.xoops.org/
16
 * @author       XOOPS Development Team
17
 */
18
19
use Xmf\Module\Helper\Cache;
20
use Xmf\Request;
21
use XoopsModules\Newbb\{
22
    Category,
23
    Helper,
24
    Utility,
25
    CategoryHandler
26
};
27
28
/** @var Helper $helper */
29
/** @var CategoryHandler $categoryHandler */
30
require_once __DIR__ . '/admin_header.php';
31
require_once \dirname(__DIR__) . '/include/functions.render.php';
32
33
xoops_cp_header();
34
35
$op     = Request::getCmd('op', Request::getCmd('op', '', 'POST'), 'GET'); //!empty($_GET['op'])? $_GET['op'] : (!empty($_POST['op'])?$_POST['op']:"");
36
$cat_id = Request::getInt('cat_id', Request::getInt('cat_id', 0, 'POST'), 'GET'); // (int)( !empty($_GET['cat_id']) ? $_GET['cat_id'] : @$_POST['cat_id'] );
37
38
//$categoryHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Category');
39
40
/**
41
 * newCategory()
42
 */
43
function newCategory(): void
44
{
45
    editCategory();
46
}
47
48
/**
49
 * editCategory()
50
 *
51
 * @param null|\XoopsObject $categoryObject
52
 * @internal param int $catid
53
 */
54
function editCategory(\XoopsObject $categoryObject = null): void
55
{
56
    global $xoopsModule;
57
    $categoryHandler = Helper::getInstance()->getHandler('Category');
58
    if (null === $categoryObject) {
59
        $categoryObject = $categoryHandler->create();
60
    }
61
    $groups_cat_access = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $groups_cat_access is dead and can be removed.
Loading history...
62
    require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
63
64
    if ($categoryObject->isNew()) {
65
        $sform = new \XoopsThemeForm(_AM_NEWBB_CREATENEWCATEGORY, 'op', xoops_getenv('SCRIPT_NAME'));
66
        $categoryObject->setVar('cat_title', '');
67
        $categoryObject->setVar('cat_image', '');
68
        $categoryObject->setVar('cat_description', '');
69
        $categoryObject->setVar('cat_order', 0);
70
        $categoryObject->setVar('cat_url', 'https://xoops.org/modules/newbb/ newBB Support');
71
    } else {
72
        $sform = new \XoopsThemeForm(_AM_NEWBB_EDITCATEGORY . ' ' . $categoryObject->getVar('cat_title'), 'op', xoops_getenv('SCRIPT_NAME'));
73
    }
74
75
    $sform->addElement(new \XoopsFormText(_AM_NEWBB_SETCATEGORYORDER, 'cat_order', 5, 10, $categoryObject->getVar('cat_order')), false);
0 ignored issues
show
Bug introduced by
It seems like $categoryObject->getVar('cat_order') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
    $sform->addElement(new \XoopsFormText(_AM_NEWBB_SETCATEGORYORDER, 'cat_order', 5, 10, /** @scrutinizer ignore-type */ $categoryObject->getVar('cat_order')), false);
Loading history...
76
    $sform->addElement(new \XoopsFormText(_AM_NEWBB_CATEGORY, 'title', 50, 80, $categoryObject->getVar('cat_title', 'E')), true);
77
    $sform->addElement(new \XoopsFormDhtmlTextArea(_AM_NEWBB_CATEGORYDESC, 'cat_description', $categoryObject->getVar('cat_description', 'E'), 10, 60), false);
0 ignored issues
show
Bug introduced by
It seems like $categoryObject->getVar('cat_description', 'E') can also be of type array and array; however, parameter $value of XoopsFormDhtmlTextArea::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
    $sform->addElement(new \XoopsFormDhtmlTextArea(_AM_NEWBB_CATEGORYDESC, 'cat_description', /** @scrutinizer ignore-type */ $categoryObject->getVar('cat_description', 'E'), 10, 60), false);
Loading history...
78
79
    $imgdir      = '/modules/' . $xoopsModule->getVar('dirname') . '/assets/images/category';
80
    $cat_image   = $categoryObject->getVar('cat_image');
81
    $cat_image   = empty($cat_image) ? 'assets/images/category/blank.gif' : $cat_image;
82
    $graph_array = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $imgdir . '/');
83
    array_unshift($graph_array, _NONE);
84
    $cat_image_select = new \XoopsFormSelect('', 'cat_image', $categoryObject->getVar('cat_image'));
85
    $cat_image_select->addOptionArray($graph_array);
86
    $cat_image_select->setExtra("onchange=\"showImgSelected('img', 'cat_image', '/" . $imgdir . "/', '', '" . XOOPS_URL . "')\"");
87
    $cat_image_tray = new \XoopsFormElementTray(_AM_NEWBB_IMAGE, '&nbsp;');
88
    $cat_image_tray->addElement($cat_image_select);
89
    $cat_image_tray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . $imgdir . '/' . $cat_image . " 'name='img' id='img' alt='' >"));
90
    $sform->addElement($cat_image_tray);
91
92
    $sform->addElement(new \XoopsFormText(_AM_NEWBB_SPONSORLINK, 'cat_url', 50, 80, $categoryObject->getVar('cat_url', 'E')), false);
93
    $sform->addElement(new \XoopsFormHidden('cat_id', $categoryObject->getVar('cat_id')));
0 ignored issues
show
Bug introduced by
It seems like $categoryObject->getVar('cat_id') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
    $sform->addElement(new \XoopsFormHidden('cat_id', /** @scrutinizer ignore-type */ $categoryObject->getVar('cat_id')));
Loading history...
94
95
    $buttonTray = new \XoopsFormElementTray('', '');
96
    $buttonTray->addElement(new \XoopsFormHidden('op', 'save'));
97
98
    $butt_save = new \XoopsFormButton('', '', _SUBMIT, 'submit');
99
    $butt_save->setExtra('onclick="this.form.elements.op.value=\'save\'"');
100
    $buttonTray->addElement($butt_save);
101
    if ($categoryObject->getVar('cat_id')) {
102
        $butt_delete = new \XoopsFormButton('', '', _CANCEL, 'submit');
103
        $butt_delete->setExtra('onclick="this.form.elements.op.value=\'default\'"');
104
        $buttonTray->addElement($butt_delete);
105
    }
106
    $sform->addElement($buttonTray);
107
    $sform->display();
108
}
109
110
switch ($op) {
111
    case 'mod':
112
        $categoryObject = ($cat_id > 0) ? $categoryHandler->get($cat_id) : $categoryHandler->create();
113
        //        if (!$newXoopsModuleGui) {
114
        //            //loadModuleAdminMenu(1, ( $cat_id > 0) ? _AM_NEWBB_EDITCATEGORY . $categoryObject->getVar('cat_title') : _AM_NEWBB_CREATENEWCATEGORY);
115
        //            echo "<legend style='font-weight: bold; color: #900;'>" . _AM_NEWBB_EDITCATEGORY . '</legend>';
116
        //        } else {
117
        $adminObject->displayNavigation(basename(__FILE__));
118
        //        }
119
        echo '<br>';
120
        editCategory($categoryObject);
121
        break;
122
    case 'del':
123
        if (!Request::getInt('confirm', 0, 'POST')) {
124
            xoops_confirm(['op' => 'del', 'cat_id' => Request::getInt('cat_id', 0, 'GET'), 'confirm' => 1], 'admin_cat_manager.php', _AM_NEWBB_WAYSYWTDTTAL);
125
            break;
126
        }
127
        $categoryObject = $categoryHandler->create(false);
128
        $categoryObject->setVar('cat_id', Request::getInt('cat_id', 0, 'POST'));
129
        $categoryHandler->delete($categoryObject);
130
131
        redirect_header('admin_cat_manager.php', 2, _AM_NEWBB_CATEGORYDELETED);
132
133
        break;
134
    case 'save':
135
        $cacheHelper = new Cache('newbb');
136
        $cacheHelper->delete('permission_category');
137
        if ($cat_id) {
138
            $categoryObject = $categoryHandler->get($cat_id);
139
            $message        = _AM_NEWBB_CATEGORYUPDATED;
140
        } else {
141
            $categoryObject = $categoryHandler->create();
142
            $message        = _AM_NEWBB_CATEGORYCREATED;
143
        }
144
145
        $categoryObject->setVar('cat_title', Request::getString('title', '', 'POST'));
146
        $categoryObject->setVar('cat_image', Request::getString('cat_image', '', 'POST'));
147
        $categoryObject->setVar('cat_order', Request::getInt('cat_order', 0, 'POST'));
148
        $categoryObject->setVar('cat_description', Request::getText('cat_description', '', 'POST'));
149
        $categoryObject->setVar('cat_url', Request::getString('cat_url', '', 'POST'));
150
151
        $cat_isNew = $categoryObject->isNew();
152
        if (!$categoryHandler->insert($categoryObject)) {
153
            $message = _AM_NEWBB_DATABASEERROR;
154
        }
155
        /** @var Category $categoryObject */
156
        if ($cat_isNew && ($cat_id == $categoryObject->getVar('cat_id'))) {
157
            $categoryHandler->applyPermissionTemplate($categoryObject);
158
        }
159
        redirect_header('admin_cat_manager.php', 2, $message);
160
        break;
161
    default:
162
        if (!$categories = $categoryHandler->getByPermission('all')) {
163
            $adminObject->addItemButton(_AM_NEWBB_CREATENEWCATEGORY, 'admin_cat_manager.php?op=mod', $icon = 'add');
164
            $adminObject->displayButton('left');
165
166
            echo '<br>';
167
            newCategory();
168
            break;
169
        }
170
        $adminObject->displayNavigation(basename(__FILE__));
171
        $adminObject->addItemButton(_AM_NEWBB_CREATENEWCATEGORY, 'admin_cat_manager.php?op=mod', $icon = 'add');
172
        $adminObject->displayButton('left');
173
174
        echo "<table class='outer' style='border-collapse: separate; border-spacing: 1px; width: 100%;'>" . "<tr><td class='odd'>";
175
        echo "<table class='outer' style='border: 0; padding: 4px; border-collapse: separate; border-spacing: 1px; width: 100%;'>";
176
        echo "<tr style='text-align:center;'>";
177
        echo "<th style='text-align:left;' class='bg3'>" . _AM_NEWBB_CATEGORY1 . '</th>';
178
        echo "<th class='bg3' width='10%'>" . _AM_NEWBB_EDIT . '</th>';
179
        echo "<th class='bg3' width='10%'>" . _AM_NEWBB_DELETE . '</th>';
180
        echo '</tr>';
181
182
        /** @var XoopsModules\Newbb\Category $onecat */
183
        foreach ($categories as $key => $onecat) {
184
            $cat_edit_link  = '<a href="admin_cat_manager.php?op=mod&cat_id=' . $onecat->getVar('cat_id') . '">' . newbbDisplayImage('admin_edit', _EDIT) . '</a>';
185
            $cat_del_link   = '<a href="admin_cat_manager.php?op=del&cat_id=' . $onecat->getVar('cat_id') . '">' . newbbDisplayImage('admin_delete', _DELETE) . '</a>';
186
            $cat_title_link = '<a href="' . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/index.php?cat=' . $onecat->getVar('cat_id') . '">' . $onecat->getVar('cat_title') . '</a>';
187
188
            echo "<tr class='odd' style='text-align:left;'>";
189
            echo '<td>' . $cat_title_link . '</td>';
190
            echo "<td style='text-align:center;'>" . $cat_edit_link . '</td>';
191
            echo "<td style='text-align:center;'>" . $cat_del_link . '</td>';
192
            echo '</tr>';
193
        }
194
        echo '</table>';
195
        echo '</td></tr></table>';
196
        echo '<fieldset>';
197
        echo '<legend>&nbsp;' . _MI_NEWBB_ADMENU_CATEGORY . '&nbsp;</legend>';
198
        echo _AM_NEWBB_HELP_CATEGORY_TAB;
199
        echo '<br>' . newbbDisplayImage('admin_edit', _EDIT) . '&nbsp;-&nbsp;' . _EDIT;
200
        echo '<br>' . newbbDisplayImage('admin_delete', _DELETE) . '&nbsp;-&nbsp;' . _DELETE;
201
        echo '</fieldset>';
202
        break;
203
}
204
205
//$cacheHelper->delete('permission_category');
206
Utility::cleanCache();
207
208
require_once __DIR__ . '/admin_footer.php';
209