myDeleteByModule()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 4
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
4
5
/**
6
 * @param       $db
7
 * @param       $gperm_modid
8
 * @param  null $gperm_name
9
 * @param  null $gperm_itemid
10
 * @return bool
11
 */
12
13
/**
14
 * @param \XoopsDatabase|null $db
15
 * @param                     $gperm_modid
16
 * @param null                $gperm_name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $gperm_name is correct as it would always require null to be passed?
Loading history...
17
 * @param null                $gperm_itemid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $gperm_itemid is correct as it would always require null to be passed?
Loading history...
18
 * @return bool
19
 */
20
function myDeleteByModule(\XoopsDatabase $db, $gperm_modid, $gperm_name = null, $gperm_itemid = null)
21
{
22
    $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', (int)$gperm_modid));
23
    if (isset($gperm_name)) {
24
        $criteria->add(new \Criteria('gperm_name', $gperm_name));
25
        if (isset($gperm_itemid)) {
26
            $criteria->add(new \Criteria('gperm_itemid', (int)$gperm_itemid));
27
        }
28
    }
29
    $sql = 'DELETE FROM ' . $db->prefix('group_permission') . ' ' . $criteria->renderWhere();
30
    if (!$result = $db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
31
        return false;
32
    }
33
34
    return true;
35
}
36
37
// require_once  dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php'; GIJ
38
$modid = \Xmf\Request::getInt('modid', 1, 'POST');
39
// we dont want system module permissions to be changed here ( 1 -> 0 GIJ)
40
if ($modid <= 0 || !is_object($xoopsUser) || !$xoopsUser->isAdmin($modid)) {
41
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
42
}
43
/** @var \XoopsModuleHandler $moduleHandler */
44
$moduleHandler = xoops_getHandler('module');
45
$module        = $moduleHandler->get($modid);
46
if (!is_object($module) || !$module->getVar('isactive')) {
47
    redirect_header(XOOPS_URL . '/admin.php', 1, _MODULENOEXIST);
48
}
49
$memberHandler = xoops_getHandler('member');
50
$group_list    = $memberHandler->getGroupList();
0 ignored issues
show
Bug introduced by
The method getGroupList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

50
$group_list    = $memberHandler->/** @scrutinizer ignore-call */ getGroupList();
Loading history...
51
if (is_array($_POST['perms']) && !empty($_POST['perms'])) {
52
    /** @var \XoopsGroupPermHandler $grouppermHandler */
53
    $grouppermHandler = xoops_getHandler('groupperm');
54
    foreach ($_POST['perms'] as $perm_name => $perm_data) {
55
        foreach ($perm_data['itemname'] as $item_id => $item_name) {
56
            // checking code
57
            // echo "<pre>" ;
58
            // var_dump( $_POST['perms'] ) ;
59
            // exit ;
60
            if (false !== myDeleteByModule($grouppermHandler->db, $modid, $perm_name, $item_id)) {
61
                if (empty($perm_data['groups'])) {
62
                    continue;
63
                }
64
                foreach ($perm_data['groups'] as $group_id => $item_ids) {
65
                    //                foreach ($item_ids as $item_id => $selected) {
66
                    $selected = isset($item_ids[$item_id]) ? $item_ids[$item_id] : 0;
67
                    if (1 === $selected) {
68
                        // make sure that all parent ids are selected as well
69
                        if ('' !== $perm_data['parents'][$item_id]) {
70
                            $parent_ids = explode(':', $perm_data['parents'][$item_id]);
71
                            foreach ($parent_ids as $pid) {
72
                                if (0 !== $pid && !array_key_exists($pid, $item_ids)) {
73
                                    // one of the parent items were not selected, so skip this item
74
                                    $msg[] = sprintf(_MD_AM_PERMADDNG, '<b>' . $perm_name . '</b>', '<b>' . $perm_data['itemname'][$item_id] . '</b>', '<b>' . $group_list[$group_id] . '</b>') . ' (' . _MD_AM_PERMADDNGP . ')';
75
                                    continue 2;
76
                                }
77
                            }
78
                        }
79
                        $gperm = $grouppermHandler->create();
80
                        $gperm->setVar('gperm_groupid', $group_id);
81
                        $gperm->setVar('gperm_name', $perm_name);
82
                        $gperm->setVar('gperm_modid', $modid);
83
                        $gperm->setVar('gperm_itemid', $item_id);
84
                        if (!$grouppermHandler->insert($gperm)) {
0 ignored issues
show
Bug introduced by
$gperm of type boolean is incompatible with the type XoopsObject expected by parameter $perm of XoopsGroupPermHandler::insert(). ( Ignorable by Annotation )

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

84
                        if (!$grouppermHandler->insert(/** @scrutinizer ignore-type */ $gperm)) {
Loading history...
85
                            $msg[] = sprintf(_MD_AM_PERMADDNG, '<b>' . $perm_name . '</b>', '<b>' . $perm_data['itemname'][$item_id] . '</b>', '<b>' . $group_list[$group_id] . '</b>');
86
                        } else {
87
                            $msg[] = sprintf(_MD_AM_PERMADDOK, '<b>' . $perm_name . '</b>', '<b>' . $perm_data['itemname'][$item_id] . '</b>', '<b>' . $group_list[$group_id] . '</b>');
88
                        }
89
                        unset($gperm);
90
                    }
91
                }
92
            } else {
93
                $msg[] = sprintf(_MD_AM_PERMRESETNG, $module->getVar('name'));
94
            }
95
        }
96
    }
97
}
98
/*
99
$backlink = XOOPS_URL.'/admin.php';
100
if ($module->getVar('hasadmin')) {
101
    $adminindex = $module->getInfo('adminindex');
102
    if ($adminindex) {
103
        $backlink = XOOPS_URL.'/modules/'.$module->getVar('dirname').'/'.$adminindex;
104
    }
105
}
106
107
$msg[] = '<br><br><a href="'.$backlink.'">'._BACK.'</a>';
108
xoops_cp_header();
109
xoops_result($msg);
110
xoops_cp_footer();  GIJ */
111