'. t('The comment module creates a discussion board for each post. Users can post comments to discuss a forum topic, weblog post, story, collaborative book page, etc. The ability to comment is an important part of involving members in a communtiy dialogue.') .'
'; $output .= ''. t('An administrator can give comment permissions to user groups, and users can (optionally) edit their last comment, assuming no others have been posted since. Attached to each comment board is a control panel for customizing the way that comments are displayed. Users can control the chronological ordering of posts (newest or oldest first) and the number of posts to display on each page. Comments behave like other user submissions. Filters, smileys and HTML that work in nodes will also work with comments. The comment module provides specific features to inform site members when new comments have been posted. On sites with active commenting from users, the administrator can turn over comment moderation to the community.') .'
'; $output .= t('You can
'. t('For more information please read the configuration and customization handbook Comment page.', array('%comment' => 'http://www.drupal.org/handbook/modules/comment/')) .'
'; return $output; case 'admin/modules#description': return t('Allows users to comment on and discuss published content.'); case 'admin/comment': case 'admin/comment/new': return t("Below is a list of the latest comments posted to your site. Click on a subject to see the comment, the author's name to edit the author's user information , \"edit\" to modify the text, and \"delete\" to remove their submission.
"); case 'admin/comment/approval': return t("Below is a list of the comments posted to your site that need approval. To approve a comment, click on \"edit\" and then change its \"moderation status\" to Approved. Click on a subject to see the comment, the author's name to edit the author's user information, \"edit\" to modify the text, and \"delete\" to remove their submission.
"); case 'admin/comment/configure': case 'admin/comment/configure/settings': return t("Comments can be attached to any node, and their settings are below. The display comes in two types: a \"flat list\" where everything is flush to the left side, and comments come in chronological order, and a \"threaded list\" where replies to other comments are placed immediately below and slightly indented, forming an outline. They also come in two styles: \"expanded\", where you see both the title and the contents, and \"collapsed\" where you only see the title. Preview comment forces a user to look at their comment by clicking on a \"Preview\" button before they can actually add the comment.
"); } } /** * Implementation of hook_menu(). */ function comment_menu($may_cache) { $items = array(); if ($may_cache) { $access = user_access('administer comments'); $items[] = array('path' => 'admin/comment', 'title' => t('comments'), 'callback' => 'comment_admin_overview', 'access' => $access); // Tabs: $items[] = array('path' => 'admin/comment/list', 'title' => t('list'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/comment/configure', 'title' => t('configure'), 'callback' => 'comment_configure', 'access' => $access, 'type' => MENU_LOCAL_TASK); // Subtabs: $items[] = array('path' => 'admin/comment/list/new', 'title' => t('new comments'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/comment/list/approval', 'title' => t('approval queue'), 'callback' => 'comment_admin_overview', 'access' => $access, 'callback arguments' => array('approval'), 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'admin/comment/configure/settings', 'title' => t('settings'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $access = user_access('post comments'); $items[] = array('path' => 'comment/edit', 'title' => t('edit comment'), 'callback' => 'comment_edit', 'access' => $access, 'type' => MENU_CALLBACK); $items[] = array('path' => 'comment/delete', 'title' => t('delete comment'), 'callback' => 'comment_delete', 'access' => $access, 'type' => MENU_CALLBACK); } else { if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) { $node = node_load(arg(2)); if ($node->nid) { $items[] = array('path' => 'comment/reply', 'title' => t('reply to comment'), 'callback' => 'comment_reply', 'access' => node_access('view', $node), 'type' => MENU_CALLBACK); } } if ((arg(0) == 'node') && is_numeric(arg(1)) && is_numeric(arg(2))) { $items[] = array('path' => ('node/'. arg(1) .'/'. arg(2)), 'title' => t('view'), 'callback' => 'node_page', 'type' => MENU_CALLBACK); } } return $items; } /** * Implementation of hook_perm(). */ function comment_perm() { return array('access comments', 'post comments', 'administer comments', 'post comments without approval'); } /** * Implementation of hook_block(). * * Generates a block with the most recent comments. */ function comment_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Recent comments'); return $blocks; } else if ($op == 'view' && user_access('access comments')) { $result = db_query_range(db_rewrite_sql('SELECT c.nid, c.* FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', 'c'), COMMENT_PUBLISHED, 0, 10); $items = array(); while ($comment = db_fetch_object($result)) { $items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'http://example.com/directory
.'));
}
}
}
}
return $edit;
}
/**
* Accepts a submission of new or changed comment content.
*
* @param $edit
* A comment array.
*
* @return
* If the comment is successfully saved the comment ID is returned. If the comment
* is not saved, FALSE is returned.
*/
function comment_save($edit) {
global $user;
if (user_access('post comments') && (user_access('administer coments') || node_comment_mode($edit['nid']) == COMMENT_NODE_READ_WRITE)) {
if (!form_get_errors()) {
// Check for duplicate comments. Note that we have to use the
// validated/filtered data to perform such check.
$duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND nid = %d AND subject = '%s' AND comment = '%s'", $edit['pid'], $edit['nid'], $edit['subject'], $edit['comment']), 0);
if ($duplicate != 0) {
watchdog('content', t('Comment: duplicate %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_WARNING);
}
if ($edit['cid']) {
// Update the comment in the database.
db_query("UPDATE {comments} SET status = '%s', timestamp = %d, subject = '%s', comment = '%s', format = '%s', uid = %d, name = '%s' WHERE cid = %d", $edit['status'], $edit['timestamp'], $edit['subject'], $edit['comment'], $edit['format'], $edit['uid'], $edit['name'], $edit['cid']);
_comment_update_node_statistics($edit['nid']);
// Allow modules to respond to the updating of a comment.
comment_invoke_comment($edit, 'update');
// Add an entry to the watchdog log.
watchdog('content', t('Comment: updated %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
}
else {
// Add the comment to database.
$status = user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
$roles = variable_get('comment_roles', array());
$score = 0;
foreach (array_intersect(array_keys($roles), array_keys($user->roles)) as $rid) {
$score = max($roles[$rid], $score);
}
$users = serialize(array(0 => $score));
// Here we are building the thread field. See the comment
// in comment_render().
if ($edit['pid'] == 0) {
// This is a comment with no parent comment (depth 0): we start
// by retrieving the maximum thread level.
$max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
// Strip the "/" from the end of the thread.
$max = rtrim($max, '/');
// Next, we increase this value by one. Note that we can't
// use 1, 2, 3, ... 9, 10, 11 because we order by string and
// 10 would be right after 1. We use 1, 2, 3, ..., 9, 91,
// 92, 93, ... instead. Ugly but fast.
$decimals = (string) substr($max, 0, strlen($max) - 1);
$units = substr($max, -1, 1);
if ($units) {
$units++;
}
else {
$units = 1;
}
if ($units == 10) {
$units = '90';
}
// Finally, build the thread field for this new comment.
$thread = $decimals . $units .'/';
}
else {
// This is comment with a parent comment: we increase
// the part of the thread value at the proper depth.
// Get the parent comment:
$parent = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $edit['pid']));
// Strip the "/" from the end of the parent thread.
$parent->thread = (string) rtrim((string) $parent->thread, '/');
// Get the max value in _this_ thread.
$max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid']));
if ($max == '') {
// First child of this parent.
$thread = $parent->thread .'.1/';
}
else {
// Strip the "/" at the end of the thread.
$max = rtrim($max, '/');
// We need to get the value at the correct depth.
$parts = explode('.', $max);
$parent_depth = count(explode('.', $parent->thread));
$last = $parts[$parent_depth];
// Next, we increase this value by one. Note that we can't
// use 1, 2, 3, ... 9, 10, 11 because we order by string and
// 10 would be right after 1. We use 1, 2, 3, ..., 9, 91,
// 92, 93, ... instead. Ugly but fast.
$decimals = (string)substr($last, 0, strlen($last) - 1);
$units = substr($last, -1, 1);
$units++;
if ($units == 10) {
$units = '90';
}
// Finally, build the thread field for this new comment.
$thread = $parent->thread .'.'. $decimals . $units .'/';
}
}
$edit['cid'] = db_next_id('{comments}_cid');
$edit['timestamp'] = time();
if ($edit['uid'] = $user->uid) {
$edit['name'] = $user->name;
}
db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], $_SERVER['REMOTE_ADDR'], $edit['timestamp'], $edit['status'], $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);
_comment_update_node_statistics($edit['nid']);
// Tell the other modules a new comment has been submitted.
comment_invoke_comment($edit, 'insert');
// Add an entry to the watchdog log.
watchdog('content', t('Comment: added %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
}
// Clear the cache so an anonymous user can see his comment being added.
cache_clear_all();
// Explain the approval queue if necessary, and then
// redirect the user to the node he's commenting on.
if ($status == COMMENT_NOT_PUBLISHED) {
drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
}
return $edit['cid'];
}
else {
return FALSE;
}
}
else {
$txt = t('Comment: unauthorized comment submitted or comment submitted to a closed node %subject.', array('%subject' => theme('placeholder', $edit['subject'])));
watchdog('content', $txt, WATCHDOG_WARNING);
drupal_set_message($txt, 'error');
return FALSE;
}
}
function comment_links($comment, $return = 1) {
global $user;
$links = array();
// If we are viewing just this comment, we link back to the node.
if ($return) {
$links[] = l(t('parent'), comment_node_url(), NULL, NULL, "comment-$comment->cid");
}
if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
if (user_access('administer comments') && user_access('access administration pages')) {
$links[] = l(t('delete'), "comment/delete/$comment->cid");
$links[] = l(t('edit'), "comment/edit/$comment->cid");
$links[] = l(t('reply'), "comment/reply/$comment->nid/$comment->cid");
}
else if (user_access('post comments')) {
if (comment_access('edit', $comment)) {
$links[] = l(t('edit'), "comment/edit/$comment->cid");
}
$links[] = l(t('reply'), "comment/reply/$comment->nid/$comment->cid");
}
else {
$links[] = theme('comment_post_forbidden');
}
}
return $links;
}
function comment_render($node, $cid = 0) {
global $user;
$mode = $_GET['mode'];
$order = $_GET['order'];
$comments_per_page = $_GET['comments_per_page'];
$comment_page = $_GET['comment_page'];
$output = '';
if (user_access('access comments')) {
// Pre-process variables.
$nid = $node->nid;
if (empty($nid)) {
$nid = 0;
}
if (empty($mode)) {
$mode = $user->mode ? $user->mode : ($_SESSION['comment_mode'] ? $_SESSION['comment_mode'] : variable_get('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED));
}
if (empty($order)) {
$order = $user->sort ? $user->sort : ($_SESSION['comment_sort'] ? $_SESSION['comment_sort'] : variable_get('comment_default_order', COMMENT_ORDER_NEWEST_FIRST));
}
if (empty($comments_per_page)) {
$comments_per_page = $user->comments_per_page ? $user->comments_per_page : ($_SESSION['comment_comments_per_page'] ? $_SESSION['comment_comments_per_page'] : variable_get('comment_default_per_page', '50'));
}
$output .= "\n";
if ($cid) {
// Single comment view.
$result = db_query('SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, u.picture, c.homepage, u.uid, u.name, u.picture, u.data, c.score, c.users', $cid, COMMENT_PUBLISHED);
if ($comment = db_fetch_object($result)) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output .= theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 1));
}
}
else {
// Multiple comment view
$query .= "SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d AND c.status = %d";
$query .= ' GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, u.picture, c.homepage, u.uid, u.name, u.picture, u.data, c.score, c.users, c.thread';
/*
** We want to use the standard pager, but threads would need every
** comment to build the thread structure, so we need to store some
** extra info.
**
** We use a "thread" field to store this extra info. The basic idea
** is to store a value and to order by that value. The "thread" field
** keeps this data in a way which is easy to update and convenient
** to use.
**
** A "thread" value starts at "1". If we add a child (A) to this
** comment, we assign it a "thread" = "1.1". A child of (A) will have
** "1.1.1". Next brother of (A) will get "1.2". Next brother of the
** parent of (A) will get "2" and so on.
**
** First of all note that the thread field stores the depth of the
** comment: depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
**
** Now to get the ordering right, consider this example:
**
** 1
** 1.1
** 1.1.1
** 1.2
** 2
**
** If we "ORDER BY thread ASC" we get the above result, and this is
** the natural order sorted by time. However, if we "ORDER BY thread
** DESC" we get:
**
** 2
** 1.2
** 1.1.1
** 1.1
** 1
**
** Clearly, this is not a natural way to see a thread, and users
** will get confused. The natural order to show a thread by time
** desc would be:
**
** 2
** 1
** 1.2
** 1.1
** 1.1.1
**
** which is what we already did before the standard pager patch. To
** achieve this we simply add a "/" at the end of each "thread" value.
** This way out thread fields will look like depicted below:
**
** 1/
** 1.1/
** 1.1.1/
** 1.2/
** 2/
**
** we add "/" since this char is, in ASCII, higher than every number,
** so if now we "ORDER BY thread DESC" we get the correct order. Try
** it, it works ;). However this would spoil the "ORDER BY thread ASC"
** Here, we do not need to consider the trailing "/" so we use a
** substring only.
*/
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.timestamp DESC';
}
else {
$query .= ' ORDER BY c.thread DESC';
}
}
else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.timestamp';
}
else {
/*
** See comment above. Analysis learns that this doesn't cost
** too much. It scales much much better than having the whole
** comment structure.
*/
$query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
}
}
// Start a form, for use with comment control.
$result = pager_query($query, $comments_per_page, 0, "SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = %d", $nid, COMMENT_PUBLISHED);
if (db_num_rows($result) && (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE || variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE_BELOW)) {
$output .= comment_controls($mode, $order, $comments_per_page);
}
while ($comment = db_fetch_object($result)) {
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$comment->depth = count(explode('.', $comment->thread)) - 1;
if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
$output .= theme('comment_flat_collapsed', $comment);
}
else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
$output .= theme('comment_flat_expanded', $comment);
}
else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
$output .= theme('comment_thread_min', $comment);
}
else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
$output .= theme('comment_thread_max', $comment);
}
}
// Use the standard pager; $pager_total is the number of returned rows,
// is global and defined in pager.inc.
$output .= theme('pager', NULL, $comments_per_page, 0, array('comments_per_page' => $comments_per_page));
if (db_num_rows($result) && (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_BELOW || variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE_BELOW)) {
$output .= comment_controls($mode, $order, $comments_per_page);
}
}
// If enabled, show new comment form.
if (user_access('post comments') && node_comment_mode($nid) == COMMENT_NODE_READ_WRITE && (variable_get('comment_form_location', COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW)) {
$output .= comment_form(array('nid' => $nid), t('Post new comment'));
}
}
return $output;
}
/**
* Menu callback; delete a comment.
*/
function comment_delete($cid) {
$comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid));
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output = '';
// We'll only delete if the user has confirmed the
// deletion using the form in our else clause below.
if ($comment->cid && $_POST['edit']['confirm']) {
drupal_set_message(t('The comment and all its replies have been deleted.'));
// Delete comment and its replies.
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
// Clear the cache so an anonymous user sees that his comment was deleted.
cache_clear_all();
drupal_goto("node/$comment->nid");
}
else if ($comment->cid) {
$output = confirm_form('comment_confirm_delete',
array(),
t('Are you sure you want to delete the comment %title?', array('%title' => theme('placeholder', $comment->subject))),
'node/'. $comment->nid,
t('Any replies to this comment will be lost. This action cannot be undone.'),
t('Delete'),
t('Cancel'));
}
else {
drupal_set_message(t('The comment no longer exists.'));
}
return $output;
}
/**
* Menu callback; present an administrative comment listing.
*/
function comment_admin_overview($type = 'new') {
$header = array(
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Author'), 'field' => 'u.name'),
array('data' => t('Status'), 'field' => 'status'),
array('data' => t('Time'), 'field' => 'c.timestamp', 'sort' => 'desc'),
array('data' => t('Operations'), 'colspan' => '2')
);
$destination = drupal_get_destination();
$status = ($type == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
$sql = 'SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.status = '. db_escape_string($status);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
while ($comment = db_fetch_object($result)) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$rows[] = array(
l($comment->subject, "node/$comment->nid", array('title' => truncate_utf8($comment->comment, 128)), NULL, "comment-$comment->cid") ." ". theme('mark', node_mark($comment->nid, $comment->timestamp)),
theme('username', $comment),
($comment->status == COMMENT_PUBLISHED ? t('Published') : t('Not published')),
format_date($comment->timestamp, 'small'),
l(t('edit'), "comment/edit/$comment->cid", array(), $destination),
l(t('delete'), "comment/delete/$comment->cid", array(), $destination)
);
}
if (!$rows) {
$rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0, tablesort_pager());
return $output;
}
/**
*** misc functions: helpers, privates, history
**/
function comment_num_all($nid) {
static $cache;
if (!isset($cache[$nid])) {
$cache[$nid] = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $nid));
}
return $cache[$nid];
}
function comment_num_replies($pid) {
static $cache;
if (!isset($cache[$pid])) {
$cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND status = %d', $pid, COMMENT_PUBLISHED));
}
return $cache[$pid];
}
/**
* get number of new comments for current user and specified node
*
* @param $nid node-id to count comments for
* @param $timestamp time to count from (defaults to time of last user access
* to node)
*/
function comment_num_new($nid, $timestamp = 0) {
global $user;
if ($user->uid) {
// Retrieve the timestamp at which the current user last viewed the
// specified node.
if (!$timestamp) {
$timestamp = node_last_viewed($nid);
}
$timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
// Use the timestamp to retrieve the number of new comments.
$result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d', $nid, $timestamp, COMMENT_PUBLISHED));
return $result;
}
else {
return 0;
}
}
/*
** Generate the basic commenting form, for appending to a node or display on a separate page.
** This is rendered by theme_comment_form.
*/
function comment_form($edit, $title = NULL) {
global $user;
$op = isset($_POST['op']) ? $_POST['op'] : '';
if ($user->uid) {
if ($edit['cid'] && user_access('administer comments')) {
if ($edit['author']) {
$author = $edit['author'];
}
elseif ($edit['name']) {
$author = $edit['name'];
}
else {
$author = $edit['registered_name'];
}
if ($edit['status']) {
$status = $edit['status'];
}
else {
$status = 0;
}
if ($edit['date']) {
$date = $edit['date'];
}
else {
$date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
}
$form['admin'] = array('#type' => 'fieldset', '#title' => t('Administration'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -2);
$form['admin']['author'] = array('#type' => 'textfield', '#parents' => array('author'), '#title' => t('Authored by'), '#size' => 30, '#maxlength' => 60, '#autocomplete_path' => 'user/autocomplete', '#default_value' => $author, '#weight' => -1);
$form['admin']['date'] = array('#type' => 'textfield', '#parents' => array('date'), '#title' => t('Authored on'), '#size' => 20, '#maxlength' => 25, '#default_value' => $date, '#weight' => -1);
$form['admin']['status'] = array('#type' => 'radios', '#parents' => array('status'), '#title' => t('Status'), '#default_value' => $status, '#options' => array(t('Published'), t('Not published')), '#weight' => -1);
}
else {
$form['author'] = array('#type' => 'item', '#title' => t('Your name'), '#value' => theme('username', $user)
);
}
}
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', 'Anonymous')
);
$form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.')
);
$form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
}
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', 'Anonymous'), '#required' => TRUE);
$form['name'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'],'#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);
$form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
}
if (variable_get('comment_subject_field', 1) == 1) {
$form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#maxlength' => 64, '#default_value' => $edit['subject']);
}
$form['comment'] = array('#type' => 'textarea', '#title' => t('Comment'), '#rows' => 15, '#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature, '#required' => TRUE
);
$form = array_merge($form, filter_form($node->format));
$form['format']['#weight'] = 18;
$form['cid'] = array('#type' => 'value', '#value' => $edit['cid']);
$form['pid'] = array('#type' => 'value', '#value' => $edit['pid']);
$form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
$form['uid'] = array('#type' => 'value', '#value' => $edit['uid']);
$form['preview'] = array('#type' => 'submit', '#value' => t('Preview comment'), '#weight' => 19);
$form['#token'] = 'comment' . $edit['nid'] . $edit['pid'];
// Only show post button if preview is optional or if we are in preview mode.
// We show the post button in preview mode even if there are form errors so that
// optional form elements (e.g., captcha) can be updated in preview mode.
if ((variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview comment')) || ($op == t('Post comment'))) {
$form['submit'] = array('#type' => 'submit', '#value' => t('Post comment'), '#weight' => 20);
}
if ($op == t('Preview comment')) {
$form['#after_build'] = 'comment_form_add_preview';
}
if ($_REQUEST['destination']) {
$form['#attributes']['destination'] = $_REQUEST['destination'];
}
if (empty($edit['cid']) && empty($edit['pid'])) {
$form['#action'] = url('comment/reply/'. $edit['nid']);
}
// Graft in extra form additions
$form = array_merge($form, comment_invoke_comment($form, 'form'));
return theme('box', $title, drupal_get_form('comment_form', $form));
}
function comment_form_add_preview($form_id, $form, $edit) {
global $user;
$output = '';
$comment = array2object(comment_validate($edit));
// Attach the user and time information.
if ($edit['author']) {
$account = user_load(array('name' => $edit['author']));
}
elseif ($user->uid) {
$account = $user;
}
if ($account) {
$comment->uid = $account->uid;
$comment->name = check_plain($account->name);
}
$comment->timestamp = $edit['timestamp'] ? $edit['timestamp'] : time();
// Preview the comment with security check.
if (!form_get_errors()) {
$output .= theme('comment_view', $comment);
}
$form['comment_preview'] = array('#value' => $output, '#weight' => -100);
$output = '';
if ($edit['pid']) {
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output .= theme('comment_view', $comment);
}
else {
$output .= node_view(node_load($edit['nid']));
$edit['pid'] = 0;
}
$form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);
return $form;
}
function comment_form_validate($form_id, $form_values) {
comment_validate($form_values);
}
function comment_form_execute($form_id, $form_values) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
$nid = $form_values['nid'];
// are we posting or previewing a reply?
if ($op == t('Post comment')) {
drupal_set_title(t('Post comment'));
if ($cid = comment_save($form_values)) {
drupal_goto("node/$nid#comment-$cid");
}
}
else if ($_POST['op'] == t('Preview comment')) {
drupal_set_title(t('Preview comment'));
}
}
/*
** Renderer or visualization functions this can be optionally
** overridden by themes.
*/
function theme_comment_form($form) {
return form_render($form);
}
function theme_comment_preview($comment, $links = array(), $visible = 1) {
$output = '