engelsystem/includes/pages/admin_rooms.php

163 lines
6.7 KiB
PHP
Raw Normal View History

2011-06-02 16:56:45 +02:00
<?php
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
global $user;
2014-08-22 22:34:13 +02:00
2012-04-26 22:37:37 +02:00
$rooms_source = sql_select("SELECT * FROM `Room` ORDER BY `Name`");
2014-08-22 22:34:13 +02:00
$rooms = array();
2012-04-26 22:37:37 +02:00
foreach ($rooms_source as $room)
2014-08-22 22:34:13 +02:00
$rooms[] = array(
'name' => $room['Name'],
'from_pentabarf' => $room['FromPentabarf'] == 'Y' ? '&#10003;' : '',
'public' => $room['show'] == 'Y' ? '&#10003;' : '',
'actions' => '<a class="ection edit" href="' . page_link_to('admin_rooms') . '&show=edit&id=' . $room['RID'] . '">edit</a> <a class="action delete" href="' . page_link_to('admin_rooms') . '&show=delete&id=' . $room['RID'] . '">delete</a>'
2012-04-26 22:37:37 +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 = "";
2014-08-22 22:34:13 +02:00
2012-04-26 22:37:37 +02:00
$angeltypes_source = sql_select("SELECT * FROM `AngelTypes` ORDER BY `name`");
2014-08-22 22:34:13 +02:00
$angeltypes = array();
$angeltypes_count = array();
2012-04-26 22:37:37 +02:00
foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype['name'];
$angeltypes_count[$angeltype['id']] = 0;
}
2014-08-22 22:34:13 +02:00
2012-04-26 22:37:37 +02:00
if (test_request_int('id')) {
$room = sql_select("SELECT * FROM `Room` WHERE `RID`=" . sql_escape($_REQUEST['id']));
if (count($room) > 0) {
$id = $_REQUEST['id'];
$name = $room[0]['Name'];
$from_pentabarf = $room[0]['FromPentabarf'];
$public = $room[0]['show'];
$needed_angeltypes = sql_select("SELECT * FROM `NeededAngelTypes` WHERE `room_id`=" . sql_escape($id));
foreach ($needed_angeltypes as $needed_angeltype)
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
} else
redirect(page_link_to('admin_rooms'));
}
2014-08-22 22:34:13 +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'])) {
2012-04-26 22:37:37 +02:00
$ok = true;
2014-08-22 22:34:13 +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');
else {
$ok = false;
$msg .= error("Please enter a name.", true);
}
2014-08-22 22:34:13 +02:00
if (isset($_REQUEST['from_pentabarf']))
2012-04-26 22:37:37 +02:00
$from_pentabarf = 'Y';
else
$from_pentabarf = '';
2014-08-22 22:34:13 +02:00
if (isset($_REQUEST['public']))
2012-04-26 22:37:37 +02:00
$public = 'Y';
else
$public = '';
2014-08-22 22:34:13 +02:00
if (isset($_REQUEST['number']))
2012-04-26 22:37:37 +02:00
$number = strip_request_item('number');
else
$ok = false;
2014-08-22 22:34:13 +02:00
foreach ($angeltypes as $angeltype_id => $angeltype) {
2014-08-22 22:34:13 +02:00
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 {
$ok = false;
$msg .= error(sprintf("Please enter needed angels for type %s.", $angeltype), true);
}
2012-04-26 22:37:37 +02:00
}
2014-08-22 22:34:13 +02:00
2012-04-26 22:37:37 +02:00
if ($ok) {
2014-08-22 22:34:13 +02:00
if (isset($id)) {
2012-04-26 22:37:37 +02:00
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($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 {
2014-08-22 22:34:13 +02:00
sql_query("INSERT INTO `Room` SET `Name`='" . sql_escape($name) . "', `FromPentabarf`='" . sql_escape($from_pentabarf) . "', `show`='" . sql_escape($public) . "', `Number`='" . sql_escape($number) . "'");
$id = sql_id();
2012-12-26 14:02:27 +01:00
engelsystem_log("Room created: " . $name . ", pentabarf import: " . $from_pentabarf . ", public: " . $public . ", number: " . $number);
}
2014-08-22 22:34:13 +02:00
2012-04-26 22:37:37 +02:00
sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`=" . sql_escape($id));
2012-12-26 14:02:27 +01:00
$needed_angeltype_info = array();
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
$angeltype_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`=" . sql_escape($angeltype_id) . " LIMIT 1");
2014-08-22 22:34:13 +02:00
if (count($angeltype_source) > 0) {
2012-12-26 14:02:27 +01:00
sql_query("INSERT INTO `NeededAngelTypes` SET `room_id`=" . sql_escape($id) . ", `angel_type_id`=" . sql_escape($angeltype_id) . ", `count`=" . sql_escape($angeltype_count));
$needed_angeltype_info[] = $angeltypes_source[0]['name'] . ": " . $angeltype_count;
}
}
2014-08-22 22:34:13 +02:00
2012-12-26 14:02:27 +01:00
engelsystem_log("Set needed angeltypes of room " . $name . " to: " . join(", ", $needed_angeltype_info));
2012-04-26 22:37:37 +02:00
success("Room saved.");
redirect(page_link_to("admin_rooms"));
}
}
2014-08-22 22:34:13 +02:00
$angeltypes_count_form = array();
2012-04-26 22:37:37 +02:00
foreach ($angeltypes as $angeltype_id => $angeltype)
$angeltypes_count_form[] = form_text('angeltype_count_' . $angeltype_id, $angeltype, $angeltypes_count[$angeltype_id]);
2014-08-22 22:34:13 +02:00
return page_with_title(admin_rooms_title(), array(
buttons(array(
button(page_link_to('admin_rooms'), "Back", 'back')
)),
$msg,
form(array(
form_text('name', "Name", $name),
form_checkbox('from_pentabarf', "Pentabarf-Import", $from_pentabarf),
form_checkbox('public', "Public", $public),
form_text('number', "Number", $number),
form_info("Needed angels:", ""),
join($angeltypes_count_form),
form_submit('submit', 'Save')
))
2012-04-26 22:37:37 +02:00
));
2014-08-22 22:34:13 +02:00
} elseif ($_REQUEST['show'] == 'delete') {
if (isset($_REQUEST['ack'])) {
2012-04-26 22:37:37 +02:00
sql_query("DELETE FROM `Room` WHERE `RID`=" . sql_escape($id) . " LIMIT 1");
sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`=" . sql_escape($id) . " LIMIT 1");
2014-08-22 22:34:13 +02:00
2012-12-26 14:02:27 +01:00
engelsystem_log("Room deleted: " . $name);
2012-04-26 22:37:37 +02:00
success(sprintf("Room %s deleted.", $name));
redirect(page_link_to('admin_rooms'));
}
2014-08-22 22:34:13 +02:00
return page_with_title(admin_rooms_title(), array(
buttons(array(
button(page_link_to('admin_rooms'), "Back", 'back')
)),
sprintf("Do you want to delete room %s?", $name),
buttons(array(
button(page_link_to('admin_rooms') . '&show=delete&id=' . $id . '&ack', "Delete", 'delete')
))
2012-04-26 22:37:37 +02:00
));
}
}
2014-08-22 22:34:13 +02:00
return page_with_title(admin_rooms_title(), array(
buttons(array(
button(page_link_to('admin_rooms') . '&show=edit', "Add", 'add')
)),
msg(),
table(array(
'name' => "Name",
'from_pentabarf' => "Pentabarf-Import",
'public' => "Public",
'actions' => ""
), $rooms)
2012-04-26 22:37:37 +02:00
));
2011-06-02 16:56:45 +02:00
}
?>