View source
<?php
function comment_delete_perm() {
return array(
'delete own comments',
'delete own comments at anytime',
'delete any comment',
'delete any comments at anytime',
'delete all comment replies',
'move all replies up',
);
}
function comment_delete_menu() {
$items['admin/content/comment/deletion'] = array(
'title' => 'Deletion settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'comment_delete_settings_form',
),
'access arguments' => array(
'administer comments',
),
'type' => MENU_LOCAL_TASK,
'file' => 'comment_delete.admin.inc',
);
return $items;
}
function comment_delete_menu_alter(&$items) {
$items['comment/delete']['page callback'] = 'drupal_get_form';
$items['comment/delete']['page arguments'] = array(
'comment_delete_form',
);
$items['comment/delete']['access callback'] = 'comment_delete_access_check';
$items['comment/delete']['access arguments'] = array(
2,
);
unset($items['comment/delete']['file']);
}
function comment_delete_access_check($cid) {
if (empty($cid)) {
return FALSE;
}
global $user;
$comment = _comment_load($cid);
$clock_expired = FALSE;
if ($clock = variable_get('comment_delete_clock', 0)) {
$clock_expired = time() - $comment->timestamp > $clock;
}
if (user_access('delete any comment') && !$clock_expired || user_access('delete any comments at anytime')) {
return TRUE;
}
if (user_access('delete own comments', $user) && !$clock_expired || user_access('delete own comments at anytime')) {
if ($user->uid == $comment->uid) {
return TRUE;
}
}
return FALSE;
}
function comment_delete_reply_actions() {
if (user_access('delete all comment replies')) {
$actions[0] = t('Delete all replies of the comment');
}
if (user_access('move all replies up')) {
$actions[1] = t('Move all replies up one level');
}
$actions[2] = t('Delete only the comment, or it\'s text and the subject if it has replies');
return $actions;
}
function comment_delete_form($form_state, $cid) {
$comment = _comment_load($cid);
if ($comment->cid) {
$form['cid'] = array(
'#type' => 'hidden',
'#value' => $cid,
);
if ($actions = comment_delete_reply_actions()) {
$form['replies'] = array(
'#type' => 'select',
'#title' => t('Action for replies'),
'#description' => t('Specify how replies should be handled when deleting a comment.'),
'#options' => $actions,
'#required' => TRUE,
'#default_value' => variable_get('comment_delete_replies', 0),
);
}
else {
$form['replies'] = array(
'#type' => 'hidden',
'#value' => variable_get('comment_delete_replies', 0),
);
}
$msg = t("Are you sure you want to delete the comment %title?", array(
'%title' => $comment->subject,
));
return confirm_form($form, $msg, "node/{$comment->nid}", NULL, t('Delete'));
}
else {
drupal_set_message(t('This comment no longer exists.'));
drupal_goto('');
}
}
function comment_delete_form_submit($form, &$form_state) {
include_once drupal_get_path('module', 'comment') . '/comment.admin.inc';
$comment = _comment_load($form_state['values']['cid']);
if (!$form_state['values']['replies']) {
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
drupal_set_message(t('The comment and all its replies have been deleted.'));
}
elseif ($form_state['values']['replies'] == 1) {
comment_delete_move_replies($comment);
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
drupal_set_message(t("The comment has been deleted and it's replies have been moved up one level."));
}
elseif ($form_state['values']['replies'] == 2) {
if (comment_num_replies($comment->cid)) {
db_query("UPDATE {comments} SET subject = '', comment = '%s' WHERE cid = %d", t('This comment has been deleted.'), $comment->cid);
drupal_set_message(t('The comment subject/body has been deleted, all replies will remain.'));
}
else {
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
drupal_set_message(t('The comment has been deleted.'));
}
}
cache_clear_all();
$form_state['redirect'] = "node/{$comment->nid}";
}
function comment_delete_move_replies($comment, $old_cid = 0) {
$parent_id = $comment->pid;
$results = db_query("SELECT * FROM {comments} WHERE pid = %d", $old_cid ? $old_cid : $comment->cid);
while ($edit = db_fetch_array($results)) {
$old_cid = $edit['cid'];
unset($edit['cid']);
$edit['pid'] = $parent_id;
$new_cid = comment_save($edit);
$new_comment = _comment_load($new_cid);
comment_delete_move_replies($new_comment, $old_cid);
}
}
function comment_delete_link($type, $comment = NULL, $teaser = FALSE) {
$links = array();
if ($type == 'comment' && !user_access('administer comments')) {
if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
if (comment_delete_access_check($comment->cid)) {
$links['comment_delete'] = array(
'title' => t('delete'),
'href' => "comment/delete/{$comment->cid}",
);
}
}
}
return $links;
}