Completed
Push — master ( 739baa...547298 )
by Michael
02:43 queued 01:13
created

MymenusLinksUtilities::mymenusAdminToggle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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 174 and the first side effect is on line 22.

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
 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         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
15
 * @package         Mymenus
16
 * @since           1.0
17
 * @author          trabis <[email protected]>, bleekk <[email protected]>
18
 */
19
20
use Xmf\Request;
21
22
require_once __DIR__ . '/admin_header.php';
23
24
$currentFile = basename(__FILE__);
25
26
$mymenusTpl       = new XoopsTpl(); // will be removed???
27
$mymenusAdminPage = 'links.php'; // will be removed???
28
29
$menusCriteria = new CriteriaCompo();
30
$menusCriteria->setSort('id');
31
$menusCriteria->setOrder('ASC');
32
$menusList = $mymenus->getHandler('menus')->getList($menusCriteria);
33
if (!$menusList) {
34
    redirect_header('menus.php', 1, _AM_MYMENUS_MSG_NOMENUS);
35
}
36
37
$valid_menu_ids = array_keys($menusList);
38
$mid            = Request::getInt('mid', Request::getInt('mid', '', 'POST'), 'GET');
39
if ($mid && in_array($mid, $valid_menu_ids)) {
40
    $menuTitle = $menusList[$mid];
41
} else {
42
    $keys      = array_keys($menusList);
43
    $mid       = $valid_menu_ids[0]; //force menu id to first valid menu id in the list
44
    $menuTitle = $menusList[$mid]; // and get it's title
45
}
46
$mymenusTpl->assign('mid', $mid);
47
$mymenusTpl->assign('menuTitle', $menuTitle);
48
$mymenusTpl->assign('menus_list', $menusList);
49
50
$id      = Request::getInt('id', 0);
51
$pid     = Request::getInt('pid', 0);
52
$start   = Request::getInt('start', 0);
53
$weight  = Request::getInt('weight', 0);
54
$visible = Request::getInt('visible', 0);
55
56
$op = Request::getCmd('op', 'list');
57
switch ($op) {
58
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
59
        case 'form':
60
            xoops_cp_header();
61
            $adminObject  = \Xmf\Module\Admin::getInstance();
62
            $adminObject->displayNavigation($currentFile);
63
            //
64
            echo mymenusAdminForm(null, $pid, $mid);
65
            //
66
            include __DIR__ . '/admin_footer.php';
67
            break;
68
    */
69
    case 'edit':
70
        echo MymenusLinksUtility::mymenusAdminForm($id, null, $mid);
71
        break;
72
73
    case 'add':
74
        MymenusLinksUtility::mymenusAdminAdd($mid);
75
        break;
76
77
    case 'save':
78
        MymenusLinksUtility::mymenusAdminSave($id, $mid);
79
        break;
80
81
    case 'delete':
82
        $id       = Request::getInt('id', null);
83
        $linksObj = $mymenus->getHandler('links')->get($id);
84
        if (Request::getBool('ok', false, 'POST') === true) {
85 View Code Duplication
            if (!$GLOBALS['xoopsSecurity']->check()) {
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...
86
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
87
            }
88
            //get sub item
89
            $linksCriteria = new CriteriaCompo();
90
            $linksCriteria->add(new Criteria('id', $id));
91
            $linksCriteria->add(new Criteria('pid', $id), 'OR');
92
            //first delete links level 2
93
            $query  = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('mymenus_links');
94
            $query  .= ' WHERE pid = (SELECT id FROM (SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('mymenus_links') . " WHERE pid = {$id}) AS sec);";
95
            $result = $GLOBALS['xoopsDB']->queryF($query);
96
            //delete links level 0 and 1
97 View Code Duplication
            if (!$mymenus->getHandler('links')->deleteAll($linksCriteria)) {
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...
98
                xoops_cp_header();
99
                xoops_error(_AM_MYMENUS_MSG_ERROR, $linksObj->getVar('id'));
100
                xoops_cp_footer();
101
                exit();
102
            }
103
            redirect_header($currentFile, 3, _AM_MYMENUS_MSG_DELETE_LINK_SUCCESS);
104 View Code Duplication
        } else {
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...
105
            xoops_cp_header();
106
            xoops_confirm(array('ok' => true, 'id' => $id, 'op' => 'delete'), //                $_SERVER['REQUEST_URI'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
107
                          Request::getString('REQUEST_URI', '', 'SERVER'), sprintf(_AM_MYMENUS_LINKS_SUREDEL, $linksObj->getVar('title')));
108
            require_once __DIR__ . '/admin_footer.php';
109
        }
110
        break;
111
112
    case 'move':
113
        xoops_cp_header();
114
        $adminObject = \Xmf\Module\Admin::getInstance();
115
        $adminObject->displayNavigation($currentFile);
116
        //
117
        MymenusLinksUtility::mymenusAdminMove($id, $weight);
118
        echo MymenusLinksUtility::mymenusAdminList($start, $mid);
119
        //
120
        include __DIR__ . '/admin_footer.php';
121
        break;
122
123
    case 'toggle':
124
        MymenusLinksUtility::mymenusAdminToggle($id, $visible);
125
        break;
126
127
    case 'order':
128
        $test  = array();
129
        $order = Request::getString('mod', '', 'POST');
130
        parse_str($order, $test);
131
        $i = 1;
132
        foreach ($test['mod'] as $order => $value) {
133
            $linksObj = $mymenus->getHandler('links')->get($order);
134
            $linksObj->setVar('weight', ++$i);
135
            // Set submenu
136
            if (isset($value)) {
137
                $linksObj->setVar('pid', $value);
138
            } else {
139
                $linksObj->setVar('pid', 0);
140
            }
141
            $mymenus->getHandler('links')->insert($linksObj);
142
            $mymenus->getHandler('links')->updateWeights($linksObj);
143
        }
144
        break;
145
146
    case 'list':
147
    default:
148
        xoops_cp_header();
149
        $adminObject = \Xmf\Module\Admin::getInstance();
150
        $adminObject->displayNavigation($currentFile);
151
        // Add module stylesheet
152
        $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/system/css/ui/' . xoops_getModuleOption('jquery_theme', 'system') . '/ui.all.css');
153
        $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . "/modules/{$mymenus->dirname}/assets/css/admin.css");
154
        $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/Frameworks/moduleclasses/moduleadmin/css/admin.css');
155
        // Define scripts
156
        $GLOBALS['xoTheme']->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
157
        $GLOBALS['xoTheme']->addScript(XOOPS_URL . "/modules/{$mymenus->dirname}/assets/js/nestedSortable.js");
158
        //$GLOBALS['xoTheme']->addScript(XOOPS_URL . '/modules/{$mymenus->dirname}/assets/js/switchButton.js');
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
159
        $GLOBALS['xoTheme']->addScript(XOOPS_URL . "/modules/{$mymenus->dirname}/assets/js/links.js");
160
        echo MymenusLinksUtility::mymenusAdminList($start, $mid);
161
        // Disable xoops debugger in dialog window
162
        require_once $GLOBALS['xoops']->path('/class/logger/xoopslogger.php');
163
        $xoopsLogger            = XoopsLogger::getInstance();
164
        $xoopsLogger->activated = true;
165
        error_reporting(-1);
166
        //
167
        include __DIR__ . '/admin_footer.php';
168
        break;
169
}
170
171
/**
172
 * Class MymenusLinksUtility
173
 */
174
class MymenusLinksUtility
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
175
{
176
177
    /**
178
     * Display the links in a menu
179
     *
180
     * @param integer $start
181
     * @param integer $mid
182
     *
183
     * @return bool|mixed|string
184
     */
185
    public static function mymenusAdminList($start = 0, $mid)
0 ignored issues
show
Coding Style introduced by
mymenusAdminList 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...
186
    {
187
        $mymenus = MymenusMymenus::getInstance();
188
        global $mymenusTpl;
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...
189
        //
190
        $linksCriteria = new CriteriaCompo(new Criteria('mid', (int)$mid));
191
        $linksCount    = $mymenus->getHandler('links')->getCount($linksCriteria);
192
        $mymenusTpl->assign('count', $linksCount);
193
        //
194
        $linksCriteria->setSort('weight');
195
        $linksCriteria->setOrder('ASC');
196
        //
197
        //        $menusArray = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
198
        if (($linksCount > 0) && ($linksCount >= (int)$start)) {
199
            $linksCriteria->setStart((int)$start);
200
            $linksArrays = $mymenus->getHandler('links')->getObjects($linksCriteria, false, false); // as array
201
            //
202
            require_once $GLOBALS['xoops']->path("modules/{$mymenus->dirname}/class/builder.php");
203
            $menuBuilder = new MymenusBuilder($linksArrays);
204
            $menusArray  = $menuBuilder->render();
205
            $mymenusTpl->assign('menus', $menusArray); // not 'menus', 'links' shoult be better
206
        }
207
        //
208
        $mymenusTpl->assign('addform', MymenusLinksUtility::mymenusAdminForm(null, null, $mid));
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
209
210
        //
211
        return $mymenusTpl->fetch($GLOBALS['xoops']->path("modules/{$mymenus->dirname}/templates/static/mymenus_admin_links.tpl"));
212
    }
213
214
    /**
215
     * @param $mid
216
     */
217
    public static function mymenusAdminAdd($mid)
0 ignored issues
show
Coding Style introduced by
mymenusAdminAdd 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...
Coding Style introduced by
mymenusAdminAdd uses the super-global variable $_POST 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...
218
    {
219
        $mymenus = MymenusMymenus::getInstance();
220
        //
221 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
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...
222
            redirect_header($GLOBALS['mymenusAdminPage'], 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
223
        }
224
        if (!$mid) {
225
            redirect_header($GLOBALS['mymenusAdminPage'] . '?op=list', 2, _AM_MYMENUS_MSG_MENU_INVALID_ERROR);
226
        }
227
        //
228
        $linksCiteria = new CriteriaCompo(new Criteria('mid', $mid));
229
        $linksCiteria->setSort('weight');
230
        $linksCiteria->setOrder('DESC');
231
        $linksCiteria->setLimit(1);
232
        $linksObjs = $mymenus->getHandler('links')->getObjects($linksCiteria);
233
        $weight    = 1;
234
        if (isset($linksObjs[0]) && ($linksObjs[0] instanceof MymenusLinks)) {
235
            $weight = $linksObjs[0]->getVar('weight') + 1;
236
        }
237
238
        $newLinksObj = $mymenus->getHandler('links')->create();
239
        //    if (!isset($_POST['hooks'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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...
240
        //        $_POST['hooks'] = array();
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...
241
        //    }
242
        if (!Request::getArray('hooks', null, 'POST')) {
243
            $_POST['hooks'] = array();
244
        }
245
        // @TODO: clean incoming POST vars
246
        $newLinksObj->setVars($_POST);
247
        $newLinksObj->setVar('weight', $weight);
248
249 View Code Duplication
        if (!$mymenus->getHandler('links')->insert($newLinksObj)) {
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...
250
            $msg = _AM_MYMENUS_MSG_ERROR;
251
        } else {
252
            $mymenus->getHandler('links')->updateWeights($newLinksObj);
253
            $msg = _AM_MYMENUS_MSG_SUCCESS;
254
        }
255
256
        redirect_header($GLOBALS['mymenusAdminPage'] . '?op=list&amp;mid=' . $newLinksObj->getVar('mid'), 2, $msg);
257
    }
258
259
    /**
260
     * @param integer $id
261
     * @param integer $mid
262
     */
263
    public static function mymenusAdminSave($id, $mid)
0 ignored issues
show
Coding Style introduced by
mymenusAdminSave 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...
Coding Style introduced by
mymenusAdminSave uses the super-global variable $_POST 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...
264
    {
265
        $mymenus = MymenusMymenus::getInstance();
266
        //
267 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
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...
268
            redirect_header($GLOBALS['mymenusAdminPage'], 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
269
        }
270
        if (!$mid) {
271
            redirect_header($GLOBALS['mymenusAdminPage'] . '?op=list', 2, _AM_MYMENUS_MSG_MENU_INVALID_ERROR);
272
        }
273
        //
274
        $mid      = (int)$mid;
275
        $linksObj = $mymenus->getHandler('links')->get((int)$id);
276
277
        //if this was moved then parent could be in different menu, if so then set parent to top level
278
        if (Request::getInt('pid', '', 'POST')) {
279
            $parentLinksObj = $mymenus->getHandler('links')->get($linksObj->getVar('pid'));  //get the parent object
280
            if (($parentLinksObj instanceof MylinksLinks)
0 ignored issues
show
Bug introduced by
The class MylinksLinks does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
281
                && ($linksObj->getVar('mid') != $parentLinksObj->getVar('mid'))) {
282
                $linksObj->setVar('pid', 0);
283
            }
284
        }
285
        // Disable xoops debugger in dialog window
286
        require_once $GLOBALS['xoops']->path('/class/logger/xoopslogger.php');
287
        $xoopsLogger            = XoopsLogger::getInstance();
288
        $xoopsLogger->activated = false;
289
        error_reporting(0);
290
291
        // @TODO: clean incoming POST vars
292
        $linksObj->setVars($_POST);
293
294 View Code Duplication
        if (!$mymenus->getHandler('links')->insert($linksObj)) {
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...
295
            $msg = _AM_MYMENUS_MSG_ERROR;
296
        } else {
297
            $msg = _AM_MYMENUS_MSG_SUCCESS;
298
        }
299
300
        redirect_header($GLOBALS['mymenusAdminPage'] . "?op=list&mid={$mid}", 2, $msg);
301
    }
302
303
    /**
304
     * @param null  $id
305
     * @param null  $pid
306
     *
307
     * @param  null $mid
308
     * @return string
309
     */
310
    public static function mymenusAdminForm($id = null, $pid = null, $mid = null)
0 ignored issues
show
Coding Style introduced by
mymenusAdminForm 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...
311
    {
312
        $mymenus = MymenusMymenus::getInstance();
313
        //
314
        // Disable xoops debugger in dialog window
315
        require_once $GLOBALS['xoops']->path('/class/logger/xoopslogger.php');
316
        $xoopsLogger            = XoopsLogger::getInstance();
317
        $xoopsLogger->activated = false;
318
        error_reporting(0);
319
320
        $pathIcon16      = Xmf\Module\Admin::iconUrl('', 16);
321
322
        //        $registry = MymenusRegistry::getInstance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
323
        //        $plugin   = MymenusPlugin::getInstance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
324
325
        $linksObj = $mymenus->getHandler('links')->get((int)$id);
326
327
        if ($linksObj->isNew()) {
328
            $formTitle = _ADD;
329
            if (isset($pid)) {
330
                $linksObj->setVar('pid', (int)$pid);
331
            }
332
            if (isset($mid)) {
333
                $linksObj->setVar('mid', (int)$mid);
334
            }
335
        } else {
336
            $formTitle = _EDIT;
337
        }
338
        $form = new XoopsThemeForm($formTitle, 'admin_form', $GLOBALS['mymenusAdminPage'], 'post', true);
339
        // links: title
340
        $formtitle = new XoopsFormText(_AM_MYMENUS_MENU_TITLE, 'title', 50, 255, $linksObj->getVar('title'));
341
        $form->addElement($formtitle, true);
342
        // links: alt_title
343
        $formalttitle = new XoopsFormText(_AM_MYMENUS_MENU_ALTTITLE, 'alt_title', 50, 255, $linksObj->getVar('alt_title'));
344
        $form->addElement($formalttitle);
345
        // links: mid
346
        $menusCriteria = new CriteriaCompo();
347
        $menusCriteria->setSort('title');
348
        $menusCriteria->setOrder('ASC');
349
        $menusList = $mymenus->getHandler('menus')->getList($menusCriteria);
350
        if (count($menusList) > 1) {
351
            // display menu options (if more than 1 menu available
352
            if (!$linksObj->getVar('mid')) { // initial menu value not set
353
                //                $menuValues = array_flip($menusList);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
354
                $formmid = new XoopsFormSelect(_AM_MYMENUS_MENU_MENU, 'mid', $mid);//array_shift($menuValues));
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
355
            } else {
356
                $formmid = new XoopsFormSelect(_AM_MYMENUS_MENU_MENU, 'mid', $linksObj->getVar('mid'));
357
            }
358
            $formmid->addOptionArray($menusList);
359
        } else {
360
            $menuKeys  = array_keys($menusList);
361
            $menuTitle = array_shift($menusList);
362
            $formmid   = new XoopsFormElementTray('Menu');
363
            $formmid->addElement(new XoopsFormHidden('mid', $menuKeys[0]));
364
            $formmid->addElement(new XoopsFormLabel('', $menuTitle, 'menuTitle'));
365
        }
366
        $form->addElement($formmid);
367
        // links: link
368
        $formlink = new XoopsFormText(_AM_MYMENUS_MENU_LINK, 'link', 50, 255, $linksObj->getVar('link'));
369
        $form->addElement($formlink);
370
        // links: image
371
        $formimage = new XoopsFormText(_AM_MYMENUS_MENU_IMAGE, 'image', 50, 255, $linksObj->getVar('image'));
372
        $form->addElement($formimage);
373
        //
374
        //$form->addElement($formparent);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
375
        // links: visible
376
        $statontxt  = "&nbsp;<img src='{$pathIcon16}/1.png' alt='" . _YES . "'>&nbsp;" . _YES . '&nbsp;&nbsp;&nbsp;';
377
        $statofftxt = "&nbsp;<img src='{$pathIcon16}/0.png' alt='" . _NO . "'>&nbsp;" . _NO . '&nbsp;';
378
        $formvis    = new XoopsFormRadioYN(_AM_MYMENUS_MENU_VISIBLE, 'visible', $linksObj->getVar('visible'), $statontxt, $statofftxt);
379
        $form->addElement($formvis);
380
        // links: target
381
        $formtarget = new XoopsFormSelect(_AM_MYMENUS_MENU_TARGET, 'target', $linksObj->getVar('target'));
382
        $formtarget->addOption('_self', _AM_MYMENUS_MENU_TARG_SELF);
383
        $formtarget->addOption('_blank', _AM_MYMENUS_MENU_TARG_BLANK);
384
        $formtarget->addOption('_parent', _AM_MYMENUS_MENU_TARG_PARENT);
385
        $formtarget->addOption('_top', _AM_MYMENUS_MENU_TARG_TOP);
386
        $form->addElement($formtarget);
387
        // links: groups
388
        $formgroups = new XoopsFormSelectGroup(_AM_MYMENUS_MENU_GROUPS, 'groups', true, $linksObj->getVar('groups'), 5, true);
389
        $formgroups->setDescription(_AM_MYMENUS_MENU_GROUPS_HELP);
390
        $form->addElement($formgroups);
391
        // @TODO: reintroduce hooks
392
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
393
            //links: hooks
394
            $formhooks = new XoopsFormSelect(_AM_MYMENUS_MENU_ACCESS_FILTER, "hooks", $linksObj->getVar('hooks'), 5, true);
395
            $plugin->triggerEvent('AccessFilter');
396
            $results = $registry->getEntry('accessFilter');
397
            if ($results) {
398
                foreach ($results as $result) {
399
                    $formhooks->addOption($result['method'], $result['name']);
400
                }
401
            }
402
            $form->addElement($formhooks);
403
        */
404
        // links: css
405
        $formcss = new XoopsFormText(_AM_MYMENUS_MENU_CSS, 'css', 50, 255, $linksObj->getVar('css'));
406
        $form->addElement($formcss);
407
        //
408
        $buttonTray = new XoopsFormElementTray('', '');
409
        $buttonTray->addElement(new XoopsFormButton('', 'submit_button', _SUBMIT, 'submit'));
410
        $button = new XoopsFormButton('', 'reset', _CANCEL, 'button');
411
        if (isset($id)) {
412
            $button->setExtra("onclick=\"document.location.href='" . $GLOBALS['mymenusAdminPage'] . "?op=list&amp;mid={$mid}'\"");
413
        } else {
414
            $button->setExtra("onclick=\"document.getElementById('addform').style.display = 'none'; return false;\"");
415
        }
416
        $buttonTray->addElement($button);
417
        $form->addElement($buttonTray);
418
419
        if (isset($id)) {
420
            $form->addElement(new XoopsFormHidden('op', 'save'));
421
            $form->addElement(new XoopsFormHidden('id', $id));
422
        } else {
423
            $form->addElement(new XoopsFormHidden('op', 'add'));
424
        }
425
426
        return $form->render();
427
    }
428
429
    /**
430
     *
431
     * Update the {@see MymenusLinks} weight (order)
432
     *
433
     * @param integer $id of links object
434
     * @param integer $weight
435
     */
436
    public static function mymenusAdminMove($id, $weight)
437
    {
438
        $mymenus = MymenusMymenus::getInstance();
439
        //
440
        $linksObj = $mymenus->getHandler('links')->get((int)$id);
441
        $linksObj->setVar('weight', (int)$weight);
442
        $mymenus->getHandler('links')->insert($linksObj);
443
        $mymenus->getHandler('links')->updateWeights($linksObj);
444
    }
445
446
    /**
447
     * @param $id
448
     * @param $visible
449
     */
450
    public static function mymenusAdminToggle($id, $visible)
0 ignored issues
show
Coding Style introduced by
mymenusAdminToggle 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...
451
    {
452
        $mymenus = MymenusMymenus::getInstance();
453
        //
454
        // Disable xoops debugger in dialog window
455
        require_once $GLOBALS['xoops']->path('/class/logger/xoopslogger.php');
456
        $xoopsLogger            = XoopsLogger::getInstance();
457
        $xoopsLogger->activated = false;
458
        error_reporting(0);
459
        //
460
        $linksObj = $mymenus->getHandler('links')->get((int)$id);
461
        $visible  = (1 === $linksObj->getVar('visible')) ? 0 : 1;
462
        $linksObj->setVar('visible', $visible);
463
        $mymenus->getHandler('links')->insert($linksObj);
464
        echo $linksObj->getVar('visible');
465
    }
466
}
467