Adding a comment count to my Blog
In order to easily count the comments for the already existing blog entries, I've made a few @formulas to do this. After that, I added a QuerySave agent to the Comment form to update the count when a new comment is posted.
The formulas
First a numeric computed field 'CommentCount':
a:=@DbLookup(""; ""; "(comments)"; @Text(@DocumentUniqueID); 2);
@If(@IsError(a); 0; @Count(a))
Then a computed field 'CommentsText':
@If(ResponseCount=0; ""; @Text(ResponseCount)+@If(ResponseCount=1; " response"; " responses"))
And finally I've modified the field 'IndexValue' to incorporate the count in the view entry:
c0:=Categories;
qm:=@If(@Middle(DbProfile; "<friendlyurls>"; "</")="yes"; "!"; "?");
c1:="<a href=\""+DbPath+qm+"open&tag="+c0+"\">"+c0+"</a>";
cc:=@Implode(c1; ", ");
a:="<div>";
b:="<strong><a href=\""+PermaLink+"\">"+Subject+"</a></strong>";
c:="<p>"+Description+"</p>";
d:="<em><a href=\""+PermaLink+"\">Permalink</a> - posted on "+@Text(PostedDate; "D0T1")+@If(cc=""; ""; " in "+cc)+@If(ResponsesText=""; ""; " - "+ResponsesText)+"</em>";
z:="</div>";
@Implode(a:b:c:d:z; @NewLine)
The Comment WebQuerySave agent
At first, the idea was very simple: do a computeWithForm of the parent document, and the added computed fields would be automatically updated. This failed: the computeWithForm function failed because values from the DbConfig document didn't come throught. I had no other choice than to rebuild the changed fields in LotusScript. In the agent, session, db and doc are defined in the 'initPage' call.
Sub Initialize
On Error Goto catch
initPage
Dim pDoc As notesdocument
Dim tmp As New stringbuffer(10)
Dim count As Integer,link As String, theDate As Variant
Dim view As notesview
Dim vc As NotesViewEntryCollection
Set pdoc=db.Getdocumentbyunid(doc.parentId(0))
Set view=db.getview("(comments)")
Set vc=view.GetAllEntriesByKey(pDoc.UniversalID)
count=vc.Count+1 ' remember: this comment is not yet in the view
Call pdoc.ReplaceItemValue("ResponseCount", count)
If count=1 Then
Call pdoc.ReplaceItemValue("ResponsesText", Cstr(count) & " response")
Else
Call pdoc.ReplaceItemValue("ResponsesText", Cstr(count) & " responses")
End If
link=pDoc.PermaLink(0)
theDate=Evaluate(|@Text(PostedDate; "D0T1")|, pDoc)
tmp.add |<div>|
tmp.add |<strong><a href="| & link & |">| & pDoc.Subject(0)+|</a></strong>|
tmp.add |<p>| & pDoc.Description(0) & |</p>|
tmp.add |<em><a href="| & link & |">Permalink</a> - posted on | & theDate(0)
If Ubound(pdoc.Categories)>0 Then
tmp.add | in | & Join(pDoc.categories, ", ")
End If
tmp.add | - | & pdoc.responsesText(0)
tmp.add |</em>|
tmp.add |</div>|
Call pDoc.ReplaceItemValue("IndexValue", tmp.collapse(NEWLINE))
Call pDoc.Save(True, False, True)
Goto finally
catch:
Print "Error " & Err & " in line " & Erl & ": " & Error$
Resume finally
finally:
End Sub
Lesson learned
Never rely on doc.computeWithForm to recalculate formulas correctly from LotusScript...
Comments
2007-11-20 11:27:03, Patrick Kwinten
what is missing on your blog is also the option to leave posts without being authorized... some downloadable examples of code would also be nice :-)
To add a comment, log in or register as new user. It's free and safe.