2017-11-05 17:13:52 +01:00
< ? php
/*****************************************************************************
* EditTag . php
* Contains the tag edit controller .
*
* Kabuki CMS ( C ) 2013 - 2017 , Aaron van Geffen
*****************************************************************************/
class EditTag extends HTMLController
{
public function __construct ()
{
$id_tag = isset ( $_GET [ 'id' ]) ? ( int ) $_GET [ 'id' ] : 0 ;
if ( empty ( $id_tag ) && ! isset ( $_GET [ 'add' ]))
throw new UnexpectedValueException ( 'Requested tag not found or not requesting a new tag.' );
2023-03-11 19:49:17 +01:00
if ( ! empty ( $id_tag ))
$tag = Tag :: fromId ( $id_tag );
// Are we allowed to edit this tag?
$user = Registry :: get ( 'user' );
if ( ! ( $user -> isAdmin () || $user -> getUserId () == $tag -> id_user_owner ))
throw new NotAllowedException ();
2017-11-05 17:13:52 +01:00
// Adding an tag?
if ( isset ( $_GET [ 'add' ]))
{
parent :: __construct ( 'Add a new tag' );
$form_title = 'Add a new tag' ;
$this -> page -> addClass ( 'edittag' );
}
// Deleting one?
elseif ( isset ( $_GET [ 'delete' ]))
{
// So far so good?
if ( Session :: validateSession ( 'get' ) && $tag -> kind !== 'Album' && $tag -> delete ())
{
header ( 'Location: ' . BASEURL . '/managetags/' );
exit ;
}
else
trigger_error ( 'Cannot delete tag: an error occured while processing the request.' , E_USER_ERROR );
}
// Editing one, then, surely.
else
{
if ( $tag -> kind === 'Album' )
trigger_error ( 'Cannot edit tag: is actually an album.' , E_USER_ERROR );
parent :: __construct ( 'Edit tag \'' . $tag -> tag . '\'' );
$form_title = 'Edit tag \'' . $tag -> tag . '\'' ;
$this -> page -> addClass ( 'edittag' );
}
// Session checking!
if ( empty ( $_POST ))
Session :: resetSessionToken ();
else
Session :: validateSession ();
if ( $id_tag )
$after_form = '<a href="' . BASEURL . '/edittag/?id=' . $id_tag . '&delete&' . Session :: getSessionTokenKey () . '=' . Session :: getSessionToken () . '" class="btn btn-danger" onclick="return confirm(\'Are you sure you want to delete this tag? You cannot undo this!\');">Delete tag</a>' ;
elseif ( ! $id_tag )
$after_form = '<button name="submit_and_new" class="btn">Save and add another</button>' ;
2023-03-11 20:01:25 +01:00
$fields = [
'kind' => [
'type' => 'select' ,
'label' => 'Kind of tag' ,
'options' => [
'Location' => 'Location' ,
'Person' => 'Person' ,
],
],
'id_user_owner' => [
'type' => 'select' ,
'label' => 'Owner' ,
'options' => [ 0 => '(nobody)' ] + Member :: getMemberMap (),
],
'tag' => [
'type' => 'text' ,
'label' => 'Tag 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 ,
],
];
if ( ! $user -> isAdmin ())
{
unset ( $fields [ 'kind' ]);
unset ( $fields [ 'id_user_owner' ]);
}
2017-11-05 17:13:52 +01:00
$form = new Form ([
'request_url' => BASEURL . '/edittag/?' . ( $id_tag ? 'id=' . $id_tag : 'add' ),
'content_below' => $after_form ,
2023-03-11 20:01:25 +01:00
'fields' => $fields ,
2017-11-05 17:13:52 +01:00
]);
// Create the form, add in default values.
$form -> setData ( $id_tag ? get_object_vars ( $tag ) : $_POST );
$formview = new FormView ( $form , $form_title ? ? '' );
$this -> page -> adopt ( $formview );
2023-03-11 18:22:27 +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 ? $tag -> id_asset_thumb : 0 ));
2023-03-11 18:22:27 +01:00
}
2023-03-11 19:49:17 +01:00
if ( isset ( $_POST [ 'changeThumbnail' ]))
$this -> processThumbnail ( $tag );
elseif ( ! empty ( $_POST ))
2023-03-12 11:32:13 +01:00
$this -> processTagDetails ( $form , $id_tag , $tag ? ? null );
2023-03-11 19:49:17 +01:00
}
private function processThumbnail ( $tag )
{
if ( empty ( $_POST ))
return ;
$tag -> id_asset_thumb = $_POST [ 'featuredThumbnail' ];
$tag -> save ();
header ( 'Location: ' . BASEURL . '/edittag/?id=' . $tag -> id_tag );
exit ;
}
private function processTagDetails ( $form , $id_tag , $tag )
{
2017-11-05 17:13:52 +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:13:52 +01:00
$data = $form -> getData ();
2023-03-11 17:23:44 +01:00
$data [ 'id_parent' ] = 0 ;
2017-11-05 17:13:52 +01:00
// Quick stripping.
2018-02-19 11:54:56 +01:00
$data [ 'slug' ] = strtr ( $data [ 'slug' ], [ ' ' => '-' , '--' => '-' , '&' => 'and' , '=>' => '' , " ' " => " " , " : " => " " , '/' => '-' , '\\' => '-' ]);
2017-11-05 17:13:52 +01:00
// Creating a new tag?
if ( ! $id_tag )
{
$return = Tag :: createNew ( $data );
if ( $return === false )
2023-03-11 13:30:02 +01:00
return $formview -> adopt ( new Alert ( 'Cannot create this tag' , 'Something went wrong while creating the tag...' , 'danger' ));
2017-11-05 17:13:52 +01:00
if ( isset ( $_POST [ 'submit_and_new' ]))
{
header ( 'Location: ' . BASEURL . '/edittag/?add' );
exit ;
}
}
// Just updating?
else
{
foreach ( $data as $key => $value )
$tag -> $key = $value ;
$tag -> save ();
}
2023-03-11 20:03:09 +01:00
// Redirect to a clean page
if ( Registry :: get ( 'user' ) -> isAdmin ())
header ( 'Location: ' . BASEURL . '/managetags/' );
else
header ( 'Location: ' . BASEURL . '/edittag/?id=' . $id_tag );
2017-11-05 17:13:52 +01:00
exit ;
}
}
}