View source
<?php
function comment_delete_permission() {
return array(
'delete own comment' => array(
'title' => t('Delete own comments'),
'description' => t('Allow user to delete their own comments within threshold.'),
),
'delete own comment anytime' => array(
'title' => t('Delete own comments anytime'),
'description' => t('Allow user to delete their own comments anytime.'),
),
'delete any comment' => array(
'title' => t('Delete any comment'),
'description' => t('Allow user to delete any comment within threshold.'),
),
'delete any comment anytime' => array(
'title' => t('Delete any comment anytime'),
'description' => t('Allow user to delete any comment anytime.'),
),
'delete comment replies' => array(
'title' => t('Delete comment replies'),
'description' => t('Allow user to delete replies to their own comments.'),
),
'move comment replies' => array(
'title' => t('Move comment replies'),
'description' => t('Allow user to move replies to their own comments.'),
),
);
}
function comment_delete_menu() {
$items['admin/content/comment/deletion'] = array(
'title' => 'Deletion',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'comment_delete_settings_form',
),
'access arguments' => array(
'administer comments',
),
'file' => 'comment_delete.admin.inc',
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function comment_delete_menu_alter(&$items) {
$items['comment/%/delete']['page callback'] = 'drupal_get_form';
$items['comment/%/delete']['page arguments'] = array(
'comment_delete_options_form',
1,
);
$items['comment/%/delete']['access callback'] = 'comment_delete_check_access';
$items['comment/%/delete']['access arguments'] = array(
1,
);
unset($items['comment/%/delete']['file']);
}
function comment_delete_comment_view($comment, $view_mode, $langcode) {
if (comment_delete_check_access($comment->cid)) {
$comment->content['links']['comment']['#links']['comment-delete'] = array(
'title' => t('delete'),
'href' => 'comment/' . $comment->cid . '/delete',
'html' => TRUE,
);
}
}
function comment_delete_check_access($cid) {
if (empty($cid) || !is_numeric($cid)) {
return FALSE;
}
global $user;
$comment = comment_load($cid);
if (!isset($comment->cid)) {
return FALSE;
}
$threshold = 0;
if ($threshold_setting = variable_get('comment_delete_threshold', 0)) {
$threshold = time() - $comment->created > $threshold_setting;
}
if (user_access('delete any comment') && !$threshold || user_access('delete any comment anytime')) {
return TRUE;
}
if ((user_access('delete own comment') && !$threshold || user_access('delete own comment anytime')) && $user->uid == $comment->uid) {
return TRUE;
}
return FALSE;
}
function comment_delete_options_form($form, &$form_state, $cid) {
$comment = comment_load($cid);
if (!isset($comment->cid)) {
drupal_goto('<front>');
}
$form_state['storage']['comment'] = $comment;
$default = variable_get('comment_delete_default', 0);
$options = array();
if (user_access('delete comment replies')) {
$options[0] = t('Delete comment and replies');
}
if (user_access('move comment replies')) {
$options[1] = t('Delete comment and move replies up');
}
$options[2] = t('Delete comment and keep replies');
if (count($options) > 1) {
$form['action'] = array(
'#type' => 'radios',
'#title' => t('How should replies to this comment be handled?'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => isset($options[$default]) ? $default : 2,
);
}
else {
$form['action'] = array(
'#type' => 'hidden',
'#value' => 2,
);
}
$message = t('Are you sure you want to delete the comment %title?', array(
'%title' => $comment->subject,
));
return confirm_form($form, $message, "node/{$comment->nid}", NULL, t('Delete'));
}
function comment_delete_options_form_submit($form, $form_state) {
$comment = $form_state['storage']['comment'];
switch ($form_state['values']['action']) {
case 0:
comment_delete_remove_replies($comment);
break;
case 1:
comment_delete_move_replies($comment);
break;
case 2:
comment_delete_keep_replies($comment);
break;
}
if ($message = variable_get('comment_delete_message', '')) {
drupal_set_message(t($message));
}
if (!isset($_GET['destination'])) {
drupal_goto('node/' . $comment->nid);
}
}
function comment_delete_soft_remove($comment) {
$comment->subject = '';
$comment->comment_body[$comment->language][0]['value'] = '';
comment_save($comment);
}
function comment_delete_remove_replies($comment) {
$soft = variable_get('comment_delete_soft', 0);
if (!$soft) {
comment_delete($comment->cid);
return;
}
$query = db_select('comment', 'c')
->fields('c', array(
'cid',
))
->condition('c.nid', $comment->nid);
$db_or = db_or();
$db_or
->condition('c.thread', $comment->thread);
$db_or
->condition('c.thread', str_replace('/', '.', $comment->thread) . '%', 'LIKE');
$query
->condition($db_or);
foreach ($query
->execute() as $reply) {
comment_delete_soft_remove(comment_load($reply->cid));
}
}
function comment_delete_keep_replies($comment) {
$soft = variable_get('comment_delete_soft', 0);
$replies = db_select('comment', 'c')
->fields('c', array(
'cid',
))
->condition('c.pid', $comment->cid, '=')
->countQuery()
->execute()
->fetchField();
if ($replies > 0) {
$comment->subject = '';
$comment->comment_body[$comment->language][0]['value'] = '';
comment_save($comment);
}
else {
if (!$soft) {
comment_delete($comment->cid);
}
else {
comment_delete_soft_remove($comment);
}
}
}
function comment_delete_move_replies($comment) {
$soft = variable_get('comment_delete_soft', 0);
$replies = db_select('comment', 'c')
->fields('c', array(
'cid',
))
->condition('c.pid', $comment->cid, '=')
->execute();
foreach ($replies as $reply) {
$reply_comment = comment_load($reply->cid);
$reply_comment->pid = $comment->pid;
comment_save($reply_comment);
}
if (!$soft) {
comment_delete($comment->cid);
}
else {
comment_delete_soft_remove($comment);
}
comment_delete_threading($comment->nid);
}
function comment_delete_threading($nid) {
$comments = array();
$results = db_select('comment', 'c')
->fields('c', array(
'cid',
'pid',
'created',
))
->condition('c.nid', $nid)
->orderBy('c.created', 'ASC')
->execute();
foreach ($results as $data) {
$comments[] = (array) $data;
}
$tree = comment_delete_threading_tree($comments);
$threads = comment_delete_threading_values($tree);
foreach ($threads as $cid => $thread_string) {
db_update('comment')
->fields(array(
'thread' => $thread_string . '/',
))
->condition('cid', $cid)
->execute();
}
}
function comment_delete_threading_tree(array $comments, $pid = 0) {
$branch = array();
foreach ($comments as $comment) {
if ($comment['pid'] == $pid) {
$children = comment_delete_threading_tree($comments, $comment['cid']);
if ($children) {
$comment['children'] = $children;
}
$branch[] = $comment;
}
}
return $branch;
}
function comment_delete_threading_values(array $tree, $prefix = '', array $threading = array(), $init = TRUE) {
$thread = $init ? '01' : '00';
uasort($tree, 'comment_delete_threading_sort');
foreach ($tree as $comment) {
$string = (!empty($prefix) ? $prefix . '.' : '') . int2vancode(sprintf('%02d', $thread++));
$threading[$comment['cid']] = $string;
if (isset($comment['children'])) {
$children = $comment['children'];
uasort($children, 'comment_delete_threading_sort');
$child_threading = comment_delete_threading_values($children, $string, $threading, FALSE);
$threading += $child_threading;
}
}
return $threading;
}
function comment_delete_threading_sort($a, $b) {
if ($a['created'] == $b['created']) {
return 0;
}
return $a['created'] < $b['created'] ? -1 : 1;
}