2017-11-05 17:09:01 +01:00
< ? php
/*****************************************************************************
* EditAlbum . php
* Contains the album edit controller .
*
* Kabuki CMS ( C ) 2013 - 2017 , Aaron van Geffen
*****************************************************************************/
class EditAlbum extends HTMLController
{
public function __construct ()
{
// Ensure it's just admins at this point.
if ( ! Registry :: get ( 'user' ) -> isAdmin ())
throw new NotAllowedException ();
$id_tag = isset ( $_GET [ 'id' ]) ? ( int ) $_GET [ 'id' ] : 0 ;
2017-11-05 17:31:17 +01:00
if ( empty ( $id_tag ) && ! isset ( $_GET [ 'add' ]) && $_GET [ 'action' ] !== 'addalbum' )
2017-11-05 17:09:01 +01:00
throw new UnexpectedValueException ( 'Requested album not found or not requesting a new album.' );
2023-03-11 19:52:30 +01:00
if ( ! empty ( $id_tag ))
$album = Tag :: fromId ( $id_tag );
2017-11-05 17:09:01 +01:00
// Adding an album?
2017-11-05 17:31:17 +01:00
if ( isset ( $_GET [ 'add' ]) || $_GET [ 'action' ] === 'addalbum' )
2017-11-05 17:09:01 +01:00
{
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?
2017-11-05 17:31:17 +01:00
if ( Session :: validateSession ( 'get' ) && $album -> kind === 'Album' && $album -> delete ())
2017-11-05 17:09:01 +01:00
{
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 = '<a href="' . BASEURL . '/editalbum/?id=' . $id_tag . '&delete&' . Session :: getSessionTokenKey () . '=' . Session :: getSessionToken () . '" class="btn btn-danger" onclick="return confirm(\'Are you sure you want to delete this album? You cannot undo this!\');">Delete album</a>' ;
elseif ( ! $id_tag )
$after_form = '<button name="submit_and_new" class="btn">Save and add another</button>' ;
2023-03-11 17:35:47 +01:00
// 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' ];
}
2017-11-05 17:09:01 +01:00
$form = new Form ([
'request_url' => BASEURL . '/editalbum/?' . ( $id_tag ? 'id=' . $id_tag : 'add' ),
'content_below' => $after_form ,
'fields' => [
'id_parent' => [
2023-03-11 17:35:47 +01:00
'type' => 'select' ,
'label' => 'Parent album' ,
'options' => $parentChoices ,
2017-11-05 17:09:01 +01:00
],
'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 ,
],
],
]);
2017-11-05 17:31:17 +01:00
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' ,
];
}
}
if ( ! isset ( $formDefaults ))
$formDefaults = isset ( $album ) ? get_object_vars ( $album ) : $_POST ;
2017-11-05 17:09:01 +01:00
// Create the form, add in default values.
2017-11-05 17:31:17 +01:00
$form -> setData ( $formDefaults );
2017-11-05 17:09:01 +01:00
$formview = new FormView ( $form , $form_title ? ? '' );
$this -> page -> adopt ( $formview );
2023-03-11 19:52:30 +01:00
if ( ! empty ( $id_tag ))
{
list ( $assets , $num_assets ) = AssetIterator :: getByOptions ([
'direction' => 'desc' ,
'limit' => 500 ,
'id_tag' => $id_tag ,
], true );
2023-03-11 20:37:39 +01:00
if ( $num_assets > 0 )
$this -> page -> adopt ( new FeaturedThumbnailManager ( $assets , $id_tag ? $album -> id_asset_thumb : 0 ));
2023-03-11 19:52:30 +01:00
}
if ( isset ( $_POST [ 'changeThumbnail' ]))
$this -> processThumbnail ( $album );
elseif ( ! empty ( $_POST ))
$this -> processTagDetails ( $form , $id_tag , $album );
}
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 ( $form , $id_tag , $album )
{
2017-11-05 17:09:01 +01:00
if ( ! empty ( $_POST ))
{
$form -> verify ( $_POST );
// Anything missing?
if ( ! empty ( $form -> getMissing ()))
2023-03-11 13:30:02 +01:00
return $formview -> adopt ( new Alert ( 'Some data missing' , 'Please fill out the following fields: ' . implode ( ', ' , $form -> getMissing ()), 'danger' ));
2017-11-05 17:09:01 +01:00
$data = $form -> getData ();
2023-03-11 17:35:47 +01:00
// 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' ));
}
2017-11-05 17:09:01 +01:00
// Quick stripping.
2017-11-05 17:31:17 +01:00
$data [ 'tag' ] = htmlentities ( $data [ 'tag' ]);
$data [ 'description' ] = htmlentities ( $data [ 'description' ]);
2018-02-19 11:54:29 +01:00
$data [ 'slug' ] = strtr ( $data [ 'slug' ], [ ' ' => '-' , '--' => '-' , '&' => 'and' , '=>' => '' , " ' " => " " , " : " => " " , '\\' => '-' ]);
2017-11-05 17:09:01 +01:00
// TODO: when updating slug, update slug for all photos in this album.
// Creating a new album?
if ( ! $id_tag )
{
$data [ 'kind' ] = 'Album' ;
2017-11-05 17:31:17 +01:00
$newTag = Tag :: createNew ( $data );
if ( $newTag === false )
2023-03-11 13:30:02 +01:00
return $formview -> adopt ( new Alert ( 'Cannot create this album' , 'Something went wrong while creating the album...' , 'danger' ));
2017-11-05 17:09:01 +01:00
if ( isset ( $_POST [ 'submit_and_new' ]))
{
2017-11-05 17:31:17 +01:00
header ( 'Location: ' . BASEURL . '/editalbum/?add&tag=' . $data [ 'id_parent' ]);
2017-11-05 17:09:01 +01:00
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 ;
}
}
}