View source
<?php
function comment_og_menu() {
$items['admin/og/comment_og'] = array(
'title' => 'Organic groups comments configuration',
'description' => 'Choose how group administrators can edit comments',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'comment_og_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
$items['comment_og/edit'] = array(
'title' => 'Edit comment',
'page callback' => 'comment_og_comment_edit',
'page arguments' => array(
2,
3,
),
'access arguments' => array(
'post comments',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function comment_og_settings() {
$form['comment_og_admin_delete'] = array(
'#type' => 'checkbox',
'#title' => t('Group administrators can delete comments posted into their groups'),
'#default_value' => variable_get('comment_og_admin_delete', 1),
);
$form['comment_og_admin_edit'] = array(
'#type' => 'checkbox',
'#title' => t('Group administrators can edit comments posted into their groups'),
'#default_value' => variable_get('comment_og_admin_edit', 1),
);
$form['comment_og_admin_edit_msg'] = array(
'#type' => 'textfield',
'#title' => t('Message to append to comments edited by group administrators'),
'#default_value' => variable_get('comment_og_admin_edit_msg', comment_og_admin_edit_msg()),
'#description' => t('Use the place holder %user to insert the user name of the administrator performing the editing. Example: ', array(
'%user' => '%user',
)) . comment_og_admin_edit_msg(),
);
$form['comment_og_admin_nonmember_access'] = array(
'#type' => 'checkbox',
'#title' => t('Group administrators can enable/disable allowing non-members to post comments into their groups'),
'#default_value' => variable_get('comment_og_admin_nonmember_access', FALSE),
'#description' => t('Allowance of non-members to post into groups may be configured per group. The default setting for each group is to disabled non-member comment posts. Enabling or disabling this option does not reset any changes to this setting made by group administrators in a group.'),
);
return system_settings_form($form);
}
function comment_og_theme() {
return array(
'comment_og_edit_msg' => array(
'arguments' => array(
'message' => NULL,
),
),
);
}
function comment_og_link_alter(&$links, $node, $comment = NULL) {
$group = og_get_group_context();
if ($group) {
if ($node->comment && isset($node->og_groups)) {
if (!comment_og_access($group)) {
unset($links['comment_add']);
}
if ($comment) {
if (!comment_og_access($group)) {
unset($links['comment_reply']);
}
}
}
}
}
function comment_og_link($type, $object, $teaser = FALSE) {
$group = og_get_group_context();
if ($type == 'comment' && node_comment_mode($object->nid) == COMMENT_NODE_READ_WRITE) {
if (og_is_group_admin($group) && !comment_access('edit', $object)) {
$links['comment_edit'] = array(
'title' => t('edit'),
'href' => "comment_og/edit/{$object->cid}/{$group->nid}",
);
}
if (og_is_group_admin($group) && !user_access('administer comments') && user_access('post comments')) {
$links['comment_delete'] = array(
'title' => t('delete'),
'href' => "comment/delete/{$object->cid}/{$group->nid}",
);
}
}
return $links;
}
function comment_og_form_alter(&$form, $form_state, $form_id) {
$group = og_get_group_context();
if ($group) {
if ($form_id == 'comment_form' && isset($form['nid'])) {
$og_node = og_get_group_context();
if (!comment_og_access($og_node)) {
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 5,
'#disabled' => TRUE,
'#description' => t('You must be a member of this group in order to post a comment.'),
);
if (isset($form['subject'])) {
unset($form['subject']);
}
if (isset($form['comment_filter']['format'])) {
unset($form['comment_filter']['format']);
}
if (isset($form['submit'])) {
unset($form['submit']);
}
if (isset($form['preview'])) {
unset($form['preview']);
}
if (isset($form['author'])) {
unset($form['author']);
}
if (isset($form['_author'])) {
unset($form['_author']);
}
}
}
if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form' && og_is_group_type($form['#node']->type)) {
$node = $form['#node'];
if (variable_get('comment_og_admin_nonmember_access', FALSE)) {
$form['comment_og_nonmember_access'] = array(
'#title' => t('Allow non-member comments'),
'#description' => t('Allow non-members to post comments in this group.'),
'#type' => 'checkbox',
'#default_value' => _comment_og_allow_nonmember_access($node->nid),
'#access' => og_is_group_admin($node),
'#weight' => 1 + module_exists('content') ? content_extra_field_weight($node->type, 'og_selective') : 0,
);
}
}
}
}
function _comment_og_allow_nonmember_access($nid) {
return (bool) db_result(db_query('SELECT grant_nonmember_access FROM {comment_og_node} con WHERE con.nid = %d', $nid));
}
function comment_og_access($og_node) {
return og_is_group_member($og_node) || _comment_og_allow_nonmember_access($og_node->nid);
}
function comment_og_nodeapi(&$node, $op) {
switch ($op) {
case 'delete':
db_query('DELETE FROM {comment_og_node} con WHERE con.nid = %d', $node->nid);
break;
case 'update':
$primary_keys = array();
if (db_result(db_query('SELECT nid FROM {comment_og_node} con WHERE con.nid = %d', $node->nid))) {
$primary_keys[] = 'nid';
}
$record = array(
'nid' => $node->nid,
'grant_nonmember_access' => $node->comment_og_nonmember_access,
);
drupal_write_record('comment_og_node', $record, $primary_keys);
break;
}
}
function comment_og_menu_alter(&$items) {
$items['comment/delete'] = array(
'title' => 'Delete comment',
'page callback' => 'comment_delete',
'access callback' => 'comment_og_comment_delete',
'access arguments' => array(
3,
),
'type' => MENU_CALLBACK,
'file' => 'comment.admin.inc',
'file path' => drupal_get_path('module', 'comment'),
);
}
function comment_og_comment_delete($gid = NULL) {
$coad = variable_get('comment_og_admin_delete', 1);
if ($gid && $coad) {
$group = node_load($gid);
og_load_group($group);
return og_is_group_admin($group);
}
else {
return user_access('administer comments');
}
}
function comment_og_comment_edit($cid = NULL, $gid = NULL) {
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid));
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$admins_can_edit = variable_get('comment_og_admin_edit', 1);
$group = $gid ? node_load($gid) : NULL;
if ($group) {
og_load_group($group);
if (comment_access('edit', $comment)) {
return comment_form_box((array) $comment);
}
elseif ($admins_can_edit && og_is_group_admin($group)) {
return comment_og_form_box((array) $comment, $group);
}
}
else {
drupal_access_denied();
}
}
function comment_og_form_box($edit, $group) {
return theme('box', NULL, drupal_get_form('comment_og_form', $edit, $group));
}
function comment_og_form(&$form_state, $edit, $group) {
global $user;
$op = isset($_POST['op']) ? $_POST['op'] : '';
$node = node_load($edit['nid']);
$edit += array(
'name' => '',
'mail' => '',
'homepage' => '',
);
if ($user->uid && og_is_group_admin($group)) {
if (!empty($edit['author'])) {
$author = $edit['author'];
}
elseif (!empty($edit['name'])) {
$author = $edit['name'];
}
else {
$author = $edit['registered_name'];
}
$form['_author'] = array(
'#type' => 'item',
'#title' => t('Authored by'),
'#value' => $author,
);
$form['author'] = array(
'#type' => 'value',
'#value' => $author,
);
}
if (variable_get('comment_subject_field_' . $node->type, 1) == 1) {
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 64,
'#default_value' => !empty($edit['subject']) ? $edit['subject'] : '',
);
}
if (!empty($edit['comment'])) {
$default = $edit['comment'];
}
else {
$default = NULL;
}
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#required' => TRUE,
);
if (!isset($edit['format'])) {
$edit['format'] = FILTER_FORMAT_DEFAULT;
}
$form['comment_filter']['format'] = filter_form($edit['format']);
$form['cid'] = array(
'#type' => 'value',
'#value' => !empty($edit['cid']) ? $edit['cid'] : NULL,
);
$form['pid'] = array(
'#type' => 'value',
'#value' => !empty($edit['pid']) ? $edit['pid'] : NULL,
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $edit['nid'],
);
$form['uid'] = array(
'#type' => 'value',
'#value' => !empty($edit['uid']) ? $edit['uid'] : 0,
);
if (!form_get_errors() && (variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL || $op == t('Preview') || $op == t('Save'))) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 19,
);
}
$form['preview'] = array(
'#type' => 'button',
'#value' => t('Preview'),
'#weight' => 20,
);
$form['#token'] = 'comment' . $edit['nid'] . (isset($edit['pid']) ? $edit['pid'] : '');
if ($op == t('Preview')) {
$form['#after_build'] = array(
'comment_og_form_add_preview',
);
}
if (empty($edit['cid']) && empty($edit['pid'])) {
$form['#action'] = url('comment/reply/' . $edit['nid']);
}
return $form;
}
function comment_og_form_add_preview($form, $form_state) {
global $user;
$form_state['values']['comment'] .= comment_og_admin_edit_msg($user);
return comment_form_add_preview($form, $form_state);
}
function comment_og_form_submit($form, &$form_state) {
_comment_form_submit($form_state['values']);
global $user;
$form_state['values']['comment'] .= comment_og_admin_edit_msg($user);
if ($cid = comment_save($form_state['values'])) {
$node = node_load($form_state['values']['nid']);
$comment_count = $node->comment_count + 1;
$page = comment_new_page_count($comment_count, 1, $node);
$form_state['redirect'] = array(
'node/' . $node->nid,
$page,
"comment-{$cid}",
);
return;
}
}
function comment_og_admin_edit_msg($user = NULL) {
$default_msg = t('[This post has been modified by @user]', array(
'@user' => '%user',
));
if ($user) {
$date = time();
$message = check_plain(variable_get('comment_og_admin_edit_msg', $default_msg));
return $message ? theme('comment_og_edit_msg', str_replace('%user', strip_tags(theme('username', $user)), $message)) : '';
}
else {
return $default_msg;
}
}
function theme_comment_og_edit_msg($message) {
return $message ? "\n\n" . '<p class="edit-msg">' . $message . '</p>' : '';
}