advanced form in user administration for confirming angeltypes

This commit is contained in:
Jan-Philipp Litza 2012-12-26 16:58:09 +01:00
parent 7344a08a2f
commit 51c6547610
3 changed files with 77 additions and 16 deletions

View File

@ -92,28 +92,38 @@ function admin_user() {
// UserAngelType subform // UserAngelType subform
list ($user_source) = sql_select($SQL); list ($user_source) = sql_select($SQL);
$selected_angel_types_source = sql_select("SELECT * FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID'])); $selected_angel_types = sql_select_single_col("SELECT `angeltype_id` FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']));
$selected_angel_types = array (); $accepted_angel_types = sql_select_single_col("SELECT `angeltype_id` FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']) . " AND `confirm_user_id` IS NOT NULL");
foreach ($selected_angel_types_source as $selected_angel_type) $nonrestricted_angel_types = sql_select_single_col("SELECT `id` FROM `AngelTypes` WHERE `restricted` = 0");
$selected_angel_types[] = $selected_angel_type['angeltype_id'];
$angel_types_source = sql_select("SELECT * FROM `AngelTypes` ORDER BY `name`"); $angel_types_source = sql_select("SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`");
$angel_types = array(); $angel_types = array();
foreach ($angel_types_source as $angel_type) foreach ($angel_types_source as $angel_type)
$angel_types[$angel_type['id']] = $angel_type['name'] . ($angel_type['restricted'] ? " (restricted)" : ""); $angel_types[$angel_type['id']] = $angel_type['name'];
if (isset ($_REQUEST['submit_user_angeltypes'])) { if (isset ($_REQUEST['submit_user_angeltypes'])) {
$selected_angel_types = array (); $selected_angel_types = array_intersect($_REQUEST['selected_angel_types'], array_keys($angel_types));
foreach ($angel_types as $angel_type_id => $angel_type_name) { $accepted_angel_types = array_diff(array_intersect($_REQUEST['accepted_angel_types'], array_keys($angel_types)), $nonrestricted_angel_types);
if (isset ($_REQUEST['angel_types_' . $angel_type_id])) if (in_array("admin_user_angeltypes", $privileges))
$selected_angel_types[] = $angel_type_id; $selected_angel_types = array_merge($selected_angel_types, $accepted_angel_types);
}
// Assign angel-types // Assign angel-types
foreach ($angel_types_source as $angel_type) { sql_start_transaction();
if (!in_array($angel_type['id'], $selected_angel_types)) sql_query("DELETE FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']));
sql_query("DELETE FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']) . " AND `angeltype_id`=" . sql_escape($angel_type['id']) . " LIMIT 1"); if (!empty($selected_angel_types)) {
$SQL = "INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES ";
foreach ($selected_angel_types as $selected_angel_type_id)
$SQL .= "(${user_source['UID']}, ${selected_angel_type_id}),";
// remove superfluous comma
$SQL = substr($SQL, 0, -1);
sql_query($SQL);
} }
if (in_array("admin_user_angeltypes", $privileges)) {
sql_query("UPDATE `UserAngelTypes` SET `confirm_user_id` = NULL WHERE `user_id` = " . sql_escape($user_source['UID']));
if (!empty($accepted_angel_types))
sql_query("UPDATE `UserAngelTypes` SET `confirm_user_id` = '" . sql_escape($user['UID']) . "' WHERE `user_id` = '" . sql_escape($user_source['UID']) . "' AND `angeltype_id` IN (" . implode(',', $accepted_angel_types) . ")");
}
sql_stop_transaction();
foreach ($selected_angel_types as $selected_angel_type_id) { foreach ($selected_angel_types as $selected_angel_type_id) {
if (sql_num_query("SELECT * FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']) . " AND `angeltype_id`=" . sql_escape($selected_angel_type_id) . " LIMIT 1") == 0) { if (sql_num_query("SELECT * FROM `UserAngelTypes` WHERE `user_id`=" . sql_escape($user_source['UID']) . " AND `angeltype_id`=" . sql_escape($selected_angel_type_id) . " LIMIT 1") == 0) {
@ -131,7 +141,11 @@ function admin_user() {
$html .= form(array ( $html .= form(array (
msg(), msg(),
form_checkboxes('angel_types', "Angeltypes", $angel_types, $selected_angel_types), form_multi_checkboxes(array('selected_angel_types' => 'gewünscht', 'accepted_angel_types' => 'akzeptiert'),
"Angeltypes",
$angel_types,
array('selected_angel_types' => $selected_angel_types, 'accepted_angel_types' => array_merge($accepted_angel_types, $nonrestricted_angel_types)),
array('accepted_angel_types' => $nonrestricted_angel_types)),
form_submit('submit_user_angeltypes', Get_Text("Save")) form_submit('submit_user_angeltypes', Get_Text("Save"))
)); ));

View File

@ -31,6 +31,12 @@ function sql_select($query) {
} }
} }
function sql_select_single_col($query) {
$result = sql_select($query);
return array_map('array_pop', $result);
}
// Execute a query // Execute a query
function sql_query($query) { function sql_query($query) {
global $con; global $con;
@ -59,4 +65,17 @@ function sql_error() {
global $con; global $con;
return mysql_error($con); return mysql_error($con);
} }
$sql_transaction_counter = 0;
function sql_start_transaction() {
global $sql_transaction_counter;
if ($sql_transaction_counter++ == 0)
sql_query("START TRANSACTION");
}
function sql_stop_transaction() {
global $sql_transaction_counter;
if ($sql_transaction_counter-- == 1)
sql_query("COMMIT");
}
?> ?>

View File

@ -36,6 +36,34 @@ function form_checkboxes($name, $label, $items, $selected) {
return form_element($label, $html); return form_element($label, $html);
} }
/**
* Rendert eine Tabelle von Checkboxen für ein Formular
* @param names Assoziatives Array mit Namen der Checkboxen als Keys und Überschriften als Values
* @param label Die Beschriftung der gesamten Tabelle
* @param items Array mit den Beschriftungen der Zeilen
* @param selected Mehrdimensionales Array, wobei $selected[foo] ein Array der in der Datenreihe foo markierten Checkboxen ist
* @param disabled Wie selected, nur dass die entsprechenden Checkboxen deaktiviert statt markiert sind
*/
function form_multi_checkboxes($names, $label, $items, $selected, $disabled = array()) {
$html = "<table><thead><tr>";
foreach ($names as $title)
$html .= "<th>$title</th>";
$html .= "</tr></thead><tbody>";
foreach ($items as $key => $item) {
$html .= "<tr>";
foreach ($names as $name => $title) {
$id = $name . '_' . $key;
$sel = array_search($key, $selected[$name]) !== false ? ' checked="checked"' : "";
if (!empty($disabled) && !empty($disabled[$name]) && array_search($key, $disabled[$name]) !== false)
$sel .= ' disabled="disabled"';
$html .= '<td style="text-align: center;"><input type="checkbox" id="' . $id . '" name="' . $name . '[]" value="' . $key . '"' . $sel . ' /></td>';
}
$html .= '<td><label for="' . $id . '">' . $item . '</label></td></tr>';
}
$html .= "</tbody></table>";
return form_element($label, $html);
}
/** /**
* Rendert eine Checkbox * Rendert eine Checkbox
*/ */