00001 <?php
00002
00003
00012 function comment_admin($type = 'new') {
00013 $edit = $_POST;
00014
00015 if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
00016 return drupal_get_form('comment_multiple_delete_confirm');
00017 }
00018 else {
00019 return drupal_get_form('comment_admin_overview', $type, arg(4));
00020 }
00021 }
00022
00037 function comment_admin_overview($type = 'new', $arg) {
00038
00039 $form['options'] = array(
00040 '#type' => 'fieldset',
00041 '#title' => t('Update options'),
00042 '#prefix' => '<div class="container-inline">',
00043 '#suffix' => '</div>',
00044 );
00045 $options = array();
00046 foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
00047 $options[$key] = $value[0];
00048 }
00049 $form['options']['operation'] = array(
00050 '#type' => 'select',
00051 '#options' => $options,
00052 '#default_value' => 'publish',
00053 );
00054 $form['options']['submit'] = array(
00055 '#type' => 'submit',
00056 '#value' => t('Update'),
00057 );
00058
00059
00060 $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
00061 $form['header'] = array(
00062 '#type' => 'value',
00063 '#value' => array(
00064 theme('table_select_header_cell'),
00065 array('data' => t('Subject'), 'field' => 'subject'),
00066 array('data' => t('Author'), 'field' => 'name'),
00067 array('data' => t('Posted in'), 'field' => 'node_title'),
00068 array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
00069 array('data' => t('Operations')),
00070 ));
00071 $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);
00072
00073
00074 $destination = drupal_get_destination();
00075 while ($comment = db_fetch_object($result)) {
00076 $comments[$comment->cid] = '';
00077 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
00078 $form['subject'][$comment->cid] = array(
00079 '#value' => l($comment->subject, 'node/' . $comment->nid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-' . $comment->cid))
00080 );
00081 $form['username'][$comment->cid] = array(
00082 '#value' => theme('username', $comment)
00083 );
00084 $form['node_title'][$comment->cid] = array(
00085 '#value' => l($comment->node_title, 'node/' . $comment->nid)
00086 );
00087 $form['timestamp'][$comment->cid] = array(
00088 '#value' => format_date($comment->timestamp, 'small')
00089 );
00090 $form['operations'][$comment->cid] = array(
00091 '#value' => l(t('edit'), 'comment/edit/' . $comment->cid, array('query' => $destination))
00092 );
00093 }
00094 $form['comments'] = array(
00095 '#type' => 'checkboxes',
00096 '#options' => isset($comments) ? $comments: array()
00097 );
00098 $form['pager'] = array(
00099 '#value' => theme('pager', NULL, 50, 0)
00100 );
00101
00102 return $form;
00103 }
00104
00108 function comment_admin_overview_validate($form, &$form_state) {
00109 $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
00110
00111 if (count($form_state['values']['comments']) == 0) {
00112 form_set_error('', t('Please select one or more comments to perform the update on.'));
00113 drupal_goto('admin/content/comment');
00114 }
00115 }
00116
00123 function comment_admin_overview_submit($form, &$form_state) {
00124 $operations = comment_operations();
00125 if ($operations[$form_state['values']['operation']][1]) {
00126
00127 $query = $operations[$form_state['values']['operation']][1];
00128 foreach ($form_state['values']['comments'] as $cid => $value) {
00129 if ($value) {
00130
00131 db_query($query, $cid);
00132 $comment = comment_load($cid);
00133 _comment_update_node_statistics($comment->nid);
00134
00135 comment_invoke_comment($comment, $form_state['values']['operation']);
00136
00137 watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/' . $comment->nid, array('fragment' => 'comment-' . $comment->cid)));
00138 }
00139 }
00140 cache_clear_all();
00141 drupal_set_message(t('The update has been performed.'));
00142 $form_state['redirect'] = 'admin/content/comment';
00143 }
00144 }
00145
00153 function theme_comment_admin_overview($form) {
00154 $output = drupal_render($form['options']);
00155 if (isset($form['subject']) && is_array($form['subject'])) {
00156 foreach (element_children($form['subject']) as $key) {
00157 $row = array();
00158 $row[] = drupal_render($form['comments'][$key]);
00159 $row[] = drupal_render($form['subject'][$key]);
00160 $row[] = drupal_render($form['username'][$key]);
00161 $row[] = drupal_render($form['node_title'][$key]);
00162 $row[] = drupal_render($form['timestamp'][$key]);
00163 $row[] = drupal_render($form['operations'][$key]);
00164 $rows[] = $row;
00165 }
00166 }
00167 else {
00168 $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
00169 }
00170
00171 $output .= theme('table', $form['header']['#value'], $rows);
00172 if ($form['pager']['#value']) {
00173 $output .= drupal_render($form['pager']);
00174 }
00175
00176 $output .= drupal_render($form);
00177
00178 return $output;
00179 }
00180
00191 function comment_multiple_delete_confirm(&$form_state) {
00192 $edit = $form_state['post'];
00193
00194 $form['comments'] = array(
00195 '#prefix' => '<ul>',
00196 '#suffix' => '</ul>',
00197 '#tree' => TRUE,
00198 );
00199
00200 $comment_counter = 0;
00201 foreach (array_filter($edit['comments']) as $cid => $value) {
00202 $comment = comment_load($cid);
00203 if (is_object($comment) && is_numeric($comment->cid)) {
00204 $subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid));
00205 $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
00206 $comment_counter++;
00207 }
00208 }
00209 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
00210
00211 if (!$comment_counter) {
00212 drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
00213 drupal_goto('admin/content/comment');
00214 }
00215 else {
00216 return confirm_form($form,
00217 t('Are you sure you want to delete these comments and all their children?'),
00218 'admin/content/comment', t('This action cannot be undone.'),
00219 t('Delete comments'), t('Cancel'));
00220 }
00221 }
00222
00226 function comment_multiple_delete_confirm_submit($form, &$form_state) {
00227 if ($form_state['values']['confirm']) {
00228 foreach ($form_state['values']['comments'] as $cid => $value) {
00229 $comment = comment_load($cid);
00230
00231 _comment_delete_thread($comment);
00232 _comment_update_node_statistics($comment->nid);
00233 }
00234 cache_clear_all();
00235 drupal_set_message(t('The comments have been deleted.'));
00236 }
00237 $form_state['redirect'] = 'admin/content/comment';
00238 }
00239
00246 function comment_delete($cid = NULL) {
00247 $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));
00248 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
00249 $output = '';
00250
00251 if (is_object($comment) && is_numeric($comment->cid)) {
00252 $output = drupal_get_form('comment_confirm_delete', $comment);
00253 }
00254 else {
00255 drupal_set_message(t('The comment no longer exists.'));
00256 }
00257
00258 return $output;
00259 }
00260
00267 function comment_confirm_delete(&$form_state, $comment) {
00268 $form = array();
00269 $form['#comment'] = $comment;
00270 return confirm_form(
00271 $form,
00272 t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
00273 'node/' . $comment->nid,
00274 t('Any replies to this comment will be lost. This action cannot be undone.'),
00275 t('Delete'),
00276 t('Cancel'),
00277 'comment_confirm_delete');
00278 }
00279
00283 function comment_confirm_delete_submit($form, &$form_state) {
00284 drupal_set_message(t('The comment and all its replies have been deleted.'));
00285 $comment = $form['#comment'];
00286
00287 _comment_delete_thread($comment);
00288 _comment_update_node_statistics($comment->nid);
00289
00290 cache_clear_all();
00291
00292 $form_state['redirect'] = "node/$comment->nid";
00293 }
00294
00301 function _comment_delete_thread($comment) {
00302 if (!is_object($comment) || !is_numeric($comment->cid)) {
00303 watchdog('content', 'Cannot delete non-existent comment.', array(), WATCHDOG_WARNING);
00304
00305 return;
00306 }
00307
00308
00309 db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
00310 watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject));
00311 comment_invoke_comment($comment, 'delete');
00312
00313
00314 $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);
00315 while ($comment = db_fetch_object($result)) {
00316 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
00317 _comment_delete_thread($comment);
00318 }
00319 }