ai-ui/modules/user/views/admin/roles.php
2024-09-09 10:22:45 +02:00

153 lines
4.6 KiB
PHP

<?php
use Piko;
use function Piko\I18n\__;
assert($this instanceof Piko\View);
/* @var $roles array */
/* @var $permissions array */
$this->title = __('user', 'Roles');
$this->registerCSSFile(Piko::getAlias('@web/js/DataTables/datatables.min.css'));
$this->registerJsFile(Piko::getAlias('@web/js/jquery-3.7.1.min.js'));
$this->registerJsFile(Piko::getAlias('@web/js/DataTables/datatables.min.js'));
$confirmDeleteMsg = __('user', 'Are you sure you want to perform this action?');
$script = <<<JS
$(function() {
$('#roles-table').DataTable({
'order': [[1, 'desc']]
});
$('#delete').click(function(e) {
if (confirm('{$confirmDeleteMsg}')) {
$('#admin-form').attr('action', '/user/admin/delete-roles')
$('#admin-form').submit()
}
});
$('#btn-new, .edit-role').on('click', function(e) {
e.preventDefault();
var data = {
id: $(this).data('id'),
parent_id: $(this).data('parent_id'),
name: '',
description: $(this).data('description')
};
var action = $(this).attr('href');
const modal = new bootstrap.Modal('#editRoleModal');
if ($(this).hasClass('edit-role')) {
data.name = $(this).text();
}
$('#role-name').val(data.name);
$('#role-description').val(data.description);
$.ajax({
method: 'get',
url: action,
})
.done(function(data) {
if (data.role.permissions) {
$('#permissions').val(data.role.permissions)
}
});
modal.show();
$('#btn-save').on('click', function() {
if ($('#role-name').val()) {
data.name = $('#role-name').val();
data.description = $('#role-description').val();
data.parent_id = $('#role-parent-id').val();
data.permissions = $('#permissions').val();
$.ajax({
method: 'post',
url: action,
data: data
})
.done(function(data) {
if (data.status == 'success') {
location.reload();
}
});
}
});
});
});
JS;
$this->registerJs($script);
?>
<?= $this->render('nav', ['page' => 'roles']) ?>
<form action="" method="post" id="admin-form">
<div class="btn-group mb-4" role="group">
<a href="<?= $this->getUrl('user/admin/edit-role') ?>" class="btn btn-primary btn-sm" id="btn-new"><?= __('user', 'New role') ?></a>
<button type="button" class="btn btn-danger btn-sm" id="delete"><?= __('user', 'Delete') ?></button>
</div>
<table class="table table-striped" id="roles-table">
<thead>
<tr>
<th><?= __('user', 'Name') ?></th>
<th><?= __('user', 'Description') ?></th>
<th><?= __('user', 'Id') ?></th>
</tr>
</thead>
<tbody>
<?php foreach($roles as $role): ?>
<tr>
<td>
<input type="checkbox" name="items[]" value="<?= $role['id'] ?>">&nbsp;
<a href="<?= $this->getUrl('user/admin/edit-role', ['id' => $role['id']])?>"
class="edit-role"
data-description="<?= $role['description'] ?>"
data-id="<?= $role['id'] ?>"><?= $role['name'] ?></a>
</td>
<td><?= $role['description'] ?></td>
<td><?= $role['id'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</form>
<div class="modal fade" id="editRoleModal" tabindex="-1" role="dialog" aria-labelledby="editRoleModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<label for="role-name"><?= __('user', 'Role name') ?></label>
<input type="text" id="role-name" class="form-control">
</div>
<div class="form-group">
<label for="role-description"><?= __('user', 'Description') ?></label>
<textarea rows="3" class="form-control" id="role-description"></textarea>
</div>
<div class="form-group">
<label for="permissions"><?= __('user', 'Role permissions') ?></label>
<select class="form-select" id="permissions" multiple>
<?php foreach ($permissions as $perm): ?>
<option value="<?= $perm['id']?>"><?= $perm['name']?></option>
<?php endforeach ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= __('user', 'Cancel') ?></button>
<button type="button" class="btn btn-primary" id="btn-save"><?= __('user', 'Save') ?></button>
</div>
</div>
</div>
</div>