engelsystem/includes/pages/admin_rooms.php

196 lines
7.4 KiB
PHP
Raw Normal View History

2011-06-02 16:56:45 +02:00
<?php
2015-05-14 17:20:46 +02:00
2013-11-25 21:04:58 +01:00
function admin_rooms_title() {
return _("Rooms");
}
2011-06-02 16:56:45 +02:00
function admin_rooms() {
2012-04-26 22:37:37 +02:00
$rooms_source = sql_select("SELECT * FROM `Room` ORDER BY `Name`");
$rooms = [];
foreach ($rooms_source as $room) {
$rooms[] = [
2014-08-22 22:34:13 +02:00
'name' => $room['Name'],
'from_pentabarf' => $room['FromPentabarf'] == 'Y' ? '&#10003;' : '',
'public' => $room['show'] == 'Y' ? '&#10003;' : '',
'actions' => buttons([
2014-08-23 19:29:12 +02:00
button(page_link_to('admin_rooms') . '&show=edit&id=' . $room['RID'], _("edit"), 'btn-xs'),
2015-05-14 17:20:46 +02:00
button(page_link_to('admin_rooms') . '&show=delete&id=' . $room['RID'], _("delete"), 'btn-xs')
])
];
}
2016-02-01 11:48:40 +01:00
$room = null;
2015-05-14 17:20:46 +02:00
2014-08-22 22:34:13 +02:00
if (isset($_REQUEST['show'])) {
2012-04-26 22:37:37 +02:00
$msg = "";
$name = "";
$from_pentabarf = "";
$public = 'Y';
$number = "";
2015-05-14 17:20:46 +02:00
2012-04-26 22:37:37 +02:00
$angeltypes_source = sql_select("SELECT * FROM `AngelTypes` ORDER BY `name`");
$angeltypes = [];
$angeltypes_count = [];
2012-04-26 22:37:37 +02:00
foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype['name'];
$angeltypes_count[$angeltype['id']] = 0;
}
2015-05-14 17:20:46 +02:00
2012-04-26 22:37:37 +02:00
if (test_request_int('id')) {
2014-12-28 13:44:56 +01:00
$room = sql_select("SELECT * FROM `Room` WHERE `RID`='" . sql_escape($_REQUEST['id']) . "'");
2012-04-26 22:37:37 +02:00
if (count($room) > 0) {
2016-09-29 12:45:06 +02:00
$room_id = $_REQUEST['id'];
2012-04-26 22:37:37 +02:00
$name = $room[0]['Name'];
$from_pentabarf = $room[0]['FromPentabarf'];
$public = $room[0]['show'];
$number = $room[0]['Number'];
2016-09-29 12:45:06 +02:00
$needed_angeltypes = sql_select("SELECT * FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
foreach ($needed_angeltypes as $needed_angeltype) {
2012-04-26 22:37:37 +02:00
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
}
} else {
2012-04-26 22:37:37 +02:00
redirect(page_link_to('admin_rooms'));
}
2012-04-26 22:37:37 +02:00
}
2015-05-14 17:20:46 +02:00
2012-04-26 22:37:37 +02:00
if ($_REQUEST['show'] == 'edit') {
2014-08-22 22:34:13 +02:00
if (isset($_REQUEST['submit'])) {
2016-09-29 12:08:12 +02:00
$valid = true;
2015-05-14 17:20:46 +02:00
if (isset($_REQUEST['name']) && strlen(strip_request_item('name')) > 0) {
2012-04-26 22:37:37 +02:00
$name = strip_request_item('name');
2016-09-29 12:45:06 +02:00
if (isset($room) && sql_num_query("SELECT * FROM `Room` WHERE `Name`='" . sql_escape($name) . "' AND NOT `RID`=" . sql_escape($room_id)) > 0) {
2016-09-29 12:08:12 +02:00
$valid = false;
$msg .= error(_("This name is already in use."), true);
}
} else {
2016-09-29 12:08:12 +02:00
$valid = false;
2014-08-23 19:29:12 +02:00
$msg .= error(_("Please enter a name."), true);
2012-04-26 22:37:37 +02:00
}
2015-05-14 17:20:46 +02:00
if (isset($_REQUEST['from_pentabarf'])) {
2012-04-26 22:37:37 +02:00
$from_pentabarf = 'Y';
} else {
2012-04-26 22:37:37 +02:00
$from_pentabarf = '';
}
2015-05-14 17:20:46 +02:00
if (isset($_REQUEST['public'])) {
2012-04-26 22:37:37 +02:00
$public = 'Y';
} else {
2012-04-26 22:37:37 +02:00
$public = '';
}
2015-05-14 17:20:46 +02:00
if (isset($_REQUEST['number'])) {
2012-04-26 22:37:37 +02:00
$number = strip_request_item('number');
} else {
2016-09-29 12:08:12 +02:00
$valid = false;
}
2015-05-14 17:20:46 +02:00
foreach ($angeltypes as $angeltype_id => $angeltype) {
if (isset($_REQUEST['angeltype_count_' . $angeltype_id]) && preg_match("/^[0-9]{1,4}$/", $_REQUEST['angeltype_count_' . $angeltype_id])) {
$angeltypes_count[$angeltype_id] = $_REQUEST['angeltype_count_' . $angeltype_id];
} else {
2016-09-29 12:08:12 +02:00
$valid = false;
2014-08-23 19:29:12 +02:00
$msg .= error(sprintf(_("Please enter needed angels for type %s.", $angeltype)), true);
}
2012-04-26 22:37:37 +02:00
}
2015-05-14 17:20:46 +02:00
2016-09-29 12:08:12 +02:00
if ($valid) {
2016-09-29 12:45:06 +02:00
if (isset($room_id)) {
sql_query("UPDATE `Room` SET `Name`='" . sql_escape($name) . "', `FromPentabarf`='" . sql_escape($from_pentabarf) . "', `show`='" . sql_escape($public) . "', `Number`='" . sql_escape($number) . "' WHERE `RID`='" . sql_escape($room_id) . "' LIMIT 1");
2012-12-26 14:02:27 +01:00
engelsystem_log("Room updated: " . $name . ", pentabarf import: " . $from_pentabarf . ", public: " . $public . ", number: " . $number);
} else {
2016-09-29 12:45:06 +02:00
$room_id = Room_create($name, $from_pentabarf, $public, $number);
if ($room_id === false) {
2015-05-14 17:20:46 +02:00
engelsystem_error("Unable to create room.");
}
2012-12-26 14:02:27 +01:00
engelsystem_log("Room created: " . $name . ", pentabarf import: " . $from_pentabarf . ", public: " . $public . ", number: " . $number);
}
2015-05-14 17:20:46 +02:00
2016-09-29 12:45:06 +02:00
sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
2012-12-26 14:02:27 +01:00
$needed_angeltype_info = array();
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
2015-08-09 17:56:03 +02:00
$angeltype = AngelType($angeltype_id);
if ($angeltype === false) {
2015-08-09 17:56:03 +02:00
engelsystem_error("Unable to load angeltype.");
}
2015-08-09 17:56:03 +02:00
if ($angeltype != null) {
2016-09-29 12:45:06 +02:00
sql_query("INSERT INTO `NeededAngelTypes` SET `room_id`='" . sql_escape($room_id) . "', `angel_type_id`='" . sql_escape($angeltype_id) . "', `count`='" . sql_escape($angeltype_count) . "'");
2015-08-09 17:56:03 +02:00
$needed_angeltype_info[] = $angeltype['name'] . ": " . $angeltype_count;
2012-12-26 14:02:27 +01:00
}
}
2015-05-14 17:20:46 +02:00
2012-12-26 14:02:27 +01:00
engelsystem_log("Set needed angeltypes of room " . $name . " to: " . join(", ", $needed_angeltype_info));
2014-08-23 19:29:12 +02:00
success(_("Room saved."));
2012-04-26 22:37:37 +02:00
redirect(page_link_to("admin_rooms"));
}
}
$angeltypes_count_form = [];
foreach ($angeltypes as $angeltype_id => $angeltype) {
$angeltypes_count_form[] = div('col-lg-4 col-md-6 col-xs-6', [
2015-05-14 17:20:46 +02:00
form_spinner('angeltype_count_' . $angeltype_id, $angeltype, $angeltypes_count[$angeltype_id])
]);
}
2015-05-14 17:20:46 +02:00
return page_with_title(admin_rooms_title(), [
buttons([
2015-05-14 17:20:46 +02:00
button(page_link_to('admin_rooms'), _("back"), 'back')
]),
2014-08-22 22:34:13 +02:00
$msg,
form([
div('row', [
div('col-md-6', [
2015-05-14 17:20:46 +02: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)
]),
div('col-md-6', [
div('row', [
div('col-md-12', [
2015-05-14 17:20:46 +02:00
form_info(_("Needed angels:"))
]),
2015-05-14 17:20:46 +02:00
join($angeltypes_count_form)
])
])
]),
2015-05-14 17:20:46 +02:00
form_submit('submit', _("Save"))
])
]);
2014-08-22 22:34:13 +02:00
} elseif ($_REQUEST['show'] == 'delete') {
if (isset($_REQUEST['ack'])) {
2016-09-29 12:45:06 +02:00
if (! Room_delete($room_id)) {
2015-12-04 21:50:39 +01:00
engelsystem_error("Unable to delete room.");
}
2015-05-14 17:20:46 +02:00
2012-12-26 14:02:27 +01:00
engelsystem_log("Room deleted: " . $name);
2014-08-23 19:29:12 +02:00
success(sprintf(_("Room %s deleted."), $name));
2012-04-26 22:37:37 +02:00
redirect(page_link_to('admin_rooms'));
}
2015-05-14 17:20:46 +02:00
return page_with_title(admin_rooms_title(), [
buttons([
2015-05-14 17:20:46 +02:00
button(page_link_to('admin_rooms'), _("back"), 'back')
]),
2014-08-23 19:29:12 +02:00
sprintf(_("Do you want to delete room %s?"), $name),
buttons([
2016-09-29 12:45:06 +02:00
button(page_link_to('admin_rooms') . '&show=delete&id=' . $room_id . '&ack', _("Delete"), 'delete')
])
]);
2012-04-26 22:37:37 +02:00
}
}
2015-05-14 17:20:46 +02:00
return page_with_title(admin_rooms_title(), [
buttons([
2015-05-14 17:20:46 +02:00
button(page_link_to('admin_rooms') . '&show=edit', _("add"))
]),
2014-08-22 22:34:13 +02:00
msg(),
table([
2014-08-23 19:29:12 +02:00
'name' => _("Name"),
'from_pentabarf' => _("Frab import"),
'public' => _("Public"),
2015-05-14 17:20:46 +02:00
'actions' => ""
], $rooms)
]);
2011-06-02 16:56:45 +02:00
}
?>