function heartbeat_comments_form_submit in Heartbeat 7
Same name and namespace in other branches
- 6.4 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 - Implements hook_menu().
File
- modules/
heartbeat_comments/ heartbeat_comments.module, line 377 - Heartbeat comments for activity.
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 ? $_REQUEST['message'] : $form_state['values']['message'];
$uaid = $ahah ? $_REQUEST['uaid'] : (isset($form_state['values']['uaid']) ? $form_state['values']['uaid'] : $form_state['clicked_button']['#post']['uaid']);
$nid = $ahah ? $_REQUEST['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($_REQUEST['first_comment']) ? $_REQUEST['first_comment'] : 0 : (isset($form_state['values']['first_comment']) ? $form_state['values']['first_comment'] : 0);
$active_comment = TRUE;
if (!user_access('add heartbeat comment') || empty($message)) {
heartbeat_print_json(array(
'status' => FALSE,
'data' => t('No comments'),
));
exit;
}
// Check the token.
$token = 'heartbeat_comment_' . $uaid;
if (!drupal_valid_token($_POST['heartbeat_comment_token'], $token)) {
drupal_json_output(array(
'status' => FALSE,
'data' => t('Access denied'),
));
exit;
}
$settings = heartbeat_plugins_get_plugin('activitycomments')
->getPlugin()
->getSettings();
$saved = NULL;
// Save the (node) comment.
if ($node_comment && module_exists('comment')) {
$comment = new stdClass();
$comment->language = $GLOBALS['language']->language;
$comment->format = '';
$comment->pid = 0;
$comment->cid = 0;
$comment->subject = '';
$comment->comment_body[LANGUAGE_NONE][0]['value'] = $message;
$comment->comment_body[LANGUAGE_NONE][0]['format'] = 'plain_text';
$comment->nid = $nid;
$comment->uid = $uid;
$comment->is_anonymous = $uid == 0 ? TRUE : FALSE;
$comment = comment_submit($comment);
comment_save($comment);
$saved = comment_load($comment->cid);
$saved->comment = $saved->comment_body['und'][0]['safe_value'];
// Check if the comment is active.
$active_comment = user_access('skip comment approval');
// Find the number of the first comment of the first unread thread.
$count = db_query('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
':status' => COMMENT_PUBLISHED,
':nid' => $nid,
':thread' => 1,
))
->fetchField();
$saved->comment_count = $count;
// Clear the heartbeat comment cache.
if ($settings['heartbeat_comments_cache']) {
cache_clear_all('heartbeat:comments:' . $uaid . ':' . $nid, 'cache');
}
}
else {
$hcid = db_insert('heartbeat_comments')
->fields(array(
'uid' => $uid,
'uaid' => $uaid,
'message' => $message,
'time' => $_SERVER['REQUEST_TIME'],
))
->execute();
if ($hcid) {
$result = db_query("SELECT s.hcid, s.uid, s.uaid, s.message AS 'comment', s.time AS 'changed',\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 = :hcid", array(
':hcid' => $hcid,
));
$saved = $result
->fetch();
// Find the number of the first comment of the first unread thread.
$saved->comment_count = db_query('SELECT COUNT(*) FROM {heartbeat_comments} WHERE uaid = :uaid', array(
':uaid' => $uaid,
))
->fetchField();
}
// Clear the heartbeat comment cache.
if ($settings['heartbeat_comments_cache']) {
cache_clear_all('heartbeat:comments:' . $uaid . ':0', 'cache');
}
}
$saved->uaid = $uaid;
$result = db_select('heartbeat_activity', 'ha')
->fields('ha', array(
'uid',
))
->condition('uaid', $uaid)
->execute();
foreach ($result as $row) {
$authorid = $row->uid;
break;
}
$accounts = user_load_multiple(array(
$authorid,
));
$author = array_shift($accounts);
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_create', $user, $saved, $author);
// For actions on related users
$result = db_query("SELECT DISTINCT(uid) FROM {heartbeat_comments} WHERE uaid = :uaid AND (uid != :uid AND uid != :author_id)", array(
':uaid' => $uaid,
':uid' => $user->uid,
':author_id' => $author->uid,
));
foreach ($result as $row) {
$related_uids[$row->uid] = 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 (!$active_comment) {
heartbeat_print_json(array(
'status' => TRUE,
'data' => theme('heartbeat_no_comment', array(
'comment' => $saved,
'node_comment' => $node_comment,
'last' => FALSE,
)),
'id' => $uaid,
));
exit;
}
elseif (isset($saved)) {
// Include the CTools tools that we need.
ctools_include('ajax');
ctools_include('modal');
if (!$first_comment || $first_comment == "false") {
$content = theme('heartbeat_comment', array(
'comment' => $saved,
'node_comment' => $node_comment,
'last' => FALSE,
));
}
else {
$heartbeatActivity = HeartbeatMessagePool::getInstance()
->getMessage($uaid);
$heartbeatActivity->uaid = $uaid;
$heartbeatActivity->additions->node_comment = $node_comment;
$heartbeatActivity->additions->has_more = FALSE;
$content = theme('heartbeat_comments', array(
'comments' => array(
$saved,
),
'activity' => $heartbeatActivity,
));
}
$label = '<span class="heartbeat-attachment-button">' . l(heartbeat_comments_get_count_label($saved->comment_count), 'heartbeat/message/' . $uaid, array(
'attributes' => array(
'onclick' => 'javascript:Drupal.heartbeat.comments.toggleComments(this, ' . $uaid . '); return false;',
),
)) . '</span>';
heartbeat_print_json(array(
'status' => TRUE,
'data' => $content,
'id' => $uaid,
'newButton' => $label,
));
exit;
}
else {
heartbeat_print_json(array(
'status' => FALSE,
'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;
}
}