ajax_comment/ 777 0 0 0 11026730575 6504 5 ajax_comment/ajax_comment.info 777 0 0 226 11037402333 12057 0 ; $Id$ name = AJAX Comment description = Aim to provide a AJAX form for user to submit comment. core = 6.x version = 0.x-dev dependencies[] = comment ajax_comment/ajax_comment.js 777 0 0 417 11026732110 11537 0 // $Id$ $(document).ready( function(){ajax_comment_auto_attach();} ); function ajax_comment_auto_attach(){ $("#comment-form input[@type=submit]").eq(0).after("").remove(); $("#comment-form button").click( function(){"return false;"}); } ajax_comment/ajax_comment.module 777 0 0 10161 11037402456 12456 0 '. t("Provide a AJAX form for user to submit comment.") .'
'; break; } return $output; } /** * hook_perm() * * @return array of access right names */ function ajax_comment_perm(){ return array('submit comment via AJAX'); } /** * hook_menu() * * @return assoicate array */ function ajax_comment_menu(){ $items = array(); $items['ajax_comment/ahah'] = array( 'title' => 'Javascript ajax comment form', 'page callback' => 'ajax_comment_ahah', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); //useless. preview cannot be done by AJAX. $items['ajax_comment/preview'] = array( 'title' => 'Javascript ajax comment preview', 'page callback' => 'ajax_comment_preview', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } /** * hook_form_alter * */ function ajax_comment_form_alter( &$form, $form_state, $form_id ){ global $user; if ( $form_id == 'comment_form' ) { if ( arg(0) == 'node' && is_numeric( arg(1) ) ){ $nid = arg(1); $form['nid'] = array( '#type' => 'hidden', '#value' => $nid, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('AJAX Save'), '#weight' => 19, '#ahah' => array( 'path' => 'ajax_comment/ahah', 'effect' => 'fade', 'wrapper' => 'comments .box, #comment-form', 'method' => 'replace', 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), ), ); /* $form['preview'] = array( '#type' => 'button', '#value' => t('AJAX Preview'), '#weight' => 19, '#ahah' => array( 'path' => 'ajax_comment/preview', 'effect' => 'fade', 'wrapper' => 'comments .box, #comment-form', 'method' => 'replace', 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), ), );*/ $form['#redirect'] = false; }elseif( arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2)) ) { $nid = arg(2); $form['nid'] = array( '#type' => 'hidden', '#value' => $nid, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('AJAX Save'), '#weight' => 19, '#ahah' => array( 'path' => 'ajax_comment/ahah', 'effect' => 'fade', 'wrapper' => 'comments .box, #comment-form', 'method' => 'replace', 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), ), ); $form['#redirect'] = false; } } } function ajax_comment_preview(){ global $user; /* $form_state['post'] = $_POST; drupal_execute('comment_form', $form_state); $output = drupal_render_form('comment_form', $form_state); drupal_json(array('status' => TRUE, 'data' => $output )); */ drupal_goto('admin'); } function ajax_comment_ahah(){ global $user; $form = drupal_get_form('comment_form'); //rebuild? form is still cached if there is error $output = theme_status_messages(); if ( !$output ) { //incomplete /* cannot retieve the last insert comment id $node->nid = $_POST['nid']; $node = node_load( array('nid'=>$_POST['nid']) , true, true); $cid = db_last_insert_id('comments', 'cid'); $output = comment_render( $node, $cid ); */ //temp solution $output = t('Your comment had been submitted. ').t(' Click !here to refresh', array('!here'=> l(t('here'), 'node/' .$_POST['nid'], array('fragment' => "new")) ) ); $output = ""; }else { //the form is cached. cannot disable the AJAX properties after error. $output .= $form; } //print $output; drupal_json(array('status' => TRUE, 'data' => $output )); }