reduce complexity of angeltype edit controller

main
msquare 8 years ago
parent 9900d6e14c
commit 1636688394

@ -73,12 +73,7 @@ function angeltype_delete_controller() {
} }
if (isset($_REQUEST['confirmed'])) { if (isset($_REQUEST['confirmed'])) {
$result = AngelType_delete($angeltype); AngelType_delete($angeltype);
if ($result === false) {
engelsystem_error("Unable to delete angeltype.");
}
engelsystem_log("Deleted angeltype: " . AngelType_name_render($angeltype));
success(sprintf(_("Angeltype %s deleted."), AngelType_name_render($angeltype))); success(sprintf(_("Angeltype %s deleted."), AngelType_name_render($angeltype)));
redirect(page_link_to('angeltypes')); redirect(page_link_to('angeltypes'));
} }
@ -95,11 +90,8 @@ function angeltype_delete_controller() {
function angeltype_edit_controller() { function angeltype_edit_controller() {
global $privileges, $user; global $privileges, $user;
$angeltype = null; // In supporter mode only allow to modify description
$name = ""; $supporter_mode = ! in_array('admin_angel_types', $privileges);
$restricted = false;
$description = "";
$requires_driver_license = false;
if (isset($_REQUEST['angeltype_id'])) { if (isset($_REQUEST['angeltype_id'])) {
$angeltype = AngelType($_REQUEST['angeltype_id']); $angeltype = AngelType($_REQUEST['angeltype_id']);
@ -107,68 +99,50 @@ function angeltype_edit_controller() {
redirect(page_link_to('angeltypes')); redirect(page_link_to('angeltypes'));
} }
$name = $angeltype['name'];
$restricted = $angeltype['restricted'];
$description = $angeltype['description'];
$requires_driver_license = $angeltype['requires_driver_license'];
if (! User_is_AngelType_supporter($user, $angeltype)) { if (! User_is_AngelType_supporter($user, $angeltype)) {
redirect(page_link_to('angeltypes')); redirect(page_link_to('angeltypes'));
} }
} else { } else {
if (! in_array('admin_angel_types', $privileges)) { if ($supporter_mode) {
redirect(page_link_to('angeltypes')); redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType_new();
} }
// In supporter mode only allow to modify description
$supporter_mode = ! in_array('admin_angel_types', $privileges);
if (isset($_REQUEST['submit'])) { if (isset($_REQUEST['submit'])) {
$valid = true; $valid = true;
if (! $supporter_mode) { if (! $supporter_mode) {
if (isset($_REQUEST['name'])) { if (isset($_REQUEST['name'])) {
$result = AngelType_validate_name($_REQUEST['name'], $angeltype); $result = AngelType_validate_name($_REQUEST['name'], $angeltype);
$name = $result->getValue(); $angeltype['name'] = $result->getValue();
if (! $result->isValid()) { if (! $result->isValid()) {
$valid = false; $valid = false;
error(_("Please check the name. Maybe it already exists.")); error(_("Please check the name. Maybe it already exists."));
} }
} }
$restricted = isset($_REQUEST['restricted']); $angeltype['restricted'] = isset($_REQUEST['restricted']);
$requires_driver_license = isset($_REQUEST['requires_driver_license']); $angeltype['requires_driver_license'] = isset($_REQUEST['requires_driver_license']);
} }
if (isset($_REQUEST['description'])) { $angeltype['description'] = strip_request_item_nl('description', $angeltype['description']);
$description = strip_request_item_nl('description');
}
if ($valid) { if ($valid) {
if (isset($angeltype)) { if ($angeltype['id'] != null) {
$result = AngelType_update($angeltype['id'], $name, $restricted, $description, $requires_driver_license); AngelType_update($angeltype);
if ($result === false) {
engelsystem_error("Unable to update angeltype.");
}
engelsystem_log("Updated angeltype: " . $name . ($restricted ? ", restricted" : "") . ($requires_driver_license ? ", requires driver license" : ""));
$angeltype_id = $angeltype['id'];
} else { } else {
$angeltype_id = AngelType_create($name, $restricted, $description, $requires_driver_license); $angeltype = AngelType_create($angeltype);
if ($angeltype_id === false) {
engelsystem_error("Unable to create angeltype.");
}
engelsystem_log("Created angeltype: " . $name . ($restricted ? ", restricted" : "") . ($requires_driver_license ? ", requires driver license" : ""));
} }
success("Angel type saved."); success("Angel type saved.");
redirect(angeltype_link($angeltype_id)); redirect(angeltype_link($angeltype['id']));
} }
} }
return [ return [
sprintf(_("Edit %s"), $name), sprintf(_("Edit %s"), $angeltype['name']),
AngelType_edit_view($name, $restricted, $description, $supporter_mode, $requires_driver_license) AngelType_edit_view($angeltype, $supporter_mode)
]; ];
} }

@ -1,56 +1,77 @@
<?php <?php
/**
* Returns an array containing the basic attributes of angeltypes.
* FIXME! This is the big sign for needing entity objects
*/
function AngelType_new() {
return [
'id' => null,
'name' => "",
'restricted' => false,
'description' => '',
'requires_driver_license' => false
];
}
/** /**
* Delete an Angeltype. * Delete an Angeltype.
* *
* @param Angeltype $angeltype * @param Angeltype $angeltype
*/ */
function AngelType_delete($angeltype) { function AngelType_delete($angeltype) {
return sql_query(" $result = sql_query("
DELETE FROM `AngelTypes` DELETE FROM `AngelTypes`
WHERE `id`='" . sql_escape($angeltype['id']) . "' WHERE `id`='" . sql_escape($angeltype['id']) . "'
LIMIT 1"); LIMIT 1");
if ($result === false) {
engelsystem_error("Unable to delete angeltype.");
}
engelsystem_log("Deleted angeltype: " . AngelType_name_render($angeltype));
return $result;
} }
/** /**
* Update Angeltype. * Update Angeltype.
* *
* @param int $angeltype_id * @param Angeltype $angeltype
* @param string $name * The angeltype
* @param bool $restricted
* @param string $description
* @param bool $requires_driver_license
*/ */
function AngelType_update($angeltype_id, $name, $restricted, $description, $requires_driver_license) { function AngelType_update($angeltype) {
return sql_query(" $result = sql_query("
UPDATE `AngelTypes` SET UPDATE `AngelTypes` SET
`name`='" . sql_escape($name) . "', `name`='" . sql_escape($angeltype['name']) . "',
`restricted`=" . sql_bool($restricted) . ", `restricted`=" . sql_bool($angeltype['restricted']) . ",
`description`='" . sql_escape($description) . "', `description`='" . sql_escape($angeltype['description']) . "',
`requires_driver_license`=" . sql_bool($requires_driver_license) . " `requires_driver_license`=" . sql_bool($angeltype['requires_driver_license']) . "
WHERE `id`='" . sql_escape($angeltype_id) . "' WHERE `id`='" . sql_escape($angeltype['id']) . "'");
LIMIT 1"); if ($result === false) {
engelsystem_error("Unable to update angeltype.");
}
engelsystem_log("Updated angeltype: " . $angeltype['name'] . ($angeltype['restricted'] ? ", restricted" : "") . ($angeltype['requires_driver_license'] ? ", requires driver license" : ""));
return $result;
} }
/** /**
* Create an Angeltype. * Create an Angeltype.
* *
* @param string $name * @param Angeltype $angeltype
* @param boolean $restricted * The angeltype
* @param string $description * @return the created angeltype
* @return New Angeltype id
*/ */
function AngelType_create($name, $restricted, $description, $requires_driver_license) { function AngelType_create($angeltype) {
$result = sql_query(" $result = sql_query("
INSERT INTO `AngelTypes` SET INSERT INTO `AngelTypes` SET
`name`='" . sql_escape($name) . "', `name`='" . sql_escape($angeltype['name']) . "',
`restricted`=" . sql_bool($restricted) . ", `restricted`=" . sql_bool($angeltype['restricted']) . ",
`description`='" . sql_escape($description) . "', `description`='" . sql_escape($angeltype['description']) . "',
`requires_driver_license`=" . sql_bool($requires_driver_license)); `requires_driver_license`=" . sql_bool($angeltype['requires_driver_license']));
if ($result === false) { if ($result === false) {
return false; engelsystem_error("Unable to create angeltype.");
} }
return sql_id(); $angeltype['id'] = sql_id();
engelsystem_log("Created angeltype: " . $angeltype['name'] . ($angeltype['restricted'] ? ", restricted" : "") . ($angeltype['requires_driver_license'] ? ", requires driver license" : ""));
return $angeltype;
} }
/** /**

@ -9,7 +9,7 @@ function admin_log() {
if (isset($_REQUEST['keyword'])) { if (isset($_REQUEST['keyword'])) {
$filter = strip_request_item('keyword'); $filter = strip_request_item('keyword');
} }
$log_entries_source = LogEntries_filter($_POST['keyword']); $log_entries_source = LogEntries_filter($filter);
$log_entries = []; $log_entries = [];
foreach ($log_entries_source as $log_entry) { foreach ($log_entries_source as $log_entry) {

@ -166,8 +166,11 @@ function test_request_int($name) {
/** /**
* Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück * Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück
*/ */
function strip_request_item_nl($name) { function strip_request_item_nl($name, $default_value = null) {
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui", '', strip_tags($_REQUEST[$name])); if (isset($_REQUEST[$name])) {
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui", '', strip_tags($_REQUEST[$name]));
}
return $default_value;
} }
/** /**

@ -47,18 +47,26 @@ function AngelType_delete_view($angeltype) {
]); ]);
} }
function AngelType_edit_view($name, $restricted, $description, $supporter_mode, $requires_driver_license) { /**
return page_with_title(sprintf(_("Edit %s"), $name), [ * Render angeltype edit form.
*
* @param Angeltype $angeltype
* The angeltype to edit
* @param boolean $supporter_mode
* Is the user a supporter of this angeltype?
*/
function AngelType_edit_view($angeltype, $supporter_mode) {
return page_with_title(sprintf(_("Edit %s"), $angeltype['name']), [
buttons([ buttons([
button(page_link_to('angeltypes'), _("Angeltypes"), 'back') button(page_link_to('angeltypes'), _("Angeltypes"), 'back')
]), ]),
msg(), msg(),
form([ form([
$supporter_mode ? form_info(_("Name"), $name) : form_text('name', _("Name"), $name), $supporter_mode ? form_info(_("Name"), $angeltype['name']) : form_text('name', _("Name"), $angeltype['name']),
$supporter_mode ? form_info(_("Restricted"), $restricted ? _("Yes") : _("No")) : form_checkbox('restricted', _("Restricted"), $restricted), $supporter_mode ? form_info(_("Restricted"), $angeltype['restricted'] ? _("Yes") : _("No")) : form_checkbox('restricted', _("Restricted"), $angeltype['restricted']),
$supporter_mode ? form_info(_("Requires driver license"), $requires_driver_license ? _("Yes") : _("No")) : form_checkbox('requires_driver_license', _("Requires driver license"), $requires_driver_license), $supporter_mode ? form_info(_("Requires driver license"), $angeltype['requires_driver_license'] ? _("Yes") : _("No")) : form_checkbox('requires_driver_license', _("Requires driver license"), $angeltype['requires_driver_license']),
form_info("", _("Restricted angel types can only be used by an angel if enabled by an archangel (double opt-in).")), form_info("", _("Restricted angel types can only be used by an angel if enabled by an archangel (double opt-in).")),
form_textarea('description', _("Description"), $description), form_textarea('description', _("Description"), $angeltype['description']),
form_info("", _("Please use markdown for the description.")), form_info("", _("Please use markdown for the description.")),
form_submit('submit', _("Save")) form_submit('submit', _("Save"))
]) ])

Loading…
Cancel
Save