You are here

function heartbeat_comments_form_submit in Heartbeat 6.4

Same name and namespace in other branches
  1. 7 modules/heartbeat_comments/heartbeat_comments.module \heartbeat_comments_form_submit()

User submitted a heartbeat comment.

1 string reference to 'heartbeat_comments_form_submit'
heartbeat_comments_menu in modules/heartbeat_comments/heartbeat_comments.module
Implementation of hook_menu().

File

modules/heartbeat_comments/heartbeat_comments.module, line 474
heartbeat_comments.module Heartbeat comments can come with two possible

Code

function heartbeat_comments_form_submit($form = array(), &$form_state = array()) {
  $ahah = empty($form) && empty($form_state);
  global $user;
  $uid = $user->uid;
  $message = '';
  $message = $ahah ? $_POST['message'] : $form_state['values']['message'];
  $uaid = $ahah ? $_POST['uaid'] : (isset($form_state['values']['uaid']) ? $form_state['values']['uaid'] : $form_state['clicked_button']['#post']['uaid']);
  $nid = $ahah ? $_POST['nid'] : (isset($form_state['values']['nid']) ? $form_state['values']['nid'] : 0);
  $node_comment = $nid > 0 && ($ahah ? $_POST['node_comment'] : $form_state['values']['node_comment']);
  $first_comment = $ahah ? isset($_POST['first_comment']) ? $_POST['first_comment'] : 0 : (isset($form_state['values']['first_comment']) ? $form_state['values']['first_comment'] : 0);

  // Check the token.
  $token = 'heartbeat_comment_' . $uaid;
  if (!drupal_valid_token($_POST['heartbeat_comment_token'], $token)) {
    drupal_json(array(
      'status' => FALSE,
      'data' => t('Access denied'),
    ));
    exit;
  }
  if (!user_access('add heartbeat comment') || empty($message)) {
    drupal_json(array(
      'status' => FALSE,
      'data' => t('No comments'),
    ));
    exit;
  }
  $saved = NULL;

  // Save the (node) comment.
  if ($node_comment) {
    $comment = array(
      'subject' => '',
      'format' => '',
      'pid' => 0,
      'cid' => 0,
      'comment' => $message,
      'nid' => $nid,
      'uid' => $uid,
    );
    $cid = comment_save($comment);
    if ($cid) {
      $saved = db_fetch_object(db_query('SELECT c.*, u.uid,
        u.name AS registered_name, u.name AS name, u.signature, u.signature_format, u.picture
        FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid
        WHERE cid = %d', $cid));
    }

    // Clear the heartbeat comment cache.
    if (variable_get('heartbeat_comments_cache', 1)) {
      cache_clear_all('heartbeat:comments:' . $uaid . ':' . $nid, 'cache');
    }
    $tuaid = $uaid;
  }
  else {

    // Get the original message from the translation table if needed.
    $tuaid = heartbeat_get_tuaid($uaid);
    if (db_query("INSERT INTO {heartbeat_comments} (uid, uaid, message, time) VALUES (%d, %d, '%s', '%s')", $uid, $tuaid, $message, date('Y-m-d H:i:s'))) {
      $hcid = db_last_insert_id('heartbeat_comments', 'hcid');
      $saved = db_fetch_object(db_query("SELECT s.hcid, s.uid, s.uaid, s.message AS 'comment', s.time,\n        u.uid, u.name AS registered_name, u.name AS name, u.signature, u.signature_format, u.picture, u.data\n        FROM {heartbeat_comments} s INNER JOIN {users} u ON s.uid = u.uid\n        WHERE s.hcid = %d", $hcid));
    }
    $uaids = heartbeat_get_uaids($tuaid);
    if (empty($uaids)) {
      $uaids = array(
        $uaid,
      );
    }

    // Alter the uaid_comments field for this user activity id.
    db_query("UPDATE {heartbeat_activity} SET uaid_comments = uaid_comments + 1 WHERE uaid IN ( %s ) ", implode(',', $uaids));

    // Clear the heartbeat comment cache.
    if (variable_get('heartbeat_comments_cache', 1)) {
      cache_clear_all('heartbeat:comments:' . $tuaid . ':0', 'cache');
    }
  }
  $authorid = db_result(db_query("SELECT uid FROM {heartbeat_activity} WHERE uaid = %d", $uaid));
  $author = heartbeat_user_load($authorid);
  module_invoke_all('heartbeat_comment_post', $user, $saved, $author);

  // Let rules know there is a comment event.
  if (!$node_comment && module_exists('rules')) {
    rules_invoke_event('heartbeat_comment_post', $user, $saved, $author);

    // For actions on related users
    $result = db_query("SELECT DISTINCT(uid) FROM {heartbeat_comments} WHERE uaid = %d AND (uid != %d AND uid != %d)", $uaid, $user->uid, $author->uid);
    while ($row = db_fetch_object($result)) {
      $related_uids[$row->uid] = heartbeat_user_load($row->uid);
    }
    if (!empty($related_uids)) {
      foreach ($related_uids as $related_user) {
        rules_invoke_event('heartbeat_related_comment_post', $user, $saved, $author, $related_user);
      }
    }
  }
  if ($ahah) {
    if (isset($saved)) {
      if (!$first_comment || $first_comment == "false") {
        $content = theme('heartbeat_comment', $saved, $node_comment);
      }
      else {
        $content = theme('heartbeat_comments', array(
          $saved,
        ), $tuaid, $node_comment);
      }
      drupal_json(array(
        'status' => TRUE,
        'data' => $content,
        'id' => $tuaid,
      ));
    }
    else {
      drupal_json(array(
        'status' => TRUE,
        'data' => 'error',
      ));
    }
    exit;
  }
  else {
    if ($saved) {
      drupal_set_message(t('Comment has been posted.'));
    }
    else {
      drupal_set_message(t('Error while posting comment.'));
    }
    return TRUE;
  }
}