Generic Comments code.

2 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: March 4, 2010   (RSS)

By Toledoh - March 3, 2010 - edited: March 3, 2010

Hi All.

The following code seems to be working well enough for me. Maybe someone out there with a bit better understanding of things could turn it into a plugin? I've got it working with the membership plugin so only members can post comments... but you can easily remove this if you wish.


Step 1: Create a multi-record section called "Comments" with the fields:
- content: text box
- rel_module: text field
- rel_item: text field
- author: text field

Step 2: Add the following code to the top of the Details Page. (I actually have this code in a separate php file, which I insert via php insert)

<?php
header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "WHATEVER/lib/viewer_functions.php";

list($a_text_snippetsRecords, $a_text_snippetsMetaData) = getRecords(array(
'tableName' => 'a_text_snippets',
));

list($commentsRecords, $commentsMetaData) = getRecords(array(
'tableName' => 'comments',
));
?>
<?php
require_once "WHATEVER/lib/viewer_functions.php";

// submit form
if (@$_REQUEST['submit']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['content']) { $errorsAndAlerts .= "Please write a comment!<br/>\n"; }

// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}comments` SET
content = '".mysql_real_escape_string( $_REQUEST['content'] )."',
rel_item = '".mysql_real_escape_string( $_REQUEST['rel_item'] )."',
rel_module = '".mysql_real_escape_string( $_REQUEST['rel_module'] )."',
author = '".mysql_real_escape_string( $_REQUEST['author'] )."',

createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

// display thanks message and clear form
$errorsAndAlerts = "Thanks! Your comment has been added below.";
$_REQUEST = array();
}

}

?>
<?php

require_once "WHATEVER/lib/viewer_functions.php";

list($commentsRecords, $commentsMetaData) = getRecords(array(
'tableName' => 'comments',
));

?>


Step 3: Add the following code to your Details Page. changing "e_blog" to whatever section you are workign with.
<?php $item = $e_blogRecord['num'] ?>
<?php $table = 'e_blog' ?>


Step 4: Insert the following code where you want the comments to be displayed. (must follow step 3, and again, I use a php include).

<h2>Comments</h2>
<div id="sectionPanelHighlight">
<?php if (!$CURRENT_USER): ?><p>Please <a href="signup.php">register</a> to add your comments</p><?php endif ?>
<?php if ($CURRENT_USER): ?><p>
<form method="post" action="">
<input type="hidden" name="submit" value="1" />
<input type="hidden" name="rel_module" value="<?php echo $table ?>">
<input type="hidden" name="rel_item" value="<?php echo $item ?>">
<input type="hidden" name="author" value="<?php echo $CURRENT_USER['username'] ?>">

<?php if (@$errorsAndAlerts): ?>
<div id="noticeDisplay">
<?php echo $errorsAndAlerts; ?>
</div>
<?php endif ?>


<textarea name="content" rows="4" class="help"><?php echo htmlspecialchars(@$_REQUEST['content']) ?></textarea><br />
<input name="add" type="submit" class="searchbutton" value="Add Record &gt;&gt;" />

</form>
</p><?php endif ?>
</div>
<div id="commentsList">

<?php foreach ($commentsRecords as $record): ?>
<?php if ($record['rel_item'] == $item && $record['rel_module'] == $table): ?>
<?php echo $record['content'] ?><br/><?php echo $record['author'] ?> - <?php echo $record['createdDate'] ?>
<hr/>
<?php endif; ?>
<?php endforeach ?>

</div>

Cheers,

Tim (toledoh.com.au)