2007-10-31 17:50:47 +00:00
< ? php
// $Id$
/**
* @ file
* Admin page callbacks for the comment module .
*/
/**
* Menu callback ; present an administrative comment listing .
*/
function comment_admin ( $type = 'new' ) {
$edit = $_POST ;
if ( isset ( $edit [ 'operation' ]) && ( $edit [ 'operation' ] == 'delete' ) && isset ( $edit [ 'comments' ]) && $edit [ 'comments' ]) {
2009-05-25 10:43:54 +00:00
return drupal_get_form ( 'comment_multiple_delete_confirm' );
2007-10-31 17:50:47 +00:00
}
else {
2009-05-25 10:43:54 +00:00
return drupal_get_form ( 'comment_admin_overview' , $type , arg ( 4 ));
2007-10-31 17:50:47 +00:00
}
}
/**
* Form builder ; Builds the comment overview form for the admin .
*
2007-12-16 21:01:45 +00:00
* @ param $type
* Not used .
* @ param $arg
2008-05-14 13:12:41 +00:00
* Current path ' s fourth component deciding the form type ( Published comments / Approval queue ) .
2007-12-16 21:01:45 +00:00
* @ return
* The form structure .
2007-10-31 17:50:47 +00:00
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see comment_admin_overview_validate ()
* @ see comment_admin_overview_submit ()
* @ see theme_comment_admin_overview ()
2007-10-31 17:50:47 +00:00
*/
function comment_admin_overview ( $type = 'new' , $arg ) {
2008-05-14 13:12:41 +00:00
// Build an 'Update options' form.
2007-10-31 17:50:47 +00:00
$form [ 'options' ] = array (
2008-05-14 13:12:41 +00:00
'#type' => 'fieldset' ,
'#title' => t ( 'Update options' ),
'#prefix' => '<div class="container-inline">' ,
'#suffix' => '</div>' ,
2007-10-31 17:50:47 +00:00
);
$options = array ();
foreach ( comment_operations ( $arg == 'approval' ? 'publish' : 'unpublish' ) as $key => $value ) {
$options [ $key ] = $value [ 0 ];
}
2008-05-14 13:12:41 +00:00
$form [ 'options' ][ 'operation' ] = array (
'#type' => 'select' ,
'#options' => $options ,
'#default_value' => 'publish' ,
);
$form [ 'options' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Update' ),
);
2007-10-31 17:50:47 +00:00
2008-05-14 13:12:41 +00:00
// Load the comments that need to be displayed.
2007-10-31 17:50:47 +00:00
$status = ( $arg == 'approval' ) ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED ;
2009-01-28 07:43:26 +00:00
$header = array (
'subject' => array ( 'data' => t ( 'Subject' ), 'field' => 'subject' ),
'author' => array ( 'data' => t ( 'Author' ), 'field' => 'name' ),
'posted_in' => array ( 'data' => t ( 'Posted in' ), 'field' => 'node_title' ),
'time' => array ( 'data' => t ( 'Time' ), 'field' => 'timestamp' , 'sort' => 'desc' ),
'operations' => array ( 'data' => t ( 'Operations' )),
);
2009-06-06 10:27:42 +00:00
$query = db_select ( 'comment' , 'c' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
2009-02-26 07:30:29 +00:00
$query -> join ( 'users' , 'u' , 'u.uid = c.uid' );
2009-02-22 16:53:41 +00:00
$query -> join ( 'node' , 'n' , 'n.nid = c.nid' );
$query -> addField ( 'u' , 'name' , 'registered_name' );
$query -> addField ( 'n' , 'title' , 'node_title' );
2009-06-06 10:27:42 +00:00
$result = $query
2009-02-22 16:53:41 +00:00
-> fields ( 'c' , array ( 'subject' , 'nid' , 'cid' , 'comment' , 'timestamp' , 'status' , 'name' , 'homepage' ))
-> fields ( 'u' , array ( 'uid' ))
-> condition ( 'c.status' , $status )
-> limit ( 50 )
2009-06-06 10:27:42 +00:00
-> orderByHeader ( $header )
-> execute ();
2009-02-22 16:53:41 +00:00
2007-10-31 17:50:47 +00:00
2008-05-14 13:12:41 +00:00
// Build a table listing the appropriate comments.
2009-01-28 07:43:26 +00:00
$options = array ();
2007-10-31 17:50:47 +00:00
$destination = drupal_get_destination ();
2009-01-28 07:43:26 +00:00
2009-02-22 16:53:41 +00:00
foreach ( $result as $comment ) {
2009-01-28 07:43:26 +00:00
$options [ $comment -> cid ] = array (
2009-06-19 13:03:53 +00:00
'subject' => l ( $comment -> subject , 'comment/' . $comment -> cid , array ( 'attributes' => array ( 'title' => truncate_utf8 ( $comment -> comment , 128 )), 'fragment' => 'comment-' . $comment -> cid )),
2009-01-28 07:43:26 +00:00
'author' => theme ( 'username' , $comment ),
'posted_in' => l ( $comment -> node_title , 'node/' . $comment -> nid ),
'time' => format_date ( $comment -> timestamp , 'small' ),
'operations' => l ( t ( 'edit' ), 'comment/edit/' . $comment -> cid , array ( 'query' => $destination )),
2008-05-14 13:12:41 +00:00
);
2007-10-31 17:50:47 +00:00
}
2009-01-28 07:43:26 +00:00
2008-05-14 13:12:41 +00:00
$form [ 'comments' ] = array (
2009-01-28 07:43:26 +00:00
'#type' => 'tableselect' ,
'#header' => $header ,
'#options' => $options ,
'#empty' => t ( 'No comments available.' ),
2008-05-14 13:12:41 +00:00
);
2009-01-28 07:43:26 +00:00
2009-07-29 06:39:35 +00:00
$form [ 'pager' ] = array ( '#theme' => 'pager' );
2008-05-14 13:12:41 +00:00
2007-10-31 17:50:47 +00:00
return $form ;
}
/**
2007-12-16 21:01:45 +00:00
* Validate comment_admin_overview form submissions .
2007-10-31 17:50:47 +00:00
*/
function comment_admin_overview_validate ( $form , & $form_state ) {
$form_state [ 'values' ][ 'comments' ] = array_diff ( $form_state [ 'values' ][ 'comments' ], array ( 0 ));
2008-05-14 13:12:41 +00:00
// We can't execute any 'Update options' if no comments were selected.
2007-10-31 17:50:47 +00:00
if ( count ( $form_state [ 'values' ][ 'comments' ]) == 0 ) {
form_set_error ( '' , t ( 'Please select one or more comments to perform the update on.' ));
2009-07-30 19:24:21 +00:00
drupal_goto ( 'admin/content/comment' );
2007-10-31 17:50:47 +00:00
}
}
/**
2007-12-16 21:01:45 +00:00
* Process comment_admin_overview form submissions .
*
2007-10-31 17:50:47 +00:00
* Execute the chosen 'Update option' on the selected comments , such as
* publishing , unpublishing or deleting .
*/
function comment_admin_overview_submit ( $form , & $form_state ) {
$operations = comment_operations ();
if ( $operations [ $form_state [ 'values' ][ 'operation' ]][ 1 ]) {
2008-05-14 13:12:41 +00:00
// Extract the appropriate database query operation.
2007-10-31 17:50:47 +00:00
$query = $operations [ $form_state [ 'values' ][ 'operation' ]][ 1 ];
foreach ( $form_state [ 'values' ][ 'comments' ] as $cid => $value ) {
if ( $value ) {
2008-05-14 13:12:41 +00:00
// Perform the update action, then refresh node statistics.
2008-11-11 21:44:01 +00:00
$query
-> condition ( 'cid' , $cid )
-> execute ();
2008-04-10 10:13:57 +00:00
$comment = comment_load ( $cid );
2007-10-31 17:50:47 +00:00
_comment_update_node_statistics ( $comment -> nid );
// Allow modules to respond to the updating of a comment.
2009-07-01 20:39:20 +00:00
module_invoke_all ( 'comment_' . $form_state [ 'values' ][ 'operation' ], $comment );
2007-10-31 17:50:47 +00:00
// Add an entry to the watchdog log.
2008-04-14 17:48:46 +00:00
watchdog ( 'content' , 'Comment: updated %subject.' , array ( '%subject' => $comment -> subject ), WATCHDOG_NOTICE , l ( t ( 'view' ), 'node/' . $comment -> nid , array ( 'fragment' => 'comment-' . $comment -> cid )));
2007-10-31 17:50:47 +00:00
}
}
cache_clear_all ();
drupal_set_message ( t ( 'The update has been performed.' ));
2009-07-30 19:24:21 +00:00
$form_state [ 'redirect' ] = 'admin/content/comment' ;
2007-10-31 17:50:47 +00:00
}
}
/**
2008-05-14 13:12:41 +00:00
* List the selected comments and verify that the admin wants to delete them .
2007-10-31 17:50:47 +00:00
*
2007-12-16 21:01:45 +00:00
* @ param $form_state
* An associative array containing the current state of the form .
* @ return
* TRUE if the comments should be deleted , FALSE otherwise .
2007-10-31 17:50:47 +00:00
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see comment_multiple_delete_confirm_submit ()
2007-10-31 17:50:47 +00:00
*/
function comment_multiple_delete_confirm ( & $form_state ) {
2009-03-14 20:13:27 +00:00
$edit = $form_state [ 'input' ];
2007-10-31 17:50:47 +00:00
2008-05-14 13:12:41 +00:00
$form [ 'comments' ] = array (
'#prefix' => '<ul>' ,
'#suffix' => '</ul>' ,
'#tree' => TRUE ,
);
// array_filter() returns only elements with actual values.
2007-10-31 17:50:47 +00:00
$comment_counter = 0 ;
foreach ( array_filter ( $edit [ 'comments' ]) as $cid => $value ) {
2008-04-10 10:13:57 +00:00
$comment = comment_load ( $cid );
2007-10-31 17:50:47 +00:00
if ( is_object ( $comment ) && is_numeric ( $comment -> cid )) {
2009-07-28 19:18:08 +00:00
$subject = db_query ( 'SELECT subject FROM {comment} WHERE cid = :cid' , array ( ':cid' => $cid )) -> fetchField ();
2008-04-14 17:48:46 +00:00
$form [ 'comments' ][ $cid ] = array ( '#type' => 'hidden' , '#value' => $cid , '#prefix' => '<li>' , '#suffix' => check_plain ( $subject ) . '</li>' );
2007-10-31 17:50:47 +00:00
$comment_counter ++ ;
}
}
$form [ 'operation' ] = array ( '#type' => 'hidden' , '#value' => 'delete' );
if ( ! $comment_counter ) {
2008-05-14 13:12:41 +00:00
drupal_set_message ( t ( 'There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.' ));
2009-07-30 19:24:21 +00:00
drupal_goto ( 'admin/content/comment' );
2007-10-31 17:50:47 +00:00
}
else {
return confirm_form ( $form ,
t ( 'Are you sure you want to delete these comments and all their children?' ),
2009-07-30 19:24:21 +00:00
'admin/content/comment' , t ( 'This action cannot be undone.' ),
2007-10-31 17:50:47 +00:00
t ( 'Delete comments' ), t ( 'Cancel' ));
}
}
/**
2007-12-16 21:01:45 +00:00
* Process comment_multiple_delete_confirm form submissions .
2007-10-31 17:50:47 +00:00
*/
function comment_multiple_delete_confirm_submit ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'confirm' ]) {
foreach ( $form_state [ 'values' ][ 'comments' ] as $cid => $value ) {
2008-04-10 10:13:57 +00:00
$comment = comment_load ( $cid );
2008-05-14 13:12:41 +00:00
// Perform the actual comment deletion.
2007-10-31 17:50:47 +00:00
_comment_delete_thread ( $comment );
_comment_update_node_statistics ( $comment -> nid );
}
cache_clear_all ();
drupal_set_message ( t ( 'The comments have been deleted.' ));
}
2009-07-30 19:24:21 +00:00
$form_state [ 'redirect' ] = 'admin/content/comment' ;
2007-10-31 17:50:47 +00:00
}
/**
* Menu callback ; delete a comment .
2007-12-16 21:01:45 +00:00
*
* @ param $cid
2008-05-14 13:12:41 +00:00
* The comment to be deleted .
2007-10-31 17:50:47 +00:00
*/
function comment_delete ( $cid = NULL ) {
2009-05-21 10:34:55 +00:00
$comment = db_query ( 'SELECT c.*, u.name AS registered_name, u.uid FROM {comment} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = :cid' , array ( ':cid' => $cid )) -> fetch ();
2007-10-31 17:50:47 +00:00
$comment -> name = $comment -> uid ? $comment -> registered_name : $comment -> name ;
$output = '' ;
if ( is_object ( $comment ) && is_numeric ( $comment -> cid )) {
$output = drupal_get_form ( 'comment_confirm_delete' , $comment );
}
else {
drupal_set_message ( t ( 'The comment no longer exists.' ));
}
return $output ;
}
/**
* Form builder ; Builds the confirmation form for deleting a single comment .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see comment_confirm_delete_submit ()
2007-10-31 17:50:47 +00:00
*/
function comment_confirm_delete ( & $form_state , $comment ) {
$form = array ();
$form [ '#comment' ] = $comment ;
return confirm_form (
$form ,
t ( 'Are you sure you want to delete the comment %title?' , array ( '%title' => $comment -> subject )),
2008-04-14 17:48:46 +00:00
'node/' . $comment -> nid ,
2007-10-31 17:50:47 +00:00
t ( 'Any replies to this comment will be lost. This action cannot be undone.' ),
t ( 'Delete' ),
t ( 'Cancel' ),
'comment_confirm_delete' );
}
2007-12-16 21:01:45 +00:00
/**
* Process comment_confirm_delete form submissions .
*/
2007-10-31 17:50:47 +00:00
function comment_confirm_delete_submit ( $form , & $form_state ) {
drupal_set_message ( t ( 'The comment and all its replies have been deleted.' ));
$comment = $form [ '#comment' ];
2008-05-14 13:12:41 +00:00
// Delete the comment and its replies.
2007-10-31 17:50:47 +00:00
_comment_delete_thread ( $comment );
_comment_update_node_statistics ( $comment -> nid );
// Clear the cache so an anonymous user sees that his comment was deleted.
cache_clear_all ();
$form_state [ 'redirect' ] = " node/ $comment->nid " ;
}
2007-12-16 21:01:45 +00:00
/**
* Perform the actual deletion of a comment and all its replies .
*
* @ param $comment
* An associative array describing the comment to be deleted .
*/
2007-10-31 17:50:47 +00:00
function _comment_delete_thread ( $comment ) {
if ( ! is_object ( $comment ) || ! is_numeric ( $comment -> cid )) {
2008-04-23 18:05:58 +00:00
watchdog ( 'content' , 'Cannot delete non-existent comment.' , array (), WATCHDOG_WARNING );
2008-05-14 13:12:41 +00:00
2007-10-31 17:50:47 +00:00
return ;
}
2008-05-14 13:12:41 +00:00
// Delete the comment.
2009-05-21 10:34:55 +00:00
db_delete ( 'comment' )
-> condition ( 'cid' , $comment -> cid )
-> execute ();
2007-10-31 17:50:47 +00:00
watchdog ( 'content' , 'Comment: deleted %subject.' , array ( '%subject' => $comment -> subject ));
2009-07-01 20:39:20 +00:00
module_invoke_all ( 'comment_delete' , $comment );
2007-10-31 17:50:47 +00:00
2008-05-14 13:12:41 +00:00
// Delete the comment's replies.
2009-05-21 10:34:55 +00:00
$result = db_query ( 'SELECT c.*, u.name AS registered_name, u.uid FROM {comment} c INNER JOIN {users} u ON u.uid = c.uid WHERE pid = :cid' , array ( ':cid' => $comment -> cid ));
foreach ( $result as $comment ) {
2007-10-31 17:50:47 +00:00
$comment -> name = $comment -> uid ? $comment -> registered_name : $comment -> name ;
_comment_delete_thread ( $comment );
}
}