Saturday, June 22, 2013

Add custom fields to JComments 2.3

If you use JComments 2.2, you can learn how to add custom fields in this article.
Below is the modification that article for JComments 2.3. The example is adding 2 custom fields Town and Accept Terms.

1. Firstly, add 2 these fields to #__jcomments (e.g. jos_jcomments):
ALTER TABLE jos_jcomments ADD COLUMN `town` varchar (255);
ALTER TABLE jos_jcomments ADD COLUMN `acceptterms` tinyint (1) DEFAULT 0;

2. Add them to the submit form in file \components\com_jcomments\tpl\default\tpl_form.php:
+Search the text comments-form-comment to find the following code:
<p>
<span>
<textarea id="comments-form-comment" name="comment" cols="65" rows="8" tabindex="5"></textarea>
</span>
</p>
+Add the following code after the above code:
<p>
<span>
<input type="text" name="town" value="" id="comments-form-town" />
<label for="comments-form-town" >Town</label>
</span>
</p>

<p>
<span>
<input class="checkbox" type="checkbox" name="acceptterms" value="0" id="comments-form-acceptterms" />
<label for ="comments-form-acceptterms" >Accept Terms of Use</label>
</span>
</p>

3. Add these fields to ajax file to send them to DB
+In addComment function of the file \components\com_jcomments\jcomments.ajax.php, search below code:
$comment->ip = $userIP;
+Add the following code after the above code:
$comment->town = isset($values['town'] ) ? $values ['town'] : '';
$comment->acceptterms = isset($values['acceptterms']) ? $values ['acceptterms'] : 0;

4. Modify _getCommentsQuery function in file \components\com_jcomments\models\jcomments.php to query these fields:
+Search the following code:
$query = "SELECT c.id, c.parent, c.object_id, c.object_group, c.userid, c.name, c.username, c.title, c.comment"
+Replace by the following code:
$query = "SELECT c.id, c.parent, c.object_id, c.object_group, c.userid, c.name, c.username, c.title, c.comment, c.town, c.acceptterms"

5. Add these fields to file \components\com_jcomments\tpl\default\tpl_comment.php to present them in the comments list:
+Search the following code:
<div class="comment-body" id="comment-body-<?php echo $comment->id; ?>"><?php echo $comment->comment; ?></div>
+Then add the following code after the above code:
<p>
<span class="comment-town" ><?php echo $comment->town; ?></span></br>
<span class="comment-accept" ><?php echo $comment->acceptterms ? 'Accepted' : 'Not accepted'; ?></span>
</p>

6. Add these fields to file  \administrator\components\com_jcomments\admin.jcomments.html.php to view them in backend:
+Search the following code:
<tr valign="top" align="left">
<td><label for="comment_text"><?php echo JText::_('A_COMMENT_TEXT'); ?></label></td>
<td><textarea class="editbox long" cols="25" rows="10" id="comment_text" name="comment"><?php echo $row->comment; ?></textarea></td>
</tr>
+Add the following code after above code:
<tr valign="top" align="left">
<td><?php echo JText::_('Town'); ?></td>
<td><input type="text" size="35" name="town" value="<?php echo $row->town; ?>" /></td>
</tr>
<tr valign="top" align="left">
<td><?php echo JText::_('Acceptterms'); ?></td>
<td><input type="text" size="35" name="acceptterms" value="<?php echo $comment->acceptterms ? 'Accepted' : 'Not accepted'; ?>" /></td>
</tr>

That's all. Now you can add your fields in your way.

1 comment:

Subscribe to RSS Feed Follow me on Twitter!