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