Commit b99aab83 authored by Dries's avatar Dries
Browse files

- Patch #163499 by Crell et al: split up comment module.

parent 5d9a398b
<?php
// $Id$
/**
* @file
* Admin page callbacks for the comment module.
*/
/**
* Menu callback; present an administrative comment listing.
*/
function comment_admin($type = 'new') {
$edit = $_POST;
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
return drupal_get_form('comment_multiple_delete_confirm');
}
else {
return drupal_get_form('comment_admin_overview', $type, arg(4));
}
}
/**
* Form builder; Builds the comment overview form for the admin.
*
* @ingroup forms
* @see comment_admin_overview_validate().
* @see comment_admin_overview_submit().
* @see theme_comment_admin_overview().
*/
function comment_admin_overview($type = 'new', $arg) {
// build an 'Update options' form
$form['options'] = array(
'#type' => 'fieldset', '#title' => t('Update options'),
'#prefix' => '<div class="container-inline">', '#suffix' => '</div>'
);
$options = array();
foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
$options[$key] = $value[0];
}
$form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish');
$form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
// load the comments that we want to display
$status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
$form['header'] = array('#type' => 'value', '#value' => array(
theme('table_select_header_cell'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Author'), 'field' => 'name'),
array('data' => t('Posted in'), 'field' => 'node_title'),
array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
array('data' => t('Operations'))
));
$result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid, n.title as node_title FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d'. tablesort_sql($form['header']['#value']), 50, 0, NULL, $status);
// build a table listing the appropriate comments
$destination = drupal_get_destination();
while ($comment = db_fetch_object($result)) {
$comments[$comment->cid] = '';
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('title' => truncate_utf8($comment->comment, 128), 'fragment' => 'comment-'. $comment->cid)));
$form['username'][$comment->cid] = array('#value' => theme('username', $comment));
$form['node_title'][$comment->cid] = array('#value' => l($comment->node_title, 'node/'. $comment->nid));
$form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
$form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination)));
}
$form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array());
$form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
return $form;
}
/**
* We can't execute any 'Update options' if no comments were selected.
*/
function comment_admin_overview_validate($form, &$form_state) {
$form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
if (count($form_state['values']['comments']) == 0) {
form_set_error('', t('Please select one or more comments to perform the update on.'));
drupal_goto('admin/content/comment');
}
}
/**
* Execute the chosen 'Update option' on the selected comments, such as
* publishing, unpublishing or deleting.
*/
function comment_admin_overview_submit($form, &$form_state) {
$operations = comment_operations();
if ($operations[$form_state['values']['operation']][1]) {
// extract the appropriate database query operation
$query = $operations[$form_state['values']['operation']][1];
foreach ($form_state['values']['comments'] as $cid => $value) {
if ($value) {
// perform the update action, then refresh node statistics
db_query($query, $cid);
$comment = _comment_load($cid);
_comment_update_node_statistics($comment->nid);
// Allow modules to respond to the updating of a comment.
comment_invoke_comment($comment, $form_state['values']['operation']);
// Add an entry to the watchdog log.
watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)));
}
}
cache_clear_all();
drupal_set_message(t('The update has been performed.'));
$form_state['redirect'] = 'admin/content/comment';
}
}
/**
* Theme the comment admin form.
*
* @ingroup themeable
*/
function theme_comment_admin_overview($form) {
$output = drupal_render($form['options']);
if (isset($form['subject']) && is_array($form['subject'])) {
foreach (element_children($form['subject']) as $key) {
$row = array();
$row[] = drupal_render($form['comments'][$key]);
$row[] = drupal_render($form['subject'][$key]);
$row[] = drupal_render($form['username'][$key]);
$row[] = drupal_render($form['node_title'][$key]);
$row[] = drupal_render($form['timestamp'][$key]);
$row[] = drupal_render($form['operations'][$key]);
$rows[] = $row;
}
}
else {
$rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
}
$output .= theme('table', $form['header']['#value'], $rows);
if ($form['pager']['#value']) {
$output .= drupal_render($form['pager']);
}
$output .= drupal_render($form);
return $output;
}
/**
* List the selected comments and verify that the admin really wants to delete
* them.
*
* @ingroup forms
* @see comment_multiple_delete_confirm_submit().
*/
function comment_multiple_delete_confirm(&$form_state) {
$edit = $form_state['post'];
$form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
// array_filter() returns only elements with actual values
$comment_counter = 0;
foreach (array_filter($edit['comments']) as $cid => $value) {
$comment = _comment_load($cid);
if (is_object($comment) && is_numeric($comment->cid)) {
$subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid));
$form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) .'</li>');
$comment_counter++;
}
}
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
if (!$comment_counter) {
drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.'));
drupal_goto('admin/content/comment');
}
else {
return confirm_form($form,
t('Are you sure you want to delete these comments and all their children?'),
'admin/content/comment', t('This action cannot be undone.'),
t('Delete comments'), t('Cancel'));
}
}
/**
* Perform the actual comment deletion.
*/
function comment_multiple_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
foreach ($form_state['values']['comments'] as $cid => $value) {
$comment = _comment_load($cid);
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
}
cache_clear_all();
drupal_set_message(t('The comments have been deleted.'));
}
return 'admin/content/comment';
}
/**
* Menu callback; delete a comment.
*/
function comment_delete($cid = NULL) {
$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 = '';
if (is_object($comment) && is_numeric($comment->cid)) {
$output = drupal_get_form('comment_confirm_delete', $comment);
}
else {
drupal_set_message(t('The comment no longer exists.'));
}
return $output;
}
/**
* Form builder; Builds the confirmation form for deleting a single comment.
*
* @ingroup forms
* @see comment_confirm_delete_submit().
*/
function comment_confirm_delete(&$form_state, $comment) {
$form = array();
$form['#comment'] = $comment;
return confirm_form(
$form,
t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
'node/'. $comment->nid,
t('Any replies to this comment will be lost. This action cannot be undone.'),
t('Delete'),
t('Cancel'),
'comment_confirm_delete');
}
function comment_confirm_delete_submit($form, &$form_state) {
drupal_set_message(t('The comment and all its replies have been deleted.'));
$comment = $form['#comment'];
// 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();
$form_state['redirect'] = "node/$comment->nid";
return;
}
function _comment_delete_thread($comment) {
if (!is_object($comment) || !is_numeric($comment->cid)) {
watchdog('content', 'Cannot delete non-existent comment.', WATCHDOG_WARNING);
return;
}
// Delete the comment:
db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject));
comment_invoke_comment($comment, 'delete');
// Delete the comment's replies
$result = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE pid = %d', $comment->cid);
while ($comment = db_fetch_object($result)) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
_comment_delete_thread($comment);
}
}
......@@ -199,6 +199,7 @@ function comment_menu() {
'description' => 'List and edit site comments and the comment moderation queue.',
'page callback' => 'comment_admin',
'access arguments' => array('administer comments'),
'file' => 'comment.admin.inc',
);
// Tabs:
......@@ -211,6 +212,7 @@ function comment_menu() {
'title' => 'Approval queue',
'page arguments' => array('approval'),
'type' => MENU_LOCAL_TASK,
'file' => 'comment.admin.inc',
);
$items['comment/delete'] = array(
......@@ -218,6 +220,7 @@ function comment_menu() {
'page callback' => 'comment_delete',
'access arguments' => array('administer comments'),
'type' => MENU_CALLBACK,
'file' => 'comment.admin.inc',
);
$items['comment/edit'] = array(
......@@ -225,6 +228,7 @@ function comment_menu() {
'page callback' => 'comment_edit',
'access arguments' => array('post comments'),
'type' => MENU_CALLBACK,
'file' => 'comment.pages.inc',
);
$items['comment/reply/%node'] = array(
'title' => 'Reply to comment',
......@@ -233,6 +237,7 @@ function comment_menu() {
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'type' => MENU_CALLBACK,
'file' => 'comment.pages.inc',
);
return $items;
......@@ -645,107 +650,6 @@ function comment_node_url() {
return arg(0) .'/'. arg(1);
}
function comment_edit($cid) {
global $user;
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid));
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
if (comment_access('edit', $comment)) {
return comment_form_box((array)$comment);
}
else {
drupal_access_denied();
}
}
/**
* This function is responsible for generating a comment reply form.
* There are several cases that have to be handled, including:
* - replies to comments
* - replies to nodes
* - attempts to reply to nodes that can no longer accept comments
* - respecting access permissions ('access comments', 'post comments', etc.)
*
* The node or comment that is being replied to must appear above the comment
* form to provide the user context while authoring the comment.
*
* @param $node
* Every comment belongs to a node. This is that node.
* @param $pid
* Some comments are replies to other comments. In those cases, $pid is the parent
* comment's cid.
*
* @return $output
* The rendered parent node or comment plus the new comment form.
*/
function comment_reply($node, $pid = NULL) {
// Set the breadcrumb trail.
drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/'. $node->nid)));
$op = isset($_POST['op']) ? $_POST['op'] : '';
$output = '';
if (user_access('access comments')) {
// The user is previewing a comment prior to submitting it.
if ($op == t('Preview comment')) {
if (user_access('post comments')) {
$output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), NULL);
}
else {
drupal_set_message(t('You are not authorized to post comments.'), 'error');
drupal_goto("node/$node->nid");
}
}
else {
// $pid indicates that this is a reply to a comment.
if ($pid) {
// load the comment whose cid = $pid
if ($comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $pid, COMMENT_PUBLISHED))) {
// If that comment exists, make sure that the current comment and the parent comment both
// belong to the same parent node.
if ($comment->nid != $node->nid) {
// Attempting to reply to a comment not belonging to the current nid.
drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
drupal_goto("node/$node->nid");
}
// Display the parent comment
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output .= theme('comment_view', $comment, $node);
}
else {
drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
drupal_goto("node/$node->nid");
}
}
// This is the case where the comment is in response to a node. Display the node.
else if (user_access('access content')) {
$output .= node_view($node);
}
// Should we show the reply box?
if (node_comment_mode($node->nid) != COMMENT_NODE_READ_WRITE) {
drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
drupal_goto("node/$node->nid");
}
else if (user_access('post comments')) {
$output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), t('Reply'));
}
else {
drupal_set_message(t('You are not authorized to post comments.'), 'error');
drupal_goto("node/$node->nid");
}
}
}
else {
drupal_set_message(t('You are not authorized to view comments.'), 'error');
drupal_goto("node/$node->nid");
}
return $output;
}
/**
* Accepts a submission of new or changed comment content.
*
......@@ -1121,55 +1025,6 @@ function comment_render($node, $cid = 0) {
return $output;
}
/**
* Menu callback; delete a comment.
*/
function comment_delete($cid = NULL) {
$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 = '';
if (is_object($comment) && is_numeric($comment->cid)) {
$output = drupal_get_form('comment_confirm_delete', $comment);
}
else {
drupal_set_message(t('The comment no longer exists.'));
}
return $output;
}
function comment_confirm_delete(&$form_state, $comment) {
$form = array();
$form['#comment'] = $comment;
return confirm_form(
$form,
t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
'node/'. $comment->nid,
t('Any replies to this comment will be lost. This action cannot be undone.'),
t('Delete'),
t('Cancel'),
'comment_confirm_delete');
}
function comment_confirm_delete_submit($form, &$form_state) {
drupal_set_message(t('The comment and all its replies have been deleted.'));
$comment = $form['#comment'];
// 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();
$form_state['redirect'] = "node/$comment->nid";
return;
}
/**
* Comment operations. We offer different update operations depending on
* which comment administration page we're on.
......@@ -1197,175 +1052,6 @@ function comment_operations($action = NULL) {
return $operations;
}
/**
* Menu callback; present an administrative comment listing.
*/
function comment_admin($type = 'new') {
$edit = $_POST;
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
return drupal_get_form('comment_multiple_delete_confirm');
}
else {
return drupal_get_form('comment_admin_overview', $type, arg(4));
}
}
function comment_admin_overview($type = 'new', $arg) {
// build an 'Update options' form
$form['options'] = array(
'#type' => 'fieldset', '#title' => t('Update options'),
'#prefix' => '<div class="container-inline">', '#suffix' => '</div>'
);
$options = array();
foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
$options[$key] = $value[0];
}
$form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish');
$form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
// load the comments that we want to display
$status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
$form['header'] = array('#type' => 'value', '#value' => array(
theme('table_select_header_cell'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Author'), 'field' => 'name'),
array('data' => t('Posted in'), 'field' => 'node_title'),
array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
array('data' => t('Operations'))
));
$result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid, n.title as node_title FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d'. tablesort_sql($form['header']['#value']), 50, 0, NULL, $status);
// build a table listing the appropriate comments
$destination = drupal_get_destination();
while ($comment = db_fetch_object($result)) {
$comments[$comment->cid] = '';
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('title' => truncate_utf8($comment->comment, 128), 'fragment' => 'comment-'. $comment->cid)));
$form['username'][$comment->cid] = array('#value' => theme('username', $comment));
$form['node_title'][$comment->cid] = array('#value' => l($comment->node_title, 'node/'. $comment->nid));
$form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
$form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination)));
}
$form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array());
$form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
return $form;
}
/**
* We can't execute any 'Update options' if no comments were selected.
*/
function comment_admin_overview_validate($form, &$form_state) {
$form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
if (count($form_state['values']['comments']) == 0) {
form_set_error('', t('Please select one or more comments to perform the update on.'));
drupal_goto('admin/content/comment');
}
}
/**
* Execute the chosen 'Update option' on the selected comments, such as
* publishing, unpublishing or deleting.
*/
function comment_admin_overview_submit($form, &$form_state) {
$operations = comment_operations();
if ($operations[$form_state['values']['operation']][1]) {
// extract the appropriate database query operation
$query = $operations[$form_state['values']['operation']][1];
foreach ($form_state['values']['comments'] as $cid => $value) {
if ($value) {
// perform the update action, then refresh node statistics
db_query($query, $cid);
$comment = _comment_load($cid);
_comment_update_node_statistics($comment->nid);
// Allow modules to respond to the updating of a comment.
comment_invoke_comment($comment, $form_state['values']['operation']);
// Add an entry to the watchdog log.
watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)));
}
}
cache_clear_all();
drupal_set_message(t('The update has been performed.'));
$form_state['redirect'] = 'admin/content/comment';
}
}
function theme_comment_admin_overview($form) {
$output = drupal_render($form['options']);
if (isset($form['subject']) && is_array($form['subject'])) {
foreach (element_children($form['subject']) as $key) {
$row = array();
$row[] = drupal_render($form['comments'][$key]);
$row[] = drupal_render($form['subject'][$key]);
$row[] = drupal_render($form['username'][$key]);
$row[] = drupal_render($form['node_title'][$key]);
$row[] = drupal_render($form['timestamp'][$key]);
$row[] = drupal_render($form['operations'][$key]);
$rows[] = $row;
}
}
else {
$rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
}
$output .= theme('table', $form['header']['#value'], $rows);
if ($form['pager']['#value']) {
$output .= drupal_render($form['pager']);
}
$output .= drupal_render($form);
return $output;
}
/**
* List the selected comments and verify that the admin really wants to delete
* them.
*/
function comment_multiple_delete_confirm(&$form_state) {
$edit = $form_state['post'];
$form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
// array_filter() returns only elements with actual values