| 1 |
<?php
|
| 2 |
// $Id: comment.admin.inc,v 1.35 2009/10/16 20:40:05 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Admin page callbacks for the comment module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback; present an administrative comment listing.
|
| 11 |
*/
|
| 12 |
function comment_admin($type = 'new') {
|
| 13 |
$edit = $_POST;
|
| 14 |
|
| 15 |
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
|
| 16 |
return drupal_get_form('comment_multiple_delete_confirm');
|
| 17 |
}
|
| 18 |
else {
|
| 19 |
return drupal_get_form('comment_admin_overview', $type, arg(4));
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Form builder; Builds the comment overview form for the admin.
|
| 25 |
*
|
| 26 |
* @param $type
|
| 27 |
* Not used.
|
| 28 |
* @param $arg
|
| 29 |
* Current path's fourth component deciding the form type (Published comments/Approval queue).
|
| 30 |
* @return
|
| 31 |
* The form structure.
|
| 32 |
* @ingroup forms
|
| 33 |
* @see comment_admin_overview_validate()
|
| 34 |
* @see comment_admin_overview_submit()
|
| 35 |
* @see theme_comment_admin_overview()
|
| 36 |
*/
|
| 37 |
function comment_admin_overview($form, &$form_state, $arg) {
|
| 38 |
// Build an 'Update options' form.
|
| 39 |
$form['options'] = array(
|
| 40 |
'#type' => 'fieldset',
|
| 41 |
'#title' => t('Update options'),
|
| 42 |
'#prefix' => '<div class="container-inline">',
|
| 43 |
'#suffix' => '</div>',
|
| 44 |
);
|
| 45 |
$options = array();
|
| 46 |
foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
|
| 47 |
$options[$key] = $value[0];
|
| 48 |
}
|
| 49 |
$form['options']['operation'] = array(
|
| 50 |
'#type' => 'select',
|
| 51 |
'#options' => $options,
|
| 52 |
'#default_value' => 'publish',
|
| 53 |
);
|
| 54 |
$form['options']['submit'] = array(
|
| 55 |
'#type' => 'submit',
|
| 56 |
'#value' => t('Update'),
|
| 57 |
);
|
| 58 |
|
| 59 |
// Load the comments that need to be displayed.
|
| 60 |
$status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
|
| 61 |
$header = array(
|
| 62 |
'subject' => array('data' => t('Subject'), 'field' => 'subject'),
|
| 63 |
'author' => array('data' => t('Author'), 'field' => 'name'),
|
| 64 |
'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
|
| 65 |
'changed' => array('data' => t('Updated'), 'field' => 'changed', 'sort' => 'desc'),
|
| 66 |
'operations' => array('data' => t('Operations')),
|
| 67 |
);
|
| 68 |
|
| 69 |
$query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
|
| 70 |
$query->join('users', 'u', 'u.uid = c.uid');
|
| 71 |
$query->join('node', 'n', 'n.nid = c.nid');
|
| 72 |
$query->addField('u', 'name', 'registered_name');
|
| 73 |
$query->addField('n', 'title', 'node_title');
|
| 74 |
$result = $query
|
| 75 |
->fields('c', array('subject', 'nid', 'cid', 'comment', 'changed', 'status', 'name', 'homepage'))
|
| 76 |
->fields('u', array('uid'))
|
| 77 |
->condition('c.status', $status)
|
| 78 |
->limit(50)
|
| 79 |
->orderByHeader($header)
|
| 80 |
->execute();
|
| 81 |
|
| 82 |
|
| 83 |
// Build a table listing the appropriate comments.
|
| 84 |
$options = array();
|
| 85 |
$destination = drupal_get_destination();
|
| 86 |
|
| 87 |
foreach ($result as $comment) {
|
| 88 |
$options[$comment->cid] = array(
|
| 89 |
'subject' => array(
|
| 90 |
'data' => array(
|
| 91 |
'#type' => 'link',
|
| 92 |
'#title' => $comment->subject,
|
| 93 |
'#href' => 'comment/' . $comment->cid,
|
| 94 |
'#options' => array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-' . $comment->cid),
|
| 95 |
),
|
| 96 |
),
|
| 97 |
'author' => theme('username', array('account' => $comment)),
|
| 98 |
'posted_in' => array(
|
| 99 |
'data' => array(
|
| 100 |
'#type' => 'link',
|
| 101 |
'#title' => $comment->node_title,
|
| 102 |
'#href' => 'node/' . $comment->nid,
|
| 103 |
),
|
| 104 |
),
|
| 105 |
'changed' => format_date($comment->changed, 'short'),
|
| 106 |
'operations' => array(
|
| 107 |
'data' => array(
|
| 108 |
'#type' => 'link',
|
| 109 |
'#title' => t('edit'),
|
| 110 |
'#href' => 'comment/' . $comment->cid . '/edit',
|
| 111 |
'#options' => array('query' => $destination),
|
| 112 |
),
|
| 113 |
),
|
| 114 |
);
|
| 115 |
}
|
| 116 |
|
| 117 |
$form['comments'] = array(
|
| 118 |
'#type' => 'tableselect',
|
| 119 |
'#header' => $header,
|
| 120 |
'#options' => $options,
|
| 121 |
'#empty' => t('No comments available.'),
|
| 122 |
);
|
| 123 |
|
| 124 |
$form['pager'] = array('#theme' => 'pager');
|
| 125 |
|
| 126 |
return $form;
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Validate comment_admin_overview form submissions.
|
| 131 |
*/
|
| 132 |
function comment_admin_overview_validate($form, &$form_state) {
|
| 133 |
$form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
|
| 134 |
// We can't execute any 'Update options' if no comments were selected.
|
| 135 |
if (count($form_state['values']['comments']) == 0) {
|
| 136 |
form_set_error('', t('Please select one or more comments to perform the update on.'));
|
| 137 |
drupal_goto('admin/content/comment');
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
/**
|
| 142 |
* Process comment_admin_overview form submissions.
|
| 143 |
*
|
| 144 |
* Execute the chosen 'Update option' on the selected comments, such as
|
| 145 |
* publishing, unpublishing or deleting.
|
| 146 |
*/
|
| 147 |
function comment_admin_overview_submit($form, &$form_state) {
|
| 148 |
$operations = comment_operations();
|
| 149 |
if ($operations[$form_state['values']['operation']][1]) {
|
| 150 |
// Extract the appropriate database query operation.
|
| 151 |
$query = $operations[$form_state['values']['operation']][1];
|
| 152 |
foreach ($form_state['values']['comments'] as $cid => $value) {
|
| 153 |
if ($value) {
|
| 154 |
// Perform the update action, then refresh node statistics.
|
| 155 |
$query
|
| 156 |
->condition('cid', $cid )
|
| 157 |
->execute();
|
| 158 |
$comment = comment_load($cid);
|
| 159 |
_comment_update_node_statistics($comment->nid);
|
| 160 |
// Allow modules to respond to the updating of a comment.
|
| 161 |
module_invoke_all('comment_' . $form_state['values']['operation'], $comment);
|
| 162 |
// Add an entry to the watchdog log.
|
| 163 |
watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/' . $comment->nid, array('fragment' => 'comment-' . $comment->cid)));
|
| 164 |
}
|
| 165 |
}
|
| 166 |
cache_clear_all();
|
| 167 |
drupal_set_message(t('The update has been performed.'));
|
| 168 |
$form_state['redirect'] = 'admin/content/comment';
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* List the selected comments and verify that the admin wants to delete them.
|
| 174 |
*
|
| 175 |
* @param $form_state
|
| 176 |
* An associative array containing the current state of the form.
|
| 177 |
* @return
|
| 178 |
* TRUE if the comments should be deleted, FALSE otherwise.
|
| 179 |
* @ingroup forms
|
| 180 |
* @see comment_multiple_delete_confirm_submit()
|
| 181 |
*/
|
| 182 |
function comment_multiple_delete_confirm($form, &$form_state) {
|
| 183 |
$edit = $form_state['input'];
|
| 184 |
|
| 185 |
$form['comments'] = array(
|
| 186 |
'#prefix' => '<ul>',
|
| 187 |
'#suffix' => '</ul>',
|
| 188 |
'#tree' => TRUE,
|
| 189 |
);
|
| 190 |
// array_filter() returns only elements with actual values.
|
| 191 |
$comment_counter = 0;
|
| 192 |
foreach (array_filter($edit['comments']) as $cid => $value) {
|
| 193 |
$comment = comment_load($cid);
|
| 194 |
if (is_object($comment) && is_numeric($comment->cid)) {
|
| 195 |
$subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
|
| 196 |
$form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
|
| 197 |
$comment_counter++;
|
| 198 |
}
|
| 199 |
}
|
| 200 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 201 |
|
| 202 |
if (!$comment_counter) {
|
| 203 |
drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
|
| 204 |
drupal_goto('admin/content/comment');
|
| 205 |
}
|
| 206 |
else {
|
| 207 |
return confirm_form($form,
|
| 208 |
t('Are you sure you want to delete these comments and all their children?'),
|
| 209 |
'admin/content/comment', t('This action cannot be undone.'),
|
| 210 |
t('Delete comments'), t('Cancel'));
|
| 211 |
}
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Process comment_multiple_delete_confirm form submissions.
|
| 216 |
*/
|
| 217 |
function comment_multiple_delete_confirm_submit($form, &$form_state) {
|
| 218 |
if ($form_state['values']['confirm']) {
|
| 219 |
comment_delete_multiple(array_keys($form_state['values']['comments']));
|
| 220 |
cache_clear_all();
|
| 221 |
$count = count($form_state['values']['comments']);
|
| 222 |
watchdog('content', 'Deleted @count comments.', array('@count' => $count));
|
| 223 |
drupal_set_message(t('Deleted @count comments.', array('@count' => $count)));
|
| 224 |
}
|
| 225 |
$form_state['redirect'] = 'admin/content/comment';
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Form builder; Builds the confirmation form for deleting a single comment.
|
| 230 |
*
|
| 231 |
* @ingroup forms
|
| 232 |
* @see comment_confirm_delete_submit()
|
| 233 |
*/
|
| 234 |
function comment_confirm_delete($form, &$form_state, $comment) {
|
| 235 |
$form['#comment'] = $comment;
|
| 236 |
return confirm_form(
|
| 237 |
$form,
|
| 238 |
t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
|
| 239 |
'node/' . $comment->nid,
|
| 240 |
t('Any replies to this comment will be lost. This action cannot be undone.'),
|
| 241 |
t('Delete'),
|
| 242 |
t('Cancel'),
|
| 243 |
'comment_confirm_delete');
|
| 244 |
}
|
| 245 |
|
| 246 |
/**
|
| 247 |
* Process comment_confirm_delete form submissions.
|
| 248 |
*/
|
| 249 |
function comment_confirm_delete_submit($form, &$form_state) {
|
| 250 |
$comment = $form['#comment'];
|
| 251 |
// Delete the comment and its replies.
|
| 252 |
comment_delete($comment->cid);
|
| 253 |
drupal_set_message(t('The comment and all its replies have been deleted.'));
|
| 254 |
watchdog('content', t('Deleted comment @cid and its replies.', array('@cid' => $comment->cid)));
|
| 255 |
// Clear the cache so an anonymous user sees that his comment was deleted.
|
| 256 |
cache_clear_all();
|
| 257 |
|
| 258 |
$form_state['redirect'] = "node/$comment->nid";
|
| 259 |
}
|