simplify user_angeltype_add_controller
This commit is contained in:
parent
9fce3b22aa
commit
1debe567f5
|
@ -101,6 +101,7 @@ function angeltype_delete_controller() {
|
|||
function angeltype_edit_controller() {
|
||||
global $privileges, $user;
|
||||
|
||||
$angeltype = null;
|
||||
$name = "";
|
||||
$restricted = false;
|
||||
$description = "";
|
||||
|
@ -266,4 +267,24 @@ function angeltypes_list_controller() {
|
|||
AngelTypes_list_view($angeltypes, in_array('admin_angel_types', $privileges))
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an angeltype from given angeltype_id request param.
|
||||
*/
|
||||
function load_angeltype() {
|
||||
if (! isset($_REQUEST['angeltype_id'])) {
|
||||
redirect(page_link_to('angeltypes'));
|
||||
}
|
||||
|
||||
$angeltype = AngelType($_REQUEST['angeltype_id']);
|
||||
if ($angeltype === false) {
|
||||
engelsystem_error("Unable to load angeltype.");
|
||||
}
|
||||
if ($angeltype == null) {
|
||||
error(_("Angeltype doesn't exist."));
|
||||
redirect(page_link_to('angeltypes'));
|
||||
}
|
||||
|
||||
return $angeltype;
|
||||
}
|
||||
?>
|
||||
|
|
|
@ -310,46 +310,30 @@ function user_angeltype_update_controller() {
|
|||
* User joining an Angeltype (Or Coordinator doing this for him).
|
||||
*/
|
||||
function user_angeltype_add_controller() {
|
||||
global $user, $privileges;
|
||||
global $user;
|
||||
|
||||
if (! isset($_REQUEST['angeltype_id'])) {
|
||||
error(_("Angeltype doesn't exist."));
|
||||
redirect(page_link_to('angeltypes'));
|
||||
$angeltype = load_angeltype();
|
||||
|
||||
// User is joining by itself
|
||||
if (! User_is_AngelType_coordinator($user, $angeltype)) {
|
||||
return user_angeltype_join_controller($angeltype);
|
||||
}
|
||||
|
||||
$angeltype = AngelType($_REQUEST['angeltype_id']);
|
||||
if ($angeltype === false) {
|
||||
engelsystem_error("Unable to load angeltype.");
|
||||
}
|
||||
if ($angeltype == null) {
|
||||
error(_("Angeltype doesn't exist."));
|
||||
redirect(page_link_to('angeltypes'));
|
||||
}
|
||||
|
||||
if (User_is_AngelType_coordinator($user, $angeltype)) {
|
||||
// Allow to add any user
|
||||
$user_id = $user['UID'];
|
||||
|
||||
// Default selection
|
||||
$user_source = $user;
|
||||
|
||||
// Load possible users, that are not in the angeltype already
|
||||
$users_source = Users_by_angeltype_inverted($angeltype);
|
||||
if ($users_source === false) {
|
||||
engelsystem_error("Unable to load users.");
|
||||
}
|
||||
|
||||
if (isset($_REQUEST['submit'])) {
|
||||
$valid = true;
|
||||
$user_source = load_user();
|
||||
|
||||
if (isset($_REQUEST['user_id']) && in_array($_REQUEST['user_id'], array_map(function ($user) {
|
||||
return $user['UID'];
|
||||
}, $users_source))) {
|
||||
$user_id = $_REQUEST['user_id'];
|
||||
} else {
|
||||
$valid = false;
|
||||
error(_("Please select a user."));
|
||||
}
|
||||
|
||||
if ($valid) {
|
||||
foreach ($users_source as $user_source) {
|
||||
if ($user_source['UID'] == $user_id) {
|
||||
if (! UserAngelType_exists($user_source, $angeltype)) {
|
||||
$user_angeltype_id = UserAngelType_create($user_source, $angeltype);
|
||||
if ($user_angeltype_id === false) {
|
||||
engelsystem_error("Unable to create user angeltype.");
|
||||
|
@ -367,15 +351,19 @@ function user_angeltype_add_controller() {
|
|||
redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
_("Add user to angeltype"),
|
||||
UserAngelType_add_view($angeltype, $users_source, $user_id)
|
||||
UserAngelType_add_view($angeltype, $users_source, $user_source['UID'])
|
||||
];
|
||||
} else {
|
||||
// Allow only me
|
||||
}
|
||||
|
||||
/**
|
||||
* A user joins an angeltype.
|
||||
*/
|
||||
function user_angeltype_join_controller($angeltype) {
|
||||
global $user, $privileges;
|
||||
|
||||
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
|
||||
if ($user_angeltype === false) {
|
||||
engelsystem_error("Unable to load user angeltype.");
|
||||
|
@ -411,7 +399,6 @@ function user_angeltype_add_controller() {
|
|||
UserAngelType_join_view($user, $angeltype)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Route UserAngelType actions.
|
||||
|
|
|
@ -299,4 +299,25 @@ function user_password_recovery_title() {
|
|||
return _("Password recovery");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a user from param user_id.
|
||||
*/
|
||||
function load_user() {
|
||||
if (! isset($_REQUEST['user_id'])) {
|
||||
redirect(page_link_to());
|
||||
}
|
||||
|
||||
$user = User($_REQUEST['user_id']);
|
||||
if ($user === false) {
|
||||
engelsystem_error("Unable to load user.");
|
||||
}
|
||||
|
||||
if ($user == null) {
|
||||
error(_("User doesn't exist."));
|
||||
redirect(page_link_to());
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -4,6 +4,24 @@
|
|||
* User angeltypes model
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks if a user joined an angeltype.
|
||||
*
|
||||
* @param User $user
|
||||
* The user to be checked
|
||||
* @param Angeltype $angeltype
|
||||
* The angeltype to be checked
|
||||
* @return boolean
|
||||
*/
|
||||
function UserAngelType_exists($user, $angeltype) {
|
||||
return sql_num_query("
|
||||
SELECT `id`
|
||||
FROM `UserAngelTypes`
|
||||
WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "'
|
||||
AND `angeltype_id`='" . sql_escape($angeltype['id']) . "'
|
||||
") > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* List users angeltypes.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue