1010
1111namespace Sitecore . Modules . WeBlog . Pipelines . CreateComment
1212{
13- public class CreateCommentItem : ICreateCommentProcessor
14- {
13+ public class CreateCommentItem : ICreateCommentProcessor
14+ {
1515 /// <summary>
1616 /// Gets or sets the <see cref="IBlogManager"/> used to access the structure of the blog and other settings.
1717 /// </summary>
@@ -21,9 +21,9 @@ public class CreateCommentItem : ICreateCommentProcessor
2121 /// Create a new instance.
2222 /// </summary>
2323 public CreateCommentItem ( )
24- : this ( null )
25- {
26- }
24+ : this ( null )
25+ {
26+ }
2727
2828 /// <summary>
2929 /// Create a new instance.
@@ -35,92 +35,99 @@ public CreateCommentItem(IBlogManager blogManager)
3535 }
3636
3737 public void Process ( CreateCommentArgs args )
38- {
38+ {
3939 Assert . ArgumentNotNull ( args , "args cannot be null" ) ;
40- Assert . IsNotNull ( args . Database , "Database cannot be null" ) ;
41- Assert . IsNotNull ( args . Comment , "Comment cannot be null" ) ;
42- Assert . IsNotNull ( args . EntryID , "Entry ID cannot be null" ) ;
40+ Assert . IsNotNull ( args . Database , "Database cannot be null" ) ;
41+ Assert . IsNotNull ( args . Comment , "Comment cannot be null" ) ;
42+ Assert . IsNotNull ( args . EntryID , "Entry ID cannot be null" ) ;
4343 Assert . IsNotNull ( args . Language , "Language cannot be null" ) ;
4444
45- var entryItem = args . Database . GetItem ( args . EntryID , args . Language ) ;
46- if ( entryItem != null )
47- {
48- var blogItem = BlogManager . GetCurrentBlog ( entryItem ) ;
49- if ( blogItem != null )
50- {
51- var template = args . Database . GetTemplate ( blogItem . BlogSettings . CommentTemplateID ) ;
52- var itemName =
53- ItemUtil . ProposeValidItemName ( string . Format ( "Comment at {0} by {1}" ,
54- GetDateTime ( ) . ToString ( "yyyyMMdd HHmmss" ) , args . Comment . AuthorName ) ) ;
55- if ( itemName . Length > 100 )
56- {
57- itemName = itemName . Substring ( 0 , 100 ) ;
58- }
59-
60- // verify the comment item name is unique for this entry
61- var query = "fast:{0}//{1}" . FormatWith ( ContentHelper . EscapePath ( entryItem . Paths . FullPath ) , itemName ) ;
62-
63- var num = 1 ;
64- var nondupItemName = itemName ;
65- while ( entryItem . Database . SelectSingleItem ( query ) != null )
66- {
67- nondupItemName = itemName + " " + num ;
68- num ++ ;
69- query = "fast:{0}//{1}" . FormatWith ( entryItem . Paths . FullPath , nondupItemName ) ;
70- }
71-
72- // need to create the comment within the shell site to ensure workflow is applied to comment
73- var shellSite = SiteContextFactory . GetSiteContext ( Sitecore . Constants . ShellSiteName ) ;
74- SiteContextSwitcher siteSwitcher = null ;
75-
76- try
77- {
78- if ( shellSite != null )
79- {
80- siteSwitcher = new SiteContextSwitcher ( shellSite ) ;
81- }
82-
83- using ( new SecurityDisabler ( ) )
84- {
85- var newItem = entryItem . Add ( nondupItemName , template ) ;
86-
87- var newComment = new CommentItem ( newItem ) ;
88- newComment . BeginEdit ( ) ;
89- newComment . Name . Field . Value = args . Comment . AuthorName ;
90- newComment . Email . Field . Value = args . Comment . AuthorEmail ;
91- newComment . Comment . Field . Value = args . Comment . Text ;
92-
93- foreach ( var entry in args . Comment . Fields )
94- {
95- newComment . InnerItem [ entry . Key ] = entry . Value ;
96- }
97-
98- newComment . EndEdit ( ) ;
99-
100- args . CommentItem = newComment ;
101- }
102- }
103- finally
104- {
105- siteSwitcher ? . Dispose ( ) ;
106- }
107- }
108- else
109- {
110- var message = "Failed to find blog for entry {0}\r \n Ignoring comment: name='{1}', email='{2}', commentText='{3}'" ;
45+ var entryItem = args . Database . GetItem ( args . EntryID , args . Language ) ;
46+ if ( entryItem != null )
47+ {
48+ var blogItem = BlogManager . GetCurrentBlog ( entryItem ) ;
49+ if ( blogItem != null )
50+ {
51+ var template = args . Database . GetTemplate ( blogItem . BlogSettings . CommentTemplateID ) ;
52+ var itemName =
53+ ItemUtil . ProposeValidItemName ( string . Format ( "Comment at {0} by {1}" ,
54+ GetDateTime ( ) . ToString ( "yyyyMMdd HHmmss" ) , args . Comment . AuthorName ) ) ;
55+ if ( itemName . Length > 100 )
56+ {
57+ itemName = itemName . Substring ( 0 , 100 ) ;
58+ }
59+
60+ // verify the comment item name is unique for this entry
61+ var query = BuildFastQuery ( entryItem , itemName ) ;
62+
63+ var num = 1 ;
64+ var nondupItemName = itemName ;
65+ while ( entryItem . Database . SelectSingleItem ( query ) != null )
66+ {
67+ nondupItemName = itemName + " " + num ;
68+ num ++ ;
69+ query = BuildFastQuery ( entryItem , nondupItemName ) ;
70+ }
71+
72+ // need to create the comment within the shell site to ensure workflow is applied to comment
73+ var shellSite = SiteContextFactory . GetSiteContext ( Sitecore . Constants . ShellSiteName ) ;
74+ SiteContextSwitcher siteSwitcher = null ;
75+
76+ try
77+ {
78+ if ( shellSite != null )
79+ {
80+ siteSwitcher = new SiteContextSwitcher ( shellSite ) ;
81+ }
82+
83+ using ( new SecurityDisabler ( ) )
84+ {
85+ var newItem = entryItem . Add ( nondupItemName , template ) ;
86+
87+ var newComment = new CommentItem ( newItem ) ;
88+ newComment . BeginEdit ( ) ;
89+ newComment . Name . Field . Value = args . Comment . AuthorName ;
90+ newComment . Email . Field . Value = args . Comment . AuthorEmail ;
91+ newComment . Comment . Field . Value = args . Comment . Text ;
92+
93+ foreach ( var entry in args . Comment . Fields )
94+ {
95+ newComment . InnerItem [ entry . Key ] = entry . Value ;
96+ }
97+
98+ newComment . EndEdit ( ) ;
99+
100+ args . CommentItem = newComment ;
101+ }
102+ }
103+ finally
104+ {
105+ siteSwitcher ? . Dispose ( ) ;
106+ }
107+ }
108+ else
109+ {
110+ var message = "Failed to find blog for entry {0}\r \n Ignoring comment: name='{1}', email='{2}', commentText='{3}'" ;
111111 Logger . Error ( string . Format ( message , args . EntryID , args . Comment . AuthorName , args . Comment . AuthorEmail , args . Comment . Text ) , typeof ( CreateCommentItem ) ) ;
112- }
113- }
114- else
115- {
116- var message = "Failed to find blog entry {0}\r \n Ignoring comment: name='{1}', email='{2}', commentText='{3}'" ;
112+ }
113+ }
114+ else
115+ {
116+ var message = "Failed to find blog entry {0}\r \n Ignoring comment: name='{1}', email='{2}', commentText='{3}'" ;
117117 Logger . Error ( string . Format ( message , args . EntryID , args . Comment . AuthorName , args . Comment . AuthorEmail , args . Comment . Text ) , typeof ( CreateCommentItem ) ) ;
118- }
119- }
120-
121- protected virtual DateTime GetDateTime ( )
122- {
123- return DateTime . Now ;
124- }
125- }
118+ }
119+ }
120+
121+ protected virtual string BuildFastQuery ( Item parentItem , string itemName )
122+ {
123+ string path = $ "{ parentItem . Paths . FullPath } /{ itemName } ";
124+ string escapePath = ContentHelper . EscapePath ( path ) ;
125+ return $ "fast:{ escapePath } ";
126+ }
127+
128+ protected virtual DateTime GetDateTime ( )
129+ {
130+ return DateTime . Now ;
131+ }
132+ }
126133}
0 commit comments