Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 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:

Comments.php
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' />
</cms:template>
<?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:form 
    masterpage='comments.php'
    mode='create'
    enctype='multipart/form-data'
    method='post'
    anchor='0'
    >
    <cms:if k_success >

    <cms:db_persist_form
        _invalidate_cache='0'
        _auto_title='1'
   username=k_member_title
    />       

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


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


<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>
</cms:if>   


</cms:form>


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
    <cms:db_persist_form 
        _invalidate_cache='0'
        _auto_title='1'
   username=k_member_title
    />


And change it to this:

Code: Select all
    <cms:db_persist_form 
        _invalidate_cache='0'
        _auto_title='1'
   username=k_member_title
   ticket_comments=k_page_id
    />

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
<ul>
<cms:reverse_related_pages 'ticket_comments' masterpage='comments.php' orderby='publish_date' order='asc' show_future_entries='1' >
<li>
<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 />
</li>
</cms:reverse_related_pages>
</ul>

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 />
</cms:if> 


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.

Dave
Image
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

Regards
2 posts Page 1 of 1

Who is online

In total there are 6 users online :: 0 registered, 0 hidden and 6 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 6 guests