You are here

function discussthis_create_discussion_form_submit in Discuss This! 7.2

Same name and namespace in other branches
  1. 7 discussthis.discussion.inc \discussthis_create_discussion_form_submit()

Save the new comment.

This function creates a new forum topic if this is the first comment on this node. Otherwise, it just adds the comment to the existing forum.

@todo We've got a problem here, if the administrator wants to moderate these comments, then we should not create the forum node. This is a similar problem to the one we see in creating the forum node before the comment is even posted to us.

I guess that the biggest problem is that a comment, to exist, needs to be attached to a node.

I see two potential solutions:

1) we handle our own comments

2) we create the comment on the node being discussed and move it later to the forum

Parameters

$form The form info:

$form_state The current state of the form, including the values:

1 string reference to 'discussthis_create_discussion_form_submit'
discussthis_create_discussion_form in ./discussthis.discussion.inc
Create a discussion comment form.

File

./discussthis.discussion.inc, line 191
Functions to create/link/delete discussions.

Code

function discussthis_create_discussion_form_submit($form, &$form_state) {
  global $user;

  // start a new transaction : if this fails, we should revert it all
  $transaction = db_transaction();
  try {

    // we got a post, so we want to create the forum node
    // load the node being discussed
    $node = node_load($form_state['values']['discussthis_nid']);
    if (!$node) {

      // what shall we do here? the admin could delete the node while
      // a user is trying to discuss it...
      drupal_set_message(t('The node being discussed is not available anymore. (nid: @nid)', array(
        '@nid' => $form_state['values']['discussthis_nid'],
      )), 'error');
      drupal_goto('');
    }
    $language = 'und';
    $discussthis_types_config = variable_get('discussthis_types_config', array());

    // Create the new topic
    $topic = (object) array();
    $topic->uid = token_replace(variable_get('discussthis_author', '[current-user:uid]'), array(
      'user' => $user,
    ));
    $topic->type = 'forum';
    $topic->taxonomy_forums['und'][0]['tid'] = isset($discussthis_types_config[$node->type][$node->type . '_forum']) ? $discussthis_types_config[$node->type][$node->type . '_forum'] : 0;
    $topic->created = time();
    $topic->status = 1;

    // To have published, else use 0
    $topic->promote = 0;
    $topic->sticky = 0;
    $topic->revision = 1;
    $topic->language = $language;
    $topic->comment = 2;
    $topic->title = token_replace(variable_get('discussthis_newsubject', 'Discussion on [node:content-type] : [node:title]'), array(
      'node' => $node,
    ));
    $topic->body[$language][0] = array(
      'value' => token_replace(variable_get('discussthis_newtemplate', DISCUSSTHIS_DEFAULT_NODE_MESSAGE), array(
        'node' => $node,
      )),
      'format' => 'full_html',
    );
    $topic->teaser = '';
    $topic->log = 'Auto Created Forum Topic';
    node_submit($topic);
    node_save($topic);
    if ($topic->nid) {

      // If it did work, associate the node with the topic
      _discussthis_set_topic($node->nid, $topic->nid);
    }
    else {

      // It did not work... hmmm...
      drupal_set_message('Forum post could not be created for your comment to be published.', 'error');
      drupal_goto();
    }

    // Create the comment on the new discussion topic
    $comment = (object) array(
      'nid' => $topic->nid,
      'cid' => 0,
      'pid' => 0,
      'uid' => $user->uid,
      'mail' => '',
      'is_anonymous' => user_is_anonymous(),
      'homepage' => '',
      'status' => COMMENT_PUBLISHED,
      'subject' => $form_state['values']['subject'],
      'language' => $node->language,
      'comment_body' => array(
        'und' => array(
          0 => array(
            'value' => $form_state['values']['comment']['value'],
            'format' => $form_state['values']['comment']['format'],
          ),
        ),
      ),
    );
    comment_submit($comment);
    comment_save($comment);
    $form_state['redirect'] = 'node/' . $topic->nid;

    // if you have boost, we want to reset the discussed node since
    // now it will look "different" (the link and eventually the new
    // comment)
    if (function_exists('boost_expire_node')) {
      boost_expire_node($node);
    }
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('discussthis', $e);
  }
}