You are here

ajax_comments.module in AJAX Comments 5

Same filename and directory in other branches
  1. 8 ajax_comments.module
  2. 6 ajax_comments.module
  3. 7 ajax_comments.module

File

ajax_comments.module
View source
<?php

function ajax_comments_form_alter($form_id, &$form) {
  if ($form_id == 'comment_form' && arg(1) != 'edit') {
    $form['#prefix'] = '<div id="comment-preview"></div>';
    $form['js_enabled'] = array(
      '#type' => 'hidden',
      '#default_value' => $_GET['q'],
      '#weight' => 0,
    );
    $form['#suffix'] = '<script>$("#comment-form #edit-js-enabled").val(1);</script>';
    if (!empty($_POST) && $_POST['form_id'] == $form_id) {

      // Redirect on successful submit, i.e., if there are no errors.

      //print_r($form);
      if (is_array($form['#pre_render'])) {
        $form['#pre_render'][] = 'ajax_comments_dispatch';
      }
      else {
        $form['#pre_render'] = array(
          'ajax_comments_dispatch',
        );
      }
    }
    $form['#submit'] = array(
      'ajax_comments_submit_dispatch' => array(),
    );
    jquery_form_add();
    $path = drupal_get_path('module', 'ajax_comments');
    drupal_add_js($path . '/ajax_comments.js', 'module');
    drupal_add_css($path . '/ajax_comments.css');
    drupal_add_js(array(
      'ajax_comments_form_alter' => array(
        'cleanurls' => (bool) variable_get('clean_url', '0'),
        'basePath' => base_path(),
      ),
    ), 'setting');
  }
}
function ajax_comments_menu($may_cache) {
  global $user;
  $items = array();
  if (!$may_cache) {
    $id = arg(1);
    if (arg(0) == 'get_token') {
      $items[] = array(
        'path' => 'get_token/' . $id,
        'callback' => 'get_token',
        'callback arguments' => array(
          $id,
        ),
        'type' => MENU_CALLBACK,
        'access' => TRUE,
      );
    }
    $items[] = array(
      'path' => 'ajaxsubmit/dispatch',
      'title' => t('ajaxsubmit'),
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
      'callback' => 'ajax_comments_dispatch',
    );
  }
  return $items;
}
function get_token($id) {
  print drupal_get_token($id);
}
function ajax_comments_submit_dispatch($form_id, $form_values) {
  $js_enabled = $form_values['js_enabled'];
  $edit = _comment_form_submit($form_values);
  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);

      // Single comment view.
      $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.picture, u.data, c.score, c.users, 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;
      }
      $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);
        foreach (module_implements('link_alter') as $module) {
          $function = $module . '_link_alter';
          $function($node, $links);
        }
        $preview .= theme('comment_view', $comment, $links);
      }
      if ($comment->pid) {
        $preview = '<div class="indented">' . $preview . '<div>';
      }
      $redirect = drupal_get_path_alias('node/' . $comment->nid) . '?r' . $cid . '#comment-' . $cid;
    }
    $result = array(
      'status' => TRUE,
      'data' => array(
        'errors' => $errors,
        'message' => theme('status_messages'),
        'preview' => $preview,
        'destination' => $redirect,
      ),
    );
    if ($js_enabled != 1) {
      return $js_enabled;
    }
    print drupal_to_js($result);
  }
}
function ajax_comments_dispatch($form_id = NULL, $form = NULL) {
  $js_enabled = $form['js_enabled']['#value'];
  if (module_exists('captcha')) {
    captcha_form_alter('comment_form', $form_c);
    captcha_pre_render('comment_form', $form_c);
    $form_c = array();
    $form_c['captcha']['captcha_token']['#id'] = 'edit-captcha-token';
    $form_c['captcha']['captcha_response']['#id'] = 'edit-captcha-response';
    $form_c['captcha']['captcha_token']['#name'] = 'captcha_token';
    $form_c['captcha']['captcha_response']['#name'] = 'captcha_response';
    $captcha = @drupal_render($form_c);
  }
  $result = array(
    'status' => TRUE,
    'data' => array(
      'errors' => form_get_errors(),
      'message' => theme('status_messages'),
      'preview' => isset($form[arg(0) . '_preview']) && !empty($form[arg(0) . '_preview']['#value']) ? $form[arg(0) . '_preview']['#value'] : NULL,
      'destination' => '',
      'captcha' => $captcha,
    ),
  );
  if ($js_enabled != 1) {
    if ($form['form_id']['#post']['op'] != t('Preview comment')) {
      drupal_goto($js_enabled);
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 19,
    );
    return $form;
  }
  print drupal_to_js($result);
  exit;
}