_comment_form.rhtml 4.09 KB
<div class="page-comment-form">

<% focus_on = logged_in? ? 'title' : 'name' %>

<% edition_mode = (defined? edition_mode) ? edition_mode : false %>
<div class="post_comment_box <%= ((defined? show_form) && show_form) ? 'opened' : 'closed' %>">

<% if display_link %>
  <h4 onclick="var d = jQuery(this).parent('.post_comment_box');
               if (d.hasClass('closed')) {
                 d.removeClass('closed');
                 d.addClass('opened');
                 d.find('input[name=comment[title]], textarea').val('');
                 d.find('.comment_form input[name=comment[<%= focus_on %>]]').focus();
               }">
    <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') %>
  </h4>
<% end %>

<% if !edition_mode && !pass_without_comment_captcha? %>
  <div id="recaptcha-container" style="display: none">
    <h3><%= _('Please type the two words below') %></h3>
    <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) %>
    <% button_bar do %>
      <%= button_to_function :add, _('Confirm'), "return false", :id => "confirm-captcha" %>
      <%= button_to_function :cancel, _('Cancel'), "jQuery.colorbox.close()" %>
    <% end %>
  </div>

  <script type="text/javascript">
    jQuery(document).bind('cbox_cleanup', function() {
      jQuery('#recaptcha-container').hide();
    });
  </script>
<% end %>

<script type="text/javascript">
function check_captcha(button, confirm_action) {
  <% if edition_mode %>
  return true;
  <% elsif pass_without_comment_captcha? %>
  button.form.confirm.value = 'true';
  button.disabled = true;
  return true;
  <% else %>
  jQuery('#recaptcha-container').show();
  jQuery.colorbox({ inline : true, href : '#recaptcha-container', maxWidth : '600px', maxHeight : '300px' });
  jQuery('#confirm-captcha').unbind('click');
  jQuery('#confirm-captcha').bind('click', function() {
    jQuery.colorbox.close();
    button.form.recaptcha_response_field.value = jQuery('#recaptcha_response_field').val();
    button.form.recaptcha_challenge_field.value = jQuery('#recaptcha_challenge_field').val();
    button.form.confirm.value = 'true';
    button.disabled = false;
    confirm_action(button);
  });
  return false;
  <% end %>
}
</script>

<% remote_form_for(:comment, comment, :url => {:profile => profile.identifier, :controller => 'comment', :action => (edition_mode ? 'update' : 'create'), :id => (edition_mode ?  comment.id : @page.id)}, :html => { :class => 'comment_form' } ) do |f| %>

  <% if comment && comment.errors.any? %>
    <%= error_messages_for :comment %>
    <script type="text/javascript">jQuery(function() { document.location.href = "<%= comment.reply_of_id.nil? ? '#page-comment-form' : '#comment-' + comment.reply_of_id.to_s %>"; });</script>
  <% end %>

  <%= hidden_field_tag(:confirm, 'false') %>

  <%= required_fields_message %>

  <% unless logged_in? %>

    <%= required labelled_form_field(_('Name'), f.text_field(:name)) %>
    <%= required labelled_form_field(_('e-mail'), f.text_field(:email)) %>
    <p>
    <%= _('If you are a registered user, you can login and be automatically recognized.') %>
    </p>

  <% end %>

  <% if !edition_mode && !pass_without_comment_captcha? %>
    <%= hidden_field_tag(:recaptcha_response_field, nil, :id => nil) %>
    <%= hidden_field_tag(:recaptcha_challenge_field, nil, :id => nil) %>
  <% end %>

  <%= labelled_form_field(_('Title'), f.text_field(:title)) %>
  <%= required labelled_form_field(_('Enter your comment'), f.text_area(:body, :rows => 5)) %>
  <%= f.hidden_field(:reply_of_id) %>

  <%= @plugins.dispatch(:comment_form_extra_contents, local_assigns).collect { |content| instance_eval(&content) }.join("") %>

  <% button_bar do %>
      <%= submit_button('add', _('Post comment'), :onclick => "if(check_captcha(this)) { save_comment(this) } else { check_captcha(this, save_comment)};return false;") %>
      <%= button_to_function :cancel, _('Cancel'), "jQuery.colorbox.close();f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %>
  <% end %>
<% end %>


</div><!-- end class="post_comment_box" -->
</div><!-- end class="page-comment-form" -->