engelsystem/includes/pages/admin_rooms.php

252 lines
9.3 KiB
PHP
Raw Normal View History

2011-06-02 16:56:45 +02:00
<?php
2015-05-14 17:20:46 +02:00
use Engelsystem\Database\DB;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_rooms_title()
{
2017-01-03 14:12:17 +01:00
return _('Rooms');
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_rooms()
{
$rooms_source = DB::select('SELECT * FROM `Room` ORDER BY `Name`');
2017-01-02 03:57:23 +01:00
$rooms = [];
$request = request();
2017-01-02 03:57:23 +01:00
foreach ($rooms_source as $room) {
$rooms[] = [
2017-01-02 15:43:36 +01:00
'name' => Room_name_render($room),
'from_pentabarf' => $room['FromPentabarf'] == 'Y' ? '&#10003;' : '',
'public' => $room['show'] == 'Y' ? '&#10003;' : '',
'actions' => table_buttons([
2017-08-28 16:21:10 +02:00
button(page_link_to('admin_rooms', ['show' => 'edit', 'id' => $room['RID']]), _('edit'), 'btn-xs'),
button(page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room['RID']]), _('delete'), 'btn-xs')
2017-01-02 15:43:36 +01:00
])
];
2017-01-02 03:57:23 +01:00
}
$room = null;
2017-01-02 15:43:36 +01:00
if ($request->has('show')) {
2017-01-03 14:12:17 +01:00
$msg = '';
$name = '';
$from_pentabarf = '';
2017-01-02 03:57:23 +01:00
$public = 'Y';
2017-01-03 14:12:17 +01:00
$number = '';
2017-01-03 03:22:48 +01:00
$room_id = 0;
2017-01-02 15:43:36 +01:00
$angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
2017-01-02 03:57:23 +01:00
$angeltypes = [];
$angeltypes_count = [];
foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype['name'];
$angeltypes_count[$angeltype['id']] = 0;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if (test_request_int('id')) {
$room = Room($request->input('id'), false);
2017-01-02 03:57:23 +01:00
if ($room === false) {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to load room.');
2017-01-02 03:57:23 +01:00
}
if ($room == null) {
redirect(page_link_to('admin_rooms'));
}
2017-01-02 15:43:36 +01:00
$room_id = $request->input('id');
2017-01-02 03:57:23 +01:00
$name = $room['Name'];
$from_pentabarf = $room['FromPentabarf'];
$public = $room['show'];
$number = $room['Number'];
2017-01-02 15:43:36 +01:00
$needed_angeltypes = DB::select(
'SELECT `angel_type_id`, `count` FROM `NeededAngelTypes` WHERE `room_id`=?',
[$room_id]
);
2017-01-02 03:57:23 +01:00
foreach ($needed_angeltypes as $needed_angeltype) {
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
}
}
2017-01-02 15:43:36 +01:00
if ($request->input('show') == 'edit') {
if ($request->has('submit')) {
2017-01-02 03:57:23 +01:00
$valid = true;
2017-01-02 15:43:36 +01:00
if ($request->has('name') && strlen(strip_request_item('name')) > 0) {
2017-01-02 03:57:23 +01:00
$name = strip_request_item('name');
if (
isset($room)
&& count(DB::select(
'SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?',
[$name, $room_id]
)) > 0
) {
2017-01-02 03:57:23 +01:00
$valid = false;
2017-01-03 14:12:17 +01:00
$msg .= error(_('This name is already in use.'), true);
2017-01-02 03:57:23 +01:00
}
} else {
$valid = false;
2017-01-03 14:12:17 +01:00
$msg .= error(_('Please enter a name.'), true);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$from_pentabarf = '';
if ($request->has('from_pentabarf')) {
2017-01-02 03:57:23 +01:00
$from_pentabarf = 'Y';
}
2017-01-02 15:43:36 +01:00
$public = '';
if ($request->has('public')) {
2017-01-02 03:57:23 +01:00
$public = 'Y';
}
2017-01-02 15:43:36 +01:00
if ($request->has('number')) {
2017-01-02 03:57:23 +01:00
$number = strip_request_item('number');
} else {
$valid = false;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
foreach ($angeltypes as $angeltype_id => $angeltype) {
2017-01-02 15:43:36 +01:00
if (
$request->has('angeltype_count_' . $angeltype_id)
&& preg_match('/^\d{1,4}$/', $request->input('angeltype_count_' . $angeltype_id))
2017-01-02 15:43:36 +01:00
) {
$angeltypes_count[$angeltype_id] = $request->input('angeltype_count_' . $angeltype_id);
2017-01-02 03:57:23 +01:00
} else {
$valid = false;
2017-01-03 14:12:17 +01:00
$msg .= error(sprintf(_('Please enter needed angels for type %s.'), $angeltype), true);
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($valid) {
if (!empty($room_id)) {
DB::update('
2017-01-03 14:12:17 +01:00
UPDATE `Room`
SET
`Name`=?,
`FromPentabarf`=?,
`show`=?,
`Number`=?
WHERE `RID`=?
2017-01-03 14:12:17 +01:00
LIMIT 1
', [
$name,
$from_pentabarf,
$public,
$number,
$room_id,
]);
2017-01-03 14:12:17 +01:00
engelsystem_log(
'Room updated: ' . $name
. ', pentabarf import: ' . $from_pentabarf
. ', public: ' . $public
. ', number: ' . $number
);
2017-01-02 03:57:23 +01:00
} else {
$room_id = Room_create($name, $from_pentabarf, $public, $number);
if ($room_id === false) {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to create room.');
2017-01-02 03:57:23 +01:00
}
2017-01-03 14:12:17 +01:00
engelsystem_log(
'Room created: ' . $name
. ', pentabarf import: '
. $from_pentabarf
. ', public: ' . $public
. ', number: ' . $number
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
NeededAngelTypes_delete_by_room($room_id);
$needed_angeltype_info = [];
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
$angeltype = AngelType($angeltype_id);
if ($angeltype != null) {
NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count);
2017-01-03 14:12:17 +01:00
$needed_angeltype_info[] = $angeltype['name'] . ': ' . $angeltype_count;
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
engelsystem_log(
2017-01-03 14:12:17 +01:00
'Set needed angeltypes of room ' . $name
. ' to: ' . join(', ', $needed_angeltype_info)
2017-01-02 15:43:36 +01:00
);
2017-01-03 14:12:17 +01:00
success(_('Room saved.'));
redirect(page_link_to('admin_rooms'));
2017-01-02 03:57:23 +01:00
}
}
$angeltypes_count_form = [];
foreach ($angeltypes as $angeltype_id => $angeltype) {
$angeltypes_count_form[] = div('col-lg-4 col-md-6 col-xs-6', [
2017-01-02 15:43:36 +01:00
form_spinner('angeltype_count_' . $angeltype_id, $angeltype, $angeltypes_count[$angeltype_id])
]);
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_rooms_title(), [
2017-01-02 15:43:36 +01:00
buttons([
2017-01-03 14:12:17 +01:00
button(page_link_to('admin_rooms'), _('back'), 'back')
2017-01-02 15:43:36 +01:00
]),
$msg,
form([
div('row', [
div('col-md-6', [
2017-01-03 14:12:17 +01:00
form_text('name', _('Name'), $name),
form_checkbox('from_pentabarf', _('Frab import'), $from_pentabarf),
form_checkbox('public', _('Public'), $public),
form_text('number', _('Room number'), $number)
2017-01-02 15:43:36 +01:00
]),
div('col-md-6', [
div('row', [
div('col-md-12', [
2017-01-03 14:12:17 +01:00
form_info(_('Needed angels:'))
2017-01-02 15:43:36 +01:00
]),
join($angeltypes_count_form)
])
])
]),
2017-01-03 14:12:17 +01:00
form_submit('submit', _('Save'))
2017-01-02 15:43:36 +01:00
])
]);
} elseif ($request->input('show') == 'delete') {
if ($request->has('ack')) {
2017-01-02 15:43:36 +01:00
if (!Room_delete($room_id)) {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to delete room.');
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
engelsystem_log('Room deleted: ' . $name);
success(sprintf(_('Room %s deleted.'), $name));
2017-01-02 03:57:23 +01:00
redirect(page_link_to('admin_rooms'));
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return page_with_title(admin_rooms_title(), [
2017-01-02 15:43:36 +01:00
buttons([
2017-01-03 14:12:17 +01:00
button(page_link_to('admin_rooms'), _('back'), 'back')
2017-01-02 15:43:36 +01:00
]),
2017-01-03 14:12:17 +01:00
sprintf(_('Do you want to delete room %s?'), $name),
2017-01-02 15:43:36 +01:00
buttons([
2017-01-03 15:32:12 +01:00
button(
2017-08-28 16:21:10 +02:00
page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room_id, 'ack' => 1]),
2017-01-03 15:32:12 +01:00
_('Delete'),
'delete btn-danger'
)
2017-01-02 15:43:36 +01:00
])
]);
2017-01-02 03:57:23 +01:00
}
2012-04-26 22:37:37 +02:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return page_with_title(admin_rooms_title(), [
2017-01-02 15:43:36 +01:00
buttons([
2017-08-28 16:21:10 +02:00
button(page_link_to('admin_rooms', ['show' => 'edit']), _('add'))
2017-01-02 15:43:36 +01:00
]),
msg(),
table([
2017-01-03 14:12:17 +01:00
'name' => _('Name'),
'from_pentabarf' => _('Frab import'),
'public' => _('Public'),
'actions' => ''
2017-01-02 15:43:36 +01:00
], $rooms)
]);
2011-06-02 16:56:45 +02:00
}