EditAlbum: fix error handling

This commit is contained in:
Aaron van Geffen 2024-05-08 13:17:31 +02:00
parent 2bfbe67d91
commit 45b59636f6

View File

@ -8,6 +8,9 @@
class EditAlbum extends HTMLController
{
private $form;
private $formview;
public function __construct()
{
// Ensure it's just admins at this point.
@ -117,7 +120,7 @@ class EditAlbum extends HTMLController
unset($fields['id_asset_thumb']);
}
$form = new Form([
$this->form = new Form([
'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'),
'content_below' => $after_form,
'fields' => $fields,
@ -141,9 +144,9 @@ class EditAlbum extends HTMLController
$formDefaults = isset($album) ? get_object_vars($album) : $_POST;
// Create the form, add in default values.
$form->setData($formDefaults);
$formview = new FormView($form, $form_title ?? '');
$this->page->adopt($formview);
$this->form->setData($formDefaults);
$this->formview = new FormView($this->form, $form_title ?? '');
$this->page->adopt($this->formview);
// If we have asset images, show the thumbnail manager
if (!empty($id_tag) && $num_assets > 0)
@ -152,7 +155,7 @@ class EditAlbum extends HTMLController
if (isset($_POST['changeThumbnail']))
$this->processThumbnail($album);
elseif (!empty($_POST))
$this->processTagDetails($form, $id_tag, $album ?? null);
$this->processTagDetails($id_tag, $album ?? null);
}
private function processThumbnail($tag)
@ -167,22 +170,22 @@ class EditAlbum extends HTMLController
exit;
}
private function processTagDetails($form, $id_tag, $album)
private function processTagDetails($id_tag, $album)
{
if (!empty($_POST))
{
$form->verify($_POST);
$this->form->verify($_POST);
// Anything missing?
if (!empty($form->getMissing()))
return $formview->adopt(new Alert('Some data missing', 'Please fill out the following fields: ' . implode(', ', $form->getMissing()), 'danger'));
if (!empty($this->form->getMissing()))
return $this->formview->adopt(new Alert('Some data missing', 'Please fill out the following fields: ' . implode(', ', $this->form->getMissing()), 'danger'));
$data = $form->getData();
$data = $this->form->getData();
// Sanity check: don't let an album be its own parent
if ($data['id_parent'] == $id_tag)
{
return $formview->adopt(new Alert('Invalid parent', 'An album cannot be its own parent.', 'danger'));
return $this->formview->adopt(new Alert('Invalid parent', 'An album cannot be its own parent.', 'danger'));
}
// Quick stripping.
@ -198,7 +201,7 @@ class EditAlbum extends HTMLController
$data['kind'] = 'Album';
$newTag = Tag::createNew($data);
if ($newTag === false)
return $formview->adopt(new Alert('Cannot create this album', 'Something went wrong while creating the album...', 'danger'));
return $this->formview->adopt(new Alert('Cannot create this album', 'Something went wrong while creating the album...', 'danger'));
if (isset($_POST['submit_and_new']))
{