user = $node->user ? $node->user : user_load( array( "uid" => $node->uid ) ); $status_msg_php = variable_get('usercomment_msg_approval_queue_php', ''); if ($status_msg_php != '') { $status_msg = eval($status_msg_php); } else { $status_msg = variable_get('usercomment_msg_approval_queue', t("Your comment will be posted once it's been approved.")); $status_msg = t($status_msg, array( '@poster' => $user->name, '@node_owner' => $node->user->name, '@subject' => $comment['subject'], )); } drupal_set_message($status_msg); db_query("UPDATE {comments} SET status = %d WHERE cid = %d", COMMENT_NOT_PUBLISHED, $comment['cid']); if (! isset($node->user->usercomment_get_notifications) || $node->user->usercomment_get_notifications == 1) { $subject = variable_get('usercomment_mail_subject', t('Someone posted a new comment!')); $message = variable_get('usercomment_mail_message', usercomment_mail_message_default()); $replacements = array( '@approver' => $node->name, '@subject' => $comment['subject'], '@comment' => $comment['comment'], '@commenter' => $comment['name'], '@link' => url('node/' . $node->nid, NULL, NULL, TRUE), '@site' => variable_get("site_name", "Drupal"), '@siteurl' => $GLOBALS["base_url"], ); $subject = t($subject, $replacements); $message = t($message, $replacements); $site_mail = variable_get('site_mail', ""); if (!strlen($site_mail)) { if (user_access('administer nodes')){ drupal_set_message(t('You should create an administrator mail address for your site! Do it here.', array('%url' => url('admin/settings/site-information'))), 'error'); } $site_mail = 'nobody@localhost'; } drupal_mail('usercomment_mail', $node->user->mail, $subject, $message, $site_mail); } } break; } } /* * Implementation of hook_help() */ function usercomment_help($section) { switch ($section) { case 'admin/help#usercomment': return t('
This module lets users delete comments on own nodes they create without giving them full comment administration access. Permissions are on a per node type basis, so it\'s a great way to, e.g., allow users to administer comments on their own blogs. Additionally, you can configure this module to force comments on selected node types to be approved before they get published. As with delete rights, this is administered by users so you don\'t have to do it yourself.
'); break; case 'admin/modules#description': return t('This module gives users some additional comment administration rights on content they create.'); break; } } /* * Implementation of hook_link() */ function usercomment_link($type, $comment = NULL, $teaser = FALSE) { $links = array(); // we're only adding links to comments, so return if not comment if ($type != "comment") return $links; if (usercomment_access_check($comment->cid)) { $links['usercomment_delete'] = array( 'title' => t('delete'), 'href' => 'usercomment/delete/' . $comment->cid, ); } if (usercomment_access_check($comment->cid, 'approve', 'link')) { $node = node_load($comment->nid); if ($comment->status == COMMENT_NOT_PUBLISHED) { $links['usercomment_approve'] = array( 'title' => t('approve'), 'href' => 'usercomment/approve/' . $comment->cid, ); } } return $links; } /* * Implementation of hook_menu() */ function usercomment_menu($may_cache) { global $user; $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/content/comment/usercomment', 'title' => t('Approval email'), 'callback' => 'drupal_get_form', 'callback arguments' => 'usercomment_admin_settings', 'type' => MENU_LOCAL_TASK, 'access' => 1, 'weight' => 20, ); } else { if (arg(0) == "usercomment" && is_numeric(arg(2))) { $op = arg(1); $cid = arg(2); $comment = _comment_load($cid); $node = node_load($comment->nid); switch ($op) { case 'approve': $callback = 'usercomment_approve'; $callback_args = array( $comment, $node, ); $access = ($user->uid == $node->uid || user_access('administer comments')); break; case 'delete': $callback = 'comment_delete'; $callback_args = array(); $access = usercomment_access_check($cid); break; } $details = array ( 'path' => 'usercomment/' . $op, 'callback' => $callback, 'type' => MENU_CALLBACK, 'access' => 1, ); if (! empty($callback_args)) { $details['callback arguments'] = $callback_args; } $items[] = $details; } } return $items; } /* * Implementation of hook_nodeapi() */ function usercomment_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { case 'view': return usercomment_approval_form($node); break; } } /* * Implementation of hook_perm() */ function usercomment_perm() { $perms = array(); $perms[] = 'post comments with no usercomment checking'; foreach (node_get_types() as $node) { $perms[] = 'delete comments on own ' . check_plain($node->type) . ' content'; $perms[] = 'approve comments on own ' . check_plain($node->type) . ' content'; } return $perms; } /* * Implementation of hook_user() */ function usercomment_user($op, &$edit, &$account, $category = NULL) { switch ($op) { case 'form': if ($category == "account") { $form['usercomment_settings'] = array( '#type' => 'fieldset', '#title' => t('Usercomment settings'), '#weight' => 5, '#collapsible' => 1, ); foreach (node_get_types() as $node) { /*patch from: http://drupal.org/files/issues/usercomment-useraccess.patch */ /*if (user_access('approve comments on own ' . $node->type . ' content', $account )) {*/ if (user_access('approve comments on own ' . $node->type . ' content', $account )) { /*end of patch*/ $form['usercomment_settings']['usercomment_approve_' . $node->type] = array( '#type' => 'checkbox', '#title' => t('Skip ' . $node->type . ' approvals'), '#default_value' => isset($edit['usercomment_approve_' . $node->type]) ? $edit['usercomment_approve_' . $node->type] : 1, '#description' => t('If you check this, other non admin users will be able to post comments on your content without prior approval. Admin users, however, will be able to post comments without approval.'), ); } } $form['usercomment_settings']['usercomment_get_notifications'] = array( '#type' => 'checkbox', '#title' => t('Receive notification emails'), '#default_value' => isset($edit['usercomment_get_notifications']) ? $edit['usercomment_get_notifications'] : 1, '#description' => t('Check here if you want to receive notification emails about new comments awaiting your approval.'), ); } return $form; break; } } /* * This function returns the count of unapproved notes for a specified node * * @param $nid - numeric node id */ function usercomment_count_unapproved($nid) { $query = "SELECT count(cid) FROM {comments} WHERE status=%d AND nid=%d"; $args = array(COMMENT_NOT_PUBLISHED, $nid); $count = db_result(db_query($query, $args)); return (int)$count; } /* * This function determines whether the specified user needs to approve comments on the specified node type * * @param $nodetype - text string specifying the name of a node type * @param $u - the user object if you have it, else a numeric uid - defaults to current user * * @return TRUE if the user must approve comments on the specified nodetype, FALSE if not */ function usercomment_requires_approval($nodetype, $u = -1) { // get the right user object if (is_object($u)) { $user_object =& $u; } elseif (is_numeric($u)) { global $user; if ($u == -1 || $u == $user->uid) { $user_object =& $user; } else { $user_object = user_load(array('uid' => $u)); } } if (is_object($user_object)) { // check the user's settings $skip_approval_field = "usercomment_approve_{$nodetype}"; if (! isset($user_object->$skip_approval_field) || $user_object->$skip_approval_field == 1) { return FALSE; // user either hasn't specified approvals or has said to skip approvals for this node type } else { return TRUE; } } else { // didn't get a valid user object return FALSE; } } /* * This function checks access perms */ function usercomment_access_check($cid, $op = 'delete', $type = '') { global $user; // admin users already have functionality so no need to show duplicate link if ($op == "delete" && user_access('administer comments')) { return FALSE; } elseif (user_access('post comments with no usercomment checking')) { return FALSE; } else { // get node info and see if node is owned by user or not $comment = _comment_load($cid); $thisnode = node_load($comment->nid); switch ($op) { case 'approve': switch ($type) { case 'update': // if it's not node owner, check if node owner lets others // post without approval if ($thisnode->uid != $user->uid) { static $access; if (! isset($access)) { /*added from patch: http://drupal.org/files/issues/usercomment-skip-check.patch*/ $thisnode->user = $thisnode->user ? $thisnode->user : user_load( array( "uid" => $thisnode->uid ) ); $reference = 'usercomment_approve_' . $thisnode->type; if (! isset($thisnode->user->$reference) || $thisnode->user->$reference == 1) { $access = FALSE; /*end of patch*/ if (! is_object($thisnode->user) ) { $thisnode->user = user_load( array( "uid" => $thisnode->uid ) ); } $access = usercomment_requires_approval($thisnode->type, $thisnode->user); }/*closing quote part of patch*/ } return $access; } $check = (user_access('approve comments on own ' . $thisnode->type . ' content') && $thisnode->uid != $user->uid); break; case 'link': default: $check = user_access('approve comments on own ' . $thisnode->type . ' content'); break; } break; case 'delete': default: $check = (user_access('delete comments on own ' . $thisnode->type . ' content') && $thisnode->uid == $user->uid); break; } if ($check) { return TRUE; } else { return FALSE; } } } /* * This function generates the approval page */ function usercomment_approve($comment, $node) { $comment->status = COMMENT_PUBLISHED; comment_save((array)$comment); $status_msg_php = variable_get('usercomment_msg_approved_php', ''); if ($status_msg_php != '') { $status_msg = eval($status_msg_php); } else { $status_msg = variable_get('usercomment_msg_approved', t("Your comment has been approved.")); $status_msg = t($status_msg, array( '@poster' => $user->name, '@node_owner' => $node->user->name, '@subject' => $comment->subject, )); } drupal_set_message($status_msg); drupal_goto ('node/' . $node->nid); } /* * This function is a modified copy of comment_render() * * I had to modify b/c comment_render() didn't let me get comments that * weren't published * not ideal, but it works * */ function usercomment_comment_render($node) { $content = ''; $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d AND c.status = %d'; $query_args[] = $node->nid; $query_args[] = COMMENT_NOT_PUBLISHED; $result = db_query($query, $query_args); while ($comment = db_fetch_object($result)) { $comment = drupal_unpack($comment); if (usercomment_access_check($comment->cid)) { $links = usercomment_link('comment', $comment, FALSE); } else { $links = module_invoke_all('link', 'comment', $comment, 0); } foreach (module_implements('link_alter') as $module) { $function = $module .'_link_alter'; $function($node, $links); } $content .= theme('comment_view', $comment, $links); } return $content; } /* * Generate the usercomment admin settings form */ function usercomment_admin_settings() { $form['mail'] = array( '#type' => 'fieldset', '#title' => t('Approval email'), '#description' => t('This page is generated by the usercomment module. The following variables are available for use in email subject/message: @approver, @commenter, @title, @comment, @link, @site, @siteurl'), ); $form['mail']['usercomment_mail_subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => variable_get('usercomment_mail_subject', t('@commenter posted a new comment!')), ); $form['mail']['usercomment_mail_message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#rows' => 10, '#default_value' => variable_get('usercomment_mail_message', usercomment_mail_message_default()), ); $form['usercomment_text'] = array( '#type' => 'fieldset', '#title' => t('Text options'), ); $form['usercomment_text']['usercomment_msg_approval_queue'] = array( '#type' => 'textfield', '#size' => 80, '#title' => t("Approval queue message"), '#description' => t("Enter the status message a user will see after submitting a comment that requires approval prior to being published. The following variables are available for use in the status message: @poster, @node_owner, @subject."), '#default_value' => variable_get('usercomment_msg_approval_queue', t("Your comment will be posted once it's been approved.")), ); $msg_php = variable_get('usercomment_msg_approval_queue_php', NULL); $form['usercomment_text']['usercomment_msg_approval_queue_php_fieldset'] = array( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => is_null($msg_php) ? FALSE : TRUE, '#title' => t('PHP code'), ); $form['usercomment_text']['usercomment_msg_approval_queue_php_fieldset']['usercomment_msg_approval_queue_php'] = array( '#type' => 'textarea', '#title' => t('Approval queue message code'), '#default_value' => $msg_php, '#cols' => 60, '#rows' => 6, '#description' => ''. t('Advanced Usage Only: PHP code that returns the text of the status message. Should not include <?php ?> delimiters.') .'
' . '' . t('Note: if set, this will override any Approval queue message text set above.') . '
', ); $form['usercomment_text']['usercomment_msg_approved'] = array( '#type' => 'textfield', '#size' => 80, '#title' => t("Approved comment message"), '#description' => t("Enter the status message a user will see after approving a comment. The following variables are available for use in the status message: @poster, @node_owner, @subject."), '#default_value' => variable_get('usercomment_msg_approved', t("Your comment has been approved.")), ); $msg_php = variable_get('usercomment_msg_approved_php', NULL); $form['usercomment_text']['usercomment_msg_approved_php_fieldset'] = array( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => is_null($msg_php) ? FALSE : TRUE, '#title' => t('PHP code'), ); $form['usercomment_text']['usercomment_msg_approved_php_fieldset']['usercomment_msg_approved_php'] = array( '#type' => 'textarea', '#title' => t('Approved comment message code'), '#default_value' => $msg_php, '#cols' => 60, '#rows' => 6, '#description' => ''. t('Advanced Usage Only: PHP code that returns the text of the status message. Should not include <?php ?> delimiters.') .'
' . '' . t('Note: if set, this will override any Approval queue message text set above.') . '
', ); return system_settings_form($form); } /* * This function defines the default message sent out by this module */ function usercomment_mail_message_default() { return <<