You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
9.4 KiB
PHTML

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