engelsystem/includes/pages/admin_groups.php

151 lines
5.1 KiB
PHP
Raw Normal View History

2011-06-02 22:40:08 +02:00
<?php
use Engelsystem\Models\Group;
use Engelsystem\Models\Privilege;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_groups_title()
{
return __('Grouprights');
2013-11-25 21:04:58 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_groups()
{
2017-01-03 14:12:17 +01:00
$html = '';
$request = request();
/** @var Group[]|Collection $groups */
$groups = Group::query()->orderBy('name')->get();
if (!$request->has('action')) {
2017-01-02 03:57:23 +01:00
$groups_table = [];
foreach ($groups as $group) {
/** @var Privilege[]|Collection $privileges */
$privileges = $group->privileges()->orderBy('name')->get();
2017-01-02 03:57:23 +01:00
$privileges_html = [];
2017-01-02 15:43:36 +01:00
foreach ($privileges as $privilege) {
2023-12-04 23:33:07 +01:00
$privileges_html[] = htmlspecialchars($privilege['name']);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$groups_table[] = [
2023-12-04 23:33:07 +01:00
'name' => htmlspecialchars($group->name),
2017-01-02 15:43:36 +01:00
'privileges' => join(', ', $privileges_html),
'actions' => button(
2022-10-18 19:15:22 +02:00
page_link_to(
'admin_groups',
['action' => 'edit', 'id' => $group->id]
2022-10-18 19:15:22 +02:00
),
2022-12-02 23:03:23 +01:00
icon('pencil') . __('edit'),
2021-07-23 01:52:19 +02:00
'btn-sm'
),
2017-01-02 15:43:36 +01:00
];
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return page_with_title(admin_groups_title(), [
2017-01-02 15:43:36 +01:00
table([
'name' => __('Name'),
'privileges' => __('Privileges'),
'actions' => '',
], $groups_table),
2017-01-02 15:43:36 +01:00
]);
2017-01-02 03:57:23 +01:00
} else {
switch ($request->input('action')) {
2017-01-02 15:43:36 +01:00
case 'edit':
2019-07-28 15:33:01 +02:00
if ($request->has('id')) {
$group_id = (int) $request->input('id');
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('Incomplete call, missing Groups ID.', true);
2017-01-02 15:43:36 +01:00
}
/** @var Group|null $group */
$group = Group::find($group_id);
if (!empty($group)) {
$privileges = groupPrivilegesWithSelected($group);
2017-01-02 15:43:36 +01:00
$privileges_form = [];
foreach ($privileges as $privilege) {
2017-01-02 15:43:36 +01:00
$privileges_form[] = form_checkbox(
'privileges[]',
2023-12-04 23:33:07 +01:00
htmlspecialchars($privilege->description . ' (' . $privilege->name . ')'),
$privilege->selected != '',
$privilege->id,
2023-12-04 23:33:07 +01:00
'privilege-' . htmlspecialchars($privilege->name)
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$privileges_form[] = form_submit('submit', __('Save'));
2023-12-04 23:33:07 +01:00
$html .= page_with_title(__('Edit group') . ' ' . htmlspecialchars($group->name), [
2017-08-28 16:21:10 +02:00
form(
$privileges_form,
page_link_to('admin_groups', ['action' => 'save', 'id' => $group->id])
),
2017-01-02 15:43:36 +01:00
]);
} else {
2017-01-03 14:12:17 +01:00
return error('No Group found.', true);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
break;
case 'save':
if (
$request->has('id')
&& $request->hasPostData('submit')
) {
$group_id = (int) $request->input('id');
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('Incomplete call, missing Groups ID.', true);
2017-01-02 15:43:36 +01:00
}
/** @var Group|null $group */
$group = Group::find($group_id);
2020-09-06 23:50:36 +02:00
$privileges = $request->request->all('privileges');
if (!empty($group)) {
$group->privileges()->detach();
2017-01-02 15:43:36 +01:00
$privilege_names = [];
foreach ($privileges as $privilege) {
$privilege = Privilege::find($privilege);
2019-07-28 15:33:01 +02:00
if ($privilege) {
$group->privileges()->attach($privilege);
$privilege_names[] = $privilege->name;
2017-01-02 15:43:36 +01:00
}
}
engelsystem_log(
'Group privileges of group ' . $group->name
2017-01-03 14:12:17 +01:00
. ' edited: ' . join(', ', $privilege_names)
2017-01-02 15:43:36 +01:00
);
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('admin_groups'));
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('No Group found.', true);
2017-01-02 15:43:36 +01:00
}
break;
}
2017-01-02 03:57:23 +01:00
}
return $html;
2011-06-02 22:40:08 +02:00
}
/**
* @param Group $group
* @return Collection|Privilege[]
*/
function groupPrivilegesWithSelected(Group $group): Collection
{
return Privilege::query()
->join('group_privileges', function ($query) use ($group) {
/** @var JoinClause $query */
$query
->where('privileges.id', '=', $query->raw('group_privileges.privilege_id'))
->where('group_privileges.group_id', $group->id)
;
}, null, null, 'left outer')
->orderBy('name')
->get([
'privileges.*',
'group_privileges.group_id as selected',
]);
}