myDeleteByModule()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 15
rs 9.9666
1
<?php
2
3
if (!defined('XOOPS_ROOT_PATH')) {
4
    exit;
5
}
6
7
/**
8
 * @param      $DB
9
 * @param      $gperm_modid
10
 * @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...
11
 * @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...
12
 * @return bool
13
 */
14
function myDeleteByModule($DB, $gperm_modid, $gperm_name = null, $gperm_itemid = null)
15
{
16
    $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)$gperm_modid));
17
    if (isset($gperm_name)) {
18
        $criteria->add(new Criteria('gperm_name', $gperm_name));
19
        if (isset($gperm_itemid)) {
20
            $criteria->add(new Criteria('gperm_itemid', (int)$gperm_itemid));
21
        }
22
    }
23
    $sql = 'DELETE FROM ' . $DB->prefix('group_permission') . ' ' . $criteria->renderWhere();
24
    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...
25
        return false;
26
    }
27
28
    return true;
29
}
30
31
// require  \dirname(__DIR__, 3) . '/include/cp_header.php'; GIJ
32
$modid = isset($_POST['modid']) ? (int)$_POST['modid'] : 1;
33
// we dont want system module permissions to be changed here ( 1 -> 0 GIJ)
34
if ($modid <= 0 || !is_object($xoopsUser) || !$xoopsUser->isAdmin($modid)) {
35
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
36
    exit();
37
}
38
/** @var \XoopsModuleHandler $moduleHandler */
39
$moduleHandler = xoops_getHandler('module');
40
$module        = $moduleHandler->get($modid);
41
if (!is_object($module) || !$module->getVar('isactive')) {
42
    redirect_header(XOOPS_URL . '/admin.php', 1, _MODULENOEXIST);
43
    exit();
44
}
45
/** @var \XoopsMemberHandler $memberHandler */
46
$memberHandler = xoops_getHandler('member');
47
$group_list    = $memberHandler->getGroupList();
48
if (is_array($_POST['perms']) && !empty($_POST['perms'])) {
49
    /** @var \XoopsGroupPermHandler $grouppermHandler */
50
    $grouppermHandler = xoops_getHandler('groupperm');
51
    foreach ($_POST['perms'] as $perm_name => $perm_data) {
52
        foreach ($perm_data['itemname'] as $item_id => $item_name) {
53
            // checking code
54
            // echo "<pre>" ;
55
            // var_dump( $_POST['perms'] ) ;
56
            // exit ;
57
            if (false !== myDeleteByModule($grouppermHandler->db, $modid, $perm_name, $item_id)) {
58
                if (empty($perm_data['groups'])) {
59
                    continue;
60
                }
61
                foreach ($perm_data['groups'] as $group_id => $item_ids) {
62
                    //              foreach ($item_ids as $item_id => $selected) {
63
                    $selected = $item_ids[$item_id] ?? 0;
64
                    if (1 == $selected) {
65
                        // make sure that all parent ids are selected as well
66
                        if ('' != $perm_data['parents'][$item_id]) {
67
                            $parent_ids = explode(':', $perm_data['parents'][$item_id]);
68
                            foreach ($parent_ids as $pid) {
69
                                if (0 != $pid && !array_key_exists($pid, $item_ids)) {
70
                                    // one of the parent items were not selected, so skip this item
71
                                    $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 . ')';
72
                                    continue 2;
73
                                }
74
                            }
75
                        }
76
                        $gperm = $grouppermHandler->create();
77
                        $gperm->setVar('gperm_groupid', $group_id);
78
                        $gperm->setVar('gperm_name', $perm_name);
79
                        $gperm->setVar('gperm_modid', $modid);
80
                        $gperm->setVar('gperm_itemid', $item_id);
81
                        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

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