View source
<?php
define('DISCUSSTHIS_DEFAULT_NODE_MESSAGE', 'Following is a discussion on the [node-type-name] item titled: [node-link].<br/>' . 'Below is the discussion so far. Feel free to add your own comments!<br/>');
function discussthis_help($section) {
switch ($section) {
case 'admin/help#discussthis':
$output = '';
$output = '<p>' . t('Displays links to discussion forums for a given node. Administrators can select the types of nodes for which this is enabled, and for each of these, which forum new topics should be created in.') . '</p>';
$output .= '<ul><li><a href="admin/settings/discussthis">Discuss This configuration settings</a></li>';
$output .= '<li><a href="admin/user/access#module-discussthis">Discuss This permissions configuration</a></li></ul>';
return $output;
}
}
function discussthis_init() {
$path = drupal_get_path('module', 'discussthis');
drupal_add_css($path . '/discussthis.css');
}
function discussthis_perm() {
return array(
'administer discuss this',
'override discuss this forums',
'access discuss this links',
'initiate discuss this topics',
);
}
function discussthis_menu() {
module_load_include('admin.inc', 'discussthis');
return _discussthis_menu();
}
function discussthis_theme() {
return array(
'discussthis_admin_settings_forums' => array(
'arguments' => array(
'form',
),
'file' => 'discussthis.admin.inc',
),
);
}
function discussthis_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'validate':
if ($node && user_access('override discuss this forums') && isset($node->discussthis)) {
if (!empty($node->discussthis['discussthis_topic'])) {
$topic_nid = $node->discussthis['discussthis_topic'];
if ($topic_nid == (int) $topic_nid && !is_null($node->nid) && $topic_nid != $node->nid && is_numeric($topic_nid)) {
$topic = node_load(array(
'nid' => (int) $topic_nid,
));
if (!$topic || $topic->type != 'forum') {
$topic_nid = FALSE;
}
}
else {
$topic_nid = FALSE;
}
if (!$topic_nid) {
form_set_error('discussthis][discussthis_topic', t('The Discuss This! forum topic #@nid was not found (or you chose this very node.) Please, try again. Use the auto-complete feature or enter the exact node identifier. If you did not change the topic identifier, it could have been deleted while you were editing this node. Simply clear the topic entry in that case.', array(
'@nid' => $node->discussthis['discussthis_topic'],
)));
unset($node->discussthis['discussthis_topic']);
}
}
if (!empty($node->discussthis['discussthis_forum'])) {
$forum_tid = $node->discussthis['discussthis_forum'];
if ((int) $forum_tid != -1) {
if ($forum_tid == (int) $node->discussthis['discussthis_forum']) {
$vid = variable_get('forum_nav_vocabulary', '');
$term = taxonomy_get_term($forum_tid);
if (!$term || $term->vid != $vid) {
form_set_error('discussthis][discussthis_forum', t('The Discuss This! forum #@tid was not found.', array(
'@tid' => $forum_tid,
)));
unset($node->discussthis['discussthis_forum']);
}
}
else {
unset($node->discussthis['discussthis_forum']);
}
}
}
}
break;
case 'insert':
case 'update':
if ($node) {
if ($node->type == 'forum') {
}
else {
if (user_access('override discuss this forums')) {
if (isset($node->discussthis['discussthis_forum'])) {
$discussthis_forum['nid'] = $node->nid;
$discussthis_forum['forum_tid'] = $node->discussthis['discussthis_forum'];
_discussthis_set_forum($discussthis_forum);
}
db_lock_table('discussthis');
$sql = "DELETE FROM {discussthis} WHERE nid = %d";
db_query($sql, $node->nid);
if (!empty($node->discussthis['discussthis_topic'])) {
$sql = "INSERT INTO {discussthis} (nid, topic_nid) VALUES (%d, %d)";
db_query($sql, $node->nid, $node->discussthis['discussthis_topic']);
}
db_unlock_tables();
}
}
}
break;
case 'delete':
$sql = 'DELETE FROM {discussthis} WHERE nid = %d OR topic_nid = %d';
db_query($sql, $node->nid, $node->nid);
$sql = 'DELETE FROM {discussthis_forums} WHERE nid = %d';
db_query($sql, $node->nid);
break;
case 'view':
if (!$teaser && $page) {
$node->content['body']['#value'] .= discussthis_comment_render($node);
}
break;
}
}
function discussthis_rules_event_info() {
return array(
'discussthis_discussion_new' => array(
'label' => t('A new discussion is started'),
'module' => 'discussthis',
'arguments' => array(
'related_node' => array(
'type' => 'node',
'label' => t('Related node.'),
),
'comment' => array(
'type' => 'comment',
'label' => t('Comment'),
),
),
),
'discussthis_discussion_update' => array(
'label' => t('A discussion is updated'),
'module' => 'discussthis',
'arguments' => array(
'related_node' => array(
'type' => 'node',
'label' => t('Related node.'),
),
'comment' => array(
'type' => 'comment',
'label' => t('Comment'),
),
),
),
);
}
function discussthis_comment(&$comment, $op) {
if ($op == 'publish' && module_exists('rules')) {
$nid = db_result(db_query("SELECT nid FROM {discussthis} WHERE topic_nid = %d", $comment['nid']));
if ($nid) {
$node = node_load($nid);
if (arg(0) == 'discussthis') {
rules_invoke_event('discussthis_discussion_new', $node, $comment);
}
else {
rules_invoke_event('discussthis_discussion_update', $node, $comment);
}
}
}
}
function discussthis_comment_render($node) {
$comments_per_page = variable_get('discussthis_comments', 0);
if ($comments_per_page <= 0) {
return '';
}
if (!user_access('access comments')) {
return '';
}
$nid = _discussthis_get_topic($node->nid);
if (!$nid) {
return '';
}
$topic = node_load(array(
'nid' => $nid,
));
$mode = _comment_get_display_setting('mode', $topic);
$order = _comment_get_display_setting('sort', $topic);
$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.signature,' . ' u.signature_format, u.picture, u.data, c.thread, c.status' . ' FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
$query_args = array(
$nid,
);
if (!user_access('administer comments')) {
$query .= ' AND c.status = %d';
$query_args[] = COMMENT_PUBLISHED;
}
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.cid DESC';
}
else {
$query .= ' ORDER BY c.thread DESC';
}
}
elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.cid';
}
else {
$query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
}
}
$query = db_rewrite_sql($query, 'c', 'cid');
$result = db_query_range($query, $query_args, 0, $comments_per_page);
$divs = 0;
$comments = '';
drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css');
while ($comment = db_fetch_object($result)) {
$comment = drupal_unpack($comment);
$comment->depth = count(explode('.', $comment->thread)) - 1;
if ($comment->uid) {
$comment->name = $comment->registered_name;
}
if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
if ($comment->depth > $divs) {
$divs++;
$comments .= '<div class="indented">';
}
else {
while ($comment->depth < $divs) {
$divs--;
$comments .= '</div>';
}
}
}
if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
$comments .= theme('comment_flat_collapsed', $comment, $topic);
}
elseif ($mode == COMMENT_MODE_FLAT_EXPANDED) {
$comments .= theme('comment_flat_expanded', $comment, $topic);
}
elseif ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
$comments .= theme('comment_thread_collapsed', $comment, $topic);
}
elseif ($mode == COMMENT_MODE_THREADED_EXPANDED) {
$comments .= theme('comment_thread_expanded', $comment, $topic);
}
}
for (; $divs > 0; --$divs) {
$comments .= '</div>';
}
if ($comments) {
return '<div class="discussthis-comments">' . theme('comment_wrapper', $comments, $topic) . '</div>';
}
return '';
}
function discussthis_link($type, $node = NULL, $teaser = FALSE) {
if ($type == 'node' && $node) {
$links = array();
$node_types = node_get_types('names');
$discussthis_nodetypes = variable_get('discussthis_nodetypes', $node_types);
if (empty($discussthis_nodetypes[$node->type])) {
return $links;
}
$discussthis_forum = _discussthis_get_forum($node->nid, $node->type);
if ($discussthis_forum['forum_tid'] <= 0) {
return $links;
}
$topic_nid = _discussthis_get_topic($node->nid);
$discussthis_link = variable_get('discussthis_link', '');
if (!$discussthis_link) {
$discussthis_link = t('Discuss This!');
}
$participate_link = variable_get('discussthis_participate', '');
if (!$participate_link) {
$participate_link = t('Participate in this discussion');
}
if ($topic_nid && user_access('access discuss this links')) {
$all = comment_num_all($topic_nid);
if (variable_get('discussthis_showcounts', 1)) {
$new = comment_num_new($topic_nid);
$counts = t(' (@new new/@all total)', array(
'@new' => $new,
'@all' => $all,
));
}
$links['discussthis'] = array(
'title' => $all == 0 ? $discussthis_link : $participate_link . $counts,
'href' => 'node/' . $topic_nid,
'attributes' => array(
'class' => 'discussthis-link',
'title' => t('Participate to the discussion about this page'),
),
);
}
elseif ($topic_nid == 0) {
if (user_access('initiate discuss this topics')) {
$links['discussthis'] = array(
'title' => $discussthis_link,
'href' => 'discussthis/new/' . $node->nid,
'attributes' => array(
'class' => 'discussthis-link',
'title' => t('Start a discussion about this page'),
'rel' => 'nofollow',
),
);
}
}
if (count($links) == 0) {
global $user;
if ($user->uid == 0 && variable_get('discussthis_login', 1) && user_access('access discuss this links')) {
if ($topic_nid) {
$all = comment_num_all($topic_nid);
if (variable_get('discussthis_showcounts', 1)) {
$new = comment_num_new($topic_nid);
$counts = t(' (@new new/@all total)', array(
'@new' => $new,
'@all' => $all,
));
}
$destination = 'destination=node/' . $topic_nid;
$appeal = $all == 0 ? $discussthis_link : $participate_link . $counts;
}
else {
$destination = 'destination=discussthis/new/' . $node->nid;
$appeal = $discussthis_link;
}
$attributes = array(
'class' => 'discussthis-login',
'title' => t('Log in or register and start a discussion about this page'),
'rel' => 'nofollow',
);
if (variable_get('user_register', 1)) {
if (variable_get('user_email_verification', 1)) {
$new_user = variable_get('discussthis_new_user', '');
if (!$new_user) {
$new_user = t('(new users have to return to the discussion after validating their new account by e-mail)');
}
$appeal .= ' ' . $new_user;
$href = t('!login or !register to @appeal', array(
'!login' => l(t('Log in'), 'user/login', array(
'query' => $destination,
'attributes' => $attributes,
)),
'!register' => l(t('Register'), 'user/register'),
'@appeal' => $appeal,
));
}
else {
$href = t('!login or !register to @appeal', array(
'!login' => l(t('Log in'), 'user/login', array(
'query' => $destination,
)),
'!register' => l(t('Register'), 'user/register', array(
'query' => $destination,
'attributes' => $attributes,
)),
'@appeal' => $appeal,
));
}
}
else {
$href = t('!login to @appeal', array(
'!login' => l('Log in', 'user/login', array(
'query' => $destination,
'attributes' => $attributes,
)),
'@appeal' => $appeal,
));
}
$links['discussthis'] = array(
'title' => $href,
'html' => TRUE,
);
}
}
return $links;
}
}
function discussthis_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'forum_node_form' && !empty($form_state['discussthis']['post'])) {
if (module_exists('captcha') && (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE)) && !user_access('skip CAPTCHA')) {
module_load_include('inc', 'captcha');
$captcha_point = captcha_get_form_id_setting('forum_node_form');
if ($captcha_point && $captcha_point->type) {
$form['buttons']['captcha']['#captcha_type'] = 'none';
}
}
}
if (!user_access('override discuss this forums')) {
return;
}
if (isset($form['type']['#value'])) {
$type = $form['type']['#value'];
}
elseif (isset($form['orig_type']['#value'])) {
$type = $form['orig_type']['#value'];
}
else {
return;
}
if ($form_id != $type . '_node_form') {
return;
}
$nid = $form['nid']['#value'];
$discussthis_forum = _discussthis_get_forum($nid, $type);
$forum_tid = $discussthis_forum['forum_tid'];
if (!$forum_tid) {
return;
}
$form['discussthis'] = array(
'#type' => 'fieldset',
'#title' => t('Discuss This'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$name = '';
$topic_nid = _discussthis_get_topic($nid);
if ($topic_nid) {
$topic = node_load(array(
'nid' => $topic_nid,
));
if ($topic) {
$name = '<strong>' . t('Current topic #@nid: @title', array(
'@title' => $topic->title,
'@nid' => $topic_nid,
)) . '</strong><br /><br />';
}
else {
$sql = "DELETE FROM {discussthis} WHERE nid = %d";
db_query($sql, $nid);
unset($topic_nid);
}
}
if (!$topic_nid) {
$forums = forum_get_forums();
$discussthis_forums[-1] = 'No Discuss This!';
foreach ($forums as $tid => $forum) {
if (empty($forum->container)) {
$discussthis_forums[$tid] = $forum->name;
}
}
$form['discussthis']['discussthis_forum'] = array(
'#type' => 'select',
'#title' => t('Forum for new discussion'),
'#required' => TRUE,
'#description' => t('Select the forum where the first discussion about this node will be created. Or select "No Discuss This!" to prevent discussions on this node. Note: if a topic is attached to this node, then this parameter will have no effect.'),
'#options' => $discussthis_forums,
'#default_value' => $forum_tid,
);
}
$form['discussthis']['discussthis_topic'] = array(
'#type' => 'textfield',
'#title' => t('Identifier of the forum topic to attach to this node'),
'#description' => $name . t('To use the auto-complete feature, enter the title of a topic to attach to this node. Otherwise, simply enter the topic identifier directly. Clear the identifier or use 0 to detach this node from the current topic it is attached to. New node can immediately be attached to a discussion topic. WARNING: this feature does not prevent you from selecting a topic from a forum that is not otherwise explicitly connected with the Discuss This! module.'),
'#default_value' => $topic_nid ? $topic_nid : '',
'#autocomplete_path' => 'discussthis/autocomplete',
'#maxlength' => 255,
);
}
function discussthis_token_list($type = 'all') {
if ($type == 'discussthis' || $type == 'all') {
$tokens['discussthis']['node-link'] = t('A link to the original node under discussion.');
$tokens['discussthis']['node-title'] = t('The title of the original node.');
$tokens['discussthis']['node-type-name'] = t('The name of the original node\'s type.');
$tokens['discussthis']['node-type'] = t('The original node\'s type.');
$tokens['discussthis']['node-teaser'] = t('Node teaser');
$tokens['discussthis']['node-body'] = t('Node body');
$tokens['discussthis']['node-body-50'] = t('First 50 characters of a body');
$tokens['discussthis']['node-body-100'] = t('First 100 characters of a body');
$tokens['discussthis']['node-body-200'] = t('First 200 characters of a body');
$tokens['discussthis']['node-body-400'] = t('First 400 characters of a body');
return $tokens;
}
}
function discussthis_token_values($type, $object = NULL, $options = array()) {
if ($type == 'discussthis' && $object) {
$object->body = str_replace('<!--break-->', '', $object->body);
$tokens['node-link'] = l($object->title, 'node/' . $object->nid);
$tokens['node-title'] = $object->title;
$tokens['node-type-name'] = node_get_types('name', $object);
$tokens['node-type'] = $object->type;
$tokens['node-teaser'] = $object->teaser;
$tokens['node-body'] = $object->body;
$tokens['node-body-50'] = substr($object->body, 0, 50);
$tokens['node-body-100'] = substr($object->body, 0, 100);
$tokens['node-body-200'] = substr($object->body, 0, 200);
$tokens['node-body-400'] = substr($object->body, 0, 400);
return $tokens;
}
}
function discussthis_new($nid) {
if (!user_access('initiate discuss this topics')) {
drupal_access_denied();
return;
}
$topic_nid = _discussthis_get_topic($nid);
if ($topic_nid) {
drupal_goto('comment/reply/' . $topic_nid, NULL, 'comment-form');
}
drupal_goto('discussthis/create/' . $nid);
}
function discussthis_boost_is_cacheable($path) {
if (strncmp($path, 'discussthis/create', 18) == 0) {
return FALSE;
}
}
function discussthis_create_form($form_state, $nid) {
if (!user_access('initiate discuss this topics')) {
drupal_access_denied();
return;
}
$title = variable_get('discussthis_new_post_title', '');
if ($title) {
drupal_set_title($title);
}
global $user;
$op = empty($form_state['post']['op']) ? '' : $form_state['post']['op'];
$is_ready = $op == t('Preview') || $op == t('Save');
if ($is_ready) {
$comment = $user;
$comment->comment = check_markup($form_state['post']['comment'], $form_state['post']['format'], FALSE);
$comment->timestamp = time();
$comment->new = FALSE;
$comment->preview = TRUE;
$comment->subject = $form_state['post']['subject'];
$node = array(
'type' => 'forum',
);
$node = (object) $node;
$form['preview_comment'] = array(
'#title' => t('Preview'),
'#value' => theme('comment', $comment, $node),
);
}
$comment_anonymous_forum = variable_get('comment_anonymous_forum', COMMENT_ANONYMOUS_MAYNOT_CONTACT);
if (!$user->uid && $comment_anonymous_forum != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
drupal_add_js(drupal_get_path('module', 'comment') . '/comment.js');
}
if ($user->uid) {
$form['_author'] = array(
'#type' => 'item',
'#title' => t('Your name'),
'#value' => theme('username', $user),
);
$form['author'] = array(
'#type' => 'value',
'#value' => $user->name,
);
}
elseif ($comment_anonymous_forum == COMMENT_ANONYMOUS_MAY_CONTACT) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 60,
'#size' => 30,
'#default_value' => variable_get('anonymous', t('Anonymous')),
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
);
$form['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
);
}
elseif ($comment_anonymous_forum == COMMENT_ANONYMOUS_MUST_CONTACT) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 60,
'#size' => 30,
'#default_value' => variable_get('anonymous', t('Anonymous')),
'#required' => TRUE,
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
'#required' => TRUE,
);
$form['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
);
}
if (variable_get('comment_subject_field_forum', 1) == 1) {
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 64,
);
}
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#required' => TRUE,
);
$form['comment_filter']['format'] = filter_form(FILTER_FORMAT_DEFAULT);
$form['uid'] = array(
'#type' => 'value',
'#value' => $user->uid,
);
$form['discussthis_nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
if ($is_ready || variable_get('comment_preview_forum', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 19,
);
$form['#submit'][] = 'discussthis_create_form_submit';
}
$form['preview'] = array(
'#type' => 'button',
'#value' => t('Preview'),
'#weight' => 20,
);
return $form;
}
function discussthis_create_form_validate($form, &$form_state) {
comment_form_validate($form, $form_state);
}
function discussthis_create_form_submit($form, &$form_state) {
global $user;
$node = node_load($form_state['values']['discussthis_nid']);
if (!$node) {
drupal_set_message(t('The node being discussed is not available anymore. (nid: @nid)', array(
'@nid' => $form_state['values']['discussthis_nid'],
)), 'error');
drupal_goto('');
}
module_load_include('inc', 'node', 'node.pages');
$sub_form_state = array();
$author = user_load(array(
'uid' => $node->uid,
));
$sub_form_state['values']['title'] = token_replace(variable_get('discussthis_newsubject', '[node-title]'), 'discussthis', $node);
if ($author) {
$sub_form_state['values']['title'] = token_replace($sub_form_state['values']['title'], 'user', $author);
}
$default_body = DISCUSSTHIS_DEFAULT_NODE_MESSAGE;
$sub_form_state['values']['body'] = token_replace(variable_get('discussthis_newtemplate', $default_body), 'discussthis', $node);
if ($author) {
$sub_form_state['values']['body'] = token_replace($sub_form_state['values']['body'], 'user', $author);
}
$sub_form_state['values']['format'] = NULL;
$forum_vid = variable_get('forum_nav_vocabulary', 1);
$vocabulary = taxonomy_vocabulary_load($forum_vid);
$discussthis_forum = _discussthis_get_forum($node->nid, $node->type);
$tid = $discussthis_forum['forum_tid'];
if ($vocabulary->tags) {
$sub_form_state['values']['taxonomy']['tags'][$vid] = taxonomy_implode_tags($tid, $forum_vid);
}
elseif ($vocabulary->multiple) {
$sub_form_state['values']['taxonomy'][$forum_vid][$tid] = $tid;
}
else {
$sub_form_state['values']['taxonomy'][$forum_vid] = $tid;
}
$sub_form_state['values']['log'] = t('Node automatically created by discussthis for comment @subject', array(
'@subject' => $form_state['values']['subject'],
));
$sub_form_state['values']['op'] = t('Save');
$sub_form_state['values']['status'] = 1;
$sub_form_state['discussthis']['post'] = 1;
$topic = (object) array(
'type' => 'forum',
);
drupal_execute('forum_node_form', $sub_form_state, $topic);
$topic->nid = $sub_form_state['nid'];
if (!$topic->nid) {
drupal_set_message('Forum post could not be created for your comment to be published.', 'error');
drupal_goto();
}
$format = NULL;
$author_name = variable_get('discussthis_author', $user->name);
if (!$author_name || $user->name == $author_name) {
$author = $user;
}
elseif ($author_name == '*') {
$author = user_load(array(
'uid' => $node->uid,
));
$format = $node->format;
}
else {
$author = user_load(array(
'name' => $author_name,
));
}
if (!$author) {
$author = array(
'uid' => 0,
);
}
$sql = "UPDATE {node} SET uid = %d WHERE nid = %d";
db_query($sql, $author->uid, $topic->nid);
if (is_null($format)) {
$format = (int) variable_get('discussthis_format_' . $node->type, 0);
if ($format > 0) {
$allowed = user_access('administer filters', $author);
if (!$allowed) {
$query = 'SELECT roles FROM {filter_formats} WHERE format = %d';
$result = db_query($query, $format);
$format_roles = db_fetch_array($result);
$allowed = count(array_intersect($author->roles, explode(',', $format_roles))) > 0;
}
if ($allowed) {
$sql = "UPDATE {node_revisions} SET format = %d" . " WHERE nid = %d" . " AND vid = (SELECT vid FROM {node} WHERE nid = %d)";
db_query($sql, $format, $topic->nid, $topic->nid);
}
}
}
db_lock_table('discussthis');
$sql = "SELECT topic_nid FROM {discussthis} WHERE nid = %d";
$other_topic_nid = db_result(db_query($sql, $node->nid));
if (!$other_topic_nid) {
$sql = "INSERT INTO {discussthis} (nid, topic_nid) VALUES (%d, %d)";
db_query($sql, $node->nid, $topic->nid);
}
db_unlock_tables();
if ($other_topic_nid) {
node_delete($topic->nid);
$topic->nid = $other_topic_nid;
}
$form_state['values']['nid'] = $topic->nid;
_comment_form_submit($form_state['values']);
comment_form_submit($form, $form_state);
$form_state['redirect'] = 'node/' . $topic->nid;
if (function_exists('boost_expire_node')) {
boost_expire_node($node);
}
}
function _discussthis_get_topic($nid) {
$sql = 'SELECT topic_nid FROM {discussthis} WHERE nid = %d';
$topic_nid = db_result(db_query($sql, $nid));
return $topic_nid ? $topic_nid : 0;
}
function _discussthis_get_forum($nid, $type) {
$sql = 'SELECT * FROM {discussthis_forums} WHERE nid = %d';
$result = db_query($sql, $nid);
$discussthis_forum = db_fetch_array($result);
if (!$discussthis_forum) {
$discussthis_forum = array(
'nid' => $nid,
'forum_tid' => variable_get('discussthis_node_' . $type, 0),
);
}
return $discussthis_forum;
}
function _discussthis_set_forum($discussthis_forum) {
db_lock_table('discussthis_forums');
$sql = "DELETE FROM {discussthis_forums} WHERE nid = %d";
db_query($sql, $discussthis_forum['nid']);
$sql = "INSERT INTO {discussthis_forums} (nid, forum_tid)" . " VALUES (%d, %d)";
db_query($sql, $discussthis_forum['nid'], $discussthis_forum['forum_tid']);
db_unlock_tables();
}