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.' );
// 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?
$album = Tag :: fromId ( $id_tag );
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
{
$album = Tag :: fromId ( $id_tag );
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>' ;
$form = new Form ([
'request_url' => BASEURL . '/editalbum/?' . ( $id_tag ? 'id=' . $id_tag : 'add' ),
'content_below' => $after_form ,
'fields' => [
'id_parent' => [
'type' => 'numeric' ,
'label' => 'Parent album ID' ,
],
'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 ,
],
],
]);
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 );
if ( ! empty ( $_POST ))
{
$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 ()), 'error' ));
$data = $form -> getData ();
// Quick stripping.
2017-11-05 17:31:17 +01:00
$data [ 'tag' ] = htmlentities ( $data [ 'tag' ]);
$data [ 'description' ] = htmlentities ( $data [ 'description' ]);
2017-11-05 17:09:01 +01:00
$data [ 'slug' ] = strtr ( strtolower ( $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' ;
2017-11-05 17:31:17 +01:00
$newTag = Tag :: createNew ( $data );
if ( $newTag === false )
2017-11-05 17:09:01 +01:00
return $formview -> adopt ( new Alert ( 'Cannot create this album' , 'Something went wrong while creating the album...' , 'error' ));
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 ;
}
}
}