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
eloc 9
c 0
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 4
nc 6
nop 4
1
<?php declare(strict_types=1);
2
3
use Xmf\Request;
4
5
/**
6
 * Module: SmartFAQ
7
 * Author: The SmartFactory <www.smartfactory.ca>
8
 * Licence: GNU
9
 * @param \XoopsDatabase $db
10
 * @param                $gperm_modid
11
 * @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...
12
 * @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...
13
 * @return bool
14
 */
15
function myDeleteByModule(\XoopsDatabase $db, $gperm_modid, $gperm_name = null, $gperm_itemid = null)
16
{
17
    $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', (int)$gperm_modid));
18
    if (null !== $gperm_name) {
0 ignored issues
show
introduced by
The condition null !== $gperm_name is always false.
Loading history...
19
        $criteria->add(new \Criteria('gperm_name', $gperm_name));
20
        if (null !== $gperm_itemid) {
21
            $criteria->add(new \Criteria('gperm_itemid', (int)$gperm_itemid));
22
        }
23
    }
24
    $sql = 'DELETE FROM ' . $db->prefix('group_permission') . ' ' . $criteria->renderWhere();
25
    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...
26
        return false;
27
    }
28
29
    return true;
30
}
31
32
// require_once  \dirname(__DIR__, 3) . '/include/cp_header.php'; GIJ
33
$modid = Request::getInt('modid', 1, 'POST');
34
// we don't want system module permissions to be changed here ( 1 -> 0 GIJ)
35
if ($modid <= 0 || !is_object($xoopsUser) || !$xoopsUser->isAdmin($modid)) {
36
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
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
}
44
/** @var \XoopsMemberHandler $memberHandler */
45
$memberHandler = xoops_getHandler('member');
46
$group_list    = $memberHandler->getGroupList();
47
if (is_array($_POST['perms']) && !empty($_POST['perms'])) {
48
    /** @var \XoopsGroupPermHandler $grouppermHandler */
49
    $grouppermHandler = xoops_getHandler('groupperm');
50
    foreach ($_POST['perms'] as $perm_name => $perm_data) {
51
        foreach ($perm_data['itemname'] as $item_id => $item_name) {
52
            // checking code
53
            // echo "<pre>" ;
54
            // var_dump( $_POST['perms'] ) ;
55
            // exit ;
56
            if (false !== myDeleteByModule($grouppermHandler->db, $modid, $perm_name, $item_id)) {
57
                if (empty($perm_data['groups'])) {
58
                    continue;
59
                }
60
                foreach ($perm_data['groups'] as $group_id => $item_ids) {
61
                    //              foreach ($item_ids as $item_id => $selected) {
62
                    $selected = $item_ids[$item_id] ?? 0;
63
                    if (1 == $selected) {
64
                        // make sure that all parent ids are selected as well
65
                        if ('' != $perm_data['parents'][$item_id]) {
66
                            $parent_ids = explode(':', $perm_data['parents'][$item_id]);
67
                            foreach ($parent_ids as $pid) {
68
                                if (0 != $pid && !array_key_exists($pid, $item_ids)) {
69
                                    // one of the parent items were not selected, so skip this item
70
                                    $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 . ')';
71
                                    continue 2;
72
                                }
73
                            }
74
                        }
75
                        $gperm = $grouppermHandler->create();
76
                        $gperm->setVar('gperm_groupid', $group_id);
77
                        $gperm->setVar('gperm_name', $perm_name);
78
                        $gperm->setVar('gperm_modid', $modid);
79
                        $gperm->setVar('gperm_itemid', $item_id);
80
                        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

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