isAdmin()) throw new NotAllowedException(); $id_tag = isset($_GET['id']) ? (int) $_GET['id'] : 0; if (empty($id_tag) && !isset($_GET['add']) && $_GET['action'] !== 'addalbum') throw new UnexpectedValueException('Requested album not found or not requesting a new album.'); if (!empty($id_tag)) $album = Tag::fromId($id_tag); // Adding an album? if (isset($_GET['add']) || $_GET['action'] === 'addalbum') { parent::__construct('Add a new album'); $form_title = 'Add a new album'; $this->page->addClass('editalbum'); } // Deleting one? elseif (isset($_GET['delete'])) { // So far so good? if (Session::validateSession('get') && $album->kind === 'Album' && $album->delete()) { header('Location: ' . BASEURL . '/managealbums/'); exit; } else trigger_error('Cannot delete album: an error occured while processing the request.', E_USER_ERROR); } // Editing one, then, surely. else { if ($album->kind !== 'Album') trigger_error('Cannot edit album: not an album.', E_USER_ERROR); parent::__construct('Edit album \'' . $album->tag . '\''); $form_title = 'Edit album \'' . $album->tag . '\''; $this->page->addClass('editalbum'); } // Session checking! if (empty($_POST)) Session::resetSessionToken(); else Session::validateSession(); if ($id_tag) $after_form = 'Delete album'; elseif (!$id_tag) $after_form = ''; // Gather possible parents for this album to be filed into $parentChoices = [0 => '-root-']; foreach (PhotoAlbum::getHierarchy('tag', 'up') as $parent) { if (!empty($id_tag) && $parent['id_tag'] == $id_tag) continue; $parentChoices[$parent['id_tag']] = $parent['tag']; } $fields = [ 'id_parent' => [ 'type' => 'select', 'label' => 'Parent album', 'options' => $parentChoices, ], 'id_asset_thumb' => [ 'type' => 'numeric', 'label' => 'Thumbnail asset ID', 'is_optional' => true, ], 'tag' => [ 'type' => 'text', 'label' => 'Album title', 'size' => 50, 'maxlength' => 255, ], 'slug' => [ 'type' => 'text', 'label' => 'URL slug', 'size' => 50, 'maxlength' => 255, ], 'description' => [ 'type' => 'textbox', 'label' => 'Description', 'size' => 50, 'maxlength' => 255, 'is_optional' => true, ], ]; // Fetch image assets for this album if (!empty($id_tag)) { list($assets, $num_assets) = AssetIterator::getByOptions([ 'direction' => 'desc', 'limit' => 500, 'id_tag' => $id_tag, ], true); if ($num_assets > 0) unset($fields['id_asset_thumb']); } $this->form = new Form([ 'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'), 'content_below' => $after_form, 'fields' => $fields, ]); // Add defaults for album if none present if (empty($_POST) && isset($_GET['tag'])) { $parentTag = Tag::fromId($_GET['tag']); if ($parentTag->kind === 'Album') { $formDefaults = [ 'id_parent' => $parentTag->id_tag, 'tag' => 'New Album Title Here', 'slug' => ($parentTag->slug ? $parentTag->slug . '/' : '') . 'NEW_ALBUM_SLUG_HERE', ]; } } elseif (empty($_POST) && count($parentChoices) > 1) { // Choose the first non-root album as the default parent reset($parentChoices); next($parentChoices); $formDefaults = ['id_parent' => key($parentChoices)]; } if (!isset($formDefaults)) $formDefaults = isset($album) ? get_object_vars($album) : $_POST; // Create the form, add in default values. $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) $this->page->adopt(new FeaturedThumbnailManager($assets, $id_tag ? $album->id_asset_thumb : 0)); if (isset($_POST['changeThumbnail'])) $this->processThumbnail($album); elseif (!empty($_POST)) $this->processTagDetails($id_tag, $album ?? null); } private function processThumbnail($tag) { if (empty($_POST)) return; $tag->id_asset_thumb = $_POST['featuredThumbnail']; $tag->save(); header('Location: ' . BASEURL . '/editalbum/?id=' . $tag->id_tag); exit; } private function processTagDetails($id_tag, $album) { if (!empty($_POST)) { $this->form->verify($_POST); // Anything missing? 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 = $this->form->getData(); // Sanity check: don't let an album be its own parent if ($data['id_parent'] == $id_tag) { return $this->formview->adopt(new Alert('Invalid parent', 'An album cannot be its own parent.', 'danger')); } // Quick stripping. $data['tag'] = htmlspecialchars($data['tag']); $data['description'] = htmlspecialchars($data['description']); $data['slug'] = strtr($data['slug'], [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '\\' => '-']); // TODO: when updating slug, update slug for all photos in this album. // Creating a new album? if (!$id_tag) { $data['kind'] = 'Album'; $newTag = Tag::createNew($data); if ($newTag === false) return $this->formview->adopt(new Alert('Cannot create this album', 'Something went wrong while creating the album...', 'danger')); if (isset($_POST['submit_and_new'])) { header('Location: ' . BASEURL . '/editalbum/?add&tag=' . $data['id_parent']); exit; } } // Just updating? else { foreach ($data as $key => $value) $album->$key = $value; $album->save(); } // Redirect to the album management page. header('Location: ' . BASEURL . '/managealbums/'); exit; } } }