Completed
Push — master ( 1aa479...f472f3 )
by Michael
02:12
created

category.php ➔ createCat()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 175
Code Lines 137

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 137
nc 64
nop 1
dl 0
loc 175
rs 5.2676
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
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 33 and the first side effect is on line 14.

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
 * Module: WF-Links
5
 * Version: v1.0.3
6
 * Release Date: 21 June 2005
7
 * Developer: John N
8
 * Team: WF-Projects
9
 * Licence: GNU
10
 */
11
12
use Xmf\Request;
13
14
require_once __DIR__ . '/admin_header.php';
15
require_once XOOPS_ROOT_PATH . '/class/xoopsform/grouppermform.php';
16
17
$op = '';
18
19
if (isset($_POST)) {
20
    foreach ($_POST as $k => $v) {
21
        ${$k} = $v;
22
    }
23
}
24
if (isset($_GET)) {
25
    foreach ($_GET as $k => $v) {
26
        ${$k} = $v;
27
    }
28
}
29
30
/**
31
 * @param int $cid
32
 */
33
function createCat($cid = 0)
34
{
35
    require_once __DIR__ . '/../class/wfllists.php';
36
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
37
38
    global $xoopsDB, $wfmyts, $xoopsModuleConfig, $totalcats, $xoopsModule;
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...
39
40
    $lid          = 0;
0 ignored issues
show
Unused Code introduced by
$lid 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...
41
    $title        = '';
42
    $imgurl       = '';
43
    $description  = '';
44
    $pid          = '';
0 ignored issues
show
Unused Code introduced by
$pid 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...
45
    $weight       = 0;
46
    $nohtml       = 0;
47
    $nosmiley     = 0;
48
    $noxcodes     = 0;
49
    $noimages     = 0;
50
    $nobreak      = 1;
51
    $spotlighttop = 0;
0 ignored issues
show
Unused Code introduced by
$spotlighttop 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...
52
    $spotlighthis = 0;
0 ignored issues
show
Unused Code introduced by
$spotlighthis 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...
53
    $client_id    = 0;
54
    $banner_id    = 0;
55
    $heading      = _AM_WFL_CCATEGORY_CREATENEW;
56
    $totalcats    = WflinksUtility::getTotalCategory();
57
58
    if ($cid) {
59
        $sql          = 'SELECT * FROM ' . $xoopsDB->prefix('wflinks_cat') . " WHERE cid=$cid";
60
        $cat_arr      = $xoopsDB->fetchArray($xoopsDB->query($sql));
61
        $title        = $wfmyts->htmlSpecialChars($cat_arr['title']);
62
        $imgurl       = $wfmyts->htmlSpecialChars($cat_arr['imgurl']);
63
        $description  = $wfmyts->htmlSpecialChars($cat_arr['description']);
64
        $nohtml       = (int)$cat_arr['nohtml'];
65
        $nosmiley     = (int)$cat_arr['nosmiley'];
66
        $noxcodes     = (int)$cat_arr['noxcodes'];
67
        $noimages     = (int)$cat_arr['noimages'];
68
        $nobreak      = (int)$cat_arr['nobreak'];
69
        $spotlighthis = (int)$cat_arr['spotlighthis'];
0 ignored issues
show
Unused Code introduced by
$spotlighthis 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...
70
        $spotlighttop = (int)$cat_arr['spotlighttop'];
0 ignored issues
show
Unused Code introduced by
$spotlighttop 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...
71
        $weight       = $cat_arr['weight'];
72
        $client_id    = $cat_arr['client_id'];
73
        $banner_id    = $cat_arr['banner_id'];
74
        $heading      = _AM_WFL_CCATEGORY_MODIFY;
75
76
        $gpermHandler = xoops_getHandler('groupperm');
77
        $groups       = $gpermHandler->getGroupIds('WFLinkCatPerm', $cid, $xoopsModule->getVar('mid'));
78
        $groups       = $groups;
0 ignored issues
show
Bug introduced by
Why assign $groups to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
79
    } else {
80
        $groups = true;
0 ignored issues
show
Unused Code introduced by
$groups 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...
81
    }
82
83
    $sform = new XoopsThemeForm($heading, 'op', xoops_getenv('PHP_SELF'), 'post', true);
84
    $sform->setExtra('enctype="multipart/form-data"');
85
86
    $sform->addElement(new XoopsFormText(_AM_WFL_FCATEGORY_TITLE, 'title', 50, 80, $title), true);
87
    $sform->addElement(new XoopsFormText(_AM_WFL_FCATEGORY_WEIGHT, 'weight', 10, 80, $weight), false);
88
89
    if ($totalcats > 0 && $cid) {
90
        $mytreechose = new WflinksXoopsTree($xoopsDB->prefix('wflinks_cat'), 'cid', 'pid');
91
        ob_start();
92
        $mytreechose->makeMySelBox('title', 'title', $cat_arr['pid'], 1, 'pid');
0 ignored issues
show
Bug introduced by
The variable $cat_arr 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...
93
        $sform->addElement(new XoopsFormLabel(_AM_WFL_FCATEGORY_SUBCATEGORY, ob_get_contents()));
94
        ob_end_clean();
95
    } else {
96
        $mytreechose = new WflinksXoopsTree($xoopsDB->prefix('wflinks_cat'), 'cid', 'pid');
97
        ob_start();
98
        $mytreechose->makeMySelBox('title', 'title', $cid, 1, 'pid');
99
        $sform->addElement(new XoopsFormLabel(_AM_WFL_FCATEGORY_SUBCATEGORY, ob_get_contents()));
100
        ob_end_clean();
101
    }
102
103
    $graph_array       = WflLists:: getListTypeAsArray(XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['catimage'], $type = 'images');
104
    $indeximage_select = new XoopsFormSelect('', 'imgurl', $imgurl);
105
    $indeximage_select->addOptionArray($graph_array);
106
    $indeximage_select->setExtra("onchange='showImgSelected(\"image\", \"imgurl\", \"" . $xoopsModuleConfig['catimage'] . '", "", "' . XOOPS_URL . "\")'");
107
    $indeximage_tray = new XoopsFormElementTray(_AM_WFL_FCATEGORY_CIMAGE, '&nbsp;');
108
    $indeximage_tray->addElement($indeximage_select);
109
    if (!empty($imgurl)) {
110
        $indeximage_tray->addElement(new XoopsFormLabel('', "<br><br><img src='" . XOOPS_URL . '/' . $xoopsModuleConfig['catimage'] . '/' . $imgurl . "' name='image' id='image' alt=''>"));
111
    } else {
112
        $indeximage_tray->addElement(new XoopsFormLabel('', "<br><br><img src='" . XOOPS_URL . "/uploads/blank.gif' name='image' id='image' alt=''>"));
113
    }
114
    $sform->addElement($indeximage_tray);
115
116
    $editor = WflinksUtility::getWysiwygForm(_AM_WFL_FCATEGORY_DESCRIPTION, 'description', $description, 15, 60, '');
117
    $sform->addElement($editor, false);
118
119
    // Select Client/Sponsor
120
    $client_select   = new XoopsFormSelect(_AM_WFL_CATSPONSOR, 'client_id', $client_id, false);
121
    $sql             = 'SELECT cid, name FROM ' . $xoopsDB->prefix('bannerclient') . ' ORDER BY name ASC';
122
    $result          = $xoopsDB->query($sql);
123
    $client_array    = [];
124
    $client_array[0] = '&nbsp;';
125
    while ($myrow = $xoopsDB->fetchArray($result)) {
126
        $client_array[$myrow['cid']] = $myrow['name'];
127
    }
128
    $client_select->addOptionArray($client_array);
129
    $client_select->setDescription(_AM_WFL_CATSPONSORDSC);
130
    $sform->addElement($client_select);
131
132
    // Select Banner
133
    $banner_select   = new XoopsFormSelect(_AM_WFL_BANNERID, 'banner_id', $banner_id, false);
134
    $sql             = 'SELECT bid, cid FROM ' . $xoopsDB->prefix('banner') . ' ORDER BY bid ASC';
135
    $result          = $xoopsDB->query($sql);
136
    $banner_array    = [];
137
    $banner_array[0] = '&nbsp;';
138
    while ($myrow = $xoopsDB->fetchArray($result)) {
139
        $banner_array[$myrow['bid']] = $myrow['bid'];
140
    }
141
    $banner_select->addOptionArray($banner_array);
142
    $banner_select->setDescription(_AM_WFL_BANNERIDDSC);
143
    $sform->addElement($banner_select);
144
145
    $options_tray = new XoopsFormElementTray(_AM_WFL_TEXTOPTIONS, '<br>');
146
147
    $html_checkbox = new XoopsFormCheckBox('', 'nohtml', $nohtml);
148
    $html_checkbox->addOption(1, _AM_WFL_DISABLEHTML);
149
    $options_tray->addElement($html_checkbox);
150
151
    $smiley_checkbox = new XoopsFormCheckBox('', 'nosmiley', $nosmiley);
152
    $smiley_checkbox->addOption(1, _AM_WFL_DISABLESMILEY);
153
    $options_tray->addElement($smiley_checkbox);
154
155
    $xcodes_checkbox = new XoopsFormCheckBox('', 'noxcodes', $noxcodes);
156
    $xcodes_checkbox->addOption(1, _AM_WFL_DISABLEXCODE);
157
    $options_tray->addElement($xcodes_checkbox);
158
159
    $noimages_checkbox = new XoopsFormCheckBox('', 'noimages', $noimages);
160
    $noimages_checkbox->addOption(1, _AM_WFL_DISABLEIMAGES);
161
    $options_tray->addElement($noimages_checkbox);
162
163
    $breaks_checkbox = new XoopsFormCheckBox('', 'nobreak', $nobreak);
164
    $breaks_checkbox->addOption(1, _AM_WFL_DISABLEBREAK);
165
    $options_tray->addElement($breaks_checkbox);
166
    $sform->addElement($options_tray);
167
168
    //    $sform -> addElement(new XoopsFormSelectGroup(_AM_WFL_FCATEGORY_GROUPPROMPT, "groups", true, $groups, 5, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
169
170
    $sform->addElement(new XoopsFormHidden('cid', $cid));
171
172
    $sform->addElement(new XoopsFormHidden('spotlighttop', $cid));
173
174
    $button_tray = new XoopsFormElementTray('', '');
175
    $hidden      = new XoopsFormHidden('op', 'save');
176
    $button_tray->addElement($hidden);
177
178
    if (!$cid) {
179
        $butt_create = new XoopsFormButton('', '', _AM_WFL_BSAVE, 'submit');
180
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addCat\'"');
181
        $button_tray->addElement($butt_create);
182
183
        $butt_clear = new XoopsFormButton('', '', _AM_WFL_BRESET, 'reset');
184
        $button_tray->addElement($butt_clear);
185
186
        $butt_cancel = new XoopsFormButton('', '', _AM_WFL_BCANCEL, 'button');
187
        $butt_cancel->setExtra('onclick="history.go(-1)"');
188
        $button_tray->addElement($butt_cancel);
189
    } else {
190
        $butt_create = new XoopsFormButton('', '', _AM_WFL_BMODIFY, 'submit');
191
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addCat\'"');
192
        $button_tray->addElement($butt_create);
193
194
        $butt_delete = new XoopsFormButton('', '', _AM_WFL_BDELETE, 'submit');
195
        $butt_delete->setExtra('onclick="this.form.elements.op.value=\'del\'"');
196
        $button_tray->addElement($butt_delete);
197
198
        $butt_cancel = new XoopsFormButton('', '', _AM_WFL_BCANCEL, 'button');
199
        $butt_cancel->setExtra('onclick="history.go(-1)"');
200
        $button_tray->addElement($butt_cancel);
201
    }
202
    $sform->addElement($button_tray);
203
    $sform->display();
204
205
    $result2 = $xoopsDB->query('SELECT COUNT(*) FROM ' . $xoopsDB->prefix('wflinks_cat') . '');
206
    list($numrows) = $xoopsDB->fetchRow($result2);
0 ignored issues
show
Unused Code introduced by
The assignment to $numrows is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
207
}
208
209
if (!isset($_POST['op'])) {
210
    $op = isset($_GET['op']) ? $_GET['op'] : 'main';
211
} else {
212
    $op = isset($_POST['op']) ? $_POST['op'] : 'main';
213
}
214
215
switch ($op) {
216
    case 'move':
217
        if (!isset($_POST['ok'])) {
218
            $cid = isset($_POST['cid']) ? $_POST['cid'] : $_GET['cid'];
219
220
            xoops_cp_header();
221
222
            require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
223
            $mytree = new WflinksXoopsTree($xoopsDB->prefix('wflinks_cat'), 'cid', 'pid');
224
            $sform  = new XoopsThemeForm(_AM_WFL_CCATEGORY_MOVE, 'move', xoops_getenv('PHP_SELF'), 'post', true);
225
            ob_start();
226
            $mytree->makeMySelBox('title', 'title', 0, 0, 'target');
227
            $sform->addElement(new XoopsFormLabel(_AM_WFL_BMODIFY, ob_get_contents()));
228
            ob_end_clean();
229
            $create_tray = new XoopsFormElementTray('', '');
230
            $create_tray->addElement(new XoopsFormHidden('source', $cid));
231
            $create_tray->addElement(new XoopsFormHidden('ok', 1));
232
            $create_tray->addElement(new XoopsFormHidden('op', 'move'));
233
            $butt_save = new XoopsFormButton('', '', _AM_WFL_BMOVE, 'submit');
234
            $butt_save->setExtra('onclick="this.form.elements.op.value=\'move\'"');
235
            $create_tray->addElement($butt_save);
236
            $butt_cancel = new XoopsFormButton('', '', _AM_WFL_BCANCEL, 'submit');
237
            $butt_cancel->setExtra('onclick="this.form.elements.op.value=\'cancel\'"');
238
            $create_tray->addElement($butt_cancel);
239
            $sform->addElement($create_tray);
240
            $sform->display();
241
            xoops_cp_footer();
242
        } else {
243
            global $xoopsDB;
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...
244
245
            $source = $_POST['source'];
246
            $target = $_POST['target'];
247
            if ($target == $source) {
248
                redirect_header("category.php?op=move&ok=0&cid=$source", 5, _AM_WFL_CCATEGORY_MODIFY_FAILED);
249
            }
250
            if (!$target) {
251
                redirect_header("category.php?op=move&ok=0&cid=$source", 5, _AM_WFL_CCATEGORY_MODIFY_FAILEDT);
252
            }
253
            $sql    = 'UPDATE ' . $xoopsDB->prefix('wflinks_links') . ' set cid = ' . $target . ' WHERE cid =' . $source;
254
            $result = $xoopsDB->queryF($sql);
255
            $error  = _AM_WFL_DBERROR . ': <br><br>' . $sql;
256
            if (!$result) {
257
                trigger_error($error, E_USER_ERROR);
258
            }
259
            redirect_header('category.php?op=default', 1, _AM_WFL_CCATEGORY_MODIFY_MOVED);
260
        }
261
        break;
262
263
    case 'addCat':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
264
265
        $groups       = Request::getArray('groups', [], 'POST');
266
        $cid          = Request::getInt('cid', 0, 'POST');
267
        $pid          = Request::getInt('pid', 0, 'POST');
268
        $weight       = (isset($_REQUEST['weight']) && $_REQUEST['weight'] > 0) ? $_REQUEST['weight'] : 0;
269
        $spotlighthis = Request::getInt('lid', 0, 'POST');
270
        $spotlighttop = ($_REQUEST['spotlighttop'] == 1) ? 1 : 0;
271
        $title        = Request::getText('title', '', 'POST');
272
        $descriptionb = Request::getText('description', '', 'POST');
273
        $imgurl       = ($_REQUEST['imgurl'] && $_REQUEST['imgurl'] !== 'blank.gif') ? Request::getUrl('imgurl', '', 'POST') : '';
274
        $client_id    = Request::getInt('client_id', 0, 'POST');
275
        if ($client_id > 0) {
276
            $banner_id = 0;
277
        } else {
278
            $banner_id = Request::getInt('banner_id', 0, 'POST');
279
        }
280
281
        $nohtml   = Request::getInt('nohtml', 0, 'POST');
282
        $nosmiley = Request::getInt('nosmiley', 0, 'POST');
283
        $noxcodes = Request::getInt('noxcodes', 0, 'POST');
284
        $noimages = Request::getInt('noimages', 0, 'POST');
285
        $nobreak  = Request::getInt('nobreak', 0, 'POST');
286
287
        if (!$cid) {
288
            $cid = 0;
289
            $sql = 'INSERT INTO '
290
                   . $xoopsDB->prefix('wflinks_cat')
291
                   . " (cid, pid, title, imgurl, description, nohtml, nosmiley, noxcodes, noimages, nobreak, weight, spotlighttop, spotlighthis, client_id, banner_id ) VALUES (0, $pid, '$title', '$imgurl', '$descriptionb', '$nohtml', '$nosmiley', '$noxcodes', '$noimages', '$nobreak', '$weight',  '$spotlighttop', '$spotlighthis', '$client_id', '$banner_id' )";
292
            if ($cid == 0) {
293
                $newid = $xoopsDB->getInsertId();
294
            }
295
296
            // Notify of new category
297
298
            global $xoopsModule;
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...
299
            $tags                  = [];
300
            $tags['CATEGORY_NAME'] = $title;
301
            $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/viewcat.php?cid=' . $newid;
302
            $notificationHandler   = xoops_getHandler('notification');
303
            $notificationHandler->triggerEvent('global', 0, 'new_category', $tags);
304
            $database_mess = _AM_WFL_CCATEGORY_CREATED;
305
        } else {
306
            if ($cid == $pid) {
307
                redirect_header('category.php', 1, _AM_WFL_ERROR_CATISCAT);
308
            }
309
            $sql           = 'UPDATE '
310
                             . $xoopsDB->prefix('wflinks_cat')
311
                             . " SET title ='$title', imgurl='$imgurl', pid =$pid, description='$descriptionb', spotlighthis='$spotlighthis' , spotlighttop='$spotlighttop', nohtml='$nohtml', nosmiley='$nosmiley', noxcodes='$noxcodes', noimages='$noimages', nobreak='$nobreak', weight='$weight', client_id='$client_id', banner_id='$banner_id' WHERE cid="
312
                             . $cid;
313
            $database_mess = _AM_WFL_CCATEGORY_MODIFIED;
314
        }
315 View Code Duplication
        if (!$result = $xoopsDB->query($sql)) {
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...
316
            XoopsErrorHandler_HandleError(E_USER_WARNING, $sql, __FILE__, __LINE__);
317
318
            return false;
319
        }
320
        redirect_header('category.php', 1, $database_mess);
321
        break;
322
323
    case 'del':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
324
325
        global $xoopsDB, $xoopsModule;
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...
326
327
        $cid    = (isset($_POST['cid']) && is_numeric($_POST['cid'])) ? (int)$_POST['cid'] : (int)$_GET['cid'];
328
        $ok     = (isset($_POST['ok']) && $_POST['ok'] == 1) ? (int)$_POST['ok'] : 0;
329
        $mytree = new WflinksXoopsTree($xoopsDB->prefix('wflinks_cat'), 'cid', 'pid');
330
331
        if ($ok == 1) {
332
            // get all subcategories under the specified category
333
            $subcategories    = $mytree->getAllChildId($cid);
334
            foreach ($subcategories as $subcategory) {
335
                // get all links in each subcategory
336
                $result = $xoopsDB->query('SELECT lid FROM ' . $xoopsDB->prefix('wflinks_links') . ' WHERE cid=' . $subcategory . ' ');
337
                // now for each linkload, delete the text data and vote ata associated with the linkload
338
                while (list($lid) = $xoopsDB->fetchRow($result)) {
339
                    $sql = sprintf('DELETE FROM %s WHERE lid = %u', $xoopsDB->prefix('wflinks_votedata'), $lid);
340
                    $xoopsDB->query($sql);
341
                    $sql = sprintf('DELETE FROM %s WHERE lid = %u', $xoopsDB->prefix('wflinks_links'), $lid);
342
                    $xoopsDB->query($sql);
343
344
                    // delete comments
345
                    xoops_comment_delete($xoopsModule->getVar('mid'), $lid);
346
                }
347
                // all links for each subcategory are deleted, now delete the subcategory data
348
                $sql = sprintf('DELETE FROM %s WHERE cid = %u', $xoopsDB->prefix('wflinks_cat'), $subcategory);
349
                $xoopsDB->query($sql);
350
                // delete altcat entries
351
                $sql = sprintf('DELETE FROM %s WHERE cid = %u', $xoopsDB->prefix('wflinks_altcat'), $subcategory);
352
                $xoopsDB->query($sql);
353
            }
354
            // all subcategory and associated data are deleted, now delete category data and its associated data
355
            $result = $xoopsDB->query('SELECT lid FROM ' . $xoopsDB->prefix('wflinks_links') . ' WHERE cid=' . $cid . '');
356
            while (list($lid) = $xoopsDB->fetchRow($result)) {
357
                $sql = sprintf('DELETE FROM %s WHERE lid = %u', $xoopsDB->prefix('wflinks_links'), $lid);
358
                $xoopsDB->query($sql);
359
                // delete comments
360
                xoops_comment_delete($xoopsModule->getVar('mid'), $lid);
361
                $sql = sprintf('DELETE FROM %s WHERE lid = %u', $xoopsDB->prefix('wflinks_votedata'), $lid);
362
                $xoopsDB->query($sql);
363
            }
364
            // delete altcat entries
365
            $sql = sprintf('DELETE FROM %s WHERE cid = %u', $xoopsDB->prefix('wflinks_altcat'), $cid);
366
            $xoopsDB->query($sql);
367
            // delete category
368
            $sql   = sprintf('DELETE FROM %s WHERE cid = %u', $xoopsDB->prefix('wflinks_cat'), $cid);
369
            $error = _AM_WFL_DBERROR . ': <br><br>' . $sql;
370
371
            // delete group permissions
372
            xoops_groupperm_deletebymoditem($xoopsModule->getVar('mid'), 'WFLinkCatPerm', $cid);
373
            if (!$result = $xoopsDB->query($sql)) {
374
                trigger_error($error, E_USER_ERROR);
375
            }
376
377
            redirect_header('category.php', 1, _AM_WFL_CCATEGORY_DELETED);
378
        } else {
379
            xoops_cp_header();
380
            xoops_confirm(['op' => 'del', 'cid' => $cid, 'ok' => 1], 'category.php', _AM_WFL_CCATEGORY_AREUSURE);
381
            xoops_cp_footer();
382
        }
383
        break;
384
385
    case 'modCat':
386
        $cid = isset($_POST['cid']) ? $_POST['cid'] : 0;
387
        xoops_cp_header();
388
389
        createCat($cid);
390
        xoops_cp_footer();
391
        break;
392
393
    case 'main':
394
    default:
395
        xoops_cp_header();
396
397
        $adminObject = \Xmf\Module\Admin::getInstance();
398
        $adminObject->displayNavigation(basename(__FILE__));
399
        $adminObject->addItemButton(_MI_WFL_ADD_LINK, 'main.php?op=edit', 'add', '');
400
        $adminObject->addItemButton(_MI_WFL_ADD_CATEGORY, 'category.php', 'add', '');
401
        $adminObject->displayButton('left', '');
402
403
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
404
        $mytree    = new WflinksXoopsTree($xoopsDB->prefix('wflinks_cat'), 'cid', 'pid');
405
        $sform     = new XoopsThemeForm(_AM_WFL_CCATEGORY_MODIFY, 'category', xoops_getenv('PHP_SELF'), 'post', true);
406
        $totalcats = WflinksUtility::getTotalCategory();
407
408
        if ($totalcats > 0) {
409
            ob_start();
410
            $mytree->makeMySelBox('title', 'title');
411
            $sform->addElement(new XoopsFormLabel(_AM_WFL_CCATEGORY_MODIFY_TITLE, ob_get_contents()));
412
            ob_end_clean();
413
            $dup_tray = new XoopsFormElementTray('', '');
414
            $dup_tray->addElement(new XoopsFormHidden('op', 'modCat'));
415
            $butt_dup = new XoopsFormButton('', '', _AM_WFL_BMODIFY, 'submit');
416
            $butt_dup->setExtra('onclick="this.form.elements.op.value=\'modCat\'"');
417
            $dup_tray->addElement($butt_dup);
418
            $butt_move = new XoopsFormButton('', '', _AM_WFL_BMOVE, 'submit');
419
            $butt_move->setExtra('onclick="this.form.elements.op.value=\'move\'"');
420
            $dup_tray->addElement($butt_move);
421
            $butt_dupct = new XoopsFormButton('', '', _AM_WFL_BDELETE, 'submit');
422
            $butt_dupct->setExtra('onclick="this.form.elements.op.value=\'del\'"');
423
            $dup_tray->addElement($butt_dupct);
424
            $sform->addElement($dup_tray);
425
            $sform->display();
426
        }
427
        createCat(0);
428
        require_once __DIR__ . '/admin_footer.php';
429
        break;
430
}
431