2007-10-31 17:50:47 +00:00
< ? php
// $Id$
/**
* @ file
* User page callbacks for the comment module .
*/
2007-12-16 21:01:45 +00:00
/**
* Form builder ; generate a comment editing form .
*
* @ param $cid
* ID of the comment to be edited .
* @ ingroup forms
*/
2007-10-31 17:50:47 +00:00
function comment_edit ( $cid ) {
global $user ;
2009-02-26 07:30:29 +00:00
$comment = db_query ( 'SELECT c.*, u.uid, u.name AS registered_name, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid' , array ( ':cid' => $cid ) ) -> fetchObject ();
2007-10-31 17:50:47 +00:00
$comment = drupal_unpack ( $comment );
$comment -> name = $comment -> uid ? $comment -> registered_name : $comment -> name ;
2008-05-14 13:12:41 +00:00
2007-10-31 17:50:47 +00:00
if ( comment_access ( 'edit' , $comment )) {
2009-04-20 21:28:15 +00:00
return theme ( 'comment_form_box' , ( array ) $comment );
2007-10-31 17:50:47 +00:00
}
else {
drupal_access_denied ();
}
}
/**
* This function is responsible for generating a comment reply form .
* There are several cases that have to be handled , including :
* - replies to comments
* - replies to nodes
* - attempts to reply to nodes that can no longer accept comments
* - respecting access permissions ( 'access comments' , 'post comments' , etc . )
*
* The node or comment that is being replied to must appear above the comment
* form to provide the user context while authoring the comment .
*
* @ param $node
* Every comment belongs to a node . This is that node .
2007-12-16 21:01:45 +00:00
*
2007-10-31 17:50:47 +00:00
* @ param $pid
* Some comments are replies to other comments . In those cases , $pid is the parent
* comment ' s cid .
*
2007-12-16 21:01:45 +00:00
* @ return
2007-10-31 17:50:47 +00:00
* The rendered parent node or comment plus the new comment form .
*/
function comment_reply ( $node , $pid = NULL ) {
// Set the breadcrumb trail.
2008-04-14 17:48:46 +00:00
drupal_set_breadcrumb ( array ( l ( t ( 'Home' ), NULL ), l ( $node -> title , 'node/' . $node -> nid )));
2007-10-31 17:50:47 +00:00
$op = isset ( $_POST [ 'op' ]) ? $_POST [ 'op' ] : '' ;
$output = '' ;
if ( user_access ( 'access comments' )) {
// The user is previewing a comment prior to submitting it.
2008-06-24 17:01:33 +00:00
if ( $op == t ( 'Preview' )) {
2007-10-31 17:50:47 +00:00
if ( user_access ( 'post comments' )) {
2009-04-20 21:28:15 +00:00
$output .= theme ( 'comment_form_box' , array ( 'pid' => $pid , 'nid' => $node -> nid ), NULL );
2007-10-31 17:50:47 +00:00
}
else {
drupal_set_message ( t ( 'You are not authorized to post comments.' ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
}
else {
// $pid indicates that this is a reply to a comment.
if ( $pid ) {
2008-05-14 13:12:41 +00:00
// Load the comment whose cid = $pid
2009-02-26 07:30:29 +00:00
$comment = db_query ( 'SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid AND c.status = :status' , array (
2008-11-16 19:41:14 +00:00
':cid' => $pid ,
2008-11-11 21:44:01 +00:00
':status' => COMMENT_PUBLISHED )) -> fetchObject ();
if ( $comment ) {
2008-05-14 13:12:41 +00:00
// If that comment exists, make sure that the current comment and the
// parent comment both belong to the same parent node.
2007-10-31 17:50:47 +00:00
if ( $comment -> nid != $node -> nid ) {
// Attempting to reply to a comment not belonging to the current nid.
drupal_set_message ( t ( 'The comment you are replying to does not exist.' ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
// Display the parent comment
$comment = drupal_unpack ( $comment );
$comment -> name = $comment -> uid ? $comment -> registered_name : $comment -> name ;
$output .= theme ( 'comment_view' , $comment , $node );
}
else {
drupal_set_message ( t ( 'The comment you are replying to does not exist.' ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
}
// This is the case where the comment is in response to a node. Display the node.
2008-10-12 04:30:09 +00:00
elseif ( user_access ( 'access content' )) {
2009-01-27 00:22:27 +00:00
$output .= drupal_render ( node_build ( $node ));
2007-10-31 17:50:47 +00:00
}
// Should we show the reply box?
2009-03-17 12:41:54 +00:00
if ( $node -> comment != COMMENT_NODE_OPEN ) {
2007-10-31 17:50:47 +00:00
drupal_set_message ( t ( " This discussion is closed: you can't post new comments. " ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
2008-10-12 04:30:09 +00:00
elseif ( user_access ( 'post comments' )) {
2009-04-20 21:28:15 +00:00
$output .= theme ( 'comment_form_box' , array ( 'pid' => $pid , 'nid' => $node -> nid ), t ( 'Reply' ));
2007-10-31 17:50:47 +00:00
}
else {
drupal_set_message ( t ( 'You are not authorized to post comments.' ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
}
}
else {
drupal_set_message ( t ( 'You are not authorized to view comments.' ), 'error' );
drupal_goto ( " node/ $node->nid " );
}
return $output ;
}
2008-06-03 16:30:20 +00:00
/**
* Publish specified comment .
*
* @ param $cid ID of comment to be published .
*/
function comment_approve ( $cid ) {
// Load the comment whose cid = $cid
2009-01-31 16:19:29 +00:00
if ( $comment = ( array ) comment_load ( $cid )) {
$comment [ 'status' ] = COMMENT_PUBLISHED ;
$comment [ 'comment_format' ] = $comment [ 'format' ];
comment_save ( $comment );
2008-06-03 16:30:20 +00:00
drupal_set_message ( t ( 'Comment approved.' ));
2009-01-31 16:19:29 +00:00
drupal_goto ( 'node/' . $comment [ 'nid' ]);
2008-06-03 16:30:20 +00:00
}
else {
drupal_set_message ( t ( 'The comment you are approving does not exist.' ), 'error' );
drupal_goto ();
}
}