type, variable_get('ajax_comments_node_types', array()), TRUE); if (($form_id == 'comment_form') && (arg(1) != 'edit') && (arg(1) != 'reply') && (arg(2) != 'edit') && (arg(2) != 'reply') && $node_type) { $form['#prefix'] = '
'; // we should set specific ID to let ahah wrapper know what to wrap on ajax // loaded comment-form even if we have many submit buttons on the page $form['preview']['#id'] = "ajax-comments-preview"; $form['preview']['#ahah'] = array( 'path' => 'ajax_comments/js', 'wrapper' => 'comment-preview', 'event' => 'click', 'method' => 'append', 'effect' => 'ajaxCommentsPreview', 'progress' => array('type' => '1bar', 'message' => t('Please wait...')), ); $form['submit']['#id'] = "ajax-comments-submit"; $form['submit']['#submit'] = array('ajax_comments_submit'); $form['submit']['#ahah'] = array( 'path' => 'ajax_comments/js', 'wrapper' => 'comment-form-content', 'event' => 'click', 'method' => 'before', 'effect' => 'ajaxCommentsSubmit', 'progress' => array('type' => '1bar', 'message' => t('Please wait...')), ); $path = drupal_get_path('module', 'ajax_comments'); drupal_add_css($path .'/jquery.scrollTo.js'); drupal_add_css($path .'/ajax_comments.css'); // Language settings $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); $languages = language_list('enabled'); $languages = $languages[1]; drupal_add_js('misc/jquery.form.js'); drupal_add_js($path .'/ajax_comments.js', 'module'); drupal_add_js(array( 'language_mode' => $mode, 'language_list' => $languages, 'clean_url' => variable_get('clean_url', 0), 'rows_in_reply' => variable_get('ajax_comments_reply_row_count', 3), 'rows_default' => variable_get('ajax_comments_default_row_count', 5), 'always_expand_main_form' => variable_get('ajax_comments_always_expand_form', TRUE), ), 'setting'); } } /* * Implementation of hook_menu() */ function ajax_comments_menu() { $items['admin/settings/ajax_comments'] = array( 'title' => 'AJAX comments', 'description' => 'AJAXifies comments on site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('admin_ajax_comments'), 'access arguments' => array('administer site configuration'), ); $items['ajax_comments/get_form_token/%/%'] = array( 'page callback' => 'get_form_token', 'page arguments' => array(2,3), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); $items['ajax_comments/instant_delete/%'] = array( 'page callback' => 'instant_delete', 'page arguments' => array(2), 'access arguments' => array('administer comments'), 'type' => MENU_CALLBACK, ); $items['ajax_comments/js'] = array( 'page callback' => 'ajax_comments_js', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } /* * AHAH callback */ function ajax_comments_js() { $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; $form['#programmed'] = $form['#redirect'] = FALSE; if ($form_state['post']['op'] == t('Preview')) { $form['#after_build'] = array('comment_form_add_preview'); } else { unset($form['#after_build']); } drupal_process_form($form_id, $form, $form_state); $errors = form_get_errors(); if (!$errors) { if ($form_state['values']['op'] == t('Preview')) { $output = '
'.$form['comment_preview']['#value'].'
'; if ($output && $form_state['values']['pid']) { $output = '
'. $output .'
'; } } elseif ($form_state['values']['op'] == t('Save')) { $output = '
'.$form_state['ajax_comment'].'
'; } $form = form_builder($form_id, $form, $form_state); } unset($form['#suffix']); unset($form['#prefix']); $output = theme('status_messages') . $output; drupal_json(array('status' => TRUE, 'data' => $output)); } /* * Comment submit routine */ function ajax_comments_submit($form, &$form_state) { //remove self unset($form_state['submit_handlers']); // ..and standart comments submit handler foreach ($form['#submit'] as $key => $value) { if ($value == 'comment_form_submit') { unset($form['#submit'][$key]); } } //execute all others form_execute_handlers('submit', $form, $form_state); //save comment just like comments module do it $edit = $form_state['values']; _comment_form_submit($edit); if ($cid = comment_save($edit)) { $errors = form_get_errors(); if (!$errors) { $node = node_load($edit['nid']); // update node stats _comment_update_node_statistics($node->nid); node_tag_new($node->nid); $query = 'SELECT c.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.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d'; $query_args = array($cid); if (!user_access('administer comments')) { $query .= ' AND c.status = %d'; $query_args[] = COMMENT_PUBLISHED; } $query = db_rewrite_sql($query, 'c', 'cid'); $result = db_query($query, $query_args); if ($comment = db_fetch_object($result)) { $comment->name = $comment->uid ? $comment->registered_name : $comment->name; $links = module_invoke_all('link', 'comment', $comment, 0); drupal_alter('link', $links, $node); //render our comment and get it back to AHAH handler $output .= theme('comment_view', $comment, $node, $links); $form_state['ajax_comment'] = $output; } } } return $output; } /* * AJAX comments settings form */ function admin_ajax_comments() { $form['ajax_comments_always_expand_form'] = array( '#type' => 'checkbox', '#title' => t('Form always expanded'), '#description' => t('This setting allows to render comments as collapsed link by default.'), '#default_value' => variable_get('ajax_comments_always_expand_form', FALSE), ); $form['ajax_comments_default_row_count'] = array( '#type' => 'textfield', '#title' => t('Default row count'), '#description' => t('Allows to control comment forms\' textarea size. Leave blank to use system defined.'), '#default_value' => variable_get('ajax_comments_default_row_count', 5), ); $form['ajax_comments_reply_row_count'] = array( '#type' => 'textfield', '#title' => t('Row count in replies'), '#description' => t('Usually, there is no need in big forms for replies, so this setting allows to reduce their size.'), '#default_value' => variable_get('ajax_comments_reply_row_count', 3), ); $form['ajax_comments_types'] = array( '#type' => 'fieldset', '#title' => t('Node types'), '#description' => t('Select the node types you want to activate ajax comments on.'), ); $form['ajax_comments_types']['ajax_comments_node_types'] = array( '#type' => 'checkboxes', '#title' => t('Types'), '#default_value' => variable_get('ajax_comments_node_types', array()), '#options' => node_get_types('names'), ); return system_settings_form($form); } /* * Misc function to fetch new form and it's values when full reload needed */ function get_form_token($nid, $pid = NULL){ drupal_set_header('Content-type: text/javascript; charset=utf-8'); global $user; $uid = $user->uid; if ($pid == 0) $pid = NULL; $fs = array(); $edit = array('nid' => $nid, 'cid' => $cid, 'pid' => $pid, 'uid' => $uid); print_r((drupal_get_form('comment_form', $edit, $title))); exit(); } /** * Comments delete callback */ function instant_delete($cid = 0){ // Check token to avoid CSRF attack. if ($cid && isset($_GET['token']) && drupal_valid_token($_GET['token'], $cid)) { drupal_set_header('Content-type: text/javascript; charset=utf-8'); module_load_include('inc', 'comment', 'comment.admin'); $comment = _comment_load($cid); // Delete comment and its replies. _comment_delete_thread($comment); _comment_update_node_statistics($comment->nid); // Clear the cache so an anonymous user sees that his comment was deleted. cache_clear_all(); print('OK'); } exit(); } /** * Implementation of hook_link_alter(). */ function ajax_comments_link_alter(&$links, $comment) { $node = node_load(arg(1)); $node_type = in_array($node->type, variable_get('ajax_comments_node_types', array()), TRUE); if($node_type){ if (isset($links['comment_delete'])) { $links['comment_delete'] = array( 'title' => t('delete'), 'href' => 'comment/delete/'. $comment->cid, 'query' => 'token='. drupal_get_token($comment->cid), ); } } } /** * Process variables for comment.tpl.php. */ function ajax_comments_preprocess_comment(&$variables) { $comment = $variables['comment']; // Insert proper delete links (fix until hook_link_alter will not work propertly) if ($variables['links'] && user_access('administer comments')) { $links = module_invoke_all('link', 'comment', $comment, 0); $links['comment_delete'] = array( 'title' => t('delete'), 'href' => 'comment/delete/'. $comment->cid, 'query' => 'token='. drupal_get_token($comment->cid), ); $variables['links'] = isset($links) ? theme('links', $links) : ''; } if (!$comment->cid) { $variables['new'] = t('preview'); $variables['comment']->new = TRUE; } else { $variables['title'] = l($comment->subject, 'node/'.$comment->nid, array('attributes' => array('class' => 'active'), 'fragment' => "comment-$comment->cid")); } }