fix: GalleryAlbum and Photo were not showing correct confirmation messages on editSave

add: Ability to edit comments to Photos
add: Ability to choose which rich editor for Albums
This commit is contained in:
Doug Bell 2008-03-04 21:26:30 +00:00
parent a69dff3dcf
commit 8af6f28988
10 changed files with 340 additions and 122 deletions

View file

@ -2,6 +2,8 @@
- fixed: Several typos in the new Calendar help documentation. - fixed: Several typos in the new Calendar help documentation.
- fix: List View now starts at the beginning of the day passed in. - fix: List View now starts at the beginning of the day passed in.
- Removed some spurious warnings from the calendar. - Removed some spurious warnings from the calendar.
- Added ability to edit comments. Entirely changed how comments work in prep
for turning it into a mixin.
7.5.4 7.5.4
- fixed: unable to remove calendar feeds in IE6 - fixed: unable to remove calendar feeds in IE6

View file

@ -23,17 +23,44 @@ my $quiet; # this line required
my $session = start(); # this line required my $session = start(); # this line required
# upgrade functions go here # upgrade functions go here
addGalleryEditCommentTemplate( $session );
addGalleryRichEditAlbum( $session );
finish($session); # this line required finish($session); # this line required
##------------------------------------------------- ##---------------------------------------------------------------------------
#sub exampleFunction { #sub exampleFunction {
# my $session = shift; # my $session = shift;
# print "\tWe're doing some stuff here that you should know about.\n" unless ($quiet); # print "\tWe're doing some stuff here that you should know about.\n" unless ($quiet);
# # and here's our code # # and here's our code
#} #}
#----------------------------------------------------------------------------
# Add a column to the Gallery
sub addGalleryEditCommentTemplate {
my $session = shift;
print "\tAdding Edit Comment Template... " unless $quiet;
$session->db->write( q{
ALTER TABLE Gallery ADD COLUMN templateIdEditComment VARCHAR(22) BINARY
} );
print "DONE!\n" unless $quiet;
}
#----------------------------------------------------------------------------
# Add a column to select rich editor for albums
sub addGalleryRichEditAlbum {
my $session = shift;
print "\tAdding Select Rich Editor for Gallery Albums..." unless $quiet;
$session->db->write( q{
ALTER TABLE Gallery ADD COLUMN richEditIdAlbum VARCHAR(22) BINARY
} );
print "DONE!\n" unless $quiet;
}
# --------------- DO NOT EDIT BELOW THIS LINE -------------------------------- # --------------- DO NOT EDIT BELOW THIS LINE --------------------------------

View file

@ -111,22 +111,53 @@ sub definition {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 appendTemplateVarsForCommentForm ( var ) =head2 appendTemplateVarsCommentForm ( var [, comment ] )
Add the template variables necessary for the comment form to the given hash Add the template variables necessary for the comment form to the given hash
reference. Returns the hash reference for convenience. reference. Returns the hash reference for convenience. C<comment> is a hash
reference of values to populate the form with.
=cut =cut
sub appendTemplateVarsForCommentForm { sub appendTemplateVarsCommentForm {
my $self = shift; my $self = shift;
my $var = shift; my $var = shift;
my $comment = shift || {};
my $session = $self->session; my $session = $self->session;
# Default comment
$comment->{ commentId } ||= "new";
$var->{ commentForm_start } $var->{ commentForm_start }
= WebGUI::Form::formHeader( $session ) = WebGUI::Form::formHeader( $session )
. WebGUI::Form::hidden( $session, { name => "func", value => "addCommentSave" } ) . WebGUI::Form::hidden( $session, {
name => "func",
value => "editCommentSave"
} )
. WebGUI::Form::hidden( $session, {
name => "commentId",
value => $comment->{ commentId }
} )
; ;
# Add hidden fields for editing a comment
if ( $comment->{ commentId } ne "new" ) {
$var->{ commentForm_start }
.= WebGUI::Form::hidden( $session, {
name => "userId",
value => $comment->{ userId }
} )
. WebGUI::Form::hidden( $session, {
name => "visitorIp",
value => $comment->{ visitorIp }
} )
. WebGUI::Form::hidden( $session, {
name => "creationDate",
value => $comment->{ creationDate }
} )
;
}
$var->{ commentForm_end } $var->{ commentForm_end }
= WebGUI::Form::formFooter( $session ); = WebGUI::Form::formFooter( $session );
@ -134,6 +165,7 @@ sub appendTemplateVarsForCommentForm {
= WebGUI::Form::HTMLArea( $session, { = WebGUI::Form::HTMLArea( $session, {
name => "bodyText", name => "bodyText",
richEditId => $self->getGallery->get("richEditIdComment"), richEditId => $self->getGallery->get("richEditIdComment"),
value => $comment->{ bodyText },
}); });
$var->{ commentForm_submit } $var->{ commentForm_submit }
@ -605,6 +637,53 @@ sub prepareView {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 processCommentEditForm ( )
Process the Comment Add / Edit Form. Returns a hash reference of properties
that can be passed to C<setComment>.
Will die with an i18n-friendly error message if something is missing or
wrong.
=cut
sub processCommentEditForm {
my $self = shift;
my $session = $self->session;
my $form = $self->session->form;
my $now = WebGUI::DateTime->new( $session, time );
my $i18n = __PACKAGE__->i18n( $session );
# Using die here to suppress line number and file path info
die $i18n->get("commentForm error no commentId") . "\n"
unless $form->get("commentId");
die $i18n->get("commentForm error no bodyText") . "\n"
unless $form->get("bodyText");
my $new = $form->get('commentId') eq "new"
? 1
: 0
;
my $visitorIp = $session->user->userId eq "1"
? $session->env->get("REMOTE_ADDR")
: undef
;
my $properties = {
commentId => $form->get("commentId"),
assetId => $self->getId,
bodyText => $form->get("bodyText"),
creationDate => ( $new ? $now->toDatabaseDate : $form->get("creationDate") ),
userId => ( $new ? $session->user->userId : $form->get("userId") ),
visitorIp => ( $new ? $visitorIp : $form->get("visitorIp") ),
};
return $properties;
}
#----------------------------------------------------------------------------
=head2 processPropertiesFromFormPost ( ) =head2 processPropertiesFromFormPost ( )
@ -656,31 +735,37 @@ sub purge {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 setComment ( commentId, properties ) =head2 setComment ( properties )
Set a comment. If C<commentId> is C<"new">, create a new comment. C<properties> Set a comment. C<properties> is a hash reference of comment information with
is a hash reference of comment information. the following keys:
assetId - The assetId of the asset this comment is for
commentId - The ID of the comment. If "new", will make a new comment.
bodyText - The body of the comment
userId - The userId of the user who made the comment
visitorIp - If the user was a visitor, the IP address of the user
creationDate - A MySQL-formatted date/time when the comment was posted
=cut =cut
sub setComment { sub setComment {
my $self = shift; my $self = shift;
my $commentId = shift;
my $properties = shift; my $properties = shift;
croak "Photo->setComment: commentId must be defined"
unless $commentId;
croak "Photo->setComment: properties must be a hash reference" croak "Photo->setComment: properties must be a hash reference"
unless $properties && ref $properties eq "HASH"; unless $properties && ref $properties eq "HASH";
croak "Photo->setComment: commentId must be defined"
unless $properties->{ commentId };
croak "Photo->setComment: properties must contain a bodyText key" croak "Photo->setComment: properties must contain a bodyText key"
unless $properties->{ bodyText }; unless $properties->{ bodyText };
$properties->{ creationDate } ||= WebGUI::DateTime->new($self->session, time)->toDatabase; $properties->{ creationDate } ||= WebGUI::DateTime->new($self->session, time)->toDatabase;
$properties->{ assetId } = $self->getId; $properties->{ assetId } = $self->getId;
$self->session->db->setRow( return $self->session->db->setRow(
"Photo_comment", "commentId", "Photo_comment", "commentId",
{ %$properties, commentId => $commentId } $properties,
); );
} }
@ -725,7 +810,7 @@ sub view {
my $session = $self->session; my $session = $self->session;
my $var = $self->getTemplateVars; my $var = $self->getTemplateVars;
$self->appendTemplateVarsForCommentForm( $var ); $self->appendTemplateVarsCommentForm( $var );
# Keywords # Keywords
my $k = WebGUI::Keyword->new( $session ); my $k = WebGUI::Keyword->new( $session );
@ -743,6 +828,8 @@ sub view {
for my $comment ( @{ $p->getPageData } ) { for my $comment ( @{ $p->getPageData } ) {
$comment->{ url_deleteComment } $comment->{ url_deleteComment }
= $self->getUrl('func=deleteComment;commentId=' . $comment->{commentId} ); = $self->getUrl('func=deleteComment;commentId=' . $comment->{commentId} );
$comment->{ url_editComment }
= $self->getUrl('func=editComment;commentId=' . $comment->{commentId} );
my $user = WebGUI::User->new( $session, $comment->{userId} ); my $user = WebGUI::User->new( $session, $comment->{userId} );
$comment->{ username } = $user->username; $comment->{ username } = $user->username;
@ -759,38 +846,6 @@ sub view {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 www_addCommentSave ( )
Save a new comment to the Photo.
=cut
sub www_addCommentSave {
my $self = shift;
my $session = $self->session;
return $session->privilege->insufficient unless $self->canComment;
my $i18n = __PACKAGE__->i18n( $session );
my $form = $self->session->form;
my $properties = {
assetId => $self->getId,
creationDate => WebGUI::DateTime->new( $session, time )->toDatabase,
userId => $session->user->userId,
visitorIp => ( $session->user->userId eq "1" ? $session->env("REMOTE_ADDR") : undef ),
bodyText => $form->get("bodyText"),
};
$self->setComment( "new", $properties );
return $self->processStyle(
sprintf $i18n->get('comment message'), $self->getUrl,
);
}
#----------------------------------------------------------------------------
=head2 www_delete ( ) =head2 www_delete ( )
Show the page to confirm the deletion of this Photo. Show a list of albums Show the page to confirm the deletion of this Photo. Show a list of albums
@ -1016,19 +1071,74 @@ sub www_edit {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 www_editSave ( ) =head2 www_editComment ( params )
Save the edit form. Overridden to display a confirm message to the user. Form to edit a comment. C<params> is a hash reference of parameters
with the following keys:
errors = An array reference of errors to show the user.
=cut =cut
sub www_editSave { sub www_editComment {
my $self = shift; my $self = shift;
$self->SUPER::www_editSave; my $params = shift;
my $session = $self->session;
my $i18n = __PACKAGE__->i18n( $self->session ); return $session->privilege->insufficient unless $self->canEdit;
sprintf $i18n->get("save message"), $self->getUrl, my $var = $self->getTemplateVars;
if ( $params->{ errors } ) {
$var->{ errors } = [ map { { "error" => $_ } } @{ $params->{errors} } ];
}
my $commentId = $session->form->get( "commentId" );
my $comment = $self->getComment( $commentId );
$self->appendTemplateVarsCommentForm( $var, $comment );
return $self->processStyle(
$self->processTemplate( $var, $self->getGallery->get("templateIdEditComment") )
);
}
#----------------------------------------------------------------------------
=head2 www_editCommentSave ( )
Save a comment being edited
=cut
sub www_editCommentSave {
my $self = shift;
my $session = $self->session;
return $session->privilege->insufficient unless $self->canEdit;
my $i18n = __PACKAGE__->i18n( $session );
my $comment = eval { $self->processCommentEditForm };
if ( $@ ) {
return $self->www_editComment( { errors => [ $@ ] } );
}
# setComment changes commentId, so keep track if we're adding a new comment
my $isNew = $comment->{commentId} eq "new";
$self->setComment( $comment );
# Return different message for adding and editing
if ( $isNew ) {
return $self->processStyle(
sprintf $i18n->get('comment message'), $self->getUrl
);
}
else {
return $self->processStyle(
sprintf $i18n->get('editCommentSave message'), $self->getUrl
);
}
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------

View file

@ -118,6 +118,13 @@ sub definition {
label => $i18n->get("maxSpacePerUser label"), label => $i18n->get("maxSpacePerUser label"),
hoverHelp => $i18n->get("maxSpacePerUser description"), hoverHelp => $i18n->get("maxSpacePerUser description"),
}, },
richEditIdAlbum => {
tab => "properties",
fieldType => "selectRichEditor",
defaultValue => "PBrichedit000000000001", # Content Managers editor
label => $i18n->get("richEditIdAlbum label"),
hoverHelp => $i18n->get("richEditIdAlbum description"),
},
richEditIdComment => { richEditIdComment => {
tab => "properties", tab => "properties",
fieldType => "selectRichEditor", fieldType => "selectRichEditor",
@ -157,6 +164,14 @@ sub definition {
label => $i18n->get("templateIdEditAlbum label"), label => $i18n->get("templateIdEditAlbum label"),
hoverHelp => $i18n->get("templateIdEditAlbum description"), hoverHelp => $i18n->get("templateIdEditAlbum description"),
}, },
templateIdEditComment => {
tab => "display",
fieldType => "template",
defaultValue => "OxJWQgnGsgyGohP2L3zJPQ",
namespace => "GalleryFile/EditComment",
label => $i18n->get("templateIdEditComment label"),
hoverHelp => $i18n->get("templateIdEditComment description"),
},
templateIdEditFile => { templateIdEditFile => {
tab => "display", tab => "display",
fieldType => "template", fieldType => "template",

View file

@ -814,29 +814,6 @@ sub www_edit {
); );
} }
#-----------------------------------------------------------------------------
=head2 www_editSave ( )
Save the asset edit form. Overridden to give a nice message when a photo or
album is added
=cut
sub www_editSave {
my $self = shift;
my $form = $self->session->form;
my $i18n = __PACKAGE__->i18n($self->session);
$self->SUPER::www_editSave;
if ( $form->get("assetId") eq "new" ) {
return sprintf $i18n->get("addFile message"), $self->getUrl,
}
else {
return sprintf $i18n->get("save message"), $self->getUrl,
}
}
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 www_showConfirmation ( ) =head2 www_showConfirmation ( )

View file

@ -260,6 +260,32 @@ our $HELP = {
], ],
}, },
'help editComment' => {
title => 'help editComment title',
body => 'help editComment body',
isa => [
{
tag => 'help common',
namespace => 'Asset_Photo',
},
{
tag => 'help commentForm',
namespace => 'Asset_Photo',
},
],
variables => [
{
name => 'errors',
description => 'helpvar errors',
variables => [
{
name => 'error',
description => 'helpvar error',
},
],
},
],
},
}; };

View file

@ -77,6 +77,16 @@ our $I18N = {
lastUpdated => 0, lastUpdated => 0,
context => '', context => '',
}, },
"richEditIdAlbum label" => {
message => "Rich Editor for Albums",
lastUpdated => 0,
context => 'Asset property label',
},
"richEditIdAlbum description" => {
message => "The Rich Text Editor to use for Albums",
lastUpdated => 0,
context => 'Asset property description',
},
"richEditIdFileComment label" => { "richEditIdFileComment label" => {
message => "Rich Editor for Comments", message => "Rich Editor for Comments",
lastUpdated => 0, lastUpdated => 0,
@ -147,6 +157,16 @@ our $I18N = {
lastUpdated => 0, lastUpdated => 0,
context => '', context => '',
}, },
"templateIdEditComment label" => {
message => "Template to Edit Comments",
lastUpdated => 0,
context => '',
},
"templateIdEditComment description" => {
message => "The template to edit a comment.",
lastUpdated => 0,
context => '',
},
"templateIdEditFile label" => { "templateIdEditFile label" => {
message => "Template to Edit Files", message => "Template to Edit Files",
lastUpdated => 0, lastUpdated => 0,

View file

@ -21,6 +21,12 @@ our $I18N = {
lastUpdated => 0, lastUpdated => 0,
}, },
'editCommentSave message' => {
message => q{The comment has been updated. <a href="%s">Back to Photo</a>.},
lastUpdated => 0,
context => q{Message after a comment is edited.},
},
'help commentForm title' => { 'help commentForm title' => {
message => 'Photo -- Comment Form', message => 'Photo -- Comment Form',
lastUpdated => 0, lastUpdated => 0,
@ -463,6 +469,48 @@ our $I18N = {
context => 'Label for "save" button', context => 'Label for "save" button',
}, },
'help editComment title' => {
message => 'Photo Edit Comment Template',
lastUpdated => 0,
context => 'Help page title',
},
'help editComment body' => {
message => 'These variables are available to the Photo Edit Comment page',
lastUpdated => 0,
context => 'Help page body text',
},
'helpvar errors' => {
message => 'A loop of error messages to show the user',
lastUpdated => 0,
context => 'Description of template variable',
},
'helpvar error' => {
message => 'The i18n error message',
lastUpdated => 0,
context => 'Description of template variable',
},
'template error happened' => {
message => q{An error occurred while processing your request.},
lastUpdated => 0,
context => "Text shown when friendly error messages are being displayed",
},
'commentForm error no commentId' => {
message => q{No comment ID was given. This indicates a problem with the template. Please notify an administrator.},
lastUpdated => 0,
context => q{Error message when no comment ID was given. This should never happen unless the template is made wrong.},
},
'commentForm error no bodyText' => {
message => q{No text was entered. Please enter some text to create a comment.},
lastUpdated => 0,
context => q{Error message for Photo comments},
},
}; };
1; 1;

View file

@ -31,6 +31,7 @@ my @versionTags = ();
push @versionTags, WebGUI::VersionTag->getWorking($session); push @versionTags, WebGUI::VersionTag->getWorking($session);
$versionTags[-1]->set({name=>"Photo Test, add Gallery, Album and 1 Photo"}); $versionTags[-1]->set({name=>"Photo Test, add Gallery, Album and 1 Photo"});
my @addArguments = ( undef, undef, { skipAutoCommitWorkflows => 1 } );
my $gallery my $gallery
= $node->addChild({ = $node->addChild({
className => "WebGUI::Asset::Wobject::Gallery", className => "WebGUI::Asset::Wobject::Gallery",
@ -39,21 +40,11 @@ my $gallery
my $album my $album
= $gallery->addChild({ = $gallery->addChild({
className => "WebGUI::Asset::Wobject::GalleryAlbum", className => "WebGUI::Asset::Wobject::GalleryAlbum",
}, }, @addArguments );
undef,
undef,
{
skipAutoCommitWorkflows => 1,
});
my $photo my $photo
= $album->addChild({ = $album->addChild({
className => "WebGUI::Asset::File::Image::Photo", className => "WebGUI::Asset::File::Image::Photo",
}, }, @addArguments );
undef,
undef,
{
skipAutoCommitWorkflows => 1,
});
$versionTags[-1]->commit; $versionTags[-1]->commit;
@ -81,17 +72,12 @@ ok(
); );
ok( ok(
!eval{ $photo->setComment("new"); 1 }, !eval{ $photo->setComment("lulz"); 1 },
"Photo->setComment fails when no second argument given", "Photo->setComment fails when first argument is not a hashref",
); );
ok( ok(
!eval{ $photo->setComment("new", "lulz"); 1 }, !eval{ $photo->setComment({ lulz => "ohai" }); 1 },
"Photo->setComment fails when second argument is not a hashref",
);
ok(
!eval{ $photo->setComment("new", { lulz => "ohai" }); 1 },
"Photo->setComment fails when hashref does not contain a bodyText key", "Photo->setComment fails when hashref does not contain a bodyText key",
); );
@ -101,9 +87,10 @@ ok(
# - All else is defaults # - All else is defaults
my $commentId; my $commentId;
ok( ok(
eval{ $commentId = $photo->setComment("new", { userId => 1, assetId => $photo->getId, bodyText => "bodyText", }); 1 }, eval{ $commentId = $photo->setComment({ commentId => "new", userId => 1, bodyText => "bodyText", }); 1 },
"Photo->setComment succeeds", "Photo->setComment succeeds",
); );
if ( $@ ) { diag $@; }
is_deeply( is_deeply(
$photo->getCommentIds, [$commentId], $photo->getCommentIds, [$commentId],
@ -142,9 +129,10 @@ like(
# - userId is visitor # - userId is visitor
# - all else is defaults # - all else is defaults
ok( ok(
eval{ $commentId = $photo->setComment("new", { userId => 1, bodyText => "bodyText", }); 1 }, eval{ $commentId = $photo->setComment({ commentId => "new", userId => 1, bodyText => "bodyText", }); 1 },
"Photo->setComment succeeds", "Photo->setComment succeeds",
); );
if ( $@ ) { diag $@; }
cmp_deeply( cmp_deeply(
$photo->getCommentIds, superbagof( $commentId ), $photo->getCommentIds, superbagof( $commentId ),
@ -156,6 +144,7 @@ ok(
eval{ $comment = $photo->getComment($commentId); 1}, eval{ $comment = $photo->getComment($commentId); 1},
"Photo->getComment does not croak.", "Photo->getComment does not croak.",
); );
if ( $@ ) { diag $@; }
is( is(
ref $comment, "HASH", ref $comment, "HASH",
@ -202,63 +191,67 @@ TODO: {
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Test www_addCommentSave page sanity checks # Test www_editCommentSave page sanity checks
my $html; my $html;
$photo $photo
= $album->addChild({ = $album->addChild({
className => "WebGUI::Asset::File::Image::Photo", className => "WebGUI::Asset::File::Image::Photo",
}, }, @addArguments );
undef,
undef,
{
skipAutoCommitWorkflows => 1,
});
# Permissions # Permissions
$html = WebGUI::Test->getPage($photo, "www_addCommentSave", { $html = WebGUI::Test->getPage($photo, "www_editCommentSave", {
userId => 1, userId => 1,
formParams => { bodyText => "yes?" }, formParams => { bodyText => "yes?" },
}); });
like( like(
$html, qr/permission denied/i, $html, qr/permission denied/i,
"www_addCommentSave -- Permission denied if not Gallery->canAddComment", "www_editCommentSave -- Permission denied if not Gallery->canAddComment",
); );
my $i18n = $photo->i18n($session); my $i18n = $photo->i18n($session);
my $errorMessage;
SKIP: { # Required: commentId
skip "www_addCommentSave needs to check for bodyText", 1; $html = WebGUI::Test->getPage($photo, "www_editCommentSave", {
userId => 3,
# Required fields formParams => { bodyText => "bodyText" },
$html = WebGUI::Test->getPage($photo, "www_addCommentSave", {
userId => 1,
formParams => { },
}); });
$errorMessage = $i18n->get("commentForm error no commentId");
like( like(
$html, $i18n->get("www_addCommentSave error missing required"), $html, qr/$errorMessage/,
"www_addCommentSave -- Must have bodyText defined", "www_editCommentSave -- Must have commentId defined",
); );
} # Required: bodyText
$html = WebGUI::Test->getPage($photo, "www_editCommentSave", {
userId => 3,
formParams => { commentId => "new" },
});
$errorMessage = $i18n->get("commentForm error no bodyText");
like(
$html, qr/$errorMessage/,
"www_editCommentSave -- Must have bodyText defined",
);
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Test www_addCommentSave functionality # Test www_editCommentSave functionality
$html = WebGUI::Test->getPage($photo, "www_addCommentSave", { $html = WebGUI::Test->getPage($photo, "www_editCommentSave", {
userId => 3, userId => 3,
formParams => { bodyText => "YES!", }, formParams => { commentId => "new", bodyText => "YES!", },
}); });
my $successMessage = sprintf($i18n->get("comment message"), $photo->getUrl); my $successMessage = sprintf($i18n->get("comment message"), $photo->getUrl);
like( like(
$html, qr/$successMessage/, $html, qr/$successMessage/,
"www_addCommentSave -- page shows success message", "www_editCommentSave -- page shows success message",
); );
my $ids = $photo->getCommentIds; my $ids = $photo->getCommentIds;
is( is(
scalar @$ids, 1, scalar @$ids, 1,
"www_addCommentSave -- Comment was added", "www_editCommentSave -- Comment was added",
); );