assign_permission.php 4.37 KB
<?php
use yii\helpers\Url;

$this->title = '分配权限';
$this->params['breadcrumbs'][] = '系统管理';
$this->params['breadcrumbs'][] = ['label' => '角色管理', 'url' => ['/system/role/index']];
$this->params['breadcrumbs'][] = $this->title;

?>

<form action="<?=Url::toRoute(["/system/role/do-set-permission", 'id' => $role->roleId])?>" name="roleForm" id="roleForm" method="post">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title"><?= $role->name ?></h3>
        </div>

        <div class="panel-body">
            <table class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th style="width:10%;">模块</th>
                    <th style="width:15%;">权限组</th>
                    <th style="width:75%;">功能权限</th>
                </tr>
                </thead>
                <tbody>

                <?php foreach ($permission as $k => $p) : ?>
                    <?php
                    $count = count($p['items']);
                    $p1 = array_shift($p['items']) ;
                    ?>
                    <tr>
                        <td <?php if($count > 0) {echo 'rowspan="'.$count.'"';} ?>><input type="checkbox" class="check-1" data-group="<?= $k ?>"  /><?= $p['label'] ?></td>
                        <td><?php if($count > 0) :?><input type="checkbox" class="check-2 check-<?= $k ?>-2" data-group="<?= $k ?>" /><?= $p1['label'] ?> <?php endif;?></td>
                        <td>

                            <?php
                            if (isset($p1['items'])) :
                                foreach ($p1['items'] as $k1 => $pv1) : ?>
                                    <input type="checkbox"  class="check-3 check-<?= $k ?>-3" name="permission[]"  value="<?= $pv1['id'] ?>" <?php if (array_key_exists($pv1['id'], $selected)) echo 'checked'  ?>  /><?php echo $pv1['label'] . '&nbsp;&nbsp;&nbsp;&nbsp;'; ?>
                                    <?php
                                endforeach;
                            endif;

                            ?>
                        </td>
                    </tr>
                    <?php foreach ($p['items'] as $p2) : ?>
                        <tr>
                            <td><input type="checkbox"  class="check-2 check-<?= $k ?>-2"  data-group="<?= $k ?>" /><?= $p2['label'] ?></td>
                            <td>
                                <?php foreach ($p2['items'] as $k3 => $p3) : ?>
                                    <input type="checkbox"  class="check-3 check-<?= $k ?>-3"  name="permission[]" value="<?= $p3['id'] ?>"  <?php if (array_key_exists($p3['id'], $selected)) echo 'checked'  ?>/> <?php echo $p3['label'] . '&nbsp;&nbsp;&nbsp;&nbsp;'; ?>
                                <?php endforeach; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endforeach; ?>

                <tr>
                    <td></td>
                    <td colspan="2">
                        <button type="submit" class="btn btn-primary" id="save">保存</button>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="btn btn-default" href="<?=Url::toRoute(["/system/role/set-permission", 'id' => $role->roleId])?>">重置</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

    </div>

</form>

<script>
    // 表单提交验证
    seajs.use("base/1.0.0/unit/validate/custom-1.0.0",function  () {
        var validator = $("#roleForm").validate({
        });
    });

    $(document).ready(function() {
        $('.check-1').change(function(){
            var o = $(this);
            var group = o.data('group');
            var check2 = '.check-' + group + '-2';

            if (o.prop('checked')) {
                $(check2).prop('checked', true)
            } else {
                $(check2).prop('checked', false)
            }

            $(check2).change()
        });

        $('.check-2').change(function(){
            var o = $(this);
            var group = o.data('group');
            var check3 = '.check-' + group + '-3';

            if (o.prop('checked')) {
                o.parent().parent().find(check3).prop('checked', true)
            } else {
                o.parent().parent().find(check3).prop('checked', false)
            }

        });
    });

</script>