Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
3 posts Page 1 of 1
Hey guys,

I've recently finished creating a ticket support system for my website, within this I needed a comment system using the members module so that users could comment replies on their tickets to talk back and forth between site staff.

I've seen comments templates on the forums already using the members module. I haven't however seen one that allows multiple users to comment and view comments on a page (Like a forum reply).

To do this, we need to use couches relations feature. We can use this alongside the members module and databound forms to create a comment system where members can reply to eachother.

First of all we need a comments template - a blank template with some editable regions that allow us to create and store the comments.

This is what I have:

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Ticket Comments' clonable='1' executable='0' >
<cms:editable type='text' name='username' label='Username' />
<cms:editable type='textarea' name='comment' label='Comment' required='1' />
<?php COUCH::invoke(); ?>

This is a straightforward template, with just the username of the member commenting and also the comments message. We will add more to this later.

Next we need to use databound forms to be able to add comments -

This is my databound form (You can place this on the page you wish to add comments to):

Code: Select all
    <cms:if k_success >


<cms:set_flash name='submit_success' value='1' />
<cms:redirect k_page_link />

    <cms:if k_error >
        <div class="error">
            <cms:each k_error >
                <br><cms:show item />

<cms:if "<cms:not submit_success />" >

<label for="f_comment">Your reply</label>
<cms:input name="comment" type="bound" />
<button class="button black" type="submit">Submit Reply</button>


This is a fairly basic databound form as you can see, it just has one input.

Please note - You must already have a template with the members module invoked (<cms:member_check_login />) for this to work, I'm sure you knew that already.
As you can see the username is auto assigned to the members title (Which in my case is their username.)

Okay, so now we can create comments that will be saved within the comments.php cloned pages. That's cool - but what if we want to use this system on more than one page? For example, every cloned page within a template has comments systems, we don't want them all to display every comment submitted on every page - each page-view must have its own thread of comments.
This is where the relation comes into play. We can assign each comments to specific page_ids by using a relation.

So, in your comments.php template you must add an editable region:

Code: Select all
<cms:editable type='relation' name='ticket_comments' masterpage='support.php' has='one' />

This allows allows us to assign each comment page created to one of the cloned pages from your page_view (In my case, 'support.php'.)

At this point, you can go to the couch admin panel and view your comments template, if you click on a page created you will see the relation dropdown allowing you to select a cloned page from support.php
That's great, but we need the relation to automatically be created when we create a comment. Going back to the databound form, find this piece of code:

Code: Select all

And change it to this:

Code: Select all

This assigns tickets_comments (The name of our relation editable) to the page_id that the comment is created from. Automatically creating our relationship.

Now we only need to output the comments that have a relationship with the current page_view . Easy enough :)

This is my comments output:
Code: Select all
<cms:reverse_related_pages 'ticket_comments' masterpage='comments.php' orderby='publish_date' order='asc' show_future_entries='1' >
<small>Replied by: <cms:show username /> on <cms:date k_page_date format='jS M Y G:i:s' gmt='1' /></small><br />
<cms:show comment />

This will output every comment in order of date (Newest is at the bottom). We have show_future_entries='1' so that we can add the next piece of code:

Code: Select all
<cms:set submit_success="<cms:get_flash 'submit_success' />" />
<cms:if submit_success >
<cms:redirect k_page_link />

This will refresh the page after the comment has been submitted, allowing the user to see their newly submitted comment. Without show_future_entries enabled the user won't always see the comment until the minute has ticked over (and couch no longer see's it as a future entry).

I hope this helps, sorry if the explanation is a little difficult to understand - I'm not the best at teaching. :)

To clarify, in my examples 'support.php' is a set of cloned pages and the comments list and databound form are within the page_view of support.php
You can use any template you wish, so long as you change the masterpages in the correct places.

Hi Dave.
Your code is innovative, I wonder how we can make a FORUM with Couch ...?
I have to make a FORUM and I will be very happy if I can only do this with Couch, I mean a totally simplified FORUM.

It will be useful if you share your thoughts


Great code explanation.

I was thinking on the same lines, but thought to visit and search the forum once. That is when I came across your code. I was looking for some a commenting system for an e-commerce project. It was a really a straight forward implementation.

I am planning on implementing the rating system from viewtopic.php?f=5&t=8133 alongwith your code. Once done I will share it here.

Also, just to confirm, this code works perfectly with the CouchCMS V2.2.1 and the Extended Users Module

Thanks and Regards,
GenXCoders (Aashish)
where innovation meets technology
3 posts Page 1 of 1