public function upsertGroupProcessAction()
{
$id = $_POST['id'] ?? null;
if ($id && !ctype_digit($id)) {
$this->flasher->error('E01 Invalid group ID: ' . $id);
$this->redirect('/users/groups');
}
$group = null;
if ($id) {
$group = Gatekeeper::findGroupById($id);
if (!$group) {
$this->flasher->error('E02 Invalid group ID: ' . $id);
}
}
try {
v::alnum('-._')->setName('Group name')->check($_POST['name']);
} catch (ValidationException $e) {
$this->flasher->error($e->getMainMessage());
echo $this->twig->render('users/groups/upsert.twig', ['flashes' => $this->flasher->display(), 'group' => $group ?: $_POST]);
return false;
}
if ($group) {
$group->name = $_POST['name'];
$group->description = $_POST['description'];
$group->save();
} else {
Gatekeeper::createGroup($_POST);
if (Gatekeeper::getLastError()) {
$this->flasher->error($this->site['debug'] ? Gatekeeper::getLastError() : "Could not create group!");
echo $this->twig->render('users/groups/upsert.twig', ['flashes' => $this->flasher->display(), 'user' => $group ?: $_POST]);
return false;
}
$this->flasher->success('Successfully created group.');
$this->redirect('/users/groups');
}
}